Universal Calculator

Pages: 12
I'm having trouble integrating a parenthesis system in. Any suggestions?
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.

Good luck.
Last edited on
I'm almost done coding it, but I'm having a problem with the parenthesis. Can you clarify what you mean by using a variable parenthesis_layer?
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

that way things in () are ignored.
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:

http://www.4shared.com/file/q5v85bUw/Main.html

Thanks again for all your help.
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:
1
2
3
4
5
6
7
bool HasParenthesis(string calc) {
	
	if (calc[0] == LParenthesis && calc[calc.length() - 1] == RParenthesis)
		return true;

	return false;
}

but it still didn't work.
Any solutions?
I fixed the bug and I'm updating the link above.
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.
Topic archived. No new replies allowed.
Pages: 12