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

📄 逆波兰.txt

📁 逆波兰式的转换
💻 TXT
字号:
#include <stdio.h>
#include <stdlib.h>
#define MAX 30
int newtop;
char exp[MAX];
void operand()
{ float opnd[MAX],d;
  char chi;
  int t=1,newnewtop=0;
  chi=exp[t];
  t++;
  while(chi!='#')
  { switch(chi)
    { case'+':
	opnd[newtop-1]=opnd[newtop-1]+opnd[newtop];
	newtop--;
	break;
      case'-':
	opnd[newtop-1]=opnd[newtop-1]-opnd[newtop];
	newtop--;
	break;
      case'*':
	opnd[newtop-1]=opnd[newtop-1]*opnd[newtop];
	newtop--;
	break;
      case'/':
	if(opnd[newtop]!=0)
	opnd[newtop-1]=opnd[newtop-1]/opnd[newtop];
	else
	{ printf("Error!\n");
	  
	}
	newtop--;
	break;
	default:
	  d=0;
	  while(chi>='0'&&chi<='9')
	  { d=10*d+chi-'0';
	    chi=exp[t];
	    t++;
	  }
	  newtop++;
	  while(chi>='a'&&chi<='z')
	  {chi=exp[t];
	   t++;
	  }
	  newtop++;
	  opnd[newtop]=d;
    }
    chi=exp[t];
    t++;
  }
  

}
void express()
{ char opmt[MAX];
  char opst[MAX];
  char chi;
  int i,k,t,j,newtop=0;
  
  printf("请输入一个对应上述LL(1)输入串的算术表达式(以#号结束):");
  i=0;
  do
  { i++;
    scanf("%chi",&opmt[i]);
	
  }
  while(opmt[i]!='#'&&i!=MAX);
  printf("\n\n");
  k=i;
  t=1;
  i=1;
  chi=opmt[i];
  i++;
  while(chi!='#')
  { switch(chi)
    { case'(':
	   newtop++;
	   opst[newtop]=chi;
	    break;
      case')':
	while (opst[newtop]!='(')
	{ exp[t]=opst[newtop];
	  newtop--;
	  t++;
	}
	newtop--;
	break;
      case'+':
      case'-':
	while(newtop!=0&&opst[newtop]!='(')
	{ exp[t]=opst[newtop];
	  newtop--;
	  t++;
	}
	newtop++;
	opst[newtop]=chi;
	break;
      case'*':
      case'/':
	while(opst[newtop]=='*'||opst[newtop]=='/')
	{ exp[t]=opst[newtop];
	  newtop--;
	  t++;
	}
	newtop++;
	opst[newtop]=chi;
	break;
      
      default:
	while(chi>='0'&&chi<='9'||chi>='a'&&chi<='z')
	{ exp[t]=chi;
	  t++;
	  chi=opmt[i];
	  i++;
	}
	i--;
	
	t++;
    }
    chi=opmt[i];
    i++;
  }



  while(newtop!=0)
  { exp[t]=opst[newtop];
    t++;
    newtop--;
  }
  
  printf("当前的算术表达式为:");
  for(j=1;j<k;j++) printf("%chi",opmt[j]);
  printf("\n\n");
  printf("\n此算术表达式的逆波兰式中间代码:");
  for(j=1;j<t;j++)printf("%chi",exp[j]);
  printf("\n\n\n");
  system("pause");
}



void main()
{ express();
 operand();
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -