数据结构--中缀表达式转后缀c语言代码实现

#include<stdlib.h>
#include<stdio.h>
#define Maxsize 10
typedef struct node{
	char data[Maxsize];
	int top;
}Stack;
bool Isempty(Stack &s){
	if(s.top == -1){
		return true;
	}
	return false;
}
bool IsFull(Stack &s){
	if(s.top+1 == Maxsize){
		return true;
	}
	return false;
}
void InitStack(Stack &s){
	s.top = -1;
}
bool PushStack(Stack &s,char data){
	if(IsFull(s)){
		return false;
	}
	s.data[++s.top] = data;
	return true;
}
bool PopStack(Stack &s,char &data){
	if(Isempty(s)){
		return false;
	}
	data = s.data[s.top--];
	return true;
}
bool GetTop(Stack &s,char &data){
	if(Isempty(s)){
		return false;
	}
	data = s.data[s.top];
	return true;
}
int length(char str[]){
	int length=0;
	while(str[length] != '\0'){
		length++;
	}
	return length;
}
bool IsNumber(char a){
	if(a >= '0' && a <= '9'){
		return true;
	}
	return false;
}
void change(Stack &s,char str[],char last[]){
	int len = length(str);
	int flag = -1;
	char data;
	for(int i=0;i<len;i++){
		/*
			如果是数字,直接拿出来,不做任何处理 
		*/
		if(IsNumber(str[i])){
			flag++;
			last[flag] = str[i];
		}
		/*
			加减运算符优先级最低,如果栈顶元素为空则直接入栈,否则将栈中存储的运算符全部弹栈,
			如果遇到左括号则停止,将弹出的左括号从新压栈,因为左括号要和又括号匹配时弹出,
			这个后面单独讨论。弹出后将优先级低的运算符压入栈中
		*/
		else if(str[i] == '+' || str[i] == '-'){
			if(Isempty(s)){
				PushStack(s,str[i]);
			}else{
				while(!Isempty(s) && s.data[s.top] != '('){
					PopStack(s,data);
					flag++;
					last[flag] = data;	
				}
				PushStack(s,str[i]);
			}
		}
		/*
			乘、除、左括号都是优先级高的,直接压栈
		*/
		else if(str[i] == '(' || str[i] == '*' || str[i] == '/'){
			PushStack(s,str[i]);
		}
		/*
			当遇到右括号是,把括号里剩余的运算符弹出,直到匹配到左括号为止
 			左括号只弹出不打印(右括号也不压栈)
		*/
		else if(str[i] == ')'){
			PopStack(s,data);
			while(data != '(' ){
				flag++;			
				last[flag] = data; 
				PopStack(s,data);
			}
		}
	}
	/*
		最后把栈中剩余的运算符依次弹栈
	*/
	while(!Isempty(s)){
		PopStack(s,data);
		flag++;			
		last[flag] = data; 
	}
	last[++flag] = '\0';
}
int main(){
	Stack s;
	InitStack(s);
	char str[] = "2*(9+6/3-5)+4";
	int len = length(str);
	char last[] ="";
	change(s,str,last);
	printf("%s",last);
} 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是中缀表达式后缀表达式的C语言代码实现: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_LEN 100 // 定义最大表达式长度 typedef struct { char data[MAX_LEN]; int top; } Stack; // 初始化栈 void InitStack(Stack *s) { s->top = -1; } // 判断栈是否为空 int IsEmpty(Stack *s) { return s->top == -1; } // 判断栈是否已满 int IsFull(Stack *s) { return s->top == MAX_LEN - 1; } // 入栈操作 void Push(Stack *s, char ch) { if (IsFull(s)) { printf("Error: stack is full!\n"); exit(1); } s->top++; s->data[s->top] = ch; } // 出栈操作 char Pop(Stack *s) { if (IsEmpty(s)) { printf("Error: stack is empty!\n"); exit(1); } char ch = s->data[s->top]; s->top--; return ch; } // 获取栈顶元素 char GetTop(Stack *s) { if (IsEmpty(s)) { printf("Error: stack is empty!\n"); exit(1); } return s->data[s->top]; } // 获取运算符优先级 int GetPriority(char ch) { if (ch == '+' || ch == '-') { return 1; } else if (ch == '*' || ch == '/') { return 2; } else { return 0; } } void InfixToSuffix(char *infix, char *suffix) { Stack s; InitStack(&s); int len = strlen(infix); int j = 0; for (int i = 0; i < len; i++) { char ch = infix[i]; if (isdigit(ch)) { // 如果是数字,直接添加到后缀表达式中 suffix[j++] = ch; } else if (ch == '(') { // 如果是左括号,入栈 Push(&s, ch); } else if (ch == ')') { // 如果是右括号,依次弹出栈顶元素直到遇到左括号 char top_ch = GetTop(&s); while (top_ch != '(') { suffix[j++] = top_ch; Pop(&s); top_ch = GetTop(&s); } Pop(&s); // 弹出左括号 } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') { // 如果是运算符 while (!IsEmpty(&s) && GetPriority(ch) <= GetPriority(GetTop(&s))) { // 当前运算符优先级小于等于栈顶运算符优先级,弹出栈顶元素添加到后缀表达式中 suffix[j++] = Pop(&s); } Push(&s, ch); // 当前运算符入栈 } else { printf("Error: invalid character!\n"); exit(1); } } // 将栈中剩余元素依次弹出添加到后缀表达式中 while (!IsEmpty(&s)) { suffix[j++] = Pop(&s); } suffix[j] = '\0'; // 添加字符串结束符 } int main() { char infix[MAX_LEN], suffix[MAX_LEN]; printf("请输入中缀表达式:"); scanf("%s", infix); InfixToSuffix(infix, suffix); printf("后缀表达式为:%s\n", suffix); return 0; } ``` 以上是中缀表达式后缀表达式的C语言代码实现,希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值