It\'s a java code and should be like the example below, you have to use stack th
ID: 3853569 • Letter: I
Question
It's a java code and should be like the example below, you have to use stack tho
Your task is to use a stack to implement a calculator with undo functionality. Your calculator should support addition, subtraction, multiplication and division. In addition, typing 'u' should undo the most recent operation and typing 'q' should quit the program. Your program should begin by creating a stack of Doubles and pushing the starting value 0.0 onto the stack. Following this, it should enter a loop in which it displays the current total (i.e. the number on the top of the stack) and then reads in a command and executes the appropriate action. Commands can be of two forms: an arithmetic operator +-or /) followed by a decimal number or a character command (u or q). If the command is of the first type, execute the appropriate operation and push the new value onto the stack. If the command is u, manipulate the stack to undo the most recent arithmetic operation. If you are already back at the first operation, then display the message "Nothing to undo." If the command is q, the program should exit. An example execution is shown below. Note that you should warn the user if their input is not a valid command.Explanation / Answer
//Please see the solution below please do thumbs up if you like the solution
import java.util.*;
public class Calculator{
static double total;
static double temptotal;
public static void main(String[] args) {
System.out.println("Total: "+total);
temptotal=total;
boolean keepRunning = true;
while (keepRunning) {
Scanner input = new Scanner(System.in);
String str = input.nextLine();
if ("q".equals(str)) {
keepRunning = false;
}
else
{
if( evaluate(str)==1)
System.out.println("Total: "+total);
}
}
}
public static int evaluate(String expression)
{
char[] tokens = expression.toCharArray();
if(tokens.length==0)
{
return 0;
}
// Stack for Operators: 'ops'
Stack<Character> ops = new Stack<Character>();
Stack<Double> values = new Stack<Double>();
if (tokens[0] == 'u' && tokens.length==1)
{
total=temptotal;
return 1;
}
if (tokens[0] == '+' || tokens[0] == '-' || tokens[0] == '*' || tokens[0] == '/')
{
ops.push(tokens[0]);
}
else
{
System.out.println("Invalid input: command must start with one of + - * / q(for quitting) or u (for undo)");
return 0;
}
if(tokens.length<2)
{
System.out.println("Invalid input: command beginning with a mathematic operator must be followed by a number");
return 0;
}
StringBuffer sbuf = new StringBuffer();
for (int i = 1; i < tokens.length; i++)
{
// Current token is a number, push it to stack for numbers
if ((tokens[i] >= '0' && tokens[i] <= '9') || (tokens[i]=='.' &&tokens.length >2))
{
sbuf.append(tokens[i]);
}
else
{
System.out.println("Invalid input: command beginning with a mathematic operator must be followed by a number");
return 0;
}
}
values.push(Double.parseDouble(sbuf.toString()));
temptotal=total;
total=applyOp(ops.pop(),total,values.pop());
return 1;
}
public static double applyOp(char op, double a, double b)
{
switch (op)
{
case '+':
return a + b;
case '-':
return a - b;
case '*':
return a * b;
case '/':
if (b == 0)
throw new
UnsupportedOperationException("Cannot divide by zero");
return a / b;
}
return 0;
}
}
output:
Total: 0.0
+17
Total: 17.0
-4
Total: 13.0
u
Total: 17.0
bob
Invalid input: command must start with one of + - * / q(for quitting) or u (for undo)
/
Invalid input: command beginning with a mathematic operator must be followed by a number
/bob
Invalid input: command beginning with a mathematic operator must be followed by a number
+3
Total: 20.0
*5
Total: 100.0
/12.5
Total: 8.0
u
Total: 100.0
q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.