Sunday, January 10, 2010

Draw the truth-table

#include<iostream>
#include<string.h>
#include<ctype.h>
char os[100];
int main()
{
                int
i=0,j=0,prcdn,prcdt,top=-1,first=0,k=0,count=0;
                char
s[100],ops[100];
                int
prcd(char a);
                void
permute(int n);
                printf("Enter
the boolean expression: ");
                scanf("%s",s);
                for(i=0;s[i]!='';i++)
                {
                                if(isalpha(s[i])!=0)
                                {
                       
                        os[j]=s[i];
                      
                          j++;                                 

















































































                                }
                                else
                               {
                                                prcdn=prcd(s[i]);
                                                if(first==0)
                       
                        {
                         
                                      top++;
                                                                ops[top]=s[i];
                                                                prcdt=prcd(ops[top]);
                                                                first=1;
                       
                        }
                      
                         else
                      
                         {
                                                                if(s[i]==')')
                                                                {
                                                                                while(ops[top]!='(')
                                                                                {
                                                                                                os[j]=ops[top];
                                                                                                top--;
                                                                                                j++;
                                                                                }
                                                                                top--;
                           
                                    }
                                                                else
if(s[i]=='(')
                                                                {
                                                                                top++;
                                                                                ops[top]=s[i];
                                                                                prcdt=prcd(ops[top]);
                                                                }
                                                                else
if(prcdn<=prcdt)
                                                                {
                                                                                while(prcdn<=prcdt
&& top>=0)
                                                                                {
                                                                                                os[j]=ops[top];
                                               
                                                j++;
                                               
                                                top--;
                                               
                                                prcdt=prcd(ops[top]);
                                                                                }
                                                                                top++;
                                                                                ops[top]=s[i];                                        



















                                                                                if(top>=0)
                                                                                {prcdt=prcd(ops[top]);}
                                                                }
                                                                else
                                                                {
                                                                                top++;
                                                                                ops[top]=s[i];
                                                                                prcdt=prcd(ops[top]);
                                                                }               



















































































































































































































































































                                                }
                                }
                }
                while(top>-1)
                {
                                os[j]=ops[top];
                                top--;
                                j++;
                }
                os[j]='';
                //printf("The postfix
equivalent is %s",os);
                for(j=0;os[j]!='';j++)
                {
                                if(isalpha(os[j])!=0)
                                {count++;}
                }                                                                                permute(count);                                                                                                                                                                                                                    
   
            scanf("%d",&i);
                return 0;
}
int prcd(char a)
{
                if(a=='(')
                {return 0;}
                else if(a=='+' || a=='-')
                {return 1;}
                else if(a=='.' || a=='/')
                {return 2;}
                else if(a=='^')
                {return 3;}
}

No comments:

Post a Comment