1.7.1 正误问题(中缀表达式)

1.7.1 正误问题

Description

uncle-lu迷上了逻辑值运算。但uncle-lu算的头都晕了,也没算出个所以然。他只好找到你,让你帮他解决这个问题。

Input

一个字符串(串长小于255255255)表达逻辑式子,内只包含true,false,or,and,not和空格,(不包含括号和xor),优先级同pascal.(not->and->or),同级左边先算,如果逻辑式有误则输出 error。

Output

运算结果:true 或者 false ,如果无法得到结果的输出error

Sample Input 1

true or false and false

Sample Output 1

true

分析:

1)完全是根据表达式进行运算,没有借助其他高级数据结构;
    map容器可以用花括号进行多个值初始化;
    将与运算符,非运算符进行运算,最后的结果只存在或运算符,只要存入的值有一个
    是1,则最后的结果是true;

/**
    1)完全是根据表达式进行运算,没有借助其他高级数据结构;
    map容器可以用花括号进行多个值初始化;
    将与运算符,非运算符进行运算,最后的结果只存在或运算符,只要存入的值有一个
    是1,则最后的结果是true;
*/


#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

int main()
{
    map<string,int>mp{{"false",0},{"true",1},{"or",2},{"and",3},{"not",4}};
    string str;
    getline(cin,str);
    int a[100],index=0;
    string temp;
    int len=str.size();
    int pre=10,cou=0,err=0;   //pre存储上一个字符的值
    int first=-1,second=-1; //first与second分别是与运算的两个操作符
    for(int i=0,j=0,l=0;i<len;)
    {
        l=0;
        while(i<len&&isspace(str[i]))
            ++i;
        j=i;    //j是一个字符的开始位置
        while(i<len&&!isspace(str[i++]))
            ++l;    //这个字符的长度
        temp=str.substr(j,l);   //取一个字符
        int flag=mp[temp];
        if(flag>=0&&flag<=1) //err记录连续数值的个数,大于等于两个表达式便是错误
            ++err;
        if(flag>=2&&flag<=3)
            err=0;  //只要中间拥有&&或||运算符,则将err变为0;
        if(pre<2&&flag==2)
        {
            a[index++]=pre; //如果前一个字符是数值且目前这个字符是或运算时,直接将前一个数值存入;

        }
        else if(err==2||(pre>=2&&pre<=3||pre==4||pre==10)&&(flag>=2&&flag<=3))
        {
            printf("error");    //如果前一个字符是&&或||,而后一个字符又是&&或||或者第一个
            return 0;           //字符就是&&或||,那么这个表达式错误
        }
        else if(flag==3)
            first=pre;  //如果这个运算符是&&,,那么将&&前面的数值赋给first,以便后面进行运算
        else if(pre==4&&(flag>=0||flag<=1)) //如果之前的运算符是取反运算符且目前这个运算符是数值
        {
            if(cou%2==1)    //cou记录非运算符的数目
                flag=(flag+1)%2;
            cou=0;
            if(first==-1)
                first=flag;
            else
                second=flag;
        }
        else if(flag==4)    //cou记录非运算符的数目
            ++cou;
        else if(first!=-1&&second==-1&&(flag>=0||flag<=1))
            second=flag;
        if(first!=-1&&second!=-1)
        {
            int tem=first*second;
            a[index++]=tem;
            first=second=-1;
            pre=tem;
            continue;   //如果是与运算,则将运算结果赋给pre
        }
        pre=flag;   //只要当前不是参与与运算,则将当前运算符赋给pre
        if(i==len-1)    //这一步是必要的,否则会进入死循环
            break;
    }
    int res=0;
    for(int i=0;i<index;++i)
    {
        if(a[i]==1)
            res=1;
    }

    if(res)
        printf("true\n");
    else
        printf("false\n");
    return 0;
}

下面这个代码是看了别人的博客所写:

http://t.csdn.cn/W1yn8

2)中缀表达式:
    把数据与符号分别用两个栈进行存储;输入一个字符存一个,当存入的是符号时,
    比较符号栈的栈顶符号元素是否优先级比目前这个符号高,如果是,则要计算前面的
    数据,再进行存储。
    cin >> str;当到达文件末尾时,cin返回EOF,所以可以用while(cin >> str )来输入
    不知道数量的文本

/**
    2)中缀表达式:
    把数据与符号分别用两个栈进行存储;输入一个字符存一个,当存入的是符号时,
    比较符号栈的栈顶符号元素是否优先级比目前这个符号高,如果是,则要计算前面的
    数据,再进行存储。
    cin >> str;当到达文件末尾时,cin返回EOF,所以可以用while(cin >> str )来输入
    不知道数量的文本
*/

/**
#include <cstdio>
#include <iostream>
using namespace std;

const int maxn =256;
int data[maxn]={0},sign[maxn]={0};
int top_d=0,top_s=0;
void pop();
int main()
{
    string str;
    while(cin >> str)
    {
        if(str=="true")
            data[++top_d]=1;
        else if(str=="false")
            data[++top_d]=0;
        else if(str=="or")
        {
            //if(top_s)
            while(top_s) //此处只能用while,而不能使用if,因为当中间有多个not的时候,如果使用if
                pop();   //语句,那么就会导致前几个not与后面运算过来的结果进行运算,而本身not
            sign[++top_s]=1;    //应该是与后面一个数字进行取反操作,导致结果出现错误
        }
        else if(str=="and")
        {
            //if(top_s&&sign[top_s]>=2)
            while(top_s&&sign[top_s]>=2)//只要栈不为空且栈顶的运算符优先级不低于and,就进行运算
                pop();
            sign[++top_s]=2;    //栈顶运算符运算完成后,就将该运算符进行入栈
        }
        else
            sign[++top_s]=3;
    }
    while(top_s)
        pop();

    if(top_d==1)    //只要表达式正确,那么最后data栈只有一个元素
        cout << boolalpha << (bool)data[top_d] <<  endl;  //boolalpha 只会对bool类型进行控制
    else
        cout << "error\n";
    return 0;
}

void pop()
{
    int temp=sign[top_s];
    switch(temp)  //switch语句有大括号
    {
        case 1: //case后面有冒号
            if(top_d<2)
            {
                printf("error\n");
                exit(0);
            }
            data[top_d-1]=data[top_d-1]||data[top_d];
            --top_d;
            break;
        case 2:
            if(top_d<2)
            {
                printf("error\n");
                exit(0);
            }
            data[top_d-1]=data[top_d-1]&&data[top_d];
            --top_d;
            break;
        case 3:
            data[top_d]=!data[top_d];
            break;
    }
    --top_s;
}
*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值