![](https://img-blog.csdnimg.cn/img_convert/eecc2f7f70fd0d905a2b0d8d14b508a9.png)
一开始看到要将一个式子翻译出来并计算的时候我有想到数据结构课上讲的后缀表示法和前缀表示法,里面很重要一点就是栈,但我们为什么要用栈呢。
因为后缀表示法是按规则将数字和符号拆开了(相对而言),我们需要恢复这个规则。后续大概会复习一下数据结构。
想通这个问题之后,我们回到问题本身,后缀表示法是不是也是先乘再加?我们用数组模仿这个。
先拆开式子,符号是符号,数字是数字,然后先算乘除,最后再做加减。其中前两步我融为了一步。
#include<iostream>
#include<string>
using namespace std;
int main(){
int n;
cin>>n;
for(int i=0;i<n;++i){
string s;
cin>>s;
int nums[4];
char single[3];
int l1=0;
int tempnum=int(s[0])-48;
int flag=0;
for(int j=1;j<7;j+=2){
nums[l1]=int(s[j+1])-48;
single[l1]=s[j];
if(s[j]=='x'){
flag=1;
tempnum*=int(s[j+1])-48;
}
else if(s[j]=='/'){
flag=1;
tempnum=tempnum/(int(s[j+1])-48);
}
else{
flag=0;
nums[l1]=tempnum;
single[l1]=s[j];
l1++;
tempnum=int(s[j+1])-48;
}
}
nums[l1]=tempnum;
int r=nums[0];
for(int j=0;j<l1;++j){
if(single[j]=='+'){
r+=nums[j+1];
}
else{
r-=nums[j+1];
}
}
if(r-24==0){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
}