nbl.cpp
来自「中缀表达式到逆波兰式转换的程序代码,学习数据结构的可以」· C++ 代码 · 共 116 行
CPP
116 行
#include <stdio.h>
#include<string.h>
#define max 64
int main(int argc, char* argv[])
{
char str[max],nbl[max],stack[max],ch;
int i,j,t,top;
printf("*****************************************\n");
printf("* Middle style expersion to NBL *\n");
printf("* !Press 1 exit *\n");
printf("*****************************************\n");
nbl_loop: i=j=t=top=0;
printf("Input a middle string:");
scanf("%s",str);
strcat(str,"#");
while(str[i]!='#')
{ ch=str[i];
if(ch=='1')
{return 0;}
else if(ch>='a'&& ch<='z')
{ nbl[t]=ch; t++; }
else if(ch=='(')
{ top++; stack[top]=ch; }
else if(ch==')')
{ while(stack[top]!='(')
{ nbl[t]=stack[top]; top--; t++; }
top--;
}
else if(ch=='+'||ch=='-')
{ while(top!=0 && stack[top]!='(')
{ nbl[t]=stack[top]; top--; t++; }
top++;
stack[top]=ch;
}
else if(ch=='*'||ch=='/')
{
while(stack[top]=='*'||stack[top]=='/')
{nbl[t]=stack[top];top--;t++; }
top++;
stack[top]=ch;
}
else if(ch=='^')
{ while(stack[top]=='^')
{nbl[t]=stack[top];top--;t++; }
top++;
stack[top]=ch;
}
else
{printf("Error:Invalid character:%c\nAccured at position:%d\n\n",ch,i);goto nbl_end;}
i++;
}
while(top!=0)
{ nbl[t]=stack[top]; t++; top--; }
printf("Output as NBL form :");
for(j=0;j<t;j++)
printf("%c",nbl[j]);
printf("\n");
nbl_end: goto nbl_loop;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?