1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
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 operator, 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 implement the following 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
End While
You must use ONE function to read a roman numeral.
B. You must correctly implement and use the following functions
in your program (-2 points off per function that is either
not correctly implemented or correctly used):
//---------------------------------------------------------------------
// This function prints, to the standard output and in purely additive
// notation, the Roman numeral corresponding to a natural number.
// If the input value is 0, nothing is printed.
// params: (in)
//---------------------------------------------------------------------
void PrintRomanNumeral ( int val )
//---------------------------------------------------------------------
// This function outputs the result in Roman numeral format to
// the standard output.
// It presumes that the operation is valid and does
// not check for that.
// params: (in, in, in, in)
//---------------------------------------------------------------------
void PrintResult ( int operand1, int operand2,
int result, char operation )
*NOTE* The above two functions should NOT perform any kind
calculation.
//---------------------------------------------------------------------
// This function returns the Roman numeral digit corresponding
// to an integer value.
// params: (in)
//---------------------------------------------------------------------
char RomanDigitChar ( int val )
//---------------------------------------------------------------------
// This function returns the integer value of a Roman numeral digit.
// params: (in)
//---------------------------------------------------------------------
int RomanDigitValue(char roman_digit)
C. Functions should be single-minded, performing a
focused task that can be well-named. You should
design pseudocode for each of these functions.
All function bodies, including the body of main,
should be NO more than 30 lines long,
including braces, blank lines and comments.
D. You must NOT use arrays. You will lose ALL points if
you use arrays.
E. You must follow the programming ground rules.
F. You must follow the formatting of the sample I/O below.
G. You must thoroughly test your program.
H. Hint: use a combination of a sentinel-controlled and end of file
loop to read in a roman numeral. Ask in class as to what the
sentinel should be. You must have ONLY one function to read in
a Roman number. Otherwise you will lose 3 points.
I. To get credit for the assignment, your solution must minimally
work on Test Case # 1 below.
J. (HINT) Have a single function that reads a roman numeral
from the standard input and returns its integer value.
It can use a loop that is a combination of a sentinel-controlled
loop and an end-of-file controlled loop. That is, you must check
for both the end of file and sentinel condition in the loop test
(which condition to check first?). You will have to read characters
from the standard input one at a time using cin.get() until the
sentinel value of '\n' is reached. As you read each character in,
convert it to its integer value using the appropriate function
listed above. Keep a running total of the sum of all the
(integer values) of the roman numerals that you read in and return
this value.
------------------------------------------------------------------------
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:
The first number is 1226
The second number is 69
Arithmetic operation is +
The sum of 1226 and 69 is MCCLXXXXV (1295)
The first number is 610
The second number is 1101
Arithmetic operation is -
The difference of 610 and 1101 is -CCCCLXXXXI (-491)
The first number is 66
The second number is 261
Arithmetic operation is /
The quotient of 66 and 261 is zero (0)
The first number is 1500
The second number is 30
Arithmetic operation is /
The quotient of 1500 and 30 is L (50)
The first number is 69
The second number is 29
Arithmetic operation is *
The product of 69 and 29 is MMI (2001)
|