后缀表达式
/*
第一版中缀表达式便后缀表达式
*/
#include<iostream>
#include<bits/stdc++.h>
#include<string.h>
#define MAX 1001
using namespace std;
enum fuhao{
};
typedef class _stack
{
private:
char *express,*sign; //express存数字,sign存字符;
int exp_top = -1; //表示栈顶
int sign_top = -1;//表示栈顶
public:
void setin_length(int x);// 输入栈的大小;
void setin_sta(char *string);//用于输入栈中的每一个元素
void setout();//用于输出栈内元素;
void transform();//转移;
} sta;
void sta::setin_length(int x)
{
express=(char *)calloc(x+1,sizeof(char));
sign=(char *)calloc(x+1,sizeof(char));
}
void sta::setin_sta(char * str)
{
char ch;int i=0;
while(1){
ch=*(str + i);
if(ch >= '0' && ch <='9') express[++exp_top] = ch;
else if(ch >='(' && ch <='/') {
if(ch == '('){
sign[++sign_top] = ch;
}
else if(ch == ')') {
while(sign[sign_top] != '('){
char temp;
temp=sign[sign_top];
sign[sign_top] = 0;
sign_top--;
express[++exp_top]=temp;
}
sign[sign_top] =0;
sign_top--;
}
else {
if(sign_top == -1) {
sign[++sign_top] = ch;
}
else {
char temp;
temp=sign[sign_top];
if(((temp == '+'|| temp =='-' ) && (ch == '*' || ch == '/')) || (temp == '(') ) {
sign[++sign_top] = ch;
}
else if((temp=='+'||temp=='-')&&(ch=='+'||ch=='-')||(temp=='*'||temp=='/')&&(ch=='*'||ch=='/')){
char temp;
temp = sign[sign_top];
sign[sign_top] = ch;
express[++exp_top] = temp;
}
else {
transform();
sign[++sign_top] = ch;
}
}
}
}
else {
transform();
break;
}
i++;
}
}
void sta::setout()
{
int i;
for(i=0;i<=exp_top;i++){
cout<<express[i];
}
cout<<endl;
}
void sta::transform(){
char ch;
while(sign_top > -1){
ch=sign[sign_top];
sign_top--;
express[++exp_top]=ch;
}
}
int main()
{
sta kit;
char express[MAX];
int len;
cout<<"Please set down the size of your stack to prevent the stack flowed out:"<<endl;
cin>>len;
kit.setin_length(len);
cout<<"Please set down your expression:"<<endl;
scanf("%s",express);
cout<<"Your Express is transformed to :"<<endl;
kit.setin_sta(express);
kit.setout();
}
标签:ch,sta,int,void,sign,char,考核,超算,第二次
From: https://www.cnblogs.com/zzxs-blog/p/16881540.html