Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <cctype>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9. char input; //holds the input value
  10. string line;
  11. int output = 0; //holds the output value of the result
  12. int n;
  13.  
  14. while (cin.get(input)) { // loop until end of file
  15.  
  16.  
  17. input = toupper(input); // if the input is lowercase convert to upper case
  18.  
  19. //value of the first symbol if it is Thousand (M)
  20.  
  21. if (input == 'M')
  22.  
  23. output = output + 1000;
  24.  
  25. //check if the first symbol is Five hundred(D)
  26.  
  27. else if (input == 'D') {
  28.  
  29. input = cin.peek(); //read next character in the input stream
  30.  
  31. input = toupper(input);
  32.  
  33. //if the next symbol is of a higher value then subtract, else add the value to the result
  34.  
  35.  
  36. if (input == 'M') {
  37.  
  38. output = output - 500;
  39.  
  40. continue;
  41.  
  42. } else {
  43.  
  44. output = output + 500;
  45.  
  46. continue;
  47.  
  48. }
  49.  
  50. }
  51.  
  52. //check if the first symbol is Hundred (C)
  53.  
  54. else if (input == 'C') {
  55.  
  56. input = cin.peek(); //read next
  57.  
  58. input = toupper(input);
  59.  
  60. if (input == 'M' || input == 'D') {
  61.  
  62. output = output - 100;
  63.  
  64. continue;
  65.  
  66. } else {
  67.  
  68. output = output + 100;
  69.  
  70. continue;
  71.  
  72. }
  73.  
  74. }
  75.  
  76. //check if the first symbol is Fifty (L)
  77.  
  78. else if (input == 'L') {
  79.  
  80. input = cin.peek();
  81.  
  82. input = toupper(input);
  83.  
  84.  
  85. output = output + 50;
  86.  
  87. continue;
  88.  
  89.  
  90. }
  91.  
  92. //check if the first symbol is Ten (X)
  93.  
  94. else if (input == 'X') {
  95.  
  96. input = cin.peek(); //read next
  97.  
  98. input = toupper(input); //convert to upper case
  99.  
  100. //check if next symbol is M or D or C or L
  101.  
  102. if (input == 'C' || input == 'L') {
  103.  
  104. output = output - 10;
  105.  
  106. continue;
  107.  
  108. } else {
  109.  
  110. output = output + 10;
  111.  
  112. continue;
  113.  
  114. }
  115.  
  116. }
  117.  
  118. //check if the first symbol is Five (V)
  119.  
  120. else if (input == 'V') {
  121.  
  122. input = cin.peek();
  123.  
  124. input = toupper(input);
  125.  
  126.  
  127. output = output + 5;
  128.  
  129. continue;
  130.  
  131.  
  132. }
  133.  
  134. //check if the first symbol is One(I)
  135.  
  136. else if (input == 'I') {
  137.  
  138. input = cin.peek();
  139.  
  140. input = toupper(input);
  141.  
  142. if (input == 'X' || input == 'V') {
  143.  
  144. output = output - 1;
  145.  
  146. continue;
  147.  
  148. } else {
  149.  
  150. output = output + 1;
  151.  
  152. continue;
  153.  
  154. }
  155.  
  156. } else break;
  157.  
  158. }
  159.  
  160.  
  161. cout << output << endl;
  162.  
  163. return 0;
  164.  
  165.  
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement