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

📄 as.cpp

📁 逆波兰式计算数值 可以进行加减乘除以及括号运算 输入数字或未知数x都可以 程序有带改进
💻 CPP
字号:
#include<iostream.h>
#include "ctype.h"
#include<string.h>
#include<stdlib.h>
char inputstream[50];   
int temp=0; 
int right;     
double E(char u);
double T(char u);
double F(char u);
double E(char u)   
{  double e=0,w=0;int q=0;double r=0;
      w=T(u);
   while (inputstream[temp]=='+'||inputstream[temp]=='-')
   {  
        q=temp;
	   temp++;
      e=T(u);
	 if (inputstream[q]=='+')
	 {cout<<"+";r+=e;}
	 else if (inputstream[q]=='-')
	 { cout<<"-";r=r-e;}
   }
 w=w+r;
return w;
}
double T(char u)   
{  double e=0,s=1,r=1;int q=0;
     s=F(u);
    while (inputstream[temp]=='*'||inputstream[temp]=='/')
   {  
         q=temp;
		temp++;
     e=F(u);
     
	 if (inputstream[q]=='*')
	 {cout<<"*";r=r*e;}
	 else if (inputstream[q]=='/')
	 {cout<<"/";r=double(r/e);}
    }
	s=double(s*r);
 return s;
   
}
double F(char u)
{  right=1;
     double e=0; 
   if (inputstream[temp]=='(')
   {
      temp++;
      e=E(u);
      if (inputstream[temp]!=')') right=0;
     else {temp++;right=1;}
   } 
else 
   {
    if(isdigit(inputstream[temp]))
    while (isdigit(inputstream[temp]))
	{   
		cout<<inputstream[temp];
        e=e*10+inputstream[temp]-48;
	        temp++;
		    right=1;
	} 
	if(isalpha(inputstream[temp]))
     if(inputstream[temp]='x')
	 {    
		 cout<<inputstream[temp];
		 
         e=u-48;
          temp++; 
          right=1;
	  }
 else   right=0;
}
   return e;
}
void main()
{
	double es=0;
	
    cout<<"请输入算术表达式:";
	cin>>inputstream; 
         char p;
          cout<<"请输入x的值:";
	      cin>>p;
		  
	     es=E(p);
	cout<<"波兰算术表达式:"<<endl;
  
   if (right) {cout<<"翻译成功!"<<endl;cout<<es<<endl;}
	else 
	cout<<"表达式有语法错误!"<<endl;


}

⌨️ 快捷键说明

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