求大神具体解释!!

qq_40998500 2017-11-09 09:50:57
#include<stdio.h>
#include<string.h>
int F(char symbol)
{
switch(symbol)
{
case'+':
case'-': return 2;

case'*':
case'/':return 4;

case'^':
case'$':return 5;

case'(':return 0;

case'#':return -1;
default:return 8;


}
}
int G(char symbol)
{
switch(symbol)
{
case'+':
case'-': return 1;

case'*':
case'/':return 3;

case'^':
case'$':return 6;

case'(':return 9;

case')':return 0;
default:return 7;

}

}


void infix_postfix(char infix[],char postfix[])
{
int top;
int j;
int i;
char s[30];
char symbol;

top=-1;
s[++top]='#';
j=0;

for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(F(s[top])>G(symbol))
{
postfix[j]=s[top--];
j++;
}
if(F(s[top])!= G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';
}
int main()
{
char infix[20];
char postfix[20];

printf("Enter a vaild infix expression\n");
scanf("%s",infix);

infix_postfix(infix,postfix);

printf("the postfix expression is\n");
printf("%s\n",postfix);
}
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_40998500 2017-11-15
  • 打赏
  • 举报
回复
for(i=0;i<strlen(infix);i++) { symbol=infix[i]; while(F(s[top])>G(symbol)) { postfix[j]=s[top--]; j++; } if(F(s[top])!= G(symbol)) s[++top]=symbol; else top--; } while(s[top]!='#') { postfix[j++]=s[top--]; } postfix[j]='\0'; 这部分不是很理解
qq_40998500 2017-11-15
  • 打赏
  • 举报
回复
能具体把该程序的步骤讲一下吗
ooolinux 2017-11-15
  • 打赏
  • 举报
回复
表达式计算的代码网上(或书里)很多,可能写法不同,原理都是差不多的,你可以先找一个写的不错有注释的,看懂了以后回头看这个,也就差不多了。
ooolinux 2017-11-09
  • 打赏
  • 举报
回复
这个代码涉及到中缀和后缀表达式,需要数据结构二叉树的知识,如果你没学过数据结构,可以先放过这个程序,以后再来理解。
ooolinux 2017-11-09
  • 打赏
  • 举报
回复
比如后缀表达式2 5 +,先后入栈 2 5,碰到一个+号,把2 5 弹出栈计算,再入栈。
qq_40998500 2017-11-09
  • 打赏
  • 举报
回复
能具体解释一下这个程序中栈的功能吗

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧