Chinaunix首页 | 论坛 | 博客
  • 博客访问: 473343
  • 博文数量: 59
  • 博客积分: 345
  • 博客等级: 二等列兵
  • 技术积分: 1380
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-18 22:44
个人简介

to be myself

文章分类

全部博文(59)

文章存档

2017年(5)

2013年(47)

2012年(3)

2011年(4)

分类: C/C++

2013-03-02 18:17:02

题意为K、A、N、C、E(分别表示与、或、非、包含于、相等5种运算)来运算p、q、r、s、t(分别为5个变量,但只能为0或者1),判断在各种取值情况下,表达式是否为恒真式。从串尾往串首运算,一个栈就OK了。遇到KANCE就运算,结果马上入栈,最后结果正确。


点击(此处)折叠或打开

  1. void Push(Stack*p,boolnum)
  2. {
  3.      p->n[++p->top]=num;
  4. }
  5. bool Pop(Stack*p)
  6. {
  7.      return p->n[p->top--];
  8. }
  9. void InitStack(Stack*p)
  10. {
  11.      p->top=-1;
  12.      memset(p->n,0,sizeof(p->n));
  13. }
  14. int IsStackEmpty(Stack*p)
  15. {
  16.      return-1== p->top;
  17. }
  18. bool Cal(char symbol,bool w,bool x)
  19. {
  20.      switch(symbol)
  21.      {
  22.          case'K' :
  23.          {
  24.              returnw&x;
  25.          }
  26.          case'A' :
  27.          {
  28.              returnw | x;
  29.          }
  30.          case'C' :
  31.          {
  32.              returnw<=x;
  33.          }
  34.          case'E' :
  35.          {
  36.              returnw==x;
  37.          }
  38.      }
  39. }
  40. int main()
  41. {
  42.      Stackst;
  43.      int i,j,len;
  44.      bool w,x,succ;
  45.      charstr[MAX],symbol,tc;
  46.      
  47.      while(scanf("%s",str) &&str[0]!='0')
  48.      {
  49.          succ=1;
  50.          if (1== (len=strlen(str))) //仅有一个变量,不恒为0
  51.          {
  52.              printf("notn");
  53.              continue;
  54.          }
  55.          for (j=0; succ&&j<32; j++) //枚举32种情况
  56.          {
  57.              InitStack(&st);
  58.              for (i=len-1; i>=0; i--)
  59.              {
  60.                  //为表达式运算标志
  61.                  if (str[i]=='K' || str[i]=='A' || str[i]=='N'
  62.                                      || str[i]=='C' || str[i]=='E')
  63.                  {
  64.                      w=Pop(&st);
  65.                      if ('N'==str[i])
  66.                      {
  67.                          Push(&st,!w);
  68.                      }
  69.                      else
  70.                      {
  71.                          x=Pop(&st);
  72.                          Push(&st,Cal(str[i],w,x));
  73.                      }
  74.                  }
  75.                  //为参与运算的变量
  76.                  else
  77.                  {
  78.                      Push(&st,pqrst[j][str[i]-'p']);
  79.                  }
  80.              }
  81.              //栈中最后剩下运算结果
  82.              if (!Pop(&st))
  83.              {
  84.                  //结果为false则表达式不恒真,退出循环
  85.                  succ=0;
  86.                  break;
  87.              }
  88.          }
  89.          if (succ)
  90.          {
  91.              printf("tautologyn");
  92.          }
  93.          else
  94.          {
  95.              printf("notn");
  96.          }
  97.      }
  98.      return 0;
  99. }

2011-03-30 22:59 发表于百度空间,今搬至CU。


阅读(1762) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~