中缀算式转后缀算式

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

中缀算式化为后缀算式: 1. 首先初始化一个栈和一个结果数组 2. 遍历中缀算式,如果是数字则将其直接加入结果数组(可以通过判断字符是否为数字实现),如果是运算符则判断其与栈顶运算符的优先级,如果栈顶运算符优先级大于当前运算符则将栈顶运算符弹出并加入结果数组,然后继续比较新的栈顶运算符和当前运算符,直到遇到优先级小于等于当前运算符的栈顶运算符或者栈为空时,将当前运算符加入栈中 3. 遍历完中缀算式后,如果栈中仍有运算符,则依次弹出并加入结果数组 4. 最终结果数组中存储着后缀算式 后缀算式求值: 1. 初始化一个栈 2. 遍历后缀算式,如果是数字则将其入栈,如果是运算符则弹出栈顶的两个数字进行运算,然后将结果压入栈中 3. 遍历完后缀算式之后,栈中仅剩一个数字,该数字即为所求结果 C语言实现: 中缀算式后缀: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> int cmp(char a, char b) { if ((a == '+' || a == '-') && (b == '*' || b == '/')) return -1; else return 1; } void infix2postfix(char *infix, char *postfix) { char stack[1000]; int top = -1, i = 0, j = 0; while (infix[i] != '\0') { if (isdigit(infix[i])) { postfix[j++] = infix[i++]; } else { while (top >= 0 && cmp(stack[top], infix[i]) == -1) { postfix[j++] = stack[top--]; } stack[++top] = infix[i++]; } } while (top >= 0) { postfix[j++] = stack[top--]; } postfix[j] = '\0'; } int eval(char op, int a, int b) { if (op == '+') return a + b; else if (op == '-') return a - b; else if (op == '*') return a * b; else return a / b; } int postfix_eval(char *postfix) { int stack[1000], top = -1, i = 0; while (postfix[i] != '\0') { if (isdigit(postfix[i])) { stack[++top] = postfix[i++] - '0'; } else { int b = stack[top--]; int a = stack[top--]; stack[++top] = eval(postfix[i++], a, b); } } return stack[top]; } int main() { char infix[1000], postfix[1000]; scanf("%s", infix); infix2postfix(infix, postfix); printf("%d\n", postfix_eval(postfix)); return 0; } ``` 注意:实现时可以将操作符的优先级需要值设为更高的整数,以免在处理除法时因为优先级问题导致结果错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值