Advertisement
wnan42

7/21/12Roman_number

Jul 24th, 2012
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct NumType{
  4.        int decimal_num;
  5.       string roman_num;
  6.        };
  7. class romanType
  8. {
  9.      
  10.       public:
  11.       NumType myNum;    
  12.      
  13.       romanType(string newnum){
  14.       myNum.roman_num=newnum;
  15.       myNum.decimal_num=convertRomantoDecimal(newnum);}
  16.      
  17.       int convertRomantoDecimal(string roman_num)
  18.       {
  19.           int intNum=0;
  20.           for(int i=0;i<roman_num.length();i++)
  21.           {
  22.                   int temp=0;
  23.                   switch (roman_num[i]){
  24.                          case 'M':
  25.                               temp=1000;break;
  26.                          case 'D':
  27.                               temp=500;break;
  28.                          case 'C':
  29.                               temp=100;break;
  30.                          case 'L':
  31.                               temp=50;break;
  32.                          case 'X':
  33.                               temp=10;break;
  34.                          case 'V':
  35.                               temp=5;break;
  36.                          case 'I':
  37.                               temp=1;break;
  38.                          default:
  39.                               cout<<"error number"<<endl;
  40.                          }
  41.                     intNum+=temp;    
  42.           }
  43.           return intNum;
  44.       }
  45.  
  46.       void printNum()
  47.       {
  48.            int choice;
  49.            cout<<"Enter 1 for Roman Number, 2 for Decimal number"<<endl;
  50.            cin>>choice;
  51.            if(choice==1){cout<<myNum.roman_num<<endl;}
  52.            else if(choice==2){cout<<myNum.decimal_num<<endl;}
  53.            else{cout<<"Invaild input."<<endl;}
  54.          
  55.       }
  56. };
  57. int main(){
  58.     romanType R1("MCXIV");
  59.     romanType R2("CCCLIX");
  60.     romanType R3("MDCLXVI");
  61.    
  62.     R1.printNum();
  63.     R2.printNum();
  64.     R3.printNum();
  65.     system("pause");
  66.     return 0;
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement