+ 12

Multi-operator Calculator in C/Java

How to write a program which performs multiple operations i.e which can calculate 2*2+1-3 ? I want only logic not source code

19th May 2019, 8:25 AM
Yamin Mansuri
Yamin Mansuri - avatar
5 Réponses
+ 7
Just saying that on code playground all operands need to be entered at once, separated by newlines.
20th May 2019, 3:28 AM
Sonic
Sonic - avatar
+ 6
A basic way: 1) Read the input 2) Divide the input in logical pieces (numbers and operators) 3) Use something for order all operations from their precedence and associativity (at example 2+3*5 is equals 3*5 = 15 + 2= 17) if do you want (usually is made by using a tree ds) 4) perfom the operations
19th May 2019, 8:35 AM
KrOW
KrOW - avatar
+ 4
Sonic not necessarily, if you create a lexical analyzer. you can calculate multiple mathematical expressions in one line with the use of stacks by changing them into a postfix notation. KrOW explained a part of how this was done with the PEDMAS method to correctly structure the expressions. this is usually how calculators and machines calculate multiple expressions. here is a link on how it's done. faculty.cs.niu.edu/~hutchins/csci241/eval.htm
20th May 2019, 6:45 AM
Shen Bapiro
Shen Bapiro - avatar