convert following program to c++ import java.util.*; import java.io.*; //Class C
ID: 3568887 • Letter: C
Question
convert following program to c++
import java.util.*;
import java.io.*;
//Class ComputeRomans will get the roman values from a file
//computes the calculations and
//writes the output into a file
public class ComputeRomans
{
Scanner scan;
//this method will gets the values from the file input.txt
//and returns output to the output.txt file
boolean doCalculation() throws Exception
{
scan= new Scanner(new File("input.txt"));
String a;
String b;
String c,convertResult;
char operator;
int x, y;
int result[]=new int[10];
int i=0;
while(scan.hasNextLine())
{
a=scan.next();
x=getOperand(a);
b=scan.next();
operator=getOperator(b);
c=scan.next();
y=getOperand(c);
result[i]=doArithmetic(x,y,operator);
i++;
}
scan.close();
PrintStream print=new PrintStream(new File("output.txt"));
for(int j=0;j<i;j++)
{
System.out.println(result[j]);
convertResult=toRoman(result[j]);
System.out.println(convertResult);
print.println(convertResult);
}
print.close();
return true;
}
//to convert the string to character to get the
//character
char getOperator(String op)
{
char a;
a=op.charAt(0);
return a;
}
//this will convert the string into integer
//and returns the integer value
int getOperand(String s)
{
String str=s;
int a=convert_from_Roman(str);
if(a==-1)
{
System.out.println("Invalid operand!!");
}
return a;
}
// Routine to convert an integer to a Roman Numeral String.
// When you do this routine, you might find it handy to
// create a utility routine that looks like:
// String addRomanDigit(String starting, int num, char digit)
String convert_to_Roman(int value)
{
int num=value;
int c=0;
String s="";
if (num==0)
return "zero";
else if (num < 0)
{
s=s+"-";
num *= -1;
}
while (num >= 1000)
{
s=s+"M";
num -= 1000;
}
while (num >= 500)
{
s=s+"D";
num -= 500;
}
while (num >= 100)
{
s=s+"C";
num -= 100;
}
while (num >= 50)
{
s=s+"L";
num -= 50;
}
while (num >= 10)
{
s=s+"X";
num -= 10;
}
while (num >= 5)
{
s=s+"V";
num -= 5;
}
while (num==1)
{
s=s+"I";
num--;
}
return s;
}
// Convert Roman Numeral String to an integer. If the
// Roman Numeral String is invalid, return -1.
int convert_from_Roman(String value)
{
int i,a=0,b;
for(i=0;i<value.length();i++)
{
b=toDecimal(value.charAt(i));
if(b==0)return -1;
a=a+b;
}
return a;
}
int toDecimal(char symbol)
{
if (symbol == 'I')
return 1;
else if (symbol == 'V')
return 5;
else if (symbol == 'X')
return 10;
else if (symbol == 'L')
return 50;
else if (symbol == 'C')
return 100;
return 0;
}
// Perform the arithmetic indicated by the operator (+ - * /)
// and return answer
int doArithmetic(int operand1, int operand2, char operator)
{
// ************** FILL IN CODE
int result;
if (operator == '+')
result = operand1 + operand2;
else if (operator == '-')
result = operand1 - operand2;
else
result = 0;
return result;
}
// Returns a string containing the numeral in Roman numeral form.
public String toRoman(int num)
{
// to store the resultant string.
String Roman = "";
// Declare and Initiate our Arrays
String onesArray[] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
String tensArray[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
String hundredsArray[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
// Get the ones in the number
int % 10;
// Get the tens
num = (num - ones) / 10;
int tens = num % 10;
// Get the hundreds
num = (num - tens) / 10;
int hundreds = num % 10;
//Get and write the thousands in the number to our string
num = (num - hundreds) / 10;
for (int i = 0; i < num; i++) {
Roman += "M";
}
//Write the hundreds
if (hundreds >= 1) {
Roman += hundredsArray[hundreds - 1];
}
//Write the tens
if (tens >= 1) {
Roman += tensArray[tens - 1];
}
//And finally, write the ones
if (ones >= 1) {
Roman += onesArray[ones - 1];
}
// Return the resultant string.
return String.valueOf(Roman);
}
//main function
public static void main(String[] args)throws Exception
{
ComputeRomans rc = new ComputeRomans();
if(rc.doCalculation())
{
System.out.println("Good Job");
}
System.out.println("Completed Roman Computations");
}
}
Explanation / Answer
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class ComputeRomans {
public:
bool doCalculation() {
ifstream input;
ofstream output;
input.open("input.txt");
string a;
string b;
string c, convertResult;
char operatr;
int x, y;
int result[10];
int i = 0;
while (!input.eof())
{
input>>a;
x = getOperand(a);
input>>b;
operatr = getOperator(b);
input>>c;
y = getOperand(c);
result[i] = doArithmetic(x, y, operatr);
i++;
}
input.close();
output.open("output.txt");
for (int j = 0; j < i; j++)
{
cout << result[j] << " ";
convertResult = toRoman(result[j]);
cout << convertResult << " ";
output << convertResult << " ";
}
output.close();
return true;
}
//to convert the string to character to get the
//character
char getOperator(string op)
{
char a;
a = op.at(0);
return a;
}
//this will convert the string into integer
//and returns the integer value
int getOperand(string s)
{
string str = s;
int a = convert_from_Roman(str);
if (a == -1)
{
cout << "Invalid operand!! ";
}
return a;
}
// Routine to convert an integer to a Roman Numeral String.
// When you do this routine, you might find it handy to
// create a utility routine that looks like:
// String addRomanDigit(String starting, int num, char digit)
string convert_to_Roman(int value)
{
int num = value;
int c = 0;
string s = "";
if (num == 0)
return "zero";
else if (num < 0)
{
s = s + "-";
num *= -1;
}
while (num >= 1000)
{
s = s + "M";
num -= 1000;
}
while (num >= 500)
{
s = s + "D";
num -= 500;
}
while (num >= 100)
{
s = s + "C";
num -= 100;
}
while (num >= 50)
{
s = s + "L";
num -= 50;
}
while (num >= 10)
{
s = s + "X";
num -= 10;
}
while (num >= 5)
{
s = s + "V";
num -= 5;
}
while (num == 1)
{
s = s + "I";
num--;
}
return s;
}
// Convert Roman Numeral String to an integer. If the
// Roman Numeral String is invalid, return -1.
int convert_from_Roman(string value)
{
int i, a = 0, b;
for (i = 0; i < (sizeof (value) / sizeof (char)); i++)
{
b = toDecimal(value.at(i));
if (b == 0)return -1;
a = a + b;
}
return a;
}
int toDecimal(char symbol)
{
if (symbol == 'I')
return 1;
else if (symbol == 'V')
return 5;
else if (symbol == 'X')
return 10;
else if (symbol == 'L')
return 50;
else if (symbol == 'C')
return 100;
return 0;
}
// Perform the arithmetic indicated by the operator (+ - * /)
// and return answer
int doArithmetic(int operand1, int operand2, char operatr)
{
// ************** FILL IN CODE
int result;
if (operatr == '+')
result = operand1 + operand2;
else if (operatr == '-')
result = operand1 - operand2;
else
result = 0;
return result;
}
// Returns a string containing the numeral in Roman numeral form.
string toRoman(int num)
{
// to store the resultant string.
string Roman = "";
// Declare and Initiate our Arrays
string onesArray[] = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
string tensArray[] = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
string hundredsArray[] = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
// Get the ones in the number
int % 10;
// Get the tens
num = (num - ones) / 10;
int tens = num % 10;
// Get the hundreds
num = (num - tens) / 10;
int hundreds = num % 10;
//Get and write the thousands in the number to our string
num = (num - hundreds) / 10;
for (int i = 0; i < num; i++) {
Roman += "M";
}
//Write the hundreds
if (hundreds >= 1) {
Roman += hundredsArray[hundreds - 1];
}
//Write the tens
if (tens >= 1) {
Roman += tensArray[tens - 1];
}
//And finally, write the ones
if (ones >= 1) {
Roman += onesArray[ones - 1];
}
// Return the resultant string.
return Roman;
}
};
//main function
int main()
{
ComputeRomans rc = ComputeRomans();
if (rc.doCalculation())
{
cout << "Good Job" << " ";
}
cout << "Completed Roman Computations ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.