1. 当遇到数字时直接输出
2. 当遇到运算符时,依次输出栈中比当前运算级别更高或相等的运算符,直到遇到栈中的 ‘(’或栈底。然后把当前运算符压入栈
3. 当遇到 ‘)’ 直接输出栈中的元素,直到遇到 '(' 结束
4. 当表达式字符串都遍历完成之后,把栈中元素依次出栈
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#define MAXSIZE 60
#define datatype char
struct Stack
{
datatype data[MAXSIZE];
int top;
};
struct String
{
datatype data;
String *next;
};
int CreateStack(Stack &s){
s.top = -1;
return 1;
}
int Push(Stack &s,datatype data){
if(s.top == MAXSIZE-1)
return 0;
s.data[++s.top] = data;
return 1;
}
int Pop(Stack &s,datatype &data){
if(s.top == -1)
return 0;
data = s.data[s.top--];
return 1;
}
datatype GetTop(Stack &s){
if(s.top!=-1)
return s.data[s.top];
return NULL;
}
int GetLength(char data[]){
return sizeof(data)/sizeof(char);
}
bool FindChar(Stack s){
while(s.top!=-1){
if(s.data[s.top--] == '(')
return true;
}
return false;
}
bool GetError(datatype data[],int length){
if(length<=0)
printf("Expression is Empty!\n");
if(length>MAXSIZE)
printf("Expression's Length is Over Max!\n");
for(int i =0;i+1<=length;i++){
if(data[i]=='+'||data[i]=='-'||data[i]=='*'||data[i]=='/'){
if(data[i] == data[i+1])
return true;
}
}
return false;
}
String *GetInierElement(Stack &s){
String *Inier = NULL;
String *head = Inier;
int i = 0;
while(s.data[s.top]!='('&&s.top!=-1){
datatype res;
Pop(s,res);
String *node = (String*)malloc(sizeof(String));
node->data = res;
node->next = NULL;
if(!Inier)
{
Inier = node;
head = Inier;
continue;
}
head->next = node;
head = node;
}
datatype da;
Pop(s,da);
return Inier;
}
String *PopAllPriorityLevel(Stack &s,datatype data){
String *Outer = NULL;
String *head = Outer;
int j = 0;
if(data == '+'||data == '-'){
while(s.top!=-1&&s.data[s.top]!='(')
{
datatype res;
Pop(s,res);
String *node = (String*)malloc(sizeof(String));
node->data = res;
node->next = NULL;
if(!Outer)
{
Outer = node;
head = Outer;
continue;
}
head->next = node;
head = node;
}
Push(s,data);
return Outer;
}
if(data == '/'||data == '*'){
while(s.top!=-1&&(s.data[s.top]!='-'&&s.data[s.top]!='+')&&s.data[s.top]!='(')
{
datatype res;
Pop(s,res);
String *node = (String*)malloc(sizeof(String));
node->data = res;
node->next = NULL;
if(!Outer)
{
Outer = node;
head = node;
continue;
}
head->next = node;
head = node;
}
Push(s,data);
return Outer;
}
return NULL;
}
String *GetRPN(Stack &s,datatype data[],int length){
String *result = NULL;
String *head = result;
int j = 0;
if(GetError(data,length)){
return NULL;
}
for(int i = 0;i<length;i++){
if(data[i]!='+'
&&data[i]!='-'
&&data[i]!='*'
&&data[i]!='/'
&&data[i]!='('
&&data[i]!=')'){
String *node = (String*)malloc(sizeof(String));
node->data = data[i];
node->next = NULL;
if(!result)
{
result = node;
head = node;
continue;
}
head->next = node;
head = node;
}else{
if(data[i]==')'){
if(!FindChar(s))
return NULL;
String *res = GetInierElement(s);
if(!res)
continue;
if(!result){
result = res;
while(res->next)
res = res->next;
head = res;
continue;
}
head->next = res;
while(res->next)
res = res->next;
head = res;
}else if(data[i]=='('){
Push(s,data[i]);
}else{
String *res = PopAllPriorityLevel(s,data[i]);
if(!res)
continue;
if(!result){
result = res;
while(res->next)
res = res->next;
head = res;
continue;
}
head->next = res;
while(res->next)
res = res->next;
head = res;
}
}
}
while(s.top!=-1)
{
datatype dl;
Pop(s,dl);
String *node = (String*)malloc(sizeof(String));
node->data = dl;
node->next = NULL;
head->next = node;
head = node;
}
return result;
}
void OutPut(datatype data[],int length,String *result){
if(!result)
{
printf("Input Error!\n");
return;
}
printf("Infix Expression: ");
for (int i = 0; i < length; i++)
{
printf("%c",data[i]);
}
printf("\n");
printf("Postfix Expression: ");
while(result){
printf("%c",result->data);
result = result->next;
}
printf("\n");
}
int main(){
Stack s;
datatype data[] = {"3*(5-2)+7"};
CreateStack(s);
int length = 0;
length = sizeof(data)/sizeof(datatype) - 1;
String *result = GetRPN(s,data,length);
OutPut(data,length,result);
system("pause");
}