- #1
kylekn
- 1
- 0
I NEED THIS C++ CODE TO WORK.The CORRECT output (line numbers added for reference):
THIS IS WHAT IS SUPPOSED TO HAPPEN.
1: MCCXXVI
2: The first number is 1226
3: LXVIIII
4: The second number is 69
5: +
6: Arithmetic operation is +
7: The sum of 1226 and 69 is MCCLXXXXV (1295)
8:
9: DCX
10: The first number is 610
11: MCI
12: The second number is 1101
13: -
14: Arithmetic operation is -
15: The difference of 610 and 1101 is -CCCCLXXXXI (-491)
16:
17: LXVI
18: The first number is 66
19: CCLXI
20: The second number is 261
21: /
22: Arithmetic operation is /
23: The quotient of 66 and 261 is zero (0)
24:
25: MD
26: The first number is 1500
27: XXX
28: The second number is 30
29: /
30: Arithmetic operation is /
31: The quotient of 1500 and 30 is L (50)
32:
33: LXVIIII
34: The first number is 69
35: XXVIIII
36: The second number is 29
37: *
38: Arithmetic operation is *
39: The product of 69 and 29 is MMI (2001)
THIS IS WHAT I GET WHEN I RUN IT
Output generated by your program from test test1.txt (line numbers added):
1: MCCXXVI
2: LXVIIII
3: +
4: The first number is 1226
5:
6: Arithmetic operation is +
7: The sum of 1226 and 69 is MCCCCLLLLLXXXXV (1295)
8:
HERE IS MY CODE.
#include <iostream>
#include <string>
using namespace std;
const int BAD_VAL = -1;
const int HIGH_NUM = 2;
const int ROMAN_I = 1 ;
const int ROMAN_V = 5 ;
const int ROMAN_X = 10 ;
const int ROMAN_L = 50 ;
const int ROMAN_C = 100 ;
const int ROMAN_D = 500 ;
const int ROMAN_M = 1000 ;
int GetRomanNumeral ();
int RomanDigitValue ( char );
char RomanDigitChar ( int );
void PrintRomenNumeral ( int );
int Choose ( int val );
void Symbol_Display ( char calc );
int calculation ( int num_1, int num_2, char calc );
int Negatives ( int val );
void Display_Text ( int num_1, int num_2, int count );
void Display_Answer ( int num_1, int num_2, int val, char calc );
void Error_Message ( int count );
int main ()
{
int fail = 0;
char calc;
int num_1, num_2 = 1;
int num = 0;
num_1 = GetRomanNumeral ();
while ( cin && fail == 0 )
{
if ( num_1 != BAD_VAL )
{
num_2 = GetRomanNumeral ();
if ( num_2 != BAD_VAL )
{
cin.get ( calc );
num = calculation ( num_1, num_2, calc );
Display_Text ( num_1, num_2, fail );
Display_Answer ( num_1, num_2, num, calc );
num_1 = GetRomanNumeral ();
return 0;
}
else
fail= 1;
Error_Message ( fail );
}
else
{
fail = 1;
Error_Message ( fail );
}
}
return 0;
}
int GetRomanNumeral ()
{
int val = 0;
int total = 0;
char roman_digit = 0;
cin.get ( roman_digit);
while ( cin && ( roman_digit != '\n' ) )
{
total = RomanDigitValue ( roman_digit );
if ( total == BAD_VAL )
return BAD_VAL;
else
val = val + total;
cin.get ( roman_digit );
}
return val;
}
int RomanDigitValue ( char roman_digit )
{
int number;
if ( roman_digit == 'I' )
number = ROMAN_I;
else if ( roman_digit == 'V' )
number = ROMAN_V;
else if ( roman_digit == 'X' )
number = ROMAN_X;
else if ( roman_digit == 'L' )
number = ROMAN_L;
else if ( roman_digit == 'C' )
number = ROMAN_C;
else if ( roman_digit == 'D' )
number = ROMAN_D;
else if ( roman_digit == 'M' )
number = ROMAN_M;
else
number = BAD_VAL;
return number;
}
char RomanDigitChar ( int val )
{
if ( val >= ROMAN_M )
return 'M';
else if ( val >= ROMAN_D )
return 'D';
else if ( val >= ROMAN_C )
return 'C';
else if ( val >= ROMAN_L )
return 'L';
else if ( val >= ROMAN_X )
return 'X';
else if ( val >= ROMAN_V )
return 'V';
else if ( val >= ROMAN_I )
return 'I';
return 0;
}
void PrintRomenNumeral ( int val )
{
char rome_dig;
val = Negatives ( val );
if ( val == 0 )
cout << "zero";
while ( val != 0 )
{
rome_dig = RomanDigitChar ( val );
cout << rome_dig;
val = Choose ( val );
}
}
int Choose ( int val )
{
if ( val >= ROMAN_M )
val = val - ROMAN_M;
else if ( val >= ROMAN_D )
val = val - ROMAN_D;
else if ( val >= ROMAN_C )
val = val - ROMAN_L;
else if ( val >= ROMAN_X )
val = val - ROMAN_X;
else if ( val >= ROMAN_V )
val = val - ROMAN_V;
else if ( val >= ROMAN_I )
val = val - ROMAN_I;
return val;
}
void Symbol_Display ( char calc )
{
if ( calc == '+' )
cout << "sum ";
else if ( calc == '-' )
cout << "difference ";
else if ( calc == '*' )
cout << "product ";
else if ( calc == '/' )
cout << "difference ";
}
int calculation ( int num_1, int num_2, char calc )
{
int total_sum;
if ( calc == '+' )
total_sum = num_1 + num_2;
else if ( calc == '-' )
total_sum = num_1 - num_2;
else if ( calc == '*' )
total_sum = num_1 * num_2;
else if ( calc == '/' )
total_sum = num_1 / num_2;
return total_sum;
}
int Negatives ( int val )
{
if ( val < 0 )
{
cout << "-";
val = val * -1;
}
return val;
}
void Display_Text ( int num_1, int num_2, int fail )
{
if ( fail == 0)
cout << "The first number is " << num_1 << endl;
else if ( fail == 1 )
cout << "The second number is " << num_2 << endl;
cout << endl;
}
void Display_Answer ( int num_1, int num_2, int val, char calc )
{
cout << "Arithmetic operation is " << calc << endl;
cout << "The ";
Symbol_Display ( calc ) ;
cout << "of " << num_1 << " and " << num_2 << " is ";
PrintRomenNumeral ( val );
cout << " (" << val << ") " << endl;
cout << endl;
}
void Error_Message ( int fail )
{
if ( fail == 0 )
cout << "The first number is invalid!";
else
cout << "The second number is invalid!";
}
THIS IS WHAT IS SUPPOSED TO HAPPEN.
1: MCCXXVI
2: The first number is 1226
3: LXVIIII
4: The second number is 69
5: +
6: Arithmetic operation is +
7: The sum of 1226 and 69 is MCCLXXXXV (1295)
8:
9: DCX
10: The first number is 610
11: MCI
12: The second number is 1101
13: -
14: Arithmetic operation is -
15: The difference of 610 and 1101 is -CCCCLXXXXI (-491)
16:
17: LXVI
18: The first number is 66
19: CCLXI
20: The second number is 261
21: /
22: Arithmetic operation is /
23: The quotient of 66 and 261 is zero (0)
24:
25: MD
26: The first number is 1500
27: XXX
28: The second number is 30
29: /
30: Arithmetic operation is /
31: The quotient of 1500 and 30 is L (50)
32:
33: LXVIIII
34: The first number is 69
35: XXVIIII
36: The second number is 29
37: *
38: Arithmetic operation is *
39: The product of 69 and 29 is MMI (2001)
THIS IS WHAT I GET WHEN I RUN IT
Output generated by your program from test test1.txt (line numbers added):
1: MCCXXVI
2: LXVIIII
3: +
4: The first number is 1226
5:
6: Arithmetic operation is +
7: The sum of 1226 and 69 is MCCCCLLLLLXXXXV (1295)
8:
HERE IS MY CODE.
#include <iostream>
#include <string>
using namespace std;
const int BAD_VAL = -1;
const int HIGH_NUM = 2;
const int ROMAN_I = 1 ;
const int ROMAN_V = 5 ;
const int ROMAN_X = 10 ;
const int ROMAN_L = 50 ;
const int ROMAN_C = 100 ;
const int ROMAN_D = 500 ;
const int ROMAN_M = 1000 ;
int GetRomanNumeral ();
int RomanDigitValue ( char );
char RomanDigitChar ( int );
void PrintRomenNumeral ( int );
int Choose ( int val );
void Symbol_Display ( char calc );
int calculation ( int num_1, int num_2, char calc );
int Negatives ( int val );
void Display_Text ( int num_1, int num_2, int count );
void Display_Answer ( int num_1, int num_2, int val, char calc );
void Error_Message ( int count );
int main ()
{
int fail = 0;
char calc;
int num_1, num_2 = 1;
int num = 0;
num_1 = GetRomanNumeral ();
while ( cin && fail == 0 )
{
if ( num_1 != BAD_VAL )
{
num_2 = GetRomanNumeral ();
if ( num_2 != BAD_VAL )
{
cin.get ( calc );
num = calculation ( num_1, num_2, calc );
Display_Text ( num_1, num_2, fail );
Display_Answer ( num_1, num_2, num, calc );
num_1 = GetRomanNumeral ();
return 0;
}
else
fail= 1;
Error_Message ( fail );
}
else
{
fail = 1;
Error_Message ( fail );
}
}
return 0;
}
int GetRomanNumeral ()
{
int val = 0;
int total = 0;
char roman_digit = 0;
cin.get ( roman_digit);
while ( cin && ( roman_digit != '\n' ) )
{
total = RomanDigitValue ( roman_digit );
if ( total == BAD_VAL )
return BAD_VAL;
else
val = val + total;
cin.get ( roman_digit );
}
return val;
}
int RomanDigitValue ( char roman_digit )
{
int number;
if ( roman_digit == 'I' )
number = ROMAN_I;
else if ( roman_digit == 'V' )
number = ROMAN_V;
else if ( roman_digit == 'X' )
number = ROMAN_X;
else if ( roman_digit == 'L' )
number = ROMAN_L;
else if ( roman_digit == 'C' )
number = ROMAN_C;
else if ( roman_digit == 'D' )
number = ROMAN_D;
else if ( roman_digit == 'M' )
number = ROMAN_M;
else
number = BAD_VAL;
return number;
}
char RomanDigitChar ( int val )
{
if ( val >= ROMAN_M )
return 'M';
else if ( val >= ROMAN_D )
return 'D';
else if ( val >= ROMAN_C )
return 'C';
else if ( val >= ROMAN_L )
return 'L';
else if ( val >= ROMAN_X )
return 'X';
else if ( val >= ROMAN_V )
return 'V';
else if ( val >= ROMAN_I )
return 'I';
return 0;
}
void PrintRomenNumeral ( int val )
{
char rome_dig;
val = Negatives ( val );
if ( val == 0 )
cout << "zero";
while ( val != 0 )
{
rome_dig = RomanDigitChar ( val );
cout << rome_dig;
val = Choose ( val );
}
}
int Choose ( int val )
{
if ( val >= ROMAN_M )
val = val - ROMAN_M;
else if ( val >= ROMAN_D )
val = val - ROMAN_D;
else if ( val >= ROMAN_C )
val = val - ROMAN_L;
else if ( val >= ROMAN_X )
val = val - ROMAN_X;
else if ( val >= ROMAN_V )
val = val - ROMAN_V;
else if ( val >= ROMAN_I )
val = val - ROMAN_I;
return val;
}
void Symbol_Display ( char calc )
{
if ( calc == '+' )
cout << "sum ";
else if ( calc == '-' )
cout << "difference ";
else if ( calc == '*' )
cout << "product ";
else if ( calc == '/' )
cout << "difference ";
}
int calculation ( int num_1, int num_2, char calc )
{
int total_sum;
if ( calc == '+' )
total_sum = num_1 + num_2;
else if ( calc == '-' )
total_sum = num_1 - num_2;
else if ( calc == '*' )
total_sum = num_1 * num_2;
else if ( calc == '/' )
total_sum = num_1 / num_2;
return total_sum;
}
int Negatives ( int val )
{
if ( val < 0 )
{
cout << "-";
val = val * -1;
}
return val;
}
void Display_Text ( int num_1, int num_2, int fail )
{
if ( fail == 0)
cout << "The first number is " << num_1 << endl;
else if ( fail == 1 )
cout << "The second number is " << num_2 << endl;
cout << endl;
}
void Display_Answer ( int num_1, int num_2, int val, char calc )
{
cout << "Arithmetic operation is " << calc << endl;
cout << "The ";
Symbol_Display ( calc ) ;
cout << "of " << num_1 << " and " << num_2 << " is ";
PrintRomenNumeral ( val );
cout << " (" << val << ") " << endl;
cout << endl;
}
void Error_Message ( int fail )
{
if ( fail == 0 )
cout << "The first number is invalid!";
else
cout << "The second number is invalid!";
}