When Are the Rules for Operator Precedence When Writing/reading a Math Expression in Java ?
This article is dedicated to my love Aida.
- Download source - 52.64 KB
Introduction
This article describes a applied mathematical parser - an interactive plan that can be used to perform basic arithmetic operations. In fact, in order to go along understanding of this article simple, I prefer to avert whatsoever unnecessary items and so y'all cannot rely on this program every bit a real calculator and it only works with integers and it can parse an expression with + , - , * , / , ( , ).
When I first started thinking about writing code to calculate a mathematical expression, I soon realized that it wasn't that easy [at least for me]. I had to delve into such concepts as parsing, tokens, lexical analysis, node trees and tables, etc.
The rules of the algorithm were the same ones we learned in algebra. For case: multiply (or dissever) before calculation or subtracting; start with inner parentheses and work outwards; if a pair of binary operators take equal precedence, perform the left operator of the pair first.
Hither is its screen shot:
Using the Code
The thought of this figurer or better to say math parser is think in tree and node way. I call up the following flick can explain the whole thought:
a+b*c*(d-due east)
Y'all tin can see the algorithm of multiplying (or dividing) before adding or subtracting; starting with inner parentheses and working outwards; if a pair of binary operators have equal precedence, performing the left operator of the pair first.
At first, I made ParseExpr() function. This function + or - two operands with each other merely before that checks if there is any * or / subsequently each operand or not and then it calls ParseFactor() function which is made to * or / two operands. Of course it checks if there is any expression in a pair of parenthesis or non, so it calls ParseTerm() function to check those expressions between braces, then at this point it reaches the value then we should parse it and then then information technology calls ParseNumber().
For example higher up at first information technology calls ParseExpr(Expression) -> ParseFactor(Expression) -> ParseTerm(Expression) -> ParseNumber(Expression) and it returns a and then information technology cuts the Expression to +b*c*(d-e) so the returned value from ParseNumber goes to op and and so it checks the first character to see if information technology is plus and there it is, and so information technology cuts the Expression to b*c*(d-e) and then it repeats the last deportment and makes up the above tree.
Blocks of code:
using System; using Organisation.Collections.Generic; using Organisation.Linq; using Organisation.Text; namespace csharpcalc { class Expression { public int ParseExpr(ref string expr) { int op , op1; op = ParseFactor(ref expr); if (expr.Length != 0 ) { if (expr[0] == ' +') { expr = expr.Substring(one, expr.Length - one); op1 = ParseExpr(ref expr); op += op1; } else if (expr[0] == ' -') { expr = expr.Substring(1, expr.Length - 1); op1 = ParseExpr(ref expr); op -= op1; } } return op; } public int ParseFactor(ref string expr) { int op, op1; op = ParseTerm(ref expr); if (expr.Length != 0) { if (expr[0] == ' *') { expr = expr.Substring(1, expr.Length - ane); op1 = ParseFactor(ref expr); op *= op1; } else if (expr[0] == ' /') { expr = expr.Substring(1, expr.Length - 1); op1 = ParseFactor(ref expr); op /= op1; } } return op; } public int ParseTerm(ref string expr) { int returnValue = 0; if (expr.Length != 0) { if (char.IsDigit(expr[0])) { returnValue = ParseNumber(ref expr); render returnValue; } else if (expr[0] == ' (') { expr = expr.Substring(i, expr.Length - one); returnValue = ParseExpr(ref expr); return returnValue; } else if (expr[0] == ' )') expr = expr.Substring(1, expr.Length - 1); } render returnValue; } public int ParseNumber(ref string expr) { string numberTemp = " "; for (int i = 0; i < expr.Length && char.IsDigit(expr[i]); i++) { if (char.IsDigit(expr[0])) { numberTemp += expr[0]; expr = expr.Substring(1, expr.Length - ane); } } return int.Parse(numberTemp); } } }
If You lot Want To ...
If you lot desire to complete this project in guild to accept double numbers too, you tin add together if-block in ParseNumber() method that even if char.IsDigit() was faux, check if it is "." and if it is so, add it to the numberTemp and and then bandage it.
If you want to add some other mathematical functions to this project such as sin(), cos(), etc., y'all can bank check the commencement char of Expression in the ParseTerm function to encounter if it is a letter or not (char.IsLetter) and then telephone call a office like ParseWord() and thereafter splitting the word, you can use a switch instance block.
Points of Interest
I am a Control Engineer, but I take always loved figurer programming and this unproblematic project brings life to my past dreams.
History
Since I was a xvi year old pupil, I had a dream of writing a math parser but I have e'er had another piece of work to do. Finally I decided to write that. After searching the literature, I came across an outline of an algorithm for parsing arithmetic expressions in Principles of Systems Programming by Robert M. Graham (Wiley, 1975).
Source: https://www.codeproject.com/Articles/88435/Simple-Guide-to-Mathematical-Expression-Parsing
0 Response to "When Are the Rules for Operator Precedence When Writing/reading a Math Expression in Java ?"
Post a Comment