Hi! I need help with a C++ program. How can I write the code using the guideline
ID: 663315 • Letter: H
Question
Hi! I need help with a C++ program. How can I write the code using the guidelines provided? You are to design and implement a Roman numeral calculator. The subtractive Roman numeral notation commonly in use today was used only rarely during the time of the Roman Republic and Empire. For ease of calculation, the Romans most frequently used a purely additive notation in which a number was simply the sum of its digits (4 equals IIII in this notation, not IV). Each number starts with as many digits of highest value as are possible and ends with only as many digits of smallest value as remain after exhausting greater values, (see example below.) You are to use this additive notation in this program, both for input and output. Your program inputs two Roman numbers and an arithmetic operator, printing the result of the operation, also as an additive Roman number. Values of the Roman digits are as follow: I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, the number MDCCCCLXXXXVI represents 1996, because 1996 really consists of: 1000 + 500 + 100 + 100 + 100 + 100 + 50 + 10 + 10 + 10 + 10 + 5 + 1. M + D + C + C + C + C + L + X + X + X + X + V + I Wrong ways to express this value would use too many lesser valued digits when more greater valued digits could have been used. In this example, the 1,500 is correctly represented by MD *not* by DDD The arithmetic operators your program should recognize in the input are +, -, /, *, and %. These operators specify the C++ operations of integer addition, subtraction, division, multiplication and modulo. One way of approaching this problem is to convert the Roman numbers into integers, perform the required operation, and convert the result back to a Roman number to print. For an input file line of MCCXXVI LXVIIII + the following might be a sample output of the program: MCCXXVI The first number is 1226 LXVIIII The second number is 69 The desired arithmetic operation: + The sum of MCCXXVI and LXVIIII is MCCLXXXXV (1295). The program should check for errors in the input, such as illegal digits or invalid operators, and display an error message when these errors are found. If an error is found, do not process that line any further. Assume the input numbers are in purely additive form - this is, digits are followed only by digits of the same or lower value. REQUIREMENTS: This program is to be done by including the following functions. These functions must NOT reference global variables directly (use parameter lists). FUNCTION get_Data void get_Data(...) This function receives the input file, reads one line of data, checks for incorrect data and sends back the values read in and a boolean variable indicating whether or not there were an error. You do *not* have to check input is in "digit decreasing" order. If an error occurs, the function prints an error message, and sets the boolean variable true. Use a void function with reference parameters. FUNCTION print_Result void print_Result(...); This void function receives the two roman numbers, the roman result and the integer result and prints messages. It does not have to return anything to the calling program. FUNCTION convert_To_Roman string convert_to_Roman(...); This function receives an integer and returns its corresponding roman number as a string. Use a value-returning function. FUNCTION convert_From_Roman int convert_from_Roman(...); This function receives a string and returns its corresponding integer number as an integer. Use a value-returning function. FUNCTION calc_Romans int calc_Romans(...); This function is given two integers and a char and returns the result of the two integers based on the char (operator). INPUT/OUTPUT: You are to make your own datafile (contents described below). Make sure to copy the data as given below INCLUDING additional data you typed in to demonstrate the robustness of your program in checking for various situations or complications. Include a copy of the file with your program and output. The format of the data file: There are two roman numbers followed by an operator per line and the program should be designed to work even if the number of lines in the datafile is changed at any time. Numbers and operators are separated by "whitespace," i.e. space(s) or tabs. Hint: Write this program in stages, concentrating on one function at a time. E.g. first write the get_Data() function and a simple main program to test it. Make sure it works, reading data from the file correctly before working on other functions. Your main program will be quite short with lots of function calls doing all the work. ********************************************************************************** Program reads input from a file named "roman.txt" containing at least the following: MCcXxVI mCC - Xvii CL + bdD MM + XVII CL - XVII CL * CL XVII / CL XVII % MMMmmDddCCCccLlXxVvIi mmm + MM BadD + MM MM = ********************************************************************************** Starting Code: #include <iostream> #include <iomanip> #include <string> #include <fstream> using namespace std; string convert_to_Roman(/*figure out the calling sequence*/) { // fill in your code } int convert_from_Roman(/* figure out the calling sequence */) { // fill in your code } int calc_romans(/*figure out the calling sequence*/) { // fill in your code // We will be discussing the string class in more detail in // subsequent chapters. The following snippet illustrates a few // features we will encounter : // string str = "abcdefg"; // for (int i=0; i < str.length(); i++) // { // char c = str[i]; // get characters one at a time from the string // You may want to explore the C++ character functions toUpper() & toLower() // } } void print_Result(/* figure out the calling sequence */) { // fill in your code } // Note the call by reference parameters: void get_Data(ifstream & infile, string & operand1, string & operand2, char & oper, bool & badInput) { // Read in operand1, operand2, oper // Check to see if an error condition has occurred. // Set badInput for any error, but main will only print out an error condition if the // reason for the error is something other than hitting end of file(out of data). // } // I would rather have you leave main alone and just make your function calls above match // the calling sequence required by main below: int main() { ifstream infile; infile.open("roman.txt"); if (!infile) { cout << "Can't open roman.txt" << endl; return -1; } while(!infile.eof()) { string operand1, operand2; char oper; bool badInput; get_Data(infile, operand1, operand2, oper, badInput); if (badInput) { if (!infile.eof()) // Check to see if we are the end of the file cout << "Skipping Bad input"<<endl; // Must be a bad input } else { int value1 = convert_from_Roman(operand1); int value2 = convert_from_Roman(operand2); cout << "The first operand is " << operand1 << "(" << value1 << ")"<<endl; cout << "The second operand is " << operand2 << "(" << value2 << ")"<<endl; cout <<"The operator is " << oper << endl; int answer = calc_romans(oper, value1, value2); print_Result(operand1, operand2, convert_to_Roman(answer), answer); cout << endl; } } infile.close(); return 0; }
Explanation / Answer
Running out of time yaar. But this is working. I missed commenting.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void get_Data(ifstream &inFile, string &romanOne, string &romanTwo, char &op, bool &flag)
{
flag = false;
inFile>>romanOne>>romanTwo>>op;
for (int i=0; i<romanOne.length(); ++i)
{
romanOne.at(i) = toupper(romanOne.at(i));
if(romanOne.at(i) != 'I' && romanOne.at(i) != 'V' && romanOne.at(i) != 'X' && romanOne.at(i) != 'L' && romanOne.at(i) != 'C' && romanOne.at(i) != 'D' && romanOne.at(i) != 'M')
{
cout<<" Error: Invalid Roman Letter. ";
flag = true;
}
}
for (int i=0; i<romanTwo.length(); ++i)
{
romanTwo.at(i) = toupper(romanTwo.at(i));
if(romanTwo.at(i) != 'I' && romanTwo.at(i) != 'V' && romanTwo.at(i) != 'X' && romanTwo.at(i) != 'L' && romanTwo.at(i) != 'C' && romanTwo.at(i) != 'D' && romanTwo.at(i) != 'M')
{
cout<<" Error: Invalid Roman Letter. ";
flag = true;
}
}
if(op != '+' && op != '-' && op != '*' && op != '/' && op != '%')
{
cout<<"Error: Invalid operator. ";
flag = true;
}
}
void print_Result(string romanOne, string romanTwo, string romanResult, int intResult)
{
cout<<"The two given roman numbers are: "<<romanOne<<", "<<romanTwo<<endl;
cout<<"The integer result is: "<<intResult<<endl;
cout<<"The roman result is: "<<romanResult<<endl;
}
string convert_to_Roman(int num)
{
int i;
string roman = "";
if(num >= 1000)
{
for(i = 0; i < num/1000; i++)
roman.append("M");
num %= 1000;
}
if(num >= 500)
{
roman.append("D");
num -= 500;
}
if(num >= 100)
{
for(i = 0; i < num/100; i++)
roman.append("C");
num %= 100;
}
if(num >= 50)
{
roman.append("L");
num -= 50;
}
if(num >= 10)
{
for(i = 0; i < num/10; i++)
roman.append("X");
num %= 10;
}
if(num >= 5)
{
roman.append("V");
num -= 5;
}
for(i = 0; i < num; i++)
roman.append("I");
return roman;
}
int convert_From_Roman(string roman)
{
int number = 0;
char c;
for (int i=0; i<roman.length(); ++i)
{
c= roman.at(i);
if(c == 'M')
number += 1000;
else if(c == 'D')
number += 500;
else if(c == 'C')
number += 100;
else if(c == 'L')
number += 50;
else if(c == 'X')
number += 10;
else if(c == 'V')
number += 5;
else
number += 1;
}
return number;
}
int calc_Romans(int num1, int num2, char op)
{
switch(op)
{
case '+': return num1+num2;
case '-': return num1-num2;
case '*': return num1*num2;
case '/': return num1/num2;
case '%': return num1%num2;
}
return 0;
}
int main()
{
string romanOne, romanTwo, romanResult;
char op;
int num1, num2, numResult;
bool errorFlag;
cout<<"Enter the file name: ";
string fileName;
cin>>fileName;
ifstream inFile;
inFile.open(fileName);
while(!inFile.eof())
{
get_Data(inFile, romanOne, romanTwo, op, errorFlag);
if(errorFlag == true)
continue;
num1 = convert_From_Roman(romanOne);
num2 = convert_From_Roman(romanTwo);
numResult = calc_Romans(num1, num2, op);
romanResult = convert_to_Roman(numResult);
print_Result(romanOne, romanTwo, romanResult, numResult);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.