Question: Download the file Calculator.java, which is part of the assignment handout. This program simulates a 4-function calculator. Input to the calculator consists of a sequence
Download the file Calculator.java, which is part of the assignment handout. This program simulates a 4-function calculator. Input to the calculator consists of a sequence of lines. The first character of each line is one of the following 7 operators: '+', '-', '*', '/', '=', 'C', and 'Q'. The first 4 operators are arithmetic operators. Lines having one of these operators also contain a floating-point number (possibly separated by whitespace) following the operator. The calculator always has a "current amount", which is initially 0. The meaning of an arithmetic operation is to modify the current amount by performing the specified arithmetic to it. The remaining 3 operators are the only things on their lines. Operator '=' causes the current amount of the calculator to be printed. Operator 'C' causes the current amount to be 0. Operator 'Q' causes the program to quit. Your assignment is to translate this program into C. You must keep the code as close to the Java version as possible, changing something only when necessary. In particular, don't get rid of (or add) any functions. But don't forget to add declarations for these functions to your C code! The only tricky part concerns input. Each line consists of the following: one character, some whitespace, a float, perhaps more whitespace, and a newline character. You should read the line character by character, except for the float. In the Java code a BufferedReader is used to do the reading, because it has the method read (which reads a single char) as well as readLine (which reads the remainder of the line, including the newline). In your C code, you need to use a mix of getchar and scanf. In particular, you will need to use getchar to read the operation, then use scanf to read the float, and then use getchar again to read the remaining characters in the line up through the newline. In Java code file it is required to catch the IO exceptions. C doesn't, so don't bother.

import java.io.*;
class Calculator {
private static Reader r = new InputStreamReader(System.in);
private static BufferedReader br = new BufferedReader(r);
public static void main(String[] args) {
float amt = 0.0F; // the current value of the calculator
char op; // the operation to be performed
float val; // the value of the operation
System.out.println("CS 2271 Calculator");
while (true) {
// Read an input line. The first char of each line is the
operation.
// Arithmetic operations are followed by whitespace and a
number.
try {
int ic = br.read();
if (ic
op = (char) ic;
if (op == 'Q') break;
String s = br.readLine();
if (takesNoArgs(op))
val = 0.0F;
else
val = Float.parseFloat(s);
}
catch (IOException e) { // abort on any IO exception
throw new RuntimeException("Something bad happened");
}
// Call a fn to process the op and its val.
amt = processOp(amt, op, val);
}
System.out.println("Thank you and goodbye!");
}
static boolean takesNoArgs(char op) {
return (op == 'C') || (op == '=');
}
static float processOp(float amt, char op, float val) {
if (op == '+')
return processPlus(amt, val);
else if (op == '-')
return processMinus(amt, val);
else if (op == '*')
return processTimes(amt, val);
else if (op == '/')
return processDivide(amt, val);
else if (op == '=')
return processPrint(amt);
else if (op == 'C')
return processClear();
else {
System.out.println("Bad Operator");
return amt;
}
}
static float processPlus(float amt, float val) {
return amt + val;
}
static float processMinus(float amt, float val) {
return amt - val;
}
static float processTimes(float amt, float val) {
return amt * val;
}
static float processDivide(float amt, float val) {
return amt / val;
}
static float processPrint(float amt) {
System.out.println(amt);
return amt;
}
static float processClear() {
return 0;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
