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

📄 kkk.cpp

📁 编写完整程序
💻 CPP
字号:
// kkk.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream> 
#include <stack> 
using namespace std; 

int pri(char optr) 
{ 
        switch (optr) 
    { 
          case '='  : return 0;
          case '>=' : return 0;
		  case '<=' : return 0;
		  case '&'  : return 4;
		  case '||' : return 4;
		  case  '!' : return 4;
		  case '^'  : return 3;
          case '*'  : return 2; 
          case '/'  : return 2; 
          case '%'  : return 2; 
          case '+'  : return 1; 
          case '-'  : return 1; 
          default   : return 0; 
    } 
} 
int main()
{            
  stack <char>s; 
  s.push('#'); 
  char ch; 
  char top; 
  cout <<"请输入中缀表达式以'#'结束:"; 
  int k=0; 
  while(cin.get(ch) && ch != '#'){
	  if(isdigit(ch)||isalpha(ch))
          cout <<ch; 
      else if( isspace(ch) )
		  ; 
	  else{
		  if( ch == '('){
			  k=1;
			  s.push(ch); 
		  }
		  else if(ch ==')'){
			  if(k!=1){
				  cout<<"表达方式输入错误!";
			break ;
			  }
              top = s.top(); 
              s.pop();
			  while( top != '(' && !s.empty()){
				  cout <<top;
				  top = s.top();
				  s.pop();
			  }
		  }
		  else
		  {
			  top = s.top();
			  s.pop();
			  while(pri(top)>=pri(ch)&&top != '(')
			  {
				  cout <<top;
				  top=s.top();
				  s.pop();
			  }
			  s.push(top);
			  s.push(ch);
		  }
	  }
  }
  top = s.top(); 
  s.pop(); 
  while(!s.empty() && top != '#') 
  { 
          cout <<top; 
          top=s.top(); 
          s.pop(); 
          
  } 
  cout <<endl; 
  return 0; 
}

⌨️ 快捷键说明

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