qq_33312212 2016-03-06 03:57 采纳率: 21.9%
浏览 1488

poj3295 运行输入之后就崩溃了 求大神看看 英汉题意如下

Description

WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula (WFF) is any string of these symbols obeying the following rules:

p, q, r, s, and t are WFFs
if w is a WFF, Nw is a WFF
if w and x are WFFs, Kwx, Awx, Cwx, and Ewx are WFFs.
The meaning of a WFF is defined as follows:
p, q, r, s, and t are logical variables that may take on the value 0 (false) or 1 (true).
K, A, N, C, E mean and, or, not, implies, and equals as defined in the truth table below.
Definitions of K, A, N, C, and E
w x Kwx Awx Nw Cwx Ewx
1 1 1 1 0 1 1
1 0 0 1 0 0 0
0 1 0 1 1 1 0
0 0 0 0 1 1 1
A tautology is a WFF that has value 1 (true) regardless of the values of its variables. For example, ApNp is a tautology because it is true regardless of the value of p. On the other hand, ApNq is not, because it has the value 0 for p=0, q=1.

You must determine whether or not a WFF is a tautology.

Input

Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.

Output

For each test case, output a line containing tautology or not as appropriate.

Sample Input

ApNp
ApNq
0
Sample Output

tautology
not
大概题意
输入由p、q、r、s、t、K、A、N、C、E共10个字母组成的逻辑表达式,
其中p、q、r、s、t的值为1(true)或0(false),即逻辑变量;
K、A、N、C、E为逻辑运算符,
K --> and: x && y
A --> or: x || y
N --> not : !x
C --> implies : (!x)||y
E --> equals : x==y
问这个逻辑表达式是否为永真式。
PS:输入格式保证是合法的
我的 代码

 #include <iostream>
#include <vector>
#include <string>
#include <stack>

using namespace std;

bool c(bool a,bool b)
{
    if(a&&!b)
    {
        return false;
    }
    else
    {
        return true;
    }
}

bool e(bool a,bool b)
{
    if(a&&b||!a&&!b)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool solve(string str,int p,int q,int r,int s,int t)
{
    stack <bool> ele;
    unsigned int i;
    for(i=str.size()-1;i>=0;i--)
    {
        switch(str[i])
        {
            case 'p':ele.push((bool)p);break;
            case 'q':ele.push((bool)q);break;
            case 'r':ele.push((bool)r);break;
            case 's':ele.push((bool)s);break;
            case 't':ele.push((bool)t);break;
            case 'K':
            {
            bool a = ele.top();
            ele.pop();
            bool b = ele.top();
            ele.pop();
            ele.push(a&&b);
            }
            break;
            case 'A':
            {
            bool a = ele.top();
            ele.pop();
            bool b = ele.top();
            ele.pop();
            ele.push(a||b);
            }
            break;
            {
            case 'N':
            bool a = ele.top();
            ele.pop();
            ele.push(!a);
            }
            break;
            case 'C':
            {
            bool f = ele.top();
            ele.pop();
            bool g = ele.top();
            ele.pop();
            ele.push(c(f,g));
            }
            break;
            case 'E':
            {
            bool h = ele.top();
            ele.pop();
            bool j = ele.top();
            ele.pop();
            ele.push(e(h,j));
            break;
            }
            default:break;
        }
    }
    return ele.top();
}

int main()
{
    string str;
    bool flag = true;
    int i = 0;
    vector <string> ans;
    while(cin>>str&&str!="0")
    {
        int p,q,r,s,t;
        for(p=0;p<=1;p++)
        {
            for(q=0;q<=1;q++)
            {
                for(r=0;r<=1;r++)
                {
                    for(s=0;s<=1;s++)
                    {
                        for(t=0;t<=0;t++)
                        {
                            flag = solve(str,p,q,r,s,t);
                            if(!flag)
                            break;
                        }
                        if(!flag)
                        break;
                    }
                    if(!flag)
                    break;
                }
                if(!flag)
                break;
            }
            if(!flag)
            break;
        }
        if(flag)
        {
            ans.push_back("tautology");
        }
        else
        {
            ans.push_back("not");
            flag = true;
        }
    }
    for(i=0;i!=ans.size();i++)
    {
        cout<<ans[i]<<endl;
    }

    return 0;
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2016-03-06 04:21
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法