离散作业之真值表

9月份的作业

输入任意表达式

求真值表

 

主要是中缀表达式-后缀表达式

以及逆波兰式求解

以及设计输出

 

//中缀表达式转换为后缀表达式,并检查是否有非法字符
int trans(char *src,int *suff,int *sLen,char *var,int *vLen){
 Stack s;
 s.ClearStack();
 int i=0;
 int j=0;
 int k=0;
 int m=0;
 int judge=0;
 char ch1;
 int mark=0;
 int *data=&mark;
 char ch=src[i];
 while(ch){
  switch(ch){
   case ' ':
    break;
   case '(':
    s.Push(ch);
    m=i+1;
    ch1=src[m];
    while(ch1&&ch1!=')'){
     if(isalpha(ch1)){
      m=strlen(src)+3;
      break;
     }
     ch1=src[++m];
    }
    if(m!=(strlen(src)+3)){
     cout<<"缺少右括号!或者括号内没有任何变元!"<<endl;
     return 0;
    }
    break;
   case ')':
    mark=0;
    while(s.GetTopData(data)){
     if(mark=='('){
      s.Pop();
      break;
     }else
      suff[j++]=s.Pop();
    }
    if(mark!='('){
     cout<<"括号不匹配!"<<endl;
     return 0;
    }
    break;
   case '!':
    s.Push(ch);
    m=i+1;
    ch1=src[m];
    while(ch1){
     if(isalpha(ch1)){
      m=strlen(src)+3;
      break;
     }
     ch1=src[++m];
    }
    if(m!=(strlen(src)+3)){
     cout<<"非运算符后没有任何变元!"<<endl;
     return 0;
    }
    break;
   case '&':
   case '|':
   case '^':
   case '-':
   case '=':
    mark=0;
    while(s.GetTopData(data)&&(getPriority(mark)<=getPriority(ch))){
     suff[j++]=s.Pop();
    }
    s.Push(ch);
    break;
   default:
    if(isalpha(ch)){
     judge=find(var,ch);
     if(!judge){
      var[k++]=ch;
      suff[j++]=k+1;  //suff存储运算符和字母,其中字母用var[index-2]表示,index表示suff中的值
     }
     else
      suff[j++]=judge+1;
    }else{
     cout<<"命题公式中含有非法字符!"<<endl;
     return 0;
    }
  }
  ch=src[++i];
 }
 while(!s.IsEmpty()){
  suff[j++]=s.Pop();
 }
 *sLen=j;
 *vLen=k;

 return 1;
}

 

int solve(int *src,int len,int vLen,int inst,bool *result){
 Stack s;
 s.ClearStack();
 int i=0;
 int j=0;
 bool fir=0;
 bool sec=0;
 int res=0;
 int ch=0;
 for(i=0;i<len;i++){
  ch=src[i];
  switch(ch){
   case '!':
    if(s.IsEmpty())
     return 0;
    else
     s.Push(!getNum(inst,s.Pop()));
    break;
   case '&':
   case '|':
   case '^':
   case '-':
   case '=':
    if(s.IsEmpty())
     return 0;
    else
     fir=getNum(inst,s.Pop());
    if(s.IsEmpty())
     return 0;
    else
     sec=getNum(inst,s.Pop());
    res=operate(ch,fir,sec);
    if(res==2)
     return 0;
    else
     s.Push(res);
    break;
   default:
    if(ch>=2&&ch<(vLen+2))
     s.Push(getNum(inst,ch));
    else{
     return 0;
    }
  }
 }
 if(s.IsEmpty()){
  return 0;
 }else{
  ch=s.Pop();
  if(s.IsEmpty()){
   if(ch)
    result[inst]=1;
   else
    result[inst]=0;
   return 1;
  }
  else{
   return 0;
  }
 }
}

 

  if(trans(in,suffix,sLen,var,vLen)){
   int cols=v;
   int rows=1<<cols;

   cout<<"后缀表达式为:";
   for(int i=0;i<s;i++){
    if(suffix[i]>1&&suffix[i]<(v+2))
     cout<<var[suffix[i]-2];
    else
     cout<<(char)suffix[i];
   }
   cout<<endl;

   bool *res=new bool[rows];
   int judge=0;
   for(int i=0;i<rows;i++){
    judge=solve(suffix,s,v,i,res);
    if(!judge){
     cout<<"输入的命题公式错误"<<endl;
     break;
    }else{
     if(!i)
      cout<<in<<"的真值表如下:"<<endl;
     int j=0;
     int *out=new int[v];
     for(j=0;j<v;j++){
      judge=i;
      out[j]=(judge>>j)&0x1;
     }
     if(isBig()){
      if(!i){
       for(j=0;j<v;j++)
        cout<<var[j]<<"\t\t";
       cout<<in<<endl;
      }
      for(j=0;j<v;j++){
       cout<<out[j]<<"\t\t";
      }
      cout<<res[i]<<endl;
     }else{
      if(!i){
       for(j=v-1;j>=0;j--)
        cout<<var[j]<<"\t\t";
       cout<<in<<endl;
      }
      for(j=v-1;j>=0;j--){
       cout<<out[j]<<"\t\t";
      }
      cout<<res[i]<<endl;
     }
    }
   }
  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值