Thread: evaluating boolean expressions to get a result

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    evaluating boolean expressions to get a result

    I am writing a program to read a bunch of text and perform boolean operations on it and outputting the result. e.g "T F && T || !" should output false.I have designed my program ,written a parser to get the chars and a method to perform boolean operations.My only problem is that evaluation of expressions is not working.


    Code:
    struct stack{
        char  *characters;
        int top;
        
        
    };
    /*This function initialises and returns a stack adt
     the stack size if allocated a default value of 4 because i know that there can 
     * only be at most 2 values on the stack at any time
     
     */
    struct stack * newStack(){
        struct stack * p= malloc(sizeof(struct stack));
        if(!p){
            fprintf(stderr,"out of memory\n");
        }
        
        p->top=0;
        p->characters=malloc(sizeof(char)*6); 
        
        return p;
    }
    
    
    /* a void method which is used to push
      * char  onto the stack
      */
     void push(char *c, struct stack *p){
        p->characters[p->top]=*c;
        p->top++;
    
    
    
    
     }
     
     /*
      a double method called pop used to
      * pop char of the stack
      */
     char * pop(struct stack *p){
         char * c;
         p->top = p->top - 1;
         c=p->characters[p->top];//assing the char the value of the index
         p->characters[p->top]=' ';  //clean up the index
       
       return c;
    
    
    
    
     }
     
     
     
    /*
     takes 4 parameters
     * a=top of stack -1
     * b=top of stack -2
     * result=where the result woud be stored
     * operation= boolean operation to be performed
     
     */
    void performoperation(char *a,char *b,char * result,char *operation){
       const char *t="T";
       const char * and="&";
       const  char *or="|";
       const  char *not="!";
        int i,j;
       // if (strcmp(a,t)==0)
            if (*a==*t)
              i=1;
              else
               i=0;
            if (*b==*t)
              j=1;
             else
               j=0;
       
        int jy=0;
       
        if(strcmp(operation,and)==0){
            jy=i && j;
        }
       
            else 
                if(strcmp(operation,or)==0)
              
                    jy=i || j;
            
                else
                    if(strcmp(operation,not)==0)
                    {
                     jy= (!i);
                
            }
        if(jy==1)
            *result='T';
        else
            *result='F';
    }
    
    
    /*
     been stuck in this method for a while
     */
    
    
    void evaluateExpression(char *expressions,struct stack * stack){
        char *a;
        char *b;
        char *res;
       
        while(*expressions!='\0'){
            if(*expressions=='T' || *expressions=='F') {//if its a t or f
                push(expressions, stack); 
          
           }
            
            else{
               
                a=pop(stack);
                b=pop(stack);
                performoperation(a,b,res,expressions);
                push(res,stack);
             
            }
               expressions++;
        }
     
        
    }
    void parseexpressionfromcommandline(char * unparsedexpression,char *parsedexpression){
        
        if(unparsedexpression==NULL )
            printf("does not point to any address");
        else {
            char value;  //this would hold my value 
            int pos=0;
            
           
            while(*unparsedexpression!='\0'){
              
                while(!isspace(*unparsedexpression)&& (*unparsedexpression)!='\0'){
                 value=(*unparsedexpression++);
             
                }//inneer while loop
                
              
            unparsedexpression++;
            parsedexpression[pos++]=value;
          
          
            }//outer while loop
            parsedexpression[pos]='\0';
         
             
        }//outer while
        
        
    }
    
    
    
    
    
    
    int main(int argc, char** argv) {
        char result[13];
        char *p="T F && F || !\0";
        char *v;
        char * u;
        //char * q='u';
        struct stack *stack;
        v=result;
        stack= newStack();
       parseexpressionfromcommandline(p,v);
       printf("%s \n",v);
           printf("%u",strlen( v));
      evaluateExpression(v,stack);
     
        return (EXIT_SUCCESS);
    }
    Last edited by sigur47; 05-06-2012 at 09:59 AM.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    One issue is that on line 89 you use a bitwise or, not a logic or.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Subsonics View Post
    One issue is that on line 89 you use a bitwise or, not a logic or.
    Thanks typo error.Can you spot anything logically wrong in my evaluate expressions method.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Can anyone please help suggest something

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Can anyone please help me

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    performoperation(a,b,res,expressions);
    My compiler MinGW GCC says "main.c|129|warning: 'res' may be used uninitialized in this function [-Wuninitialized]|"

    Code:
    c=p->characters[p->top];//assing the char the value of the index
    main.c|49|warning: assignment makes pointer from integer without a cast [enabled by default]|
    Tim S.
    Last edited by stahta01; 05-06-2012 at 05:28 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You've been here long enough to have 115 posts and your only description of this program is "not working"?
    You can do better than that, and bumping is never okay IMHO. If you ever want to bump then you either didn't wait long enough or didn't provide enough information, or something like that.

    Now, please explain the problem...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by iMalc View Post
    You've been here long enough to have 115 posts and your only description of this program is "not working"?
    You can do better than that, and bumping is never okay IMHO. If you ever want to bump then you either didn't wait long enough or didn't provide enough information, or something like that.

    Now, please explain the problem...
    When ver i insert an item in evaluate expressionss,It pushes the item onto the stack But the second time it attempts to push onto the stack,Its not able to do so

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Your stack seems to work, but your pop function is wrong. c should be declared as a char, not pointer to char.

    You need to isolate the problem to a particular function, that will help anyone who tries to help you. After all, if you are not willing to do that yourself, why would someone else be?

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by sigur47 View Post
    When ver i insert an item in evaluate expressionss,It pushes the item onto the stack But the second time it attempts to push onto the stack,Its not able to do so
    As evidenced by what?
    You're actually stating your conclusion, or assessment of the problem, but for other people what you actually need to do is to state the symptoms. If this seems hard you're probably overthinking it. Just forget you know anything about what the program does, and state I enter "blah" and it outputs "blah", or it goes into an infinite loop, or it outputs nothing, or it crashes, or whatever it does. This perhaps seems overly simple if you've been overthinking it, but it's actually a good common ground starting point to start from and ensure everyone is on the same page.

    I'm just trying to help you explain things better as it will help you in the future also.

    I too also think that you've quite over-used pointers here. Push for example doesn't need to be given a pointer to a char, it just needs to be given the char. Pop doesn't need to return a pointer to the char, it just needs to be given the char. Stuff like that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    1>x.c(49): warning C4047: '=' : 'char *' differs in levels of indirection from 'char'
    1>x.c(129): warning C4700: uninitialized local variable 'res' used
    1>x.c(156): warning C4701: potentially uninitialized local variable 'value' used

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    505
    Code:
    void parseexpressionfromcommandline(char * unparsedexpression,char *parsedexpression){
        
        if(unparsedexpression==NULL )
            printf("does not point to any address");
        else {
            char value;  //this would hold my value 
            int pos=0;
            
           
            while(*unparsedexpression!='\0'){
              
                while(!isspace(*unparsedexpression)&& (*unparsedexpression)!='\0'){
                 value=(*unparsedexpression++);
             
                }//inneer while loop
                
              
            unparsedexpression++;
            parsedexpression[pos++]=value;
          
          
            }//outer while loop
            parsedexpression[pos]='\0';
         
             
        }//outer while
        
        
    }
    
    
    
    
    
    
    int main(int argc, char** argv) {
        char result[13];
        char *p="T F && F || !\0";
        char *v;
        char * u;
        //char * q='u';
        struct stack *stack;
        v=result;
        stack= newStack();
       parseexpressionfromcommandline(p,v);
       printf("%s \n",v);
           printf("%u",strlen( v));
      evaluateExpression(v,stack);
     
        return (EXIT_SUCCESS);
    }

    This will crash because parseexpressionfromcommandline expects an output buffer, and you are passing it a wild pointer.

    As a general rule, you need to unit test your fucntions one by one, and make sure they work, before expecting the entire program to work. Almost no-one can write a non-trivial bug-free program first go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. istringstream evaluating to boolean ?
    By rodrigorules in forum C++ Programming
    Replies: 2
    Last Post: 04-10-2010, 02:01 PM
  2. Replies: 5
    Last Post: 12-04-2008, 08:15 PM
  3. Evaluating multiple expressions in an If statement.
    By bitslap in forum C Programming
    Replies: 4
    Last Post: 11-01-2008, 04:38 AM
  4. boolean evaluating objects
    By cyberfish in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2007, 09:44 PM
  5. Evaluating Prefix expressions
    By deane034 in forum C Programming
    Replies: 8
    Last Post: 05-04-2007, 03:31 PM