Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // CPS 171 MP4 Wonyoung Lee : Roman calculator.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- int convert_from_roman_to_decimal(char n)
- {
- switch (n){
- case 'I':
- return 1;
- break;
- case 'V':
- return 5;
- break;
- case 'X':
- return 10;
- break;
- case 'L':
- return 50;
- break;
- case 'C':
- return 100;
- break;
- case 'D':
- return 500;
- break;
- case 'M':
- return 1000;
- break;
- default:
- break;
- }
- }
- string convert_from_decimal_to_roman(int num)
- {
- string romanNum = " ";
- while (num > 0){
- if (num - 1000 >= 0){
- romanNum += "M";
- num -= 1000;
- }
- else if (num - 500 >= 0){
- romanNum += "D";
- num -= 500;
- }
- else if (num - 100 >= 0){
- romanNum += "C";
- num -= 100;
- }
- else if (num - 50 >= 0){
- romanNum += "L";
- num -= 50;
- }
- else if (num - 10 >= 0){
- romanNum += "X";
- num -= 10;
- }
- else if (num - 9 == 0){
- romanNum += "IX";
- num -= 9;
- }
- else if (num - 5 >= 0){
- romanNum += "V";
- num -= 5;
- }
- else if (num - 4 == 0){
- romanNum += "IV";
- num -= 4;
- }
- else if (num - 1 >= 0){
- romanNum += "I";
- num -= 1;
- }
- }
- return romanNum;
- }
- int main()
- {
- string romanNum1 = " ";
- string romanNum2 = " ";
- string romanResult = " ";
- int transferNum1 = 0;
- int transferNum2 = 0;
- int transferResult = 0;
- char oper = ' ';
- ifstream inputfile;
- inputfile.open("c:\\temp\\mp4romanletrdata.txt");
- inputfile >> romanNum1 >> romanNum2 >> oper;
- for (int i = 0; i < romanNum1.length(); i++) //first roman number convert to decimal.
- {
- transferNum1 += convert_from_roman_to_decimal(romanNum1[i]); //use convert_from_roman_to_decimal function to change 1 roman number to decimal.
- }
- for (int i = 0; i < romanNum2.length(); i++) //second roman number convert to decimal.
- {
- transferNum2 += convert_from_roman_to_decimal(romanNum2[i]);
- }
- transferResult = transferNum1 + transferNum2;
- cout << transferNum1 << " " << oper << " " << transferNum2 << " = " << transferResult;
- romanResult = convert_from_decimal_to_roman(transferResult);
- cout << romanResult << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement