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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误