中缀表达式转后缀表达式的应用
我的算法以及思路:
首先将输入的东西作为字符串存入到一个字符串数组,然后将中缀表达式转化为后缀表达式(其中关键在于运算符的转化,比较栈顶元素和当前运算符的优先级,如果栈顶元素的优先级大,则压入栈中,否则把栈里的运算符弹出直到为空,并且加入到后缀表达式的字符串中,再压栈,最后将后缀表达式转化为计算结果
样例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
double date;
char opt;
struct node* pre;
}* pnode, node;
typedef struct stack {
pnode bottom; //指向底部
pnode top; //指向头部
}* pstack, stack;
pstack create_stack(void) {
pstack s = (pstack)malloc(sizeof(stack));
s->bottom = s->top = (pnode)malloc(sizeof(node));
return s;
}
void push(pstack s, char date) {
pnode t = (pnode)malloc(sizeof(node));
t->pre = s->top;
t->date = date;
s->top = t;
}
void pop(pstack s) {
if (s->top == s->bottom) {
printf("不能弹出,此时栈为空\n");
return;
}
pnode t = s->top;
s->top = s->top->pre;
free(t);
}
int empty(pstack s) {
if (s->top == s->bottom) {
return 1;
}
else {
return 0;
}
}
int top(pstack s) {
return s->top->date;
}
void push_num(pstack s, int date) {
pnode t = (pnode)malloc(sizeof(node));
t->pre = s->top;
t->date = date;
s->top = t;
}
int main(void) {
pstack s_opt = create_stack();
pstack s_num = create_stack();
char str[100], suffix[100];
int opt[100];
memset(opt, 0, sizeof(opt));
scanf("%s",str);
opt['+'] = opt['-'] = 1;
opt['*'] = opt['/'] = 2;
opt['('] = opt[')'] = 0;
int len = strlen(str), p = 0, flag = 1;
//printf("%d\n",len);
for (int i = 0; i < len; i++) {
if (str[i] <= '9' && str[i] >= '0') {
suffix[p++] = str[i];
if (i+1 < len && str[i+1] <= '9' && str[i+1] >= '0');
else suffix[p++] = ' ';
flag = 0;
continue;
}
else {
flag++;
}
if (str[i] == '(') {
push(s_opt, str[i]);
}
else if (str[i] == ')') {
while(top(s_opt) != '(') {
//printf("%c\n", top(s_opt));
suffix[p++] = top(s_opt);
pop(s_opt);
}
pop(s_opt);
//printf("1");
}
else if (empty(s_opt) && str[i] != ' ') {
push(s_opt, str[i]);
}
else {
while(!empty(s_opt) && opt[str[i]] <= opt[top(s_opt)]) {
suffix[p++] = top(s_opt);
pop(s_opt);
}
push(s_opt, str[i]);
}
if (flag >= 1) {
// suffix[p++] = ' ';
}
}
while(!empty(s_opt)) {
suffix[p++] = top(s_opt);
pop(s_opt);
}
for (int i = 0; i < p-1; i++) {
printf("%c",suffix[i]);
}
printf("\n");
pstack number_stack = create_stack();
flag = 0;
double sum = 0, ans = 0;
for (int i = 0; i < p; i++) {
if (suffix[i] <= '9' && suffix[i] >= '0') {
sum = sum*10 + (suffix[i]-'0');
flag = 1;
}
else {
if (flag == 1) {
push(number_stack, sum);
sum = 0;
flag = 0;
}
if (suffix[i] == '+' || suffix[i] == '-' || suffix[i] == '*' || suffix[i] == '/') {
double t1 = number_stack->top->date;
pop(number_stack);
if (suffix[i] == '+') {
number_stack->top->date += t1;
}
else if (suffix[i] == '-') {
number_stack->top->date -= t1;
}
else if (suffix[i] == '*') {
number_stack->top->date *= t1;
}
else if (suffix[i] == '/') {
number_stack->top->date /= t1;
}
}
}
}
printf("%lf",number_stack->top->date);
return 0;
}
遇到的问题及总结:
1、括号要拿出来特殊处理下,比如遇到’(‘直接入栈,就是为了等到’)’,遇到后就将它两之间的操作符都弹到后缀表达式中
2、优先级:乘除大于加减,在操作符入栈的时候要按照优先级大小,栈顶元素优先级小于等于直接入栈,否则把栈中元素弹出来直到当前操作符小于等于栈中操作符的优先级为止
3、转化为字符串的时候,两位数字以上的要进行特殊处理,我是通过加的一个空格,最后计算的时候也要注意,用一个sum来存当前的数字