中缀算式转后缀算式

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值