So, according to your assignment description, you account for operator precedence by evaluating the same expression over and over until there is only a number (or error):
I start with
5 + 3 * 5 -2
The most precedent operator is *, so:
5 + 3 * 5 -2
5 + 15 -2
The remaining operators have equal precedence, so I'll just take them left to right:
5 + 15 -2
20 -2
and
20 -2
18
There are no more operators, so the answer is 18.
Now, you are supposed to store the expression in an array. So you could have an array of string, where each element is either a number or an operator:
'5', '+', '3', '*', '5', '-', '2'
Or you could have an array of some
record type that tells you whether it is a number or an operator, say:
type tType = (number, add, subtract, multiply, ...);
type tElement = record element_type: tType; number: integer end;
This is how I would do it (if forced to use an array), but if it is beyond you just stick to an array of strings. I assume the latter in what follows, but the concepts work either way.
So, even a bracketed expression is easily stuck in an array:
5 * (2 + (3 + 1))
becomes
'5', '*', '(', '2', '+', '(', '3', '+', '1', ')', ')'
This makes it easy to find operators. The brackets are special, in that they work on a sub-array. Hence:
5 * (2 + 3)
has the () operator, which means to evaluate a subexpression. We'll do that first:
2 + 3
becomes
5
Now we can stick it back into the original expression and remove the ():
5 * 5
Evaluating further:
25
Your professor wants you to use an array, alas. You can move stuff around every time, or you can just transform unused parts (which have been "removed") into, say, the empty string, which means just to ignore it. For example, taking:
1 + 2 + 3
and transforming it into
3 + 3
can have two affects on your array. Starting with:
'1', '+', '2', '+', '3'
you can transform it to:
'3', '+', '3' (or
'3', '+', '3', '', '')
or
'3', '', '', '+', '3'
Both ways have caveats you must be aware of. The first means you must shift elements downward. If you are allowed to use dynamic arrays you can get rid of extra elements at the end, otherwise you must set them to ''. The second way requires your code to skip '' when finding elements adjacent to an operator.
Well, that's enough to think about. Consider how you want to make it work and get started. Remember, it is perfectly OK to use functions and procedures to do something. For example, you might have a function to search your array for the operator with the highest precedence and return its index. Another might modify take the array and an index to an operator and do the operation.
Oh yeah, almost forgot. Convert between numbers and strings with
strToInt() and
intToStr().
Hope this helps.