# C++ Notes: Examples: Roman Numerals

      1 // roman.cpp - from Data Abstractions and Structures using C++
      2 #include <iostream>
      3 #include <stdlib.h>
      4 using namespace std;
      5
      6 //=== prototypes
      7 void PrintValue(int, char, int);
      8 void Convert(const char str[], int& decVal,  bool& strValid);
      9 int  DecEquiv(const char str[]);
     10
     11 //===================================================================== main
     12 int main(int argc, char *argv[]) {
     13     char str1[31];
     14     char str2[31];
     15     char op;
     16     int dec1;
     17     int dec2;
     18
     19     bool str1Valid;
     20     bool str2Valid;
     21     bool opValid;
     22
     23     cout << "Enter Roman numeral expression: ";
     24     while (cin >> str1 >> op >> str2) {
     25         Convert(str1, dec1, str1Valid);
     26         Convert(str2, dec2, str2Valid);
     27         opValid = op=='+' || op=='-' || op=='*' || op=='/';
     28         if (str1Valid && str2Valid && opValid) {
     29             PrintValue(dec1, op, dec2);
     30         } else {
     31             cout << "Bad data" << endl;
     32         }
     33         cout << "Enter Roman numeral expression: ";
     34     }
     35
     36     system("PAUSE");
     37     return 0;
     38 }//end main
     39
     40 //=============================================================== PrintValue
     41 void PrintValue(int dec1, char op, int dec2) {
     42     cout << "Decimal value: ";
     43     switch (op) {
     44         case '+': cout << dec1 + dec2; break;
     45         case '-': cout << dec1 - dec2; break;
     46         case '*': cout << dec1 * dec2; break;
     47         case '/': cout << float(dec1) / float(dec2); break;
     48     }
     49     cout << "\n\n";
     50 //end PrintValue
     51 }
     52
     53
     54 //================================================================== Convert
     55 void Convert(const char str[], int& decVal,  bool& strValid) {
     56     decVal = DecEquiv(str);
     57     strValid = (decVal > 0);
     58 }//end Convert
     59
     60
     61 //================================================================= DecEquiv
     62 int DecEquiv(const char str[]) {
     63     bool dataValid = true;
     64     int  intSum = 0;
     65     int  i = 0;
     66
     67     while (dataValid && str[i] != '\0') {
     68         switch (str[i]) {
     69             case 'M': intSum += 1000; break;
     70             case 'D': intSum += 500;  break;
     71             case 'C': intSum += 100;  break;
     72             case 'L': intSum += 50;   break;
     73             case 'X': intSum += 10;   break;
     74             case 'V': intSum += 5;    break;
     75             case 'I': intSum += 1;    break;
     76             default : dataValid = false;
     77         }
     78         i++;
     79     }
     80     return (dataValid ? intSum : 0);
     81 }//end decEquiv