Question: How do i add sin cos tan sqrt to this postfix calc import java.util.Arrays; import java.util.HashMap; import java.util.Scanner; import java.util.Stack; public class PostFixCalc { public
How do i add sin cos tan sqrt to this postfix calc
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;
public class PostFixCalc {
public static boolean isCommandPromptOn = true;
public static HashMap
public static String[] input;
public static boolean variableCalcuation = false;
public static void main(String[] args)
{
Scanner myScanner = new Scanner(System.in);
System.out.println("Integer postfix calculator with memory by Samuel Fipps");
do
{
System.out.print(" > ");
input = myScanner.nextLine().split("\\s");
commandOperator(input);
}while(isCommandPromptOn);
}
public static void commandOperator(String[] command)
{
variableCalcuation = false;
if(command.length > 1)
{
if(command[1].equals("="))
{
variableCalcuation = true;
double temp = postFixExpr(Arrays.copyOfRange(command,2,command.length));
if(temp != -123) {
myVariables.put(command[0], temp);
}
return;
}
}
for (int i = 0; i < command.length; i++)
{
switch (command[i])
{
case "quit":
isCommandPromptOn = false;
break;
case "clear":
myVariables.clear();
break;
case "delete":
myVariables.remove(command[i + 1]);
break;
case "var":
System.out.println(myVariables);
break;
case "":
System.out.println("Invalid input!");
break;
case "about":
System.out.println("Postfix calculator by Samuel Fipps");
System.out.println("Operators and functions: + - * / ^ sin cos tan sqrt");
System.out.println("Commands: var clear quit about");
break;
default:
postFixExpr(command);
return;
}
}
}
public static double postFixExpr(String[] exprArray)
{
double result = -123;
Stack
for (int i =0; i < exprArray.length;i++)
{
try {
switch (exprArray[i]) {
case "+":
double sum = exprStack.pop() + exprStack.pop();
exprStack.push(sum);
break;
case "-":
double b = exprStack.pop();
double a = exprStack.pop();
exprStack.push(a - b);
break;
case "*":
exprStack.push(exprStack.pop() * exprStack.pop());
break;
case "/":
double b1 = exprStack.pop();
double a1 = exprStack.pop();
if (b1 == 0) {
System.out.println("Division by zero. Undefined.");
result = -123;
break;
}
exprStack.push((double) (a1 / b1));
break;
case "^":
double b2 = exprStack.pop();
double a2 = exprStack.pop();
exprStack.push((double) (Math.pow(a2, b2)));
break;
// case "!":
// double a3 = exprStack.pop();
//exprStack.push(factorialCalculator(a3));
//break;
case "":
System.out.println("Invalid Input. Please try again!");
break;
default:
if (myVariables.containsKey(exprArray[i])) {
exprStack.push(myVariables.get(exprArray[i]));
} else {
try {
exprStack.push(Double.parseDouble(exprArray[i]));
} catch (Exception e) {
myVariables.put(exprArray[i], 0.0);
try {
exprStack.push(myVariables.get(exprArray[i]));
} catch (Exception e1) {
System.out.println("Error: " + e);
}
}
}
break;
}
}
catch (Exception e){
if(!variableCalcuation){
System.out.println("Error! Too many operators.");
}else
{
System.out.println("Error! Too many operators. ");
return -123;
}
}
}
if(exprStack.size() == 0){
if(variableCalcuation){
return -123;
}
return result;
}
else if(exprStack.size() == 1 ){
result = exprStack.pop();
System.out.println(result);
return result;
}
else
{
if(variableCalcuation){
System.out.println("Error! Not enough operators.");
return -123;
}
System.out.println("Error! Not enough operators.");
return result;
}
}
public static double factorialCalculator(double num){
if(num == 0){
return 1;
}
else{
return num * factorialCalculator(num -1);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
