Laura Croft is getting ready to make a trip to Egypt to check out a new tomb. Sh
ID: 3568345 • Letter: L
Question
Laura Croft is getting ready to make a trip to Egypt to check out a new tomb. She has been
notified that the each door into the tomb is locked by an ancient combination lock. To enter the tomb, a
person must solve a simple mathematical calculation. Unfortunately all of the numbers are written in
Roman numerals. As part of her team, your job is to write a program that will quickly perform the
calculation and present the result as a Roman numeral.
As with previous programs, this program must use modular programming. Remember that functions
should be designed to do a single task.
The program will read the calculations from a file, perform the operation on the Roman numerals and
send the answer to a file.
Input: All input will come from a file named input.txt. The file will contain simple mathematical
equations, each one on a separate line. The format for each line will be:
<Roman numeral><space><operator><space><Roman numeral>
Each Roman numeral will be valid and will represent a number from 1 100. The operators available
are + and -. There will be no multiplication or division in the file, only addition and subtraction. There is
no limit to the number of calculations in the file.
Output: All output will be written to a file named output.txt. Once the calculation is performed, the
answer will be written to the file in Roman numeral form. Each answer will be written on a separate
line.
Roman Numerals: This project will involve the following Roman numerals:
? C = 100
? L = 50
? X = 10
? V = 5
? I = 1
The letters that comprise Roman numerals will generally be in the order listed above. For example
LXXVI represents 50 + 10 + 10 + 5 + 1 or 76. Occasionally the letters may appear in an improper order.
This happens or values of 4 and 9. For example, XL = 40 (-10 + 50). When the letter is not in the right
order, it signifies the number out of order is a negative number rather than a positive number.
Reading From the File: It will probably be easier for you to complete this assignment by reading from
the file a character at a time and determining what to do with the character you read. And since the
positivity of the letter in the Roman numeral is dependent upon the next letter in the Roman numeral,
you will want to write the program to read in the next character before deciding if the value is positive
or negative.
Explanation / Answer
PLEASE PROVIDE THE input.txt FILE CONTAINING DATA
#include<iostream>
#include<fstream>
using namespace std;
int value(char roman)
{
switch(roman)
{
case 'I':return 1;
case 'V':return 5;
case 'X':return 10;
case 'L':return 50;
case 'C':return 100;
case 'D':return 500;
case 'M':return 1000;
}
}
int getdec(const string& input)
{
int sum=0; char prev='%';
for(int i=(input.length()-1); i>=0; i--)
{
if(value(input[i])<sum && (input[i]!=prev))
{ sum -= value(input[i]);
prev = input[i];
}
else
{
sum += value(input[i]);
prev = input[i];
}
}
return sum;
}
std::string a_romano(int value){
struct romandata_t { int value; char const* numeral; };
static romandata_t const romandata[] =
{ 1000, "M",
900, "CM",
500, "D",
400, "CD",
100, "C",
90, "XC",
50, "L",
40, "XL",
10, "X",
9, "IX",
5, "V",
4, "IV",
1, "I",
0, NULL }; // end marker
std::string result;
for (romandata_t const* current = romandata; current->value > 0; ++current){
while (value >= current->value){
result += current->numeral;
value -= current->value;
}
}
return result;
}
int main()
{
ifstream file;
file.open("input.txt");
ofstream outfile;
outfile.open("output.txt");
char Roman1[20];
int R1;
char Roman2[20];
int R2;
char operatr;
while(!file.eof())
{
file>>Roman1>>operatr>>Roman2;
R1= getdec(Roman1);
R2= getdec(Roman2);
switch (operatr)
{
case '+':
outfile<<a_romano(R1+R2).c_str()<<" ";
break;
case '-':
outfile<<a_romano(R1-R2).c_str()<<" ";
break;
default:
break;
}
cout<<" ";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.