Laura Croft is getting ready to make a trip to Egypt to check out a new tomb. Sh
ID: 3569136 • 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.
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
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.