Advertisement
deleanuradu

roman2arab

Oct 23rd, 2019
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <deque>
  3. #include <string>
  4.  
  5. int main() {
  6.     std::deque<int> arab;
  7.    
  8.     std::cout << "Please enter a roman numeral in All-Caps..." << std::endl;
  9.     std::string roman;
  10.     std::getline(std::cin, roman);
  11.  
  12.     for (unsigned int i = 0; i < roman.size(); i++) {
  13.         int temp;
  14.         switch (roman[i]) {
  15.             case 'I':
  16.                 temp = 1;
  17.                 break;
  18.             case 'V':
  19.                 temp = 5;
  20.                 break;
  21.             case 'X':
  22.                 temp = 10;
  23.                 break;
  24.             case 'L':
  25.                 temp = 50;
  26.                 break;
  27.             case 'C':
  28.                 temp = 100;
  29.                 break;
  30.             case 'D':
  31.                 temp = 500;
  32.                 break;
  33.             case 'M':
  34.                 temp = 1000;
  35.                 break;
  36.         }
  37.    
  38.         arab.emplace_back(temp);
  39.     }
  40.  
  41.     int result = 0;
  42.    
  43.     while(arab.size() > 0){
  44.         int temp = 0;
  45.  
  46.         if (arab.size() == 1) {
  47.             result += arab[0];
  48.             arab.pop_front();
  49.             break;
  50.         }
  51.  
  52.         if (arab[0] >= arab[1]) {
  53.             result += arab[0];
  54.             arab.pop_front();
  55.         }
  56.         else {
  57.             result -= arab[0];
  58.             arab.pop_front();
  59.         }
  60.  
  61.     }
  62.    
  63.     std::cout << "Result: " << result << std::endl;
  64.     std::getchar();
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement