Advertisement
Need4Sleep

main.cpp (DailyC++) 7/21/12

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