Römische Zahlen Python?

1 Antwort

Ich habe dies mal mit C++ gemacht:

// Zweck: röem Zahl in Dezimalzahl umwandeln

// Pfad: roemdez.cpp

#include <iostream>

#include <cstring>

using namespace std;

int main ()

{

   char romanz[20];

   char c;

  do

  {   

     cout << "Gebe roemische Zahl ein: ";

     cin >> romanz;

     int len = strlen(romanz);

     int number = 0;

     int counter = 0;

     int b4sum = 0;

     int sum = 0;

     for (counter = len; counter >= 0; counter--)

      {

       if (romanz[counter] == 'M')

         number = 1000;

       else if (romanz[counter] == 'D')

         number = 500;

       else if (romanz[counter] == 'C')

         number = 100;

       else if (romanz[counter] == 'L')

         number = 50;

       else if (romanz[counter] == 'X')Dezimalzahl in roemische Zahl umrechnen

         number = 10;

       else if (romanz[counter] == 'V')

         number = 5;

       else if (romanz[counter] == 'I')

         number = 1;

       else

         number = 0;

       if (b4sum > number)

         sum = b4sum - number;

       else

         sum = sum + number;

       b4sum = number;

      }

    cout << "Die Dezimalzahl ist: " << sum << endl;

    cout<<"\n\nNeue Zahl berechnen ? (j/n) ";

     cin>>c;

   }

    while(c=='j');

   return 0;

}

Und umgekehrt:

// Zweck: Dezimalzahl in roemische Zahl umrechnen

// Pfad: dezroem.cpp

#include <iostream>

using namespace std;

int main()

{       int a;

       float b;

       char c;

       cout<<"\nHier werden arabische Zahlen in roemische umgerechnet!";

       do                                                                               //Abbruch- oder Wiederholschleife

       {       cout<<"\n\nGib eine Zahl ein:";

               do                                                                       //Eingabeschleife

               {       cin>>b;                                                       //Falscheingabe

                       a=(int)b;

                       if (b>8000 || b<1)

                               cout<<"\n\nFalsche Eingabe\n\nGib eine Zahl ein:";

                       else

                       {       if(a!=b)

                                       cout<<"\n\nFalsche Eingabe\n\nGib eine Zahl ein:";

                       }

               }

               while(b>8000 || b<1 || a!=b);               //Roemische Ziffer

               cout<<"\nRoemische Zahl:";

               do

               {       while (b/1000>=1)

                       {       cout<<"M";

                               b=b-1000;

                       }       

               }

               while(b>1000);

               do

               {       while (b/500>=1)

                       {       cout<<"D";

                               b=b-500;

                       }

               }

               while(b>500);

               do

               {       while (b/100>=1)

                       {       cout<<"C";

                               b=b-100;

                       }

               }

               while(b>100);

               do

               {       while (b/50>=1)

                       {       cout<<"L";

                               b=b-50;

                       }

               }

               while(b>50);

               do

               {       while (b/10>=1)

                       {       cout<<"X";

                               b=b-10;

                       }

               }

               while(b>10);

               do

               {       while (b/5>=1)

                       {       cout<<"V";

                               b=b-5;

                       }

               }

               while(b>5);

               do

               {       while (b/1>=1)

                       {       cout<<"I";

                               b=b-1;

                       }

               }

               while(b>1);

               do

               {       cout<<"\n\nNeue Zahl berechnen ? (j/n) ";

                       cin>>c;

               }

               while((c!='j') && (c!='n'));

       }

       while(c=='j');

}

Woher ich das weiß:eigene Erfahrung