Hi Folks,

I'm making a Calculator on Visual Studio (Nope, it's not a College or University assignment).

I'm at the part where I have added code to all of the number buttons but now need to see how the program is going to calculate sums of x amount of numbers. What I basically mean by that is - I've successfully made the program so that if i type in one number and then press "+" and then type another and then press "Equals" then it will give me the answer. That's dead simple...of course. However, How or what algorithm do i take in to consideration for a scenario where the user not only inputs two numbers but inputs more, for example: 2 + 4 / 4 * 4.... You get the picture.

This is my pseudo code/abstract/english way of thinking of the possible solution:

User types something in and hits a specific operator
System stores that number AND the operator in two different indexes
Index 0 of the list contains the number and index 1 contains the operator
do the same for however many times the user wants UNTIL the user presses equals.

When equals is pressed, get the list, make substrings of that list by chopping and seperating the numbers from the operators. Once that is done, (this is really complicated for me..lol) get the first number, check the operator next to it and do apply that number and the operator to the next number. So if the first number was 1 and the operator next to it was a +, then expect to 1+something. If the number after the operator was, for example .. a number 3 then 1+3, of course. Now do this for the whole list. The list will have a sequence of: number | operator | number | operator | number | operator | number ......... etc.

Easily said,(well, almost) how would I go about implementing that? :yawn:

Any help will be...how shall I say, GRATELY appreciated.

Hi there,

well you can make this for instance:
you first enter 1, a will get the value 1, then you hit +, then a number again: 3 -> b will get 3, you hit plus again and what happens? exactly, the result will get into a again :D and you enter a number after the last operator and that will get into b and so on, I hope you get the idea :)

Had the same problem once.
I worked it out for myself with the aid of some books and the net.
What you will need is a scanner, a parser and an engine that drives it all.
Look at these snippets, when put together in one project they make up a nice console calculator.
http://www.daniweb.com/code/snippet217185.html
http://www.daniweb.com/code/snippet217186.html
http://www.daniweb.com/code/snippet217187.html
This can be done better.
Perhaps it is your job just to do that!

If you want to do it properly you should look into mathematical operator precedence; if the user enters "2 + 3 * 4" and you parse it item by item you will get "2 + 3 = 5 * 4 = 20" when the correct mathematical answer should be "3 * 4 = 12 + 2 = 14" because the multiply operator has a higher precedecne so is calculated before the addition.
If you dont want to apply precedence then you could make something similar to the old style windows calculator, or old style physical one, by processing each calculation as it is entered and not storing the operations that came before. So you only ever store one number. Initially you store the first number, then once user enters operator and second number you perform the calculation and replace the first number with the result. Then repeat for any operators and numbers that follow, just saving the overal result and not the individual steps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.