#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#ifndef _Stack_h
struct StackRecord;
typedef struct StackRecord *Stack;
int IsEmpty(Stack s);
int IsFull(Stack s);
Stack CreateStack(int MaxElements);
void DisposeStack(Stack s);
void MakeEmpty(Stack s);
void Push(int x,Stack s);
int Top(Stack s);
void Pop(Stack s);
int TopAndPop(Stack s);
#endif
//head is dummy
#define EmptyTOS -1
#define MinStackSize 5
struct StackRecord{
int capacity;
int topofstack;
int *a;
};
int IsEmpty(Stack s){
return s->topofstack == EmptyTOS;
}
int IsFull(Stack s){
return s->topofstack == s->capacity -1;
}
Stack CreateStack(int MaxElements){
Stack s;/*
if(MaxElements<MinStackSize){
printf("Stack size is too small\n");
return 0;
}
*/
s = (Stack)malloc(sizeof(struct StackRecord));
if(s == NULL){
printf("Out of Space!\n");
return 0;
}
s->a = (int *)malloc(sizeof(int)*MaxElements);
if(s->a == NULL){
printf("Out of Space\n");
return 0;
}
s->capacity = MaxElements;
MakeEmpty(s);
return s;
}
void DisposeStack(Stack s){
if(s!= NULL){
free(s->a);
free(s);
}
}
void MakeEmpty(Stack s){
s->topofstack = EmptyTOS;
}
void Push(int x,Stack s){
if(IsFull(s))
printf("Stack is Full\n");
s->a[++s->topofstack] = x;
}
int Top(Stack s){
if(!IsEmpty(s))
return s->a[s->topofstack];
printf("Stack is Empty\n");
return 0;
}
void Pop(Stack s){
if(IsEmpty(s))
printf("Stack is Empty\n");
else s->topofstack--;
}
int TopAndPop(Stack s){
if(!IsEmpty(s))
return s->a[s->topofstack--];
printf("Stack is Empty\n");
return 0;
}
int compare(char a,char b){
if((a == '+'||a == '-')&&(b == '*'||b == '/'))
return -1;
else if((a == '*'||a == '/')&&(b == '+'||b == '-'))
return 1;
else if((a == '*'||a == '/')&&(b == '*'||b == '/'))
return 0;
else if((a == '+'||a == '-')&&(b == '+'||b == '-'))
return 0;
else if(b == ')'&&a !='(')
return 1;
else if(a == '(')
return -1;
}
char *InfixToPostfix(char *express){
int len,i,j;
char postfix[50],ch;
Stack s;
len = strlen(express);
s = CreateStack(len);
for(i = 0,j;i<len;i++){
if(express[i]>='0'&&express[i]<='9'||express[i] == '.'){
postfix[j++] = express[i];
if((express[i+1]<'0' ||express[i+1]>'9')&&express[i+1]!='.')
postfix[j++] = ' ';
}
else {
if(express[i] == '+'&&express[i-1]!=')'&&(i == 0||express[i-1]<'0'||express[i-1]>'9'))
continue;
if(express[i] == '-'&&express[i-1]!=')'&&(i == 0||express[i-1]<'0'||express[i-1]>'9')){
postfix[j++] = express[i];
continue;
}
if(express[i] == '('||IsEmpty(s))
Push(express[i],s);
/* else if(express[i] == ')'){
while(!IsEmpty(s)&&ch = top(s)&&ch!='('){
postfix[j++] = ch;
postfix[j++] = ' ';
pop(s);
}
pop(s);
}*/
else
{
while(!IsEmpty(s)&&(ch =Top(s))!='\0'&&compare(ch,express[i])>=0){
Pop(s);
postfix[j++] = ch;
postfix[j++] = ' ';
}
if(ch == '('&&express[i] == ')')
Pop(s);
else
Push(express[i],s);
}
}
}
while(!IsEmpty(s)){
ch = Top(s);
postfix[j++] = ch;
postfix[j++] = ' ';
Pop(s);
}
if(postfix[j-1] == ' ')
postfix[j-1] = '\0';
else postfix[j] = '\0';
return postfix;
}
int main(){
char express[21];
char *postfix;
int i,j,n;
scanf("%s",express);
postfix = InfixToPostfix(express);
printf("%s",postfix);
return 0;
}
中缀算式转后缀算式
最新推荐文章于 2023-01-14 19:53:26 发布