When we reach the end of the input, any operators that remain on the operator stack are processed and removed until the operator stack is empty.This continues until the operator stack is empty or until an operator with lower precedence than the input operator appears at the top of the stack. If we encounter an operator token in the input with a precedence that is less than or equal to the precedence of the operator token at the top of the operator stack, we process and remove the operator at the top of the stack.If we encounter an operator token in the input with a precedence that is greater than the precedence of the operator token at the top of the operator stack, we push the new operator token onto the operator stack.If we encounter an operator token in the input and the operator stack is empty, we push the operator token onto the operator stack.Here are the key elements of the evaluation algorithm. This logic is driven in part by the precedence of the operators involved: the operators + and - have a precedence of 1, while the operators * and / have a precedence of 2. The most important logic in the evaluation algorithm tells us when to process the operators sitting on the operator stack. After the processing is complete, we discard the operator token. We then apply the operator to the two numbers to produce a result, and then push that number back onto the top of the value stack. We do this by popping the operator off of the operator stack and popping two numbers off the value stack. When we encounter an operator token, we will push it onto the operator stack.Īt various points in the algorithm we will process an operator.
Any number tokens that we encounter get pushed onto the value stack. The basic algorithm processes the tokens from the input in a simple left to right order. The algorithm makes use of two stacks, a value stack and an operator stack. We are now ready to see the basic algorithm for computing arithmetic statements without parentheses.
Initially, we are going to see how to handle expressions likeĮventually we will extend the basic algorithm to also handle parentheses, making it possible to evaluate expressions like Next, we are going to extend this problem to that of evaluating more complex arithmetic expressions.
The middle String in the resulting array contains the operator, so we can use a simple switch statement to process the operator. Next, the program uses the String class's split method to split the input into an array of String objects. The program uses the Scanner class's nextLine method to read the entire line of input that the user types, thus grabbing the entire expression at once. The program works with very simple arithmetic expressions, such as Here is an example program, based on an example from the text, that can evaluate very simple arithmetic expressions. Just click follow in the button under my name to get notified when I publish the next step.NetBeans project An algorithm to evaluate arithmetic expressions
Step 23: Test the program (Congratulations!)
This is the code for the equality button(you already know how to find where to place this code) Step 22: Write code for the equality button Now let’s write code for the equality sign to perform the calculation. Note: you can only work with 1 to 9 at this time Note that nothing happens when you click the equals button. Run the program, test the 0 – 9 buttons as well as the operations. tText(txtResult.getText()+ ” /”) įor the multiplication button, write the code below:
tText(txtResult.getText()+ ” -“) įor the division button, write the code below
Step 20: Code for the +(plus) and -(minus), /(division) and multiplication buttonsįor the plus button, write the code below: This is the code that would clear the display when the CE button is clicked. Copy and paste the code below in the position This takes you to where you would write the code for this button. Right-click on CE button, choose events, choose Mouse, choose MouseClick. And also get used to various parts of the code. Step 17: Take some time to look through the codes to make sure you got it right. Step 16: Do the same for buttons 3, 4, 5, 6, 7, 8, 9 and 0