一.题目要求
假设给出的中缀表达式由单字母变量和双目四则运算算符构成。试编写程序,将一个通常书写形式且书写正确的表达式转换为逆波兰式(后缀表达式)。
二.设计思路
①在主函数中输入字符串(中缀表达式)
②入栈规则(函数trans中规则):
循环遍历字符串中的每个元素,如果:
a.元素是字母(a,b,c....),则直接printf出来。
b.元素是运算符* / ,则直接入栈。
c.元素str[i]是运算符+ - ,有两种情况:
c1.栈为空:不存在竞争,直接入栈。
c2.栈不为空:看看栈顶元素是什么(pop),如果是‘(’,则将‘(’重新压入栈,因为它 的优先级更高,如果是其他符号(* / + -),都先输出它们,再进行pop,只到栈顶元素 为‘(’ ,在这之后将str[i]入栈。
d.元素是‘)’: 将栈中的元素以此print出来,直到栈顶元素为‘(’ 。
e.在遍历完元素后,在以此将栈中的元素打印出来。
三. 实现代码
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define max 100//无;
typedef struct stack
{
int top;
char data[max];
}stack;
void init(stack* s)
{
s->top = -1;
}
bool isempty(stack s)//不用回传,不带*
{
if (s.top == -1)//不带*的形式用.
{
return true;
}
else
{
return false;//false指的是不为空
}
}
void push(stack* s, char c)
{
if (s->top == max - 1)
{
printf("栈满了");
return;
}
else
{
s->top += 1;
s->data[s->top] = c;
return;
}
}
void pop(stack* s, char* c)//c带*原因,在主函数中设置c,将c传至函数。用void类型是因为回传两个参数
{
if (s->top == -1)
{
return;
}
else
{
*c = s->data[s->top];
s->top -= 1;
}
}
void trans(char str[], int len)//str[]
{
stack s;
init(&s);
int i = 0;
char c;
for (i; i < len; i++)
{
//是字母就打印
if (str[i] >= 'a' && str[i] <= 'z')
{
printf("%c", str[i]);
}
else if (str[i] == '(' || str[i] == '*' || str[i] == '/')//( * / 不管什么情况都入栈
{
push(&s, str[i]);
}
else if (str[i] == '+' || str[i] == '-')//+ - 看情况
{
if (s.top == -1)//栈是空的
{
push(&s, str[i]);
}
else
{
char popchar;
pop(&s, &popchar);
while (popchar != '(')
{
printf("%c", popchar);
pop(&s, &popchar);
}
//此时popchar="("
push(&s, popchar);
push(&s, str[i]);
}
}
else if (str[i] == ')')
{
char popchar_2;
pop(&s, &popchar_2);
while (popchar_2 != '(')
{
printf("%c", popchar_2);
pop(&s, &popchar_2);
}
}//处理完栈里连(都不剩。
}
while (s.top != -1)
{
char pr;
pop(&s, &pr);
printf("%c", pr);
}
}
int main()
{
char str[max];
scanf("%s", str);
int l;
l = strlen(str);
trans(str, l);
}
四. 运行结果
ps:第一篇博客,有不对不好的地方欢迎各位高手批评