Thread: parentheses solver

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    20

    parentheses solver

    How can i solve mathematic expression wif parentheses for example (2*3)+3??

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    Quote Originally Posted by Adak View Post
    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.
    Besides RPN? Any other methods? Im just a beginner of C

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    what you want to do is not for beginner...
    You can use third party library. just google...

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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.

  7. #7
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Quote Originally Posted by Adak View Post
    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.
    Postfix expressions? That is:

    a + b //infix

    ab+ //postfix

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    Quote Originally Posted by Adak View Post
    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.
    Im given an assignment to make a calculator source with string input, and what i learned so far is the basic of C like array, pointer, simple function, string etc, so i barely understand RPN or those i never study in my class.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    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();
    }
    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.

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Shunting yard algorithm is simple

  11. #11
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    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.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Posts
    20

    Unhappy

    Quote Originally Posted by Syscal View Post
    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.
    I never learn stack and i dont know how to use it

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    20
    Quote Originally Posted by Adak View Post
    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.
    Do you mean loop and read the whole string? If i put '(' and ')' as operator, how can i tell to read the value and operator between '(' and ')', then do the expression within it 1st, then replace the answer value to it?

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    74
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parentheses preference
    By brewbuck in forum Tech Board
    Replies: 22
    Last Post: 07-24-2010, 01:53 PM
  2. Value initialization using an empty pair of parentheses
    By zephon in forum C++ Programming
    Replies: 43
    Last Post: 09-07-2009, 11:58 AM
  3. Parentheses checker with stacks
    By cannsyl in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2008, 02:17 PM
  4. Target Solver
    By P4R4N01D in forum C Programming
    Replies: 4
    Last Post: 01-14-2008, 05:49 PM
  5. Scrabble Solver
    By Coder87C in forum C++ Programming
    Replies: 3
    Last Post: 08-11-2005, 03:36 PM