+14

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

5/19/2019 8:25:25 AM

Yamin Mansuri

5 Answers

New Answer

+7

Just saying that on code playground all operands need to be entered at once, separated by newlines.

+7

I tried code evil function you can see here you define the keywords and if matches it search for the number after that it excite function define in the code. https://code.sololearn.com/cf40iiEcoj4T/?ref=app

+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

+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

+2

I did a similar calculator some days ago in c++ The logic is pretty simple, when you perform a calculation the result will be affected by the next calculation and so on... Example: (2*3+5) 2*3=6, 6+5=11... Here's the code anyway: https://code.sololearn.com/c4XkK4K4o2wz/?ref=app I don't know why this in sololearn doesn't work, but if you use others ide, it works