标签:str 计算机 mes pac 就是 show ptr case names
文/Pleiades_Antares
首先是中缀表达式的c++程序,需要注意本程序只局限于一位数!!(改良版很容易鸭重点是理解题目)
附上代码与详解
//中缀表达式求值,only one digit!!
//感谢假牙! (假牙=我的计算机偶像quq)
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
stack<int> opnd;//数栈
stack<char> optr;//符号栈
string s;//字符串
char pri[9][9]{">><<<<>>",">><<<<>>",">>>><<>>",">>>><<>>",">>>>><>>","<<<<<<= "," ","<<<<<< ="};//优先级
int dth(char a){
if(a=='+') return 0;
if(a=='-') return 1;
if(a=='*') return 2;
if(a=='/') return 3;
if(a=='^') return 4;
if(a=='(') return 5;
if(a==')') return 6;
if(a=='\0') return 7;
}
int cal(int n1,char tr,int n2){//计算的具体方法
if(tr=='+') return n1+n2;
if(tr=='-') return n1-n2;
if(tr=='*') return n1*n2;
if(tr=='/') return n1/n2;
if(tr=='^'){
int ans=1;
for(int i=0;i<n2;i++) ans*=n1;
return ans;
}
}
int main(){
optr.push('\0');//最开始的符号
int i=0;
cin>>s;//输入的数
while(!optr.empty()){//当符号栈不为空
char c=s[i];
if(isdigit(c)){//假如是数
opnd.push(c-'0');//入数栈
i++;
}
else{//假如是符号
char cmp=pri[dth(optr.top())][dth(c)];//读取优先级数组
if(cmp=='<'){
optr.push(c);
i++;
}
else if(cmp=='='){
optr.pop();
i++;
}
else if(cmp=='>'){
int n2=opnd.top();
opnd.pop();
int n1=opnd.top();
opnd.pop();
int result=cal(n1,optr.top(),n2);
opnd.push(result);
optr.pop();
}
}
}
cout<<opnd.top();
return 0;
}
中缀表达式比较慢,我们还可以来看看后缀表达式——计算机偏爱的语言。
警告
警告
警告
警告
以下内容由聪明机智善良的stellar原创,未经许可不得抄袭(虽然也没啥···)【???画风迷死】
话不多说,上家伙!
#include<iostream>
#include<cstring>
#include<stack>
//demi myself!
using namespace std;
char a[105];
stack<int> opnd;
int cal(int n1,char y,int n2){
if (y=='+') return n1+n2;
if (y=='-') return n1-n2;
if (y=='*') return n1*n2;
if (y=='/') return n1/n2;
}
int main(){
cin>>a;
int l=strlen(a);
for(int i=0;i<l;i++){
if (a[i]>='0'&&a[i]<='9'){
opnd.push(a[i]-'0');
}
else{
int n2=opnd.top();
opnd.pop();
int n1=opnd.top();
opnd.pop();
int ans=cal(n1,a[i],n2);
opnd.push(ans);
}
}
cout<<opnd.top()<<endl;
return 0;
}
上代码
#include<iostream>
#include<cstring>
#include<stack>
//demi myself!
using namespace std;
char a[105];
stack<int> opnd;
int cal(int n1,char y,int n2){
if (y=='+') return n1+n2;
if (y=='-') return n1-n2;
if (y=='*') return n1*n2;
if (y=='/') return n1/n2;
}
int main(){
cin>>a;
int l=strlen(a);
for(int i=l-1;i>=0;i--){
if (a[i]>='0'&&a[i]<='9'){
opnd.push(a[i]-'0');
}
else{
int n1=opnd.top();
opnd.pop();
int n2=opnd.top();
opnd.pop();
int ans=cal(n1,a[i],n2);
opnd.push(ans);
}
}
cout<<opnd.top()<<endl;
return 0;
}
标签:str 计算机 mes pac 就是 show ptr case names
原文地址:https://www.cnblogs.com/irischen/p/biaodashiqiuzhi-1.html