C语言(中缀转后缀)

摘自<<数据结构与算法分析.第二版>>我目前看的就是这本书

a+b*c+(d*e+f)*g

转换成后缀表达式为:a b c * + d e * f + g * +

        当读到一个操作数的时候,立即把它放到输出中.操作符不立即输出,所以必须先存在某个地方.正确做法是将已经捡到过的操作符放进栈中而不是放到输出中.当遇到左圆括号时我们也要将其推入栈中.我们从一个空栈开始计算。

        如果见到一个右括号,那么就将栈元素弹出,将弹出的符号写出直到我们遇到一个(对应的)左括号,但是这个左括号只被弹出,并不输出。

      如果见到任何其他符号("+" "*" "("),那么我们从栈中弹出栈元素直到发现优先级更低的元素为止。有一个例外:除非是在处理一个")"的时候,否则我们绝不从栈中移走"("。对于这种操作,“+”的优先级最低,而"("的优先级最高。当从栈弹出元素的工作完成后,我们将操作符压入栈中。

     最后,如果读到输入的末尾,我们将栈元素弹出直到该栈变成空栈,将符号写到输出中。

这里再做个补充比如:a - b - c转换成ab- c- 而不是转换成 abc- - -。

如下代码里面有很多赋值为空白字符的,本来可以不这样写的,只不过在中缀转换为后缀完成后,方便计算结果值,不过计算的过程我没写,如果想写的话也可以作为补充.这里我们用的是int(整型)数值,不是double(浮点数)。

中缀转换为后缀之后 OutPut里面存储的数据也变为后缀表达式.想计算结果的话可以写成下面的形式,我也不多写过程。

while((ch=OutPut[i++])!='\0')

atoi函数用于将字符串转换为整数。

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define SIZE 50
struct Stack {
	int choice;
	struct Stack* next;
};
typedef struct Stack* Stack_t;

void reStack(Stack_t *stack_t);
void Posh(Stack_t* stack_t, int ch);
void Created_at(Stack_t* stack_t, char OutPut[]);
int Top(Stack_t* stack_t);
int kStack(Stack_t stack_t);
int Pop(Stack_t* stack_t);

int main(void) {
	Stack_t stack_t;
	char OutPut[SIZE],str[SIZE];
	int ch;
	int i = 0,j=0;
	int point_t,point_c,val;
	reStack(&stack_t);
	Created_at(&stack_t, OutPut);
	printf("%s", OutPut);
	return 0;
}
/*转后缀*/
void Created_at(Stack_t* stack_t, char OutPut[]) {
	char ch;
	int i = 0;
	int top = 0;
	char str[SIZE];
	while ((ch = getchar()) != '#') {
		if (isdigit(ch)) {
			OutPut[i++] = ch;
			OutPut[i] = '\0';
			continue;
		}
		if (ch != '('&&OutPut[i - 1] != ' ') {/*以防出现两个空白字符*/
			OutPut[i++] = ' ';
			OutPut[i] = '\0';
		}
		if (ch == '+') {
			top = Top(stack_t);
			if (top == '(' || top == 0) {
				Posh(stack_t, ch);
				continue;
			}
			else {
				while ((top = Top(stack_t)) != 0&&top!='(') {
					OutPut[i++] = Pop(stack_t);
					OutPut[i++] = ' ';
				}
				OutPut[i] = '\0';
				Posh(stack_t, ch);
			}
		}
		else if (ch == '-') {
			top = Top(stack_t);
			if (top == '(' || top == 0) {
				Posh(stack_t, ch);
				continue;
			}
			else {
				while ((top = Top(stack_t)) != 0 && top != '(') {
					OutPut[i++] = Pop(stack_t);
					OutPut[i++] = ' ';
				}
				OutPut[i] = '\0';
				Posh(stack_t, ch);
			}
		}
		else if (ch == '*') {
			top = Top(stack_t);
			if (top == '(' || top == 0) {
				Posh(stack_t, ch);
				continue;
			}
			else if (top == '/' || top == '*') {
				OutPut[i++] = Pop(stack_t);
				OutPut[i++] = ' ';
				OutPut[i] = '\0';
				Posh(stack_t, ch);
			}
			else Posh(stack_t, ch);
		}
		else if (ch == '/') {
			top = Top(stack_t);
			if (top == '(' || top == 0) {
				Posh(stack_t, ch);
				continue;
			}
			else if (top == '/' || top == '*') {
				OutPut[i++] = Pop(stack_t);
				OutPut[i++] = ' ';
				OutPut[i] = '\0';
				Posh(stack_t, ch);
			}
			else Posh(stack_t, ch);
		}
		else if (ch == '(') {
			Posh(stack_t, ch);
		}
		else if (ch == ')') {
			while (1) {
				top = Top(stack_t);
				if (top != '('&&top!=0) {
					OutPut[i++] = Pop(stack_t);
					OutPut[i++] = ' ';
					OutPut[i] = '\0';
				}
				else {
					top = Pop(stack_t);/*扔掉左括号*/
					break;
				}
			}
		}
	}
	while ((top=Pop(stack_t)) != 0) {
		if(OutPut[i-1]!=' ')
		OutPut[i++] = ' ';

		OutPut[i++] =top;
	}
	OutPut[i] = '\0';
}
/*初始化*/
void reStack(Stack_t* stack_t) {
	*stack_t = NULL;
}
/*压栈*/
void Posh(Stack_t* stack_t, int ch) {
	Stack_t stack_c = (Stack_t)malloc(sizeof(struct Stack));
	stack_c->choice = ch;
	stack_c->next = NULL;
	if (stack_t == NULL)
		*stack_t = stack_c;
	else {
		stack_c->next = *stack_t;
		*stack_t = stack_c;
	}
}
/*查看顶栈*/
int Top(Stack_t* stack_t) {
	if (kStack(*stack_t))/*栈为空*/
		return 0;
	return (*stack_t)->choice;
}
/*查看栈是否为空*/
int kStack(Stack_t stack_t) {
	return stack_t == NULL;
}
/*出栈*/
int Pop(Stack_t *stack_t) {
	if (kStack(*stack_t))/*栈为空*/
		return 0;
	int top = (*stack_t)->choice;
	Stack_t stack_c = *stack_t;
	*stack_t = stack_c->next;
	free(stack_c);
	return top;
}

 

 

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值