中缀转后缀&后缀求值

/*			
 *			Infix to Postfix, and get the value for Postfix.
 *			Date: 2013/3/8
 *													NolanJian
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct stacknode;
typedef struct stacknode StackNode;
typedef StackNode *StackPointer;

int ValueOfPostfix(char *postfix); 
void InfixToPostfix(char *infix, char *postfix);
char GetTop(StackPointer top);
void Push(StackPointer *top, int ch); 
void Pop(StackPointer *top, char *postfix, int *i, int ch); 
void StringToInt(char *postfix);
struct stacknode {
	int Num;
	StackPointer Next;	
};

int main() {
	char infix[100], postfix[100];
	while(scanf("%s", infix)) {
		memset(postfix, '\0', 100);
		InfixToPostfix(infix, postfix);
		printf("Infix:     %s\n", infix);
		printf("Postfix:   %s\n", postfix);
		StringToInt(postfix);
		printf("Result:    %d\n", ValueOfPostfix(postfix));
	}
	return 0;
}

void StringToInt(char *postfix) {
	while(*postfix) {
		if(*postfix >= '0' && *postfix <= '9')
			*postfix -= '0';
		postfix++;
	}
}

void Push(StackPointer *top, int ch) {
	StackPointer tmp;
	if(*top == NULL) {
		tmp = (StackPointer)malloc(sizeof(StackNode));
		tmp->Next = NULL;
		tmp->Num = ch;
		(*top) = tmp;
		return ;
	}
	tmp = (StackPointer)malloc(sizeof(StackNode));
	tmp->Next = (*top);
	tmp->Num = ch;
	(*top) = tmp;
}

void Pop(StackPointer *top, char *postfix, int *i, int ch) {
	StackPointer tmp; 
	if(ch == '+' || ch == '-' || ch == '*' || ch == '/') {
		postfix[(*i)++] = (*top)->Num;
		tmp = *top;
		*top = (*top)->Next;
		free(tmp);
		return ;
	}
	if(ch == ')') {
		while((*top)->Num != '(') {
			postfix[(*i)++] = (*top)->Num;
			tmp = *top;
			*top = (*top)->Next;
			free(tmp);
		}
		tmp = *top;
		*top = (*top)->Next;
		free(tmp);
		return ;
	}
	if(ch == 3) {
		postfix[(*i)++] = (*top)->Num;
		tmp = *top;
		*top = (*top)->Next;
		free(tmp);
		return ;
	}
	tmp = *top;
	*top = (*top)->Next;
	free(tmp);
}

void InfixToPostfix(char *infix, char *postfix) {
	StackPointer top = NULL;
	int i = 0, flag;
	char ch;
	while(*infix) {
		if(*infix >= '0' && *infix <= '9')
			postfix[i++] = *infix;
		else if(*infix == ')')
			Pop(&top, postfix, &i, *infix);
		else if(*infix == '*' || *infix == '/' || *infix == '(')
			Push(&top, *infix);
		else if(*infix == '+' || *infix == '-') {
			ch = 0;
			if(!top) {
				Push(&top, *infix);
				infix++;
				continue;
			}
			while(top){
				flag = 0;
				ch = GetTop(top);
				if(ch == '(') {
					Push(&top, *infix);
					flag = 1;
					break;
				}
				else
					Pop(&top, postfix, &i, *infix);
			}
			if(!flag)
				Push(&top, *infix);
		}
		infix++;
	}
	while(top)
		Pop(&top, postfix, &i, 3);
	postfix[i] = '\0';
}

char GetTop(StackPointer top) {
	return top->Num;
}

int ValueOfPostfix(char *postfix) {
	StackPointer top = NULL; 
	int a, b;
	while(*postfix) {
		if(*postfix >= 0 && *postfix <= 9) {
			Push(&top, *postfix);
			postfix++;
			continue;
		}
		if(*postfix == '+') {
			b = GetTop(top);
			Pop(&top, 0, 0, 0);
			a = GetTop(top);
			Pop(&top, 0, 0, 0);
			Push(&top, a + b);
			postfix++;
			continue;
		}
		if(*postfix == '-') {
			b = GetTop(top);
			Pop(&top, 0, 0, 0);
			a = GetTop(top);
			Pop(&top, 0, 0, 0);
			Push(&top, a - b);
			postfix++;
			continue;
		}	
		if(*postfix == '*') {
			b = GetTop(top);
			Pop(&top, 0, 0, 0);
			a = GetTop(top);
			Pop(&top, 0, 0, 0);
			Push(&top, a * b);
			postfix++;
			continue;
		}	
		if(*postfix == '/') {
			b = GetTop(top);
			Pop(&top, 0, 0, 0);
			a = GetTop(top);
			Pop(&top, 0, 0, 0);
			Push(&top, a / b);
			postfix++;
			continue;
		}	
	}
	return top->Num;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值