Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 A, Stack1gen B) { do { char op = A.pop(); double opnd1 = B.pop(); double opnd2 = B.pop(); double val = 0.0; switch (op) { case '+': val = opnd1 + opnd2; break; case '-': val = opnd1 - opnd2; break; case '*': val = opnd1 * opnd2; break; case '/': val = opnd1/opnd2; break; } B.push(val); }while((A.getSize()>0) && (A.getTop() != ')')); if((A.getSize()>0) && (A.getTop() == ')')) { A.pop(); } }

//evaluate the current operation at top of A and push the result onto B public static void eval(Stack1gen A, Stack1gen B) { char op = A.pop(); //current operator double opnd1 = B.pop(); //operands double opnd2 = B.pop(); double val = 0.0; switch (op) //evaluate { case '+': val = opnd1 + opnd2; break; case '-': val = opnd1 - opnd2; break; case '*': val = opnd1 * opnd2; break; case '/': val = opnd1/opnd2; break; } B.push(val); //push result onto B }

//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 A) { char topOp = A.getTop(); if((token == '*') || (token == '/')) { return true; //token has precedence, so it will be pushed onto A } else { if((topOp == '*') || (topOp == '/')) { return false; //operator at top of A has precedence } else { return true; //token is ')', which is always pushed } } }

//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 stackB = new Stack1gen(); //operands Stack1gen stackA = new Stack1gen(); //operators

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions