All about programming in GNU/LINUX

Code which gives the Truth table for a given Boolean expression for all possible combination of inputs(A-B+AB- is read as A bar B + A Bbar)

//the code is to be run in a compiler called code blocks http://www.codeblocks.org/downloads/26
#include <iostream>
#include <stdio.h>
void sort(char *in , int n);
void swap(char *in,int i,int minpos);
int binsearch(char* in,int n,char temp);

using namespace std;
int main()
{
        int i,n,k,max,j=0,bit,l=0,ret,q,z;
        char temp;
        cout << "Enter the no.of.number of inputs : ";
        cin >> n;
        char in[n];
        cout <<"\nEnter the names of Input Variables: ";
        for (i=0; i<n; ++i)
        {
                cin >> in[i];
        }

        cout << "\nInput : ";
        for (i=0; i<n; ++i)
        {
            cout << in[i] << "  ";
        }
        sort(in,n);//The order of arguments <array(in), n>
        cout <<"\nThe sorted inputs are: ";
        for(i=0;i<n;i++)
         cout<<"\t"<<in[i];
        k=(1<<n);
        int out[k];
        max=2*k*n;
        char equation[max];
        fflush(stdin);
        cout<<"\nEnter the Boolean Expression: ";
         fgets(equation,max,stdin);
        for(i=0;i<k;i++)
        {
            j=0;
            while(equation[j]&&out[i]!=1)
            {

              temp=equation[j];

              ret=binsearch(in,n,temp);

              if(ret==-1)
               {
                   cout<<"\n\t\t\t\tiNVALID INPUT, RUN THE PROGRAM AGAIN";
                   exit(EXIT_FAILURE);
               }
              bit=i&(1<<ret);

              if(equation[j+1]=='-')
               {
                   ++j;
                   bit=!bit;
               }
             if(bit==0)
             {
              while((equation[j]!='+') && (equation[j]!=NULL))
              {
                    j++;
              }
              if(equation[j]=='+')
                  ++j;

             }

             if(bit)
             {
                 ++j;

                 if(equation[j]=='+'||equation[j+1]==NULL)
                  {

                  out[i]=1;

                  }
             }
           }
            if(bit==0)
             {
                 out[i]=0;

             }
        }

  printf("\n");
cout<<"\nThe equation is : "<<equation;
cout<<"\n";
for(i=n-1;i>=0;i--)
 cout<<in[i]<<"\t";
cout<<"OUTPUT\t";
for(i=0;i<k;i++)
{
    cout<<"\n";
    for(q=n-1;q>=0;q--)
    {
        z=i&(1<<q);
        if(z==0)
         cout<<"0"<<"\t";
        if(z)
         cout<<"1"<<"\t";

    }
 cout<<out[i];
}

}
void sort(char *in , int n)
{
    int i,j,min,minpos;
    for(i=0;i<n-1;i++)
    {
       min=in[i];
       minpos=i;
       for(j=i+1;j<n;j++)
        {
            if(min<in[j])
            {
                minpos=j;
                min=in[j];
            }
            swap(in,i,minpos);
        }
    }

}

void swap(char *in,int i,int minpos)
{
 char temp;
 temp=in[i];
 in[i]=in[minpos];
 in[minpos]=temp;
}

int binsearch(char *in,int n,char key)
{
    int i;

     for(i=0;i<n;i++)
     {
         if(in[i]==key)
          return i;
     }
     return -1;

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s