I've created this calculator, which I'm using arrays in to accept any amount of numbers. For example, you can enter " 5 - 6 - 3 - 5 " then subtract them all. However, what I really want to do is using different operators in the same operation. For example, " 5 + 3 * 9 " which I have no clue how to do it.
How can I do it?
Here is how I would approach this problem:
1. Store all numbers in an array (x), and all operations in a different array(op). The reason is that you would need to apply * and / before + or -. Note that the length of op is equal to length of x +1
2. Loop over the operator array. If op[i]=='*' then replace op[i] with '+', x[i+1] is going to be replaced by x[i]*x[i+1] and replace x[i] with 0. Similarly, if you have op[i]=='/' then x[i+1] is replaced by x[i]/x[i+1]
3. At this point your op array will have only + or -. You start with result=x[0], and loop over the op array. If op[i]=='+', you add x[i+1] to the result, else you subtract it from the result
Ideally you need to extend your code so the user can enter a whole equation in one go (using getline()) and then split it up into numbers and operators (storing these in your array(s))
And replace your goto + label with a do-while loop!!
And then...
You might want to read up about the shunting yard algorithm
Ideally you need to extend your code so the user can enter a whole equation in one go (using getline()) and then split it up into numbers and operators (storing these in your array(s))