I am using Stack class to calculate simple arithmetic expressions involving integers, such as 1+2*3.your program would execute operations in the order given,without regarding to the precedence of operators. *Thus, the expression 1+2*3 should be calculated (1+2)*3=9,not 1+(2*3)=7.
I couldn't figure out,theStack.pop() item ,how to get every item into the array,because one is integer,next is operator and so on.Can anyone have any idea,please?
theStack.pop() only returns Strings with length 1. There are no integers yet. You still have to interpret them.
Where the line
str+=theStack.pop();
is, you have to check the character. You have allways to remember 2 numbers: the current result and the number in the cache. Also you have to remember wich operation was before the number in the cache. If the charakter is a int, multiply the cache with 10 and add the int value. If you find a fraction separator (hope, this is the right expression, I'm not English), you must stop multiplying the cache, but divide the new value with 10 or 100 or 1000 (depends on it's position) and add it to the cache. If you find an operator or there are no more characters, execute the previous operation, then store the new operation and set cache to 0.
At the beginning, you can set the previousOperation variable to null or NOTHING. A alternative would be to remove NOTHING from the list and to set the first operation to ADDITION since no operator bvefore the first value means that the value i added to 0.