数据结构PTA习题:习题3.11 表达式转换 (25分)

习题3.11 表达式转换 (25分)

算术表达式有前缀表示法、中缀表示法和后缀表示法等形式。日常使用的算术表达式是采用中缀表示法,即二元运算符位于两个运算数中间。请设计程序将中缀表达式转换为后缀表达式。

输入格式:
输入在一行中给出不含空格的中缀表达式,可包含+、-、*、/以及左右括号(),表达式不超过20个字符。
输出格式:
在一行中输出转换后的后缀表达式,要求不同对象(运算数、运算符号)之间以空格分隔,但结尾不得有多余空格。

输入样例:

2+3*(7-4)+8/4

输出样例:

2 3 7 4 - * + 8 4 / +

刚开始尝试的时候一直显示“格式错误”,网上检索阅读了蒋赟jiangyun的一篇博客后才知道自己的问题在哪里:没有考虑连续输入数字的情况,例如123,仅考虑了单个数字的输入。。。
原文链接:https://blog.csdn.net/weixin_42240329/article/details/84192447

C语言代码如下:

#include <stdio.h>
#include<stdlib.h>
struct S
{
 char *data;
 int top;
 int maxsize;
};
typedef struct S * Stack;
void push(Stack S, char c);
char pop(Stack S);
int main()
{
	Stack St;
 	St = (Stack)malloc(sizeof(struct S));
	St->data = (char *)malloc(21 * sizeof(char));
 	St->top = -1;
	St->maxsize = 21;
 	char a[40];
	char c;
 	int i = 0;
 	while ((a[i++] = getchar()) != '\n'); //读入数据
 	a[i] = '\0';
 	i = 0;
 	int flag = 1; //为了保证数字连续输入时,可以连续输出,例如输入123,输出123,而不是1 2 3
 	while (a[i] != '\0')
 	{
 		if (a[i] >= '0'&&a[i] <= '9')
  		{
   			if (flag == 1) { printf("%c", a[i]); }
   			else { printf(" %c", a[i]); flag = 1; }
  		}
  		else if (a[i] == '.') //考虑运算数为非整数的情况
  		{
   			printf("%c", a[i]);
   			i++;
   			while (a[i] >= '0'&&a[i] <= '9')
   			{
    				printf("%c", a[i]);
   				i++;
   			}
   			i--;
  		}
  else if (a[i] == '+')
  {
   if (i == 0); //第一个输入字符为+
   else {
   	if (St->top != -1 && St->data[St->top] != '(')
    	{
    	while (St->top != -1 && St->data[St->top] != '(')
     {
      c = pop(St);
      printf(" %c", c);
     }
    }
    push(St, a[i]);
    flag = 0;
   }
  }
  else if (a[i] == '-')
  {
   if (i == 0) { printf("%c", a[i]); } //第一个输入字符为-
   else {
    if (St->top != -1 && St->data[St->top] != '(')
    {
     while (St->top != -1 && St->data[St->top] != '(')
     {
      c = pop(St);
      printf(" %c", c);
     }
    }
    push(St, a[i]);
    flag = 0;
   }
  }
  else if (a[i] == '*')
  {
   if (St->top != -1 && (St->data[St->top] == '*' || St->data[St->top] == '/'))
   {
    while (St->top != -1 && (St->data[St->top] == '*' || St->data[St->top] == '/'))
    {
     c = pop(St);
     printf(" %c", c);
    }
   }
   push(St, a[i]);
   flag = 0;
  }
  else if (a[i] == '/')
  {
   if (St->top != -1 && (St->data[St->top] == '*' || St->data[St->top] == '/'))
   {
    while (St->top != -1 && (St->data[St->top] == '*' || St->data[St->top] == '/'))
    {
     c = pop(St);
     printf(" %c", c);
    }
   }
   push(St, a[i]);
   flag = 0;
  }
  else if (a[i] == '(')
  {
   push(St, a[i]);
   if (a[i + 1] == '+') { i++; } //像(+5)的情况
   else if (a[i + 1] == '-') { printf(" %c", a[i + 1]); flag = 1; i++; } //像(-5)的情况
  }
  else if (a[i] == ')')
  {
   while (St->data[St->top] != '(')
   {
    c = pop(St);
    printf(" %c", c);
   }
   c = pop(St);
  }
  i++;
 }
 if (St->top != -1) //全部遍历后判断堆栈是否为空,不为空弹出栈内所有运算符
 {
  while (St->top != -1)
  {
   c = pop(St);
   printf(" %c", c);
  }
 }
 return 0;
}
void push(Stack S, char c)//压栈
{
 S->top++;
 S->data[S->top] = c;
}
char pop(Stack S)//出栈
{
 return S->data[S->top--];
}

最后归纳一下“中缀表达式转换为后缀表达式”的基本过程:
从左到右读取中缀表达式的每个对象,对不同对象按不同情况处理:
1.遇到运算数,直接输出;
2.遇到左括号,将其压入堆栈;
3.遇到右括号,表明括号内的中缀表达式已经扫描完毕,将栈顶的运算符弹出并输出,直到遇到左括号(左括号也出栈,但不输出);
4.遇到运算符
若该运算符的优先级大于栈顶运算符的优先级时,则该运算符压栈;
若该运算符的优先级小于等于栈顶运算符时,将栈顶运算符弹出并输出,再比较新的栈顶运算符,按同样的处理方法,直到该运算符大于栈顶运算符优先级为止,然后将该运算符压栈;
5.若中缀表达式中的各对象处理完毕,则把堆栈中存留的运算符一并输出。

发布了21 篇原创文章 · 获赞 2 · 访问量 1638

猜你喜欢

转载自blog.csdn.net/wulila/article/details/105159357
今日推荐