Firstly, your code is much more complicated than it needs to be.
The functions you need: float evaluate(string) The main function which tries to split input string to numbers and operators and evaluate the result.
float get_number(string) A function which converts a string to number. You can use a stringstream for that.
int find_operator(string) Function to find the operator of lowest precedence and return it's position in the string. Return -1 if there are no operators.
float calculate(float, char, float) Your PerformCalculation.
bool has_parentheses(string)
string remove_parentheses(string) A function to convert "(2+2)" to "2+2"
In evaluate, first call find_operator. Then.
If it succeeds, return calculate( evaluate(left), operator, evaluate(right)); where left and right are the pieces you get then you split the string at operator position. You can get them with string constructor: string(input, 0, operator_position-1) and string(input, operator_position+1).
If it fails, call has_parentheses. If it does, return evaluate(remove_parentheses(input));
If it doesn't, input must be a number, so return get_number(input);
For this scheme to work, find_operator has to ignore things in parentheses. Have a variable parentheses_layer. Every time you find a ( increment it and when you find a ), decrement it. Only look for operators if it is 0.
has_parentheses and remove_parentheses should go from the beginning of the string an look for (, and then go from the end and look for ). You don't need to worry about any other symbols. No valid string will reach this function if there is something outside the parentheses.
int layer = 0
for i = 1 to input length
if input[i] == '(' then layer++;
if input[i] == ')' then layer--;
if layer == 0 then
check if input[i] is an operator
Wow. I can't thank you enough for your help. I finished coding the calculator and I guess the next step is to integrate it in with the Universal Calculator. Maybe in the future I'll integrate commands like sin, log, e, etc. Here's the code for the final parsing calculator, and suggestions are welcome as always:
I discovered a bug in the program:
When you input a calculation that doesn't have a set of parenthesis on the edges, but rather in the middle of the string, it spits out a random answer. I tried replacing the code in the HasParenthesis function with:
I like how your code got so much shorter.
A few more things to polish:
GetOperator doesn't really need three loops. If you have a variable to store the last variable, and a function to measure it's priority, you can do it in one.
Evaluate has a lot of repetition too. You call Evaluate on both sLeft and sRight so if you remove () from sCalc you only need to do it once.
Another problem is that (I think. I didn't compile it) your code won't work with gaps. It's not hard to fix and it would make the whole program feel more polished.