Help with calculator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Help with calculator

I'm trying to build a calculator that will perform any basic calculation on any number of digits...I don't know how many digits the user will want to input, so im making the user enter everything as a string(on a line)...now I don't know how to make it follow bodmas https://code.sololearn.com/cNfXhMxg6oye/?ref=app

13th Dec 2018, 3:54 PM
Joseph Ojo
Joseph Ojo - avatar
11 Answers
+ 1
This is how I solved it, if you want to check. But if you want to make it without spoiling, just some work and you can make it, it is very good practice. https://code.sololearn.com/ce1IzovFCvCI/?ref=app
14th Dec 2018, 11:36 AM
Seb TheS
Seb TheS - avatar
+ 2
I had done the same thing with java. I would post it if I could - its in the computer 😣. So I summarise you what I have done there. First I stored operators and numbers separately in an array (you could use python lists.) And the program consists of 2 methods. 1 for calculate and other to do BODMAS. Calculating function: Here is the place I use my list of operators and digits. If you observed there are n+1 digits if you have n operators. Keep that in your mind. to Then I stored the first number in a variable which used to get the answer. and now we can iterate over 2 arrays without any problem - excluding the first number. So it checks the operator and take the action accordingly using switch statements. let me show an example expr = "5+4*5" oprtrs = [+,*] //got by splitting expr (cannot remember methods in python). nums = [5,4,5] m = nums[0] for x in range(len(oprtrs)): if oprtrs[x] == "+": m = m + nums[x+1] #implement for others. to be continued....
15th Dec 2018, 1:43 PM
Seniru
Seniru - avatar
+ 2
...continuing from previous But still the math do not happen under BODMAS. That s the place we need the 2nd function which handles BODMAS. It is clear that + and - have same precedence. No matter with the order we calculated with those operators. Same effect with * and / too. So we can group +,- and *,/. And here how the magic happens. In this method we are going to split plus and minus from input string. So we get expressions next to those operators. It would look something like this. exp = "5+4*3-10" lookAtThis = [5,4*3,10] #we have split the + and - here. andThePlusAndMinus = [+,-] We have to evaluate the expressions in the lookAtThis array using the same method as discussed before And finally this method will return 5+12-10. So we could easily pass this string to the method we discussed before and it will successfully return an answer which is evaluated under BODMAS rule. PS: Do not forget to support brackets. The procedure is same as the thing we done after splitting + and -.
15th Dec 2018, 1:55 PM
Seniru
Seniru - avatar
+ 1
Seb TheS I don't get it
14th Dec 2018, 9:29 AM
Joseph Ojo
Joseph Ojo - avatar
+ 1
Seb TheS I looped through the string, when I come across an operator, I take the index before and after it, then I perform the operator operation on it
14th Dec 2018, 9:35 AM
Joseph Ojo
Joseph Ojo - avatar
0
Femi Ojo How would you separate numbers and operator marks from the user input?
14th Dec 2018, 9:33 AM
Seb TheS
Seb TheS - avatar
0
Femi Ojo Can't you make calculator with that information?
14th Dec 2018, 9:37 AM
Seb TheS
Seb TheS - avatar
0
Seb TheS the calculator is working, not just working right For example 2+2*2 = 6 But it's going to ouput 8 I just want to do something that isn't restricted to a particular number of input, most calculator I see (non web ones) are mostly two numbers, they take two input from user and the operator and viola I can't account for the number of digits the user will want to input, that's why I'm taking it as a string. What I don't know what to do is to tell it what to look for first...I'm thinking of using stack, don't know how to implement it yet, maybe if I turn it to a prefix expression , before that I'll even have to check if the string is valid What I mean by valid 2*/4 (invalid) I'm lost
14th Dec 2018, 9:45 AM
Joseph Ojo
Joseph Ojo - avatar
0
That problem may need recursion.
14th Dec 2018, 9:57 AM
Seb TheS
Seb TheS - avatar
0
Seb TheS I was thinking about it, too far z = input() print(z) That wouldn't limit the user to 2 inputs, the user can enter everything on a line that way....thank you
14th Dec 2018, 10:17 AM
Joseph Ojo
Joseph Ojo - avatar