Here's what the output looks like:
Nothing flashy, but it's functional.
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
using namespace std;
char RomanNumeral[10];
float numberarray[10];
float number1, number2;
float ans;
float convert();
float temp = 0;
int again();
int main()
{
cout << "Roman Numeral Calculator\n\n";
cout << "_____________________________
cout << "Enter the first Roman Numeral: ";
convert();
number1 = temp;
cout <<"\nEnter +, -, *, or /: "; //add, subtract...etc.
char action;
cin >> action;
cout << "\nEnter the second Roman Numeral: ";
convert();
number2 = temp;
//Perform the selected action on the two numbers
if(action == '+')
ans = number1 + number2;
else if(action == '-')
ans = number1 - number2;
else if(action == '*')
ans = number1 * number2;
else if(action == '/')
ans = number1 / number2;
else
{cout<<"Error, please try again";
main();}
if(action == '+')
ans = number1 + number2;
else if(action == '-')
ans = number1 - number2;
else if(action == '*')
ans = number1 * number2;
else if(action == '/')
ans = number1 / number2;
else
{cout<<"Error, please try again";
main();}
cout<<dec<<"Answer: "<<ans<<endl;
again();
return 0;
} //end main
float convert()
{
for(int y = 0; y < 10; ++y) //reset the arrays
{
RomanNumeral[y] = 0;
numberarray[y] = 0;
}
cin >> RomanNumeral; //get user input
for(int y = 0; y <= 10; ++y) //Populate the number array with decimal values, allowing for upper or lowercase roman numeral entries
{
if((RomanNumeral[y] == 'I') || (RomanNumeral[y] == 'i'))
{numberarray[y] = 1;}
else if((RomanNumeral[y] == 'V')||(RomanNumeral[y] == 'v'))
{numberarray[y] = 5;}
else if((RomanNumeral[y] == 'X')||(RomanNumeral[y] == 'x'))
{numberarray[y] = 10;}
else if((RomanNumeral[y] == 'L')||(RomanNumeral[y] == 'l'))
{numberarray[y] = 50;}
else if((RomanNumeral[y] == 'C')||(RomanNumeral[y] == 'c'))
{numberarray[y] = 100;}
else if((RomanNumeral[y] == 'D')||(RomanNumeral[y] == 'd'))
{numberarray[y] = 500;}
else if((RomanNumeral[y] == 'M')||(RomanNumeral[y] == 'm'))
{numberarray[y] = 1000;}
}
temp = numberarray[0]; //set the variable equal to the first value in the number array
for(int y = 1; y < 10; ++y) //This is where the magic happens
{
if(numberarray[y] > numberarray[y-1])
temp += (numberarray[y] - 2*numberarray[y-1]); //the 2 multiplier is key. without it the program adds the value back in and ruins the answer.
else if(numberarray[y] < numberarray[y-1])
temp += numberarray[y];
else if(numberarray[y] == numberarray[y-1])
temp += numberarray[y];
}
cout<<"\nDecimal equivalent = "<<temp<<endl;
return(temp); //Send decimal equivalent back to main
} //end convert
int again() //Run main again or bail out of the program
{
char selection;
cout<<endl<<"Another calculation? y/n ";
cin >>selection;
if((selection == 'y')||selection == 'Y')
{
for(int y = 0; y<10; ++y)
{
numberarray[y] = 0; //reset numberarray to all zeros
RomanNumeral[y] = 0; //reset letterarray
}
number1 = 0;
number2 = 0;
temp = 0; //everything is reset to zero for next round
main();
}
else
{
cout<<endl;
cin.ignore();
system("pause");
}
return 0;
} // end again
No comments:
Post a Comment