Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. void task3() {
  2.    
  3.     std::string roman;
  4.     int arabic = 0;
  5.    
  6.     std::cin >> roman;
  7.    
  8.     bool check = true;
  9.    
  10.     while (std::cin >> roman) {
  11.         for (int i = 0; i < roman.length(); i++) {
  12.             if (roman[i] == 'I' || roman[i] == 'V' || roman[i] == 'X' || roman[i] == 'L' ||
  13.                 roman[i] == 'C' || roman[i] == 'D' || roman[i] == 'M') {
  14.                 check = true;    
  15.             }
  16.             else {
  17.                 std::cout << "Wrong input data." << std::endl;
  18.                 std::exit(0);
  19.             }
  20.         }
  21.     }
  22.    
  23.     int current_value = 0;
  24.  
  25.    
  26.     bool order = false;
  27.    
  28.  
  29.     for (int i = 0; i < roman.length(); i++) {
  30.        
  31.        
  32.         if (roman[i] == 'C' && roman[i+1] == 'M' || roman[i] == 'C' && roman[i+1] == 'M' || roman[i] == 'C' && roman[i+1] == 'M' || ) {
  33.             order = false;
  34.             break;
  35.         }
  36.        
  37.        
  38.         if (roman[i] == 'C' && roman[i+1] == 'M') {
  39.             current_value += 900;
  40.         }
  41.         else if (roman[i] == 'C' && roman[i+1] == 'D') {
  42.             current_value += 400;
  43.         }
  44.         else if (roman[i] == 'X' && roman[i+1] == 'C') {
  45.             current_value += 90;
  46.         }
  47.         else if (roman[i] == 'X' && roman[i+1] == 'L') {
  48.             current_value += 40;
  49.         }
  50.         else if (roman[i] == 'I' && roman[i+1] == 'X') {
  51.             current_value += 9;
  52.         }
  53.         else if (roman[i] == 'I' && roman[i+1] == 'V') {
  54.             current_value += 4;
  55.         }
  56.         else if(roman[i] == 'M') {
  57.             current_value += 1000;
  58.         }
  59.         else if (roman[i] == 'D') {
  60.             current_value += 500;
  61.         }
  62.         else if (roman[i] == 'C') {
  63.             current_value += 100;
  64.         }
  65.         else if (roman[i] == 'L') {
  66.             current_value += 50;
  67.         }
  68.         else if (roman[i] == 'X') {
  69.             current_value += 10;
  70.         }
  71.         else if (roman[i] == 'V') {
  72.             current_value += 5;
  73.         }
  74.         else if (roman[i] == 'I') {
  75.             current_value += 1;
  76.         }
  77.     }
  78.    
  79.     arabic = current_value;
  80.  
  81.     if (/*order == false || */arabic < 1 || arabic > 3999) {
  82.         std::cout << "Wrong input data." << std::endl;
  83.     }
  84.     else {
  85.         std::cout << "Converted " << roman << "to " << arabic << std::endl;
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement