Advertisement
darkhelmet125

RomanType - implementation file

Oct 4th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. /*Matt Short
  2. CPSC 131
  3. Purpose: Contains the implementation of
  4. the class members for RomanType
  5. */
  6. //IMPLEMENTATION FILE
  7. #include <iostream>
  8. #include "RomanType.h"
  9. using namespace std;
  10. RomanType::RomanType()
  11. {
  12. };
  13. void RomanType::getArabicNumber()
  14. {
  15.     cout<<"Please enter an Arabic Number: ";
  16.     cin>>arabicInput;
  17. }
  18. void RomanType::getRomanNumber()
  19. {
  20.     cout<<"Please enter a Roman Numeral: ";
  21.     cin>>romanInput;
  22. }
  23. void RomanType::printRoman()
  24. {
  25.     cout<<romanInput<<endl;
  26. }
  27. void RomanType::printArabic()
  28. {
  29.     cout<<arabicInput<<endl;
  30. }
  31. void RomanType::convertToRoman()
  32. {
  33.     int arabic2 = arabicInput;//a copy of arabicInput
  34.     string romanLets[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};//array of roman numerals
  35.     int arabicNums[] = {1000,  900, 500,  400, 100,   90,  50,   40,  10,    9,   5,    4,   1};//matching array of arabic numbers
  36.     string romanString = "";//string to hold roman numerals
  37.     for(int i=0;i<13;i++)//loop that writes roman numerals into romanString
  38.     {
  39.         while(arabic2>=arabicNums[i])
  40.         {
  41.             arabic2 -= arabicNums[i];
  42.             romanString += romanLets[i];
  43.         }
  44.     }
  45.     int len = romanString.size();//int that holds length of romanString
  46.     for(int i=0;i<=len;i++)//loop that moves romanString into romanInput
  47.     {
  48.         romanInput[i]=romanString[i];
  49.     }
  50.     cout<<"Conversion complete."<<endl;
  51. }
  52. void RomanType::convertToArabic()
  53. {
  54.      int len = strlen(romanInput);//int that holds length of romanInput
  55.      int number = 0;//int to hold value of roman numeral
  56.      int i = 0;//counting variable
  57.      int number2 = 0;//int to hold value of previous roman numeral
  58.      int sum = 0;//int to hold total/total-thus-far in arabic numbers
  59.      for (i=0;i<len;i++)
  60.          {
  61.              if (romanInput[i] == 'M' || romanInput[i] == 'm')
  62.                  number = 1000;
  63.              else if (romanInput[i] == 'D' || romanInput[i] == 'd')
  64.                  number = 500;
  65.              else if (romanInput[i] == 'C' || romanInput[i] == 'c')
  66.                  number = 100;
  67.              else if (romanInput[i] == 'L' || romanInput[i] == 'l')
  68.                  number = 50;
  69.              else if (romanInput[i] == 'X' || romanInput[i] == 'x')
  70.                  number = 10;
  71.              else if (romanInput[i] == 'V' || romanInput[i] == 'v')
  72.                  number = 5;
  73.              else if (romanInput[i] == 'I' || romanInput[i] == 'i')
  74.                  number = 1;
  75.              else
  76.                  number = 0;
  77.  
  78.              if (number2 < number && number2!=0)
  79.                  sum += number - number2*2;
  80.              else
  81.                  sum += number;
  82.              number2 = number;
  83.         }
  84.      arabicInput=sum;
  85.      cout<<"Conversion complete."<<endl;
  86. }
  87. RomanType::~RomanType()
  88. {
  89. };
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement