C++自定义数据结构实现中缀表达式的计算-代码
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define MaxSize 100
typedef struct{
int data[MaxSize];
int top;
}SqStack1;
typedef struct{
char data[MaxSize];
int top;
}SqStack2;
//初始化栈
void InitStack(SqStack1 &S){
S.top=-1;
}
//初始化栈
void InitStack(SqStack2 &S){
S.top=-1;
}
//判断栈是否为空
bool StackEmpty(SqStack1 S){
if(S.top==-1){
return true;
}else{
return false;
}
}
//判断栈是否为空
bool StackEmpty(SqStack2 S){
if(S.top==-1){
return true;
}else{
return false;
}
}
//新元素入栈
bool Push(SqStack1 &S,int x){
if(S.top==MaxSize-1){
return false;
}
S.top+=1;
S.data[S.top]=x;
return true;
}
//新元素入栈
bool Push(SqStack2 &S,char x){
if(S.top==MaxSize-1){
return false;
}
S.top+=1;
S.data[S.top]=x;
return true;
}
//栈顶元素出站用 X来保存
bool Pop(SqStack1 &S,int &x){
if(S.top==-1){
return false;
}
x=S.data[S.top];
S.top-=1;
return true;
}
//栈顶元素出站用 X来保存
bool Pop(SqStack2 &S,char &x){
if(S.top==-1){
return false;
}
x=S.data[S.top];
S.top-=1;
return true;
}
//查询栈顶元素
bool Top(SqStack1 &S,int &x){
if(S.top==-1){
return false;
}
x=S.data[S.top];
return true;
}
//查询栈顶元素
bool Top(SqStack2 &S,char &x){
if(S.top==-1){
return false;
}
x=S.data[S.top];
return true;
}
map<char,int> mp;//存放自定义优先级
string str;//存放输入表达式
SqStack1 S1;//存放数字
SqStack2 S2;//存放字符
//优先级初始化 符号栈+数字栈 初始化
void init(){
mp['+']=1;
mp['-']=1;
mp['*']=2;
mp['/']=2;
InitStack(S1);
InitStack(S2);
}
//后缀表达式计算
void Caculate(){
int a;
Pop(S1,a);
int b;
Pop(S1,b);
char p;
Pop(S2,p);
int result=0;
if(p=='+'){
result=b+a;
}
if(p=='-'){
result=b-a;
}
if(p=='*'){
result=b*a;
}
if(p=='/'){
result=b/a;
}
Push(S1,result);
}
//中缀表达式--》后缀 表达式
void Handle(){
getline(cin,str);
for(int i=0;i<str.length();i++){
if(str[i]>='0' && str[i]<='9'){//处理多位数字入栈
int temp=0;
int j=i;
while(j<str.length() && (str[j]>='0' && str[j]<='9')){
temp=temp*10+str[j]-'0';
j++;
}
Push(S1,temp); //数字压栈
i=j-1;
}else if(str[i]=='('){//左括号入栈
Push(S2,str[i]); //字符压栈
}else if(str[i]==')'){//遇到右括号全部出栈 直到左括号
char tmp;
Top(S2,tmp);
while(tmp !='('){//循环查看栈顶元素是否是)括号
Caculate(); //不是左括号 计算
Top(S2,tmp);
}
char tmp1;
Pop(S2,tmp1);//左括号出栈
}else{
char tmp;
Top(S2,tmp);
while(!StackEmpty(S2) && mp[tmp]>=mp[str[i]]){//判断栈中的优先级和当前优先级的关系
Caculate();
Top(S2,tmp);
}
Push(S2,str[i]);
}
}
//当符号栈中还有元素时
while(!StackEmpty(S2)){
Caculate();
}
//输出数字栈的栈顶元素
int num;
Top(S1,num);
cout<<num<<endl;
}
int main(){
init();
Handle();
}
结果截图
2.C++利用自带容器实现中缀表达式的计算
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
stack<int> num;
stack<char> op;
map<char,int> mp;
string str;//存放输入表达式
//优先级初始化
void init(){
mp['+']=1;
mp['-']=1;
mp['*']=2;
mp['/']=2;
}
//后缀计算
void Caculate(){
int a=num.top();
num.pop();
int b=num.top();
num.pop();
char p=op.top();
op.pop();
int result=0;
if(p=='+'){
result=b+a;
}
if(p=='-'){
result=b-a;
}
if(p=='*'){
result=b*a;
}
if(p=='/'){
result=b/a;
}
num.push(result);
}
//中缀--》后缀
void Handle(){
getline(cin,str);
for(int i=0;i<str.length();i++){
if(str[i]>='0' && str[i]<='9'){//处理多位数字入栈
int temp=0;
int j=i;
while(j<str.length() && (str[j]>='0' && str[j]<='9')){
temp=temp*10+str[j]-'0';
j++;
}
num.push(temp);
i=j-1;
}else if(str[i]=='('){//左括号入栈
op.push(str[i]);
}else if(str[i]==')'){//遇到右括号全部出栈 直到左括号
while(op.top()!='('){
Caculate();
}
op.pop();//左括号出栈
}else{
while(op.size() && mp[op.top()]>=mp[str[i]]){//判断栈中的优先级和当前优先级的关系
Caculate();
}
op.push(str[i]);
}
}
//当符号栈中还有元素时
while(op.size()){
Caculate();
}
cout<<num.top()<<endl;
}
int main(){
init();
Handle();
}
结果截图