Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  RzymArab.exe
Language: C/C++
Code:
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

class RzymArab
{
     private:
          static string liczby[13];
          static int konwersja[13];
     public:
          static int rzym2arab(string rzym)
          {
               int arab=0, w1=0, w2=0, counter1=0,counter5=0,counter10=0,counter50=0,counter100=0,counter500=0;
               for(int i=0; i<rzym.length(); i++)
               {
                    char ch = rzym[i];
                    if(i+3 < rzym.length() && ch==rzym[i+1] && ch==rzym[i+2] && ch==rzym[i+3])
                         throw "Nie mozna postawic obok siebie czterech jednakowych znakow.";
                    switch(ch)
                    {
                         case 'I':
                              w1=1;
                              if(counter1>0)
                              {
                                   throw "Nieprawidlowe dane";
                              }
                              if(i+1<rzym.length()  && rzym[i+1] == 'V')
                              {
                                   w1=4;
                                   i++;
                                   counter1++;
                              }
                              else if(i+1<rzym.length()  && rzym[i+1] == 'X')
                              {
                                   w1=9;
                                   i++;
                                   counter1++;
                              }
                              break;
                         case 'V':
                              w1=5;
                              if(counter5>0)
                              {
                                   throw "Nieprawidlowe dane";
                              }
                              counter5++;
                              break;
                         case 'X':
                              w1=10;
                              if(counter10>0)
                              {
                                   throw "Nieprawidlowe dane";
                              }
                              if(i+1<rzym.length() && rzym[i+1] == 'L')
                              {
                                   w1=40;
                                   i++;
                                   counter10++;
                              }
                              if(i+1<rzym.length() && rzym[i+1] == 'C')
                              {
                                   w1=90;
                                   i++;
                                   counter10++;
                              }
                              break;
                         case 'L':
                              w1=50;
                              if(counter50>0)
                              {
                                   throw "Nieprawidlowe dane";
                              }
                              counter50++;
                              break;
                         case 'C':
                              w1=100;
                              if(counter100>0)
                              {
                                   throw "Nieprawidlowe dane";
                              }
                              
                              if(i+1<rzym.length() && rzym[i+1] == 'D')
                              {
                                   w1=400;
                                   i++;
                                   counter100++;
                              }
                              else if(i+1<rzym.length() && rzym[i+1] == 'M')
                              {
                                   w1=900;
                                   i++;
                                   counter100++;
                              }
                              break;
                         case 'D':
                              w1=500;
                              if(counter500>0)
                              {
                                   throw "Nieprawidlowe dane";
                              }
                              counter500++;
                              break;
                         case 'M':
                              w1=1000;
                              break;
                         default:
                              throw "Brak znaku" + rzym[i];
                    }
                    if(w1>w2 && w2!=0)
                         throw "Blad przy kolejnosci znakow";
                    w2=w1;
                    arab+=w1;
               }
                    return arab;
     }
     static string arab2rzym(int arab)
     {
          if(arab>3999 || arab < 1)
               throw "Spoza zakresu rzymskiego.";     
          string rzym;
          int i = 0;
          while(arab>0 && i<13)
          {
               if(arab >= konwersja[i])
               {
                    arab-=konwersja[i];
                    rzym+=liczby[i];
               }
               else
                    i++;
          }
          return rzym;
     }
};
     string RzymArab::liczby[13] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX","V","IV","I"};
     int RzymArab::konwersja[13] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9,  5, 4, 1 };

bool is_number(const string& s)
{
     string::const_iterator it = s.begin();
     while (it != s.end() && isdigit(*it)) ++it;
     return !s.empty() && it == s.end();
}
int main(int argc, char **args)
{
     for(int i = 1; i < argc; i++)
     {
          try
          {
               if(is_number(args[i]))
                    printf("%s n", RzymArab::arab2rzym(stoi(args[i])).c_str());
               else
                    printf("%i n", RzymArab::rzym2arab(args[i]));

          }
          catch(const char* ex)
          {
               printf("%s n", ex);
          }
     }
     return 0;
}

          
Comments: