Advertisement
Toxotsist

roman to arabic (probably)

Nov 1st, 2020
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int convert(char c) {
  6.     if ((c == 'I') || (c == 'V') || (c == 'X') || (c == 'L') || (c == 'C') || (c == 'D') || (c == 'M')) {
  7.         switch (c) {
  8.         case 'M': return 1000;
  9.         case 'D': return 500;
  10.         case 'C': return 100;
  11.         case 'L': return 50;
  12.         case 'X': return 10;
  13.         case 'V': return 5;
  14.         case 'I': return 1;
  15.         default: return 0;
  16.         }
  17.     }
  18.     else return 0;
  19. }
  20.  
  21. int translate(const string str) {
  22.     int res = 0; int tmp = 0;
  23.     bool status = true;
  24.     for (int i = 0; i < size(str); i++) {
  25.         int x = convert(str[i]);
  26.         if (tmp == 0) {
  27.             tmp = x;
  28.             res += x;
  29.         }
  30.         else
  31.         {
  32.             if (tmp >= x) {
  33.                 res += x;
  34.             }
  35.             else {
  36.                 res += x - 2*tmp;
  37.             }
  38.             tmp = x;
  39.  
  40.         }
  41.     }
  42.  
  43.     return res;
  44. }
  45.  
  46. int main() {
  47.     string str;
  48.     cin >> str;
  49.     cout << translate(str);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement