Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Matt Short
- CPSC 131
- Purpose: Contains the implementation of
- the class members for RomanType
- */
- //IMPLEMENTATION FILE
- #include <iostream>
- #include "RomanType.h"
- using namespace std;
- RomanType::RomanType()
- {
- };
- void RomanType::getArabicNumber()
- {
- cout<<"Please enter an Arabic Number: ";
- cin>>arabicInput;
- }
- void RomanType::getRomanNumber()
- {
- cout<<"Please enter a Roman Numeral: ";
- cin>>romanInput;
- }
- void RomanType::printRoman()
- {
- cout<<romanInput<<endl;
- }
- void RomanType::printArabic()
- {
- cout<<arabicInput<<endl;
- }
- void RomanType::convertToRoman()
- {
- int arabic2 = arabicInput;//a copy of arabicInput
- string romanLets[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};//array of roman numerals
- int arabicNums[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};//matching array of arabic numbers
- string romanString = "";//string to hold roman numerals
- for(int i=0;i<13;i++)//loop that writes roman numerals into romanString
- {
- while(arabic2>=arabicNums[i])
- {
- arabic2 -= arabicNums[i];
- romanString += romanLets[i];
- }
- }
- int len = romanString.size();//int that holds length of romanString
- for(int i=0;i<=len;i++)//loop that moves romanString into romanInput
- {
- romanInput[i]=romanString[i];
- }
- cout<<"Conversion complete."<<endl;
- }
- void RomanType::convertToArabic()
- {
- int len = strlen(romanInput);//int that holds length of romanInput
- int number = 0;//int to hold value of roman numeral
- int i = 0;//counting variable
- int number2 = 0;//int to hold value of previous roman numeral
- int sum = 0;//int to hold total/total-thus-far in arabic numbers
- for (i=0;i<len;i++)
- {
- if (romanInput[i] == 'M' || romanInput[i] == 'm')
- number = 1000;
- else if (romanInput[i] == 'D' || romanInput[i] == 'd')
- number = 500;
- else if (romanInput[i] == 'C' || romanInput[i] == 'c')
- number = 100;
- else if (romanInput[i] == 'L' || romanInput[i] == 'l')
- number = 50;
- else if (romanInput[i] == 'X' || romanInput[i] == 'x')
- number = 10;
- else if (romanInput[i] == 'V' || romanInput[i] == 'v')
- number = 5;
- else if (romanInput[i] == 'I' || romanInput[i] == 'i')
- number = 1;
- else
- number = 0;
- if (number2 < number && number2!=0)
- sum += number - number2*2;
- else
- sum += number;
- number2 = number;
- }
- arabicInput=sum;
- cout<<"Conversion complete."<<endl;
- }
- RomanType::~RomanType()
- {
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement