Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include <string>
- using namespace std;
- class Arabrzym
- {
- private:
- static const string rzymskie [] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
- static const int arabskie [] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
- static bool isInt(string line)
- {
- bool isInt=true;
- try
- {
- int Integer = stoi( line );
- }
- catch(const std::exception)
- {
- isInt=false;
- }
- return isInt;
- }
- static bool aarabskie(string t)
- {
- int n = t.length();
- for (int i = 1; i < n; i++)
- if (t.at(i) != '0' && t.at(i) != '1' && t.at(i) != '2' && t.at(i) != '3' && t.at(i) != '4' && t.at(i) != '5' && t.at(i) != '6' && t.at(i) != '7' && t.at(i) != '8' && t.at(i) != '9')
- return false;
- return true;
- }
- public : static int rzym2arab (string rzym)
- {
- int result=0;
- int tmp=0;
- int nameLength = rzym.length();
- int repeat = 0;
- for(int i=0; i<rzymskie.length; i++)
- {
- repeat = 0;
- while(rzym.startsWith(rzymskie[i], tmp))
- {
- result=result+arabskie[i];
- tmp=tmp+rzymskie[i].length();
- repeat++;
- if(repeat > 3 ||
- repeat > 1 && rzymskie[i].length() > 1 ||
- rzym.substr(0, tmp - 1).contains(rzymskie[i]) && repeat == 1)
- cout << "Nieprawidłowa dana";
- }
- }
- if( rzym.length() > tmp || result>3999 ){
- cout << "Nieprawidłowa dana";
- }
- return result;
- };
- public: static string arab2rzym (int arab,int sumuj)
- {
- if(arab<1 || arab>3999 || sumuj>0)
- cout << "Nieprawidłowa dana";
- std::string parametr = new std::string("");
- for(int i=0; i<arabskie.length; ++i)
- {
- while(arab>=arabskie[i])
- {
- parametr.append(rzymskie[i]);
- arab=arab-arabskie[i];
- }
- }
- return parametr;
- }
- public: static void main(string args [])
- {
- for(string t: args)
- {
- try
- {
- if(isInt(t))
- {
- int n = stoi( t);
- bool element=aarabskie(t);
- int sumuj=0;
- if(element==false){
- sumuj=sumuj+1;
- }
- cout << (arab2rzym(n,sumuj));
- }
- else
- {
- cout << (rzym2arab(t));
- }
- }
- catch (string args)
- {
- cout << "args exception";
- continue;
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement