资源简介

用C语言实现离散数学中的任意合式公式的真值表

资源截图

代码片段和文件信息

/*
   &   表示合取  
   |   表示析取 
   %   表示条件 
   *  表示双条件 
   !  表示否定 
*/

#include
#include
#include
#include
#include
#define MaxSize 50
#define  object  10 
typedef struct //储存变量带值 
{
char ch[object] ;
bool value[object] ;
}Val ;

bool Judge(char*Arrayint beginint end)//判断公式是否为合式公式 
{        int sign=0L=4;
         int m;
     for(m=begin;m<=end;m++)
       {  if(Array[m]==‘(‘)
           {
 sign++;
            if(sign==2)
                {  if(Array[m-1]==‘&‘)//(Q*(P*Q)) 
                     {  L=3;
                       break;
                     }
                     if(Array[m-1]==‘|‘)//(Q*(P*Q)) 
                     {  L=3;
                       break;
                     }
                     if(Array[m-1]==‘%‘)//(Q*(P*Q)) 
                     {  L=3;
                       break;
                     }
                     if(Array[m-1]==‘*‘)//(Q*(P*Q)) 
                     {  L=3;
                       break;
                     }
                } 
            }
          if(Array[m]==‘)‘)
           { sign--;
             if(sign==1)
               { if(Array[m+1]==‘!‘)//(!(P*Q))
                   { L=1;
                     break;
                   }
                  if(Array[m+1]==‘*‘)//((P*Q)*Q) ((Q*P)*(S*R))  
                   { L=2;
                     break;
                   }
                   if(Array[m+1]==‘%‘)//((P*Q)*Q) ((Q*P)*(S*R))  
                   { L=2;
                     break;
                   }
                   if(Array[m+1]==‘&‘)//((P*Q)*Q) ((Q*P)*(S*R))  
                   { L=2;
                     break;
                   }
                   if(Array[m+1]==‘|‘)//((P*Q)*Q) ((Q*P)*(S*R))  
                   { L=2;
                     break;
                   }
               }              
              if(sign==0)
                { L=4;
                  break;
                } 
           } 
       }

    switch(L)
    {   case 1: Judge(Arraym+2end-1);break;//(P*Q)
        case 2: Judge(Arraybegin+1m);//(Q*P)
                Judge(Arraym+2end-1);//(S*R)
                break;
        case 3: Judge(Arraybegin+1m-2);//Q
                Judge(Arraymend-1);break;//(Q*P)
                /* (P*Q)  P  (!P)*/ 
        case 4: if((Array[begin]>=64&&Array[begin]<=90)&&(Array[begin+1]==‘\0‘||Array[begin+1]==‘)‘||Array[begin+1]==‘*‘||Array[begin+1]==‘%‘||Array[begin+1]==‘&‘||Array[begin+1]==‘|‘))
            {return true; break;} //P 
                if((Array[begin+2]==‘*‘||Array[begin+2]==‘%‘||Array[begin+2]==‘&‘||Array[begin+2]==‘|‘)&&(Array[begin+1]>=64&&Array[begin+1]<=90)&&(Array[m-1]>=64&&Array[m-1]<=90)&&(end-begin)<=5)
                {return true;break;} //(P*Q)
                if(Array[begin+1]==‘!‘&&(Array[m-1]>=64&&Array[m-1]<=90))
                {return true;break;} //(!P)                
       default:   return fals

评论

共有 条评论