Write a C++ program to implement a form of a Roman numeral calculator. We are us
ID: 3640211 • Letter: W
Question
Write a C++ program to implement a form of a Roman numeral calculator.We are using the purely additive form of Roman numerals. By that, we
mean that a number is simply the sum of its digits; for example, 4 equals
IIII, in our additive notation. This means that we are NOT using IV for 4.
Each Roman numeral must start with the digit of highest value and ends with
the digit of smallest value. That is 9 is VIIII and NOT IIIIV.
Your program continually (in a loop) inputs 2 Roman numbers and an
arithmetic operator and prints the result of the operation as a Roman
number. The values of the Roman digits (upper case letters only)
are as follows:
Roman Digit Value of Roman Digit
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
So, the Roman number MMVIIII represents 2009. The arithmetic operators
that your program must recognize in the input are +, -, *, and /. These
should perform the C++ integer operations of addition, subtraction,
multiplication, and division, respectively. Your program must loop,
processing 2 Roman numbers with an opertor, finishing when end of file
is reached.
You do not have to ensure that the input is in purely additive form,
i.e., your program does NOT have to check for this.
You can assume that only positive numbers will be entered as input and
you don't have to check for negative numbers.
If the result is negative, you must print out a minus sign followed
by the absolute value of the result printed as a Roman Numeral. See
the sample runs below.
If the result is zero, print the word "zero".
------------------------------------------------------------------------
The following additional requirements must be followed:
A. Your main function must adhere to the normal end of file loops, e.g.
--- Pseudocode ----
read a roman numeral [Hint: use a value returning function]
while not at the end of file do
echo first number
read second roman numeral
echo second number
read operator
echo operator
calculate the result
print results
read a roman numeral
B. The above pseudocode suggests some functions.
Keep these functions single-minded, performing a
focused task that can be well-named.
C. You must NOT use arrays. You will lose all points if
you use arrays.
D. You must follow the formatting of the sample I/O below.
E. Hint: use a combination of a sentinel-controlled and end of file
loop to read in a roman numeral. You must have ONLY one function to read in
a Roman number. Otherwise you will lose 5 points.
F. To get credit for the assignment, your solution must minimally
work on Test Case # 1 below.
------------------------------------------------------------------------
Sample I/O
Below are two sample runs.
They do NOT cover all cases.
Input for Run 1:
MCCXXVI
LXVIIII
+
DCX
MCI
-
LXVI
CCLXI
/
MD
XXX
/
LXVIIII
XXVIIII
*
The output for Test Run 1:
MCCXXVI
The first number is 1226
LXVIIII
The second number is 69
+
Arithmetic operation is +
The sum of 1226 and 69 is MCCLXXXXV (1295)
DCX
The first number is 610
MCI
The second number is 1101
-
Arithmetic operation is -
The difference of 610 and 1101 is -CCCCLXXXXI (-491)
LXVI
The first number is 66
CCLXI
The second number is 261
/
Arithmetic operation is /
The quotient of 66 and 261 is zero (0)
MD
The first number is 1500
XXX
The second number is 30
/
Arithmetic operation is /
The quotient of 1500 and 30 is L (50)
LXVIIII
The first number is 69
XXVIIII
The second number is 29
*
Arithmetic operation is *
The product of 69 and 29 is MMI (2001)
Input for Test Run 2:
CCLXXXVII
XVIII
*
DXXVIIII
M
-
III
II
+
X
V
-
V
V
+
V
V
-
XXXX
X
+
XXXX
X
*
Output for Run 2:
CCLXXXVII
The first number is 287
XVIII
The second number is 18
*
Arithmetic operation is *
The product of 287 and 18 is MMMMMCLXVI (5166)
DXXVIIII
The first number is 529
M
The second number is 1000
-
Arithmetic operation is -
The difference of 529 and 1000 is -CCCCLXXI (-471)
III
The first number is 3
II
The second number is 2
+
Arithmetic operation is +
The sum of 3 and 2 is V (5)
X
The first number is 10
V
The second number is 5
-
Arithmetic operation is -
The difference of 10 and 5 is V (5)
V
The first number is 5
V
The second number is 5
+
Arithmetic operation is +
The sum of 5 and 5 is X (10)
V
The first number is 5
V
The second number is 5
-
Arithmetic operation is -
The difference of 5 and 5 is zero (0)
XXXX
The first number is 40
X
The second number is 10
+
Arithmetic operation is +
The sum of 40 and 10 is L (50)
XXXX
The first number is 40
X
The second number is 10
*
Arithmetic operation is *
The product of 40 and 10 is CCCC (400)
Explanation / Answer
try this one
#include <iostream>
#include <fstream>
using namespace std;
//---------------------------------------------------------
// FUNCTION DEFINITIONS
//---------------------------------------------------------
int toDecimal(char symbol)
{
if (symbol == 'I')
return 1;
else if (symbol == 'V')
return 5;
else if (symbol == 'X')
return 10;
else if (symbol == 'L')
return 50;
else if (symbol == 'C')
return 100;
else if (symbol == 'D')
return 500;
else if (symbol == 'M')
return 1000;
return 0;
}
//---------------------------------------------------------
int nextRomanNumber(ifstream& fin)
{
char temp[29]; //maximum char could be 28
temp[0] = 0;
int i = 0;
int romanNum = 0;
fin >> temp;
if (temp[0] == 0) //no input from fin
return 0; //Roman number does not have zero number,
//so 0 is an invalid number
//return 0 indicates the function cannot obtain Roman number from file input
while (temp[i] != 0)
{
romanNum += toDecimal(temp[i]);
i++;
}
return romanNum;
}
//---------------------------------------------------------
char nextOperator(ifstream& fin)
{
char oper = ' ';
fin >> oper;
return oper;
}
//---------------------------------------------------------
void calculateResult(int lhs, int rhs, char oper, int &result)
{
if (oper == '+')
result = lhs + rhs;
else if (oper == '-')
result = lhs - rhs;
else if (oper == '*')
result = lhs * rhs;
else if (oper == '/')
result = lhs / rhs;
else
result = 0;
}
//---------------------------------------------------------
void printRomanNumber(int num)
{
if (num == 0)
cout << "zero";
else if (num < 0)
{
cout << "-";
num *= -1;
}
while (num >= 1000)
{
cout << "M";
num -= 1000;
}
while (num >= 500)
{
cout << "D";
num -= 500;
}
while (num >= 100)
{
cout << "C";
num -= 100;
}
while (num >= 50)
{
cout << "L";
num -= 50;
}
while (num >= 10)
{
cout << "X";
num -= 10;
}
while (num >= 5)
{
cout << "V";
num -= 5;
}
while (num != 0)
{
cout << "I";
num--;
}
}
//---------------------------------------------------------
// MAIN
//---------------------------------------------------------
int main()
{
//LOCAL DECLARATIONS
int num1;
int num2;
char oper;
int result;
ifstream fin;
char fileName[20];
char t;
//PROCEDURES
cout << "Enter file name: ";
cin >> fileName;
cout << endl;
fin.open(fileName);
if (!fin)
{
cout << "Error: cannot open data file. Program aborting. ";
return 1;
}
while (!fin.eof())
{
num1 = nextRomanNumber(fin);
if (num1 != 0)
{
printRomanNumber(num1);
cout << " The first number is " << num1 << endl;
num2 = nextRomanNumber(fin);
printRomanNumber(num2);
cout << " The second number is " << num2 << endl;
oper = nextOperator(fin);
cout << oper << " Arithmetic operation is " << oper << endl;
calculateResult(num1, num2, oper, result);
cout << "The ";
if (oper == '+')
cout << "sum";
else if (oper == '-')
cout << "difference";
else if (oper == '*')
cout << "product";
else if (oper == '/')
cout << "quotient";
cout << " of " << num1 << " and " << num2 << " is ";
printRomanNumber(result);
cout << " (" << result << "). ";
cout << endl;
}
}
fin.close();
cin.ignore();
cin.get(t);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.