⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nbl.cpp

📁 中缀表达式到逆波兰式转换的程序代码,学习数据结构的可以
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -