combining conditions

i can add or subtract numbers fine but have trouble implementing a + / - sign

numbers = 1 2 3 4 5 [these inputs are from a text file]
total+=numbers #adds all together as 15

total-=numbers #subtracts all as -15 

how can i combine them, say,
1 + 2 - 3 + 4 - 5
i have tried:
if x = '+'
else if x = '-'

the error i get is:
In function 'int main()':
9:14: warning: 'total' may be used uninitialized in this function [-Wmaybe-uninitialized]
You can use negative numbers in the text file and still += to get the same result, however you'd run into the same problem if you wanted * and / for instance. I'd sugget writing a parser that gets each token in the text file by whitespace and create a FSM to conclude what should happen on each token. Bjarne Stroustrup creates one in his book if you are able to get it. It's a great start, the code eventually evolves into handling brackets and being able to write var1=30 and so on, making a little basic parser it's pretty nifty.

Last edited on
Registered users can post here. Sign in or register to post.