Equation Verifier! Almost complete!

This is a discussion on Equation Verifier! Almost complete! within the C++ Programming forums, part of the General Programming Boards category; Code: void checkSymbols(string infix){ string acceptedSymbols = "0123456789+-*/^()", acceptedNumbers = "0123456789", acceptedOperators = "+-*/^"; int numberCount = 0, opCount = ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    155

    Equation Verifier! Almost complete!

    Code:
    void checkSymbols(string infix){
    	string acceptedSymbols = "0123456789+-*/^()", 
    	       acceptedNumbers = "0123456789", 
    		   acceptedOperators = "+-*/^";
    	int numberCount = 0, opCount = 0, leftParCount = 0, rightParCount = 0;
    
    	for(unsigned int i = 0; i < infix.length(); i++){
    	
    		if (acceptedSymbols.find(infix[i], 0) == string::npos)
    			outputPostfixError(1);
    			
    		if (acceptedNumbers.find(infix[i], 0) != string::npos)
    			numberCount++;
    			
    		if (acceptedOperators.find(infix[i], 0) != string::npos)
    			opCount++;
    			
    		if (infix[i] == '(')
    			leftParCount++;
    			
    		if (infix[i] == ')')
    			rightParCount++;
    	}
    
    	if ((numberCount - 1) != opCount) 
    		outputPostfixError(2);
    		
    	if (leftParCount != rightParCount)
    		outputPostfixError(3);
    	
    } //end void checkSymbols
    This is a code to verify if a given equation is correct. Like if I enter "3 + 4 - (6 * 5) / 2" it is correct, but if I do "a + 5" or "a ++ 3)" or "((1 + 2)" they will all give errors, for it must be a solvable equation with no unknown variables.

    However, I am pretty sure all these errors have been fixed, but I do not know how to check for incorrect parenthesis direction. Like if the user enters ")1 + 2( - 2" or something to that extent. Parenthesis can be nested so this makes it a bit harder, like this would be accepted: ((1 + 5) / 3)

    But of course, I don't want ")(1 + 5( / 3)"

    Any way I can solve this?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    26
    I had an assignment similar to this!

    You can use stack to solve this problem:
    PHP Code:

    for (every character in the infix)
    {

    case 
    '(':
    push to the stack

    case ')':
      if (
    stack is not empty)
         
    pop the stack

      
    else
       return 
    0;

    default:
     
    // do nothing

    }
    if (
    stack is not empty)
     return 
    0

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Even simplier, use an integer value (that starts at 0). Traverse the expression. For every ( add 1 to the variable, and for every ) subtract 1.
    If the variable ever go below 0 or if it isn't 0 at the end, there is something wrong.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Magos: Whoops I misread your message, sorry!!! Yours also works!
    Last edited by Nakeerb; 11-30-2002 at 01:23 PM.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Lazy Student: Thank you! That method worked beautifully!

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    155
    Code:
    void checkSymbols(string infix){
    	string acceptedSymbols = "0123456789+-*/^()", 
    	       acceptedNumbers = "0123456789", 
    		   acceptedOperators = "+-*/^";
    	int numberCount = 0, opCount = 0;
    	int parenthesisChecker = 0;
    
    	for(unsigned int i = 0; i < infix.length(); i++){
    	
    		if (acceptedSymbols.find(infix[i], 0) == string::npos)
    			outputPostfixError(1);
    			
    		if (acceptedNumbers.find(infix[i], 0) != string::npos)
    			numberCount++;
    			
    		if (acceptedOperators.find(infix[i], 0) != string::npos)
    			opCount++;
    			
    		if (infix[i] == '(')
    			parenthesisChecker++;
    		
    		if (infix[i] == ')')
    			parenthesisChecker--;
    		
    		
    		if (parenthesisChecker < 0)
    			outputPostfixError(3);
    				
    	} //end for loop
    	
    
    	if (((numberCount - 1) != opCount) || (opCount == 0)) 
    		outputPostfixError(2);
    		
    	if (parenthesisChecker != 0)
    			outputPostfixError(3);
    	
    } //end void checkSymbols
    Last edited by Nakeerb; 11-30-2002 at 01:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 11-04-2006, 03:06 AM
  2. IDEA: Equation solver
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 01-07-2003, 10:46 AM
  3. Equation solving
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-24-2002, 01:13 PM
  4. why different answers for this equation
    By james_nkh in forum C Programming
    Replies: 2
    Last Post: 05-21-2002, 12:29 PM
  5. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 08:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21