当前位置: 代码迷 >> 综合 >> 数据结构实验之栈与队列二:一般算术表达式转换成后缀式oj
  详细解决方案

数据结构实验之栈与队列二:一般算术表达式转换成后缀式oj

热度:55   发布时间:2024-01-14 23:09:29.0

数据结构实验之栈与队列二:一般算术表达式转换成后缀式

Time Limit: 1000MS Memory Limit: 65536KB
Problem Description

对于一个基于二元运算符的算术表达式,转换为对应的后缀式,并输出之。

Input

输入一个算术表达式,以‘#’字符作为结束标志。

Output

输出该表达式转换所得到的后缀式。

Example Input
a*b+(c-d/e)*f#
Example Output
ab*cde/-f*+
#include <stdio.h>
#include <string.h>int main()
{int n,i,top = 0,m;char c[100000],str[100000];scanf("%s",c);n = strlen(c);for(i = 0;i < n;i++){if(c[i] == '#'){}else if(c[i]<='z'&&c[i]>='a'){printf("%c",c[i]);}else if(c[i] == '('){top++;str[top] = c[i];}else if(c[i] == ')'){for(;str[top] != '(';top--)printf("%c",str[top]);top--;}else if(c[i] == '*'||c[i] == '/'){while(1){if(str[top] =='+'||str[top]=='-'||str[top]=='('||top==0){top++;str[top] = c[i];break;}printf("%c",str[top]);top--;}}else if(c[i] == '+'||c[i]=='-'){while(1){if(str[top]=='('||top==0){top++;str[top] = c[i];break;}printf("%c",str[top]);top--;}}}for(i = top;top > 0;top--){printf("%c",str[top]);}printf("\n");return 0;
}