I want to make a simple calculator which can read an expression and calculate it. Below I've posted the source code of my calculator program. It does the job but have some major problems-
1. The Result Shows Up Many Times and also shows my intermediate results which I dont want to show up at the screen.
2. It keeps the result saved after entering one expression and getting the result, which causes wrong result on calculating another expression in the same program run.
-------------------------------------------------------------------------------
What I Want It To Do:
1. Shows The Final Result For Each Expression.
2. Reset All Values and Program to Initial Stage After Getting Final Result.
NOTE: I'm a Beginner so try to make source code as simple as possible.
I've corrected the mistake. It must have been something wrong with the for loop you used so I replaced it with a do loop instead. Also, at the end there was no need to put an else otherwise whenever you did a divide, it would crash. Finally, changing the variables from an integer to a double allows you to do calculations with decimal points.
Thanks For Your Help But Now Things Got More Complicated LOL-
----------------------------------------------------------------------------------------------------
#Tom56785
I Forgot To tell you that my calculator was able to calculate any kind of expression such as 2+2, 2+5+5+9, 5+8/9 etc. What I'm Trying to say is it was able to perform calculation on any length of operation input.
----------------------------------------------------------------------------------------------------
#kemort
Your Code Returns Result as 0 when entered more than two values and operands. example 2+2+2
----------------------------------------------------------------------------------------------------
Please Guys Help Me
I Used For Loop To Continuosly Read The Entered Expression And Solve It. Try Running My Source Code with the following inputs:-
1. 5+5
2. 8+4+5
3. 5+5/2
4. 5+5+5+5+5
Alse Try To Enter Another Expression During The Same Run-
5+5+5
1+1+1
You'll Understand The Problem After Running The Program.
don't worry about the math logic now just make it calculate normally such as left to right plain calculation. I'll try to add the Math Logic And Grammar later.
example 5+5*5=50 will be fine even though it's incorrect
5+5*5=30 I suppose by our usual rules
Yeah I'll keep you informed. Might take some time as it's midnight here. But I'll get back ASAP.
With the more complex challenges, leave those to another stage. The way it can be handled - i.e. brackets - is the intermediate calculations and operators are pushed onto a stack until a bracket is closed upon which the stack is pulled down. If that conveys anything.
I just entered into a new region lol
infix to postfix conversion and using rpn
but I wanted to make a new calculator not to make something that already exists
RPN calculators do '5 push-up, 5, add'. The operator works on the bottom two registers and the answer is displayed and entered in the bottom register. A bracket is equivalent to a push, an operator brings the stack down (pop).
RPN is easier to implement. But you can get '5+5=' to work.
After an hour or so of trying to come up with a solution to this issue you'll be glad to know I've finally found one! It took me a while but here's the code: