Question
JAVA CODE for arithmetic expression evaluator: Need code to permit spaces between numbers and operators. OK: 3.14 + 75 or ( 3.14+75 ) or 3.14
JAVA CODE for arithmetic expression evaluator:
Need code to permit spaces between numbers and operators.
OK: 3.14 + 75 or ( 3.14+75 ) or 3.14 +75 or 3.14+ 75 etc.
Not OK: 3 . 14 or 3 . 1 4 etc.
MY CODE:
import java.util.Scanner; /* arithmetic expression evaluator; evaluate an arithmetic expression represented as a string withou blanks and negative numbers indicated by underscore.*/ public class ExprEval { /*expressions are evaluated from right to left, using a stack A of arithmetic operators and a stack B of operands. In this method, a subexpression is evaluated by popping the current operator from A, its 2 operands from B, performing the operation and pushing the result onto B, and then repeating until a right parenthesis is encountered or there are no more operators */ public static void evalDown(Stack1gen
//evaluate the current operation at top of A and push the result onto B public static void eval(Stack1gen
//compare the current operator token in the input string to the operator //on top of stack A to determine precedence. public static boolean prec(char token, Stack1gen
//the program inputs a single expression string via keyboard, //evaluates it, and displays the result. public static void main(String[] args) { Scanner kb = new Scanner(System.in);
Stack1gen
Expr exp = new Expr(); String ee = exp.getExpr(); //the input expression (as a String) int pp; //pointer to current token in input string char token; //current token //loop to scan the string right to left do { pp = exp.getp(); token = ee.charAt(pp); if(token == ')') { stackA.push(token); //always push a right parenthesis to delimit a subexpression pp--; exp.setp(pp); }
//if the token is a left parenthesis, //evaluate the corresponding subexpression if(token == '(') { evalDown(stackA, stackB); pp--; exp.setp(pp); }
if((token=='+')||(token=='-')||(token=='*')||(token=='/')) { //If the token is an arithmetic operator of higher precedence //than the topmost operator on stack A, push the token onto A. if((stackA.getSize() == 0) || (prec(token, stackA) == true)) { stackA.push(token); pp--; exp.setp(pp); } //If the token is an arithmetic operator of lower precedence than that //at the top of the stack, then evaluate the operation at the top of stack A. else { eval(stackA, stackB); } } //if the token is the rightmost digit of a number or a decimal point on the right, //form the number as a double and push onto stack B if(((token<='9')&&(token>='0'))||(token=='.')) { stackB.push(exp.formNum()); } }while(exp.getp()>=0);//continue to scan from right to left
//after completely scanning the input string, evaluate any remaining op[erations while(stackA.getSize()>0) { eval(stackA,stackB); }
System.out.println(stackB.pop()); } //end of main }
You can decide on your own rules for spaces, but include these
in the comments to the "user".
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started