Advertisement
Guest User

wsv

a guest
Mar 30th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class Arabrzym
  7. {
  8.  
  9. private:
  10. static const string rzymskie [] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  11. static const int arabskie [] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  12.  
  13. static bool isInt(string line)
  14. {
  15. bool isInt=true;
  16. try
  17. {
  18. int Integer = stoi( line );
  19. }
  20. catch(const std::exception)
  21. {
  22. isInt=false;
  23. }
  24. return isInt;
  25. }
  26.  
  27.  
  28. static bool aarabskie(string t)
  29. {
  30. int n = t.length();
  31. for (int i = 1; i < n; i++)
  32. 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')
  33. return false;
  34.  
  35. return true;
  36. }
  37.  
  38. public : static int rzym2arab (string rzym)
  39. {
  40. int result=0;
  41. int tmp=0;
  42. int nameLength = rzym.length();
  43.  
  44. int repeat = 0;
  45.  
  46. for(int i=0; i<rzymskie.length; i++)
  47. {
  48. repeat = 0;
  49. while(rzym.startsWith(rzymskie[i], tmp))
  50. {
  51. result=result+arabskie[i];
  52. tmp=tmp+rzymskie[i].length();
  53. repeat++;
  54.  
  55. if(repeat > 3 ||
  56. repeat > 1 && rzymskie[i].length() > 1 ||
  57. rzym.substr(0, tmp - 1).contains(rzymskie[i]) && repeat == 1)
  58. cout << "Nieprawidłowa dana";
  59. }
  60. }
  61.  
  62.  
  63. if( rzym.length() > tmp || result>3999 ){
  64. cout << "Nieprawidłowa dana";
  65. }
  66. return result;
  67. };
  68.  
  69. public: static string arab2rzym (int arab,int sumuj)
  70. {
  71.  
  72. if(arab<1 || arab>3999 || sumuj>0)
  73. cout << "Nieprawidłowa dana";
  74.  
  75. std::string parametr = new std::string("");
  76. for(int i=0; i<arabskie.length; ++i)
  77. {
  78. while(arab>=arabskie[i])
  79. {
  80. parametr.append(rzymskie[i]);
  81. arab=arab-arabskie[i];
  82. }
  83. }
  84.  
  85. return parametr;
  86. }
  87.  
  88. public: static void main(string args [])
  89. {
  90. for(string t: args)
  91. {
  92. try
  93. {
  94. if(isInt(t))
  95. {
  96. int n = stoi( t);
  97. bool element=aarabskie(t);
  98. int sumuj=0;
  99. if(element==false){
  100. sumuj=sumuj+1;
  101. }
  102. cout << (arab2rzym(n,sumuj));
  103. }
  104. else
  105. {
  106. cout << (rzym2arab(t));
  107. }
  108. }
  109. catch (string args)
  110. {
  111. cout << "args exception";
  112. continue;
  113. }
  114. }
  115. }
  116.  
  117. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement