Hello so after sometime with C language like 8 days? i started with C++ this is my second day and i made this calculator :D it's working properly and the loop is good too. but i've seen similar calculator and they look very complicated O.O
please try my code and tell me how it is :D
This works fine for a simple operation with two numbers.
I think the reason why you mentioned having seen much more complicated programs for calculators is maybe because they can handle multiple numbers operations.
For instance, imagine that you have a calculator that doesn't ask you for the two numbers and the operator but rather lets you input the whole operation in such a fashion:
45 + 4 / 8
You would have to read this as a string, separate each number and operator, define priorities between operations (* and / go before + and -) and then calculate everything.
Complexity can scale up even more if you start taking brackets into account..
reverse polish is a different style.
lets see...
if you had cos(15^3+23) you would do it like this
15 commit
3 x to y (result, 15^3 committed)
23 add
result 15^3+23 committed
cos (result final answer committed)
this is usually done with a stack, you push values and when you get an operation, you pull off 2 values, perform the operation, and push the result back.
an algebraic calculator would use () buttons and several extra steps.
You can google reverse polish for a better explain … this was rather oversimplified.
A "real" infix calculator with parentheses and stuff is a great exercise if you're inclined. Maybe in a few weeks: it'd be quite a challenge with only 8 days of experience.
I've got a bug report for you. Can you fix it?
Program output: end-of-input causes output spam
Environment:
Calculator program is named my-calc.exe on Linux 4.18.5, x86_64
Summary:
Running my-calc.exe with incomplete input generates output spam; htop(1) reports my-calc.exe single-core CPU usage at 100%. Ctrl-C can be used to terminate the program.
Steps to reproduce:
At a terminal:
1. Run ./my-calc.exe
program prompts "first number";
2. Type Ctrl-D
Expected behavior:
Program terminates
Actual behavior:
Program produces a constant repeating pattern of output, beginning with:
second number
enter yer operator
wanna go again?. press Y or N
first number
second number
enter yer operator
wanna go again?. press Y or N
first number
Repeating continually. Program no longer responds to input.