当前位置:网站首页>1.7.1 正误问题(中缀表达式)
1.7.1 正误问题(中缀表达式)
2022-07-24 03:52:00 【疯疯癫癫才自由】
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;
}下面这个代码是看了别人的博客所写:
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;
}
*/
边栏推荐
- The local picture cannot be displayed after the uniapp H5 is packaged
- Pat grade a 1043 is it a binary search tree
- 训练赛《眼不见,心不烦,理不乱》题解
- Istio architecture extension mechanism
- Two stroke engine mean value model simulation
- How safe is Volvo XC90? Come and have a look
- SqlServer 备份还原
- Application of motion capture in automatic control field
- Pat class a 1040 long symmetric string
- QT ROS related operations (running Terminal instructions, publishing and subscribing to custom message topics or services, subscribing to images and displaying)
猜你喜欢

C語言經典練習題(2)——“冒泡排序(Bubble Sort)“

Worthington mammalian lactate dehydrogenase study -- Characteristics and determination scheme

uniapp H5打包后本地图片无法显示问题

Bet on the whole scene, what is the odds of glory?

Technical dry goods | evaluation index based on mindspire detailed perflexity language model

Worthington purified enzyme preparation helps neonatal cardiomyocyte isolation system

Appendtofile append failed

Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)

Solution to the problem of "out of sight, out of mind, out of order" in the training competition

Redis transaction learning
随机推荐
Mongo from start to installation and problems encountered
Common properties and traversal of trees and binary trees
Machine learning notes - image homography estimation based on deep learning (homographynet)
。其中调用时传入t指与Devi遍的,根本问t2,
Technical dry goods | evaluation index based on mindspire detailed perflexity language model
STL set容器
力扣(LeetCode)204. 计数质数(2022.07.23)
MPLS VPN cross domain -optionb
Database foundation and installation
"Wei Lai Cup" 2022 Niuke summer multi school training camp 1 (summary of some topics)
PyTorch生态简介
Y74. Chapter IV Prometheus large factory monitoring system and practice -- Introduction to promql and monitoring pod resources (V)
Pat class a 1040 long symmetric string
Worthington's test of hepatocyte separation system and related optimization schemes
MySQL message queue list structure
Using global data to realize data sharing in wechat applet
Preliminary use of swagger2
因此可命令传递给系统内由用户确稳定。对于主的
Rpc-bdy (5) - automatic service logoff, load balancing
Exercices classiques de langue C (2) - « tri des bulles »