Problem: Write a c++ program that will convert roman numbers into its equivalent decimal numbers.
This program takes a roman number as input and converts into decimal number. please make sure to keep the caps lock on. Run this program in devc++
This program takes a roman number as input and converts into decimal number. please make sure to keep the caps lock on. Run this program in devc++
#include <iostream> #include <string> using namespace std; class RomanNumber { private: string romanNumber; int sum; public: RomanNumber( string input) { romanNumber = input; } int convert() { int length = romanNumber.length(); int previous = 0; bool error = false; int nIndex = 0; sum = 0; while( (error == false) && (nIndex < length) ) { switch(romanNumber[nIndex]) { case 'M': sum += 1000; if(previous < 1000) { sum -= 2 * previous; } previous = 1000; break; case 'D': sum += 500; if(previous < 500) { sum -= 2 * previous; } previous = 500; break; case 'C': sum += 100; if(previous < 100) { sum -= 2 * previous; } previous = 100; break; case 'L': sum += 50; if(previous < 50) { sum -= 2 * previous; } previous = 50; break; case 'X': sum += 10; if(previous < 10) { sum -= 2 * previous; } previous = 10; break; case 'V': sum += 5; if(previous < 5) { sum -= 2 * previous; } previous = 5; break; case 'I': sum += 1; if(previous < 1) { sum -= 2 * previous; } previous = 1; break; default: cout << romanNumber[nIndex] << " is not a Roman Numeral!" << endl; error = true; sum = 0; } // switch nIndex++; } // while return sum; } }; int main() { system("color 1F"); string myInput; cout<<"\n\n\t\t....ROMAN NUMBER TO DECIMAL CONVERTER....\n\n"; cout<<"\n\n\tEnter your input in Roman Number(Plz Keep Caps Lock On): "; cin>>myInput; RomanNumber myRomanNumber(myInput); int value=myRomanNumber.convert(); cout << "\n\n\t\tRoman Number " << myInput << " is equals to Decimal " << value <<endl<<endl; system("pause"); return 0; }
0 comments:
Post a Comment