Logo
IT Dienstleistungen

Romanizer V2

Umwandlung von arabischen Zahlen als Kommandozeilenargument

roman2.c

#include <stdlib.h>                                               
#include <stdio.h>                                                
 
/*zuordnung roemische/arabische ziffern festlegen*/               
#define M       1000                                              
#define D       500                                               
#define C       100                                               
#define L       50                                                
#define X       10                                                
#define V       5                                                 
#define I       1                                                 
 
/*anzahl roemische zahlzeichen*/                                  
#define zeichen  7                                                
 
int roman[zeichen]      = { M, 0, C, 0, X, 0, I };                
char show_roman[zeichen]= { 'M', 'D', 'C', 'L', 'X', 'V', 'I' };  
int count[zeichen]      = { 0, 0, 0, 0, 0, 0, 0};                 
 
int main(int argc,char *argv[]) {                                 
      int i, j=0, k, tmp_zahl;                                    
      char tmp_wort[128];                                         
 
  if (argc<2) {
      printf("\n=== Romanizer ===\n");
      printf("Bitte eine Zahl anhaengen.\n");
    return 1;                                
  }                                          
 
  tmp_zahl = atoi(argv[1]);                  
      for (i=0; i<zeichen; i=i+2) {          
        if (tmp_zahl >= roman[i]) {          
           count[i]        = tmp_zahl / roman[i];
           tmp_zahl        = tmp_zahl - roman[i] * count[i];
        }                                                   
        else {                                              
           count[i] = 0;                                    
        }                                                   
      }                                                     
      for (k=0; k<zeichen; k++) {                           
        while (count[k] != 0 ) {                            
         if (k == 0 ) {                                     
           tmp_wort[j++] = show_roman[k];
           count[k] -= 1;
         }
         else {
          if (count[k] == 9 ) {
            tmp_wort[j++] = show_roman[k];
            tmp_wort[j++] = show_roman[k-2];
            count[k] = 0;
          }
          else if (count[k] == 4 ) {
            tmp_wort[j++] = show_roman[k];
            tmp_wort[j++] = show_roman[k-1];
            count[k] = 0;
          }
          else if (count[k] > 4 && count[k] < 9) {
            tmp_wort[j++] = show_roman[k-1];
            count[k] -= 5;
          }
          else {
            tmp_wort[j++] = show_roman[k];
            count[k] -= 1;
          }
         }
        }
      }
  tmp_wort[j] = '\0';
  printf("%s\n",tmp_wort);
  return 0;
}

Seiten-Werkzeuge