How can i solve mathematic expression wif parentheses for example (2*3)+3??
How can i solve mathematic expression wif parentheses for example (2*3)+3??
The easiest way is to use RPN (Reverse Polish Notation). It may seem a bit awkward at first, but RPN is a god-send once you get started, with math expressions.
Wikipedia has a great write up on it.
You can scan the math expression, and use the laws of precedence in math, to reduce it down "by hand". It's not really easy, though. Not as robust, either. Easy to get bugs.
What is this for? Beginner's don't usually get asked to reduce math expressions.
what you want to do is not for beginner...
You can use third party library. just google...
One way to look at it is recursively. If your function is given a string to solve, it should find the first set of parentheses, and call itself with the contents of that parentheses, replacing it with the return value of that call. It should repeat this until there are no parentheses (nested parentheses will obviously be taken care of automatically by the recursion). That isn't the most elegant solution considering the way you typically work with strings in C, but maybe it'll give you some ideas on how to think about this.
Having said that, I agree with Adak. Parsing the expression into RPN or a similar structure makes it straightforward to evaluate the expression in code. You might have a look at the "shunting yard algorithm" for how to get from what you have now, to RPN.
This is part of my code for simple math with string input and multiple operators. Now i need to add in the parentheses solver to make my calculator able to achieve the goals.Code:{ int yee = 0; float ans = 0; float tempnum[200][200]; char tempopt[200][200]; int asd = 0; asd = strlen(opt); for(j=0;j<asd;j++) { if(opt[j]=='^') { ans = pow(num[j],num[j+1]); for(yee=j;yee<asd+1;yee++) { num[yee]=num[yee+1]; } for(yee=j;yee<asd;yee++) { opt[yee] = opt[yee+1]; } num[j] = ans; j--; } } for(j=0;j<asd;j++) { if(opt[j]=='*') { ans = num[j] * num[j+1]; for(yee=j;yee<asd+1;yee++) { num[yee]=num[yee+1]; } for(yee=j;yee<asd;yee++) { opt[yee] = opt[yee+1]; } num[j] = ans; j--; } if(opt[j]=='/') { ans = num[j] / num[j+1]; for(yee=j;yee<asd+1;yee++) { num[yee]=num[yee+1]; } for(yee=j;yee<asd;yee++) { opt[yee] = opt[yee+1]; } num[j] = ans; j--; } } getchar(); }
Shunting yard algorithm is simple
I wrote one in JAVA as an assignment from my instructor a few semesters ago. Though he wanted us to create a "stack" with a linked list and push and pop the values of the string on and off the stack.
Do you want to try scanning the expression, then?
It's perhaps the easiest for a beginner. Those are the only two levels of difficulty I know of. Post fix is the same as RPN, and Shunting Yard is no easier than RPN, imo.
The logic is:
With the expression in one line of a char array, scan it from left to right, and locate the innermost expression, and perform the indicated operation. Do the operators in the right order of precedence for math (* and /, before + and -).
Continue by looking for the next innermost expression, and repeat until the entire expression has been reduced, and all operations have been done.
it seems what you're instructor is expecting is something more easy, like asking for the numbers and operations separately, and have a cycle to do more operations.