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

📄 混合运算.cpp

📁 混合四则运算
💻 CPP
字号:
#include<iostream>
using namespace std;
#define MAX 20
//================================
template <class T>
class Seqstack{
T elem[MAX];
 int top ;
 public:
	 Seqstack();
     void  InitStack( );
int push(T );
int pop(T&);         
int Isempty();
int Isfull();
T gettop();
 void print() ;
 };//-----------------------------
template <class T>
Seqstack<T>::Seqstack()
{top=-1; }
template <class T>
 void  Seqstack<T>::InitStack( )
 { 
 top=-1;
}
template <class T>
 int Seqstack<T>::push(T x)
{ if(top==MAX-1)return(0);
top++; 
elem[top]=x;
return(1);
}//-------------------------------
template <class T>
int Seqstack<T>::pop(T & q)
{ 
if(top==-1)
return(0);
else{
q= elem[top];
top--;
return(1);
}}//-------------------------------
template <class T>
int Seqstack<T>::Isfull( )
{return(top==MAX-1 ? 1:0);
}//--------------------------------
template <class T>
int Seqstack<T>::Isempty( )
{return (top==-1 ?  1:0);
}
template <class T>
T Seqstack<T>::gettop()
{if(top!=-1)
return elem[top];}
template <class T>
void Seqstack<T>::print(){
    for (int j=0;j<top+1;++j)
cout<<elem[j]<<endl;
 }; //--------------------------------
// 以上是一个栈类
//================================== 
  struct  Linkqueuenode{
	char data;
  Linkqueuenode *next;
  } ;
class Linkqueue{
Linkqueuenode *front;
Linkqueuenode *rear;
public:
Linkqueue();
int enterqueue( char x);
int deletequeue( char &x);
char getttop( ){
	return front->next->data; 
 };
char getnexttop(){
return front->next->next->data;}
~Linkqueue();
};
//------------------------------------
Linkqueue::Linkqueue(){
front=new Linkqueuenode;
if(front!=NULL)
{rear=front;
front->next=NULL;  
}}
//-------------------------------------
int Linkqueue::enterqueue(char x)
{Linkqueuenode * newnode;
newnode= new  Linkqueuenode ;
if(newnode!=NULL)
{
	newnode->data=x; 
newnode->next=NULL;
rear->next=newnode;
rear=newnode;
return 1;}
else return 0;
}//-----------------------------------
int Linkqueue::deletequeue(char &x)
{
	Linkqueuenode*  p;
if(front==rear)
return(0);
p=front->next;
front->next=p->next;
if(rear==p)
rear=front;
 x=p->data; 
delete p;
p=NULL;
return(1);
}//--------------------------------------
Linkqueue::~Linkqueue()
{Linkqueuenode *d;
     while(front->next!=NULL)
     {
         d=front;
         front=front->next;
         delete d;
         d=NULL;
}}//以上是队列的类
//========================================
class Formoperate{
	Seqstack<double> ovs;
	Seqstack<char> optr;
Linkqueue queue;
public:
 
	double donin(double temp);             //小数的处理
double getnumber(char  &ch);               //字符转换为数字
int in(char  ch);                          //判断是数字还是字符
int compare(char hh,char hhh);              //比较优先级
double execute(double a,char opp,double b);//出栈运算
double expevaluation();                    //运算过程
};
double Formoperate::getnumber(char  &ch)
{double a;
if(ch=='.')
return 10;
 else 
return  a=ch-48;
}//-------------------------------------------------
 
int Formoperate::in(char  ch){
	int i,j=0;
char  opset[]={'+','-','*','/','=','(',')','[',']','{','}' };
    for(i=0;i<11;i++)
    {if( ch==opset[i]) j++;}
        if(j==1)    return 1;
        else return 0;
}
int Formoperate::compare(char hh,char hhh){
	char optr[11]={'+','-','*','/','=','(',')','[',']','{','}'};
	int pro[11][11]={
		{0,0,1,1,0,1,0,1,0,1,0},
		{0,0,1,1,0,1,0,1,0,1,0},
		{0,0,0,0,0,1,0,1,0,1,0},
        {0,0,0,0,0,1,0,1,0,1,0}, 
		{1,1,1,1,3,1,3,1,3,1,3},
		{1,1,1,1,3,3,1,3,3,3,3},
		{2,2,2,2,2,3,3,3,2,3,2},
		{1,1,1,1,3,1,3,3,1,3,3},
		{2,2,2,2,2,3,3,3,3,3,2},
		{1,1,1,1,3,1,3,1,3,1,1},
		{2,2,2,2,2,3,3,3,3,3,2}
};
	int m,n;
for(int i=0;i<11;++i)
if(hh==optr[i])m=i;
for(int j=0;j<11;++j)
 if(hhh==optr[j]) n=j; 
 return pro[m][n];}
	 
double Formoperate::execute(double a,char opp,double b) {
	switch(opp)
{
	case '+': return a+b;break;
case'-':return a-b;break;
case'*':return a*b;break;
case'/':return a/b;break;
}}//-----------------------------------------------------------
	double Formoperate::donin(double temp)        //小数的处理
	{while(temp>=1)
{temp/=10;}
return temp; 
	}//--------------------------------------------------------
double Formoperate::expevaluation()
{
	int  tt=0;
	double v, a,n, b,temp1=0,temp2=0,nn=0,temp=0;
char ch,op,input;

cout<<"请输入一个表达式串(以=结尾)如1.2-6*2.6-{11+[2.9-0.9*(2.3/2)]}="<<endl;
optr.push('=');
while(input!='=')
{input=getchar();
queue.enterqueue(input);}
	
while(ch!='='|| optr.gettop()!='=') 
{ ch=queue.getttop();
if(ch=='-'&&nn==0)ovs.push(0);nn=1;
	
		if(!in( ch )||ch=='.')//不是运算符是运算数
{	queue.deletequeue(ch);
			n=getnumber( ch);
    
			if(n==10||tt==1)//tt是为了区分输入的是小数位还是整数位 n是区分小数点等等
    { if(n==10) 
	n=0;
	temp1=temp1*10+n;
	tt=1;
	}
else if(tt==0 )
temp2=temp2*10+n;//处理整数

	if( in(queue.getttop()))
	{
		temp1=donin(temp1);
	temp=temp1+temp2;//最后将小数部分和整数部分相加
	ovs.push(temp); 
	temp=0;
	temp1=0;
	temp2=0;
	tt=0;}	}

else
switch(compare(optr.gettop(),ch))
{	case 0:
	optr.pop(op);
ovs.pop(b);
ovs.pop(a); 
v=execute(a,op,b);
ovs.push(v); 
break;
case 1:if(queue.getnexttop()=='-'&&
		   (queue.getttop()=='('||queue.getttop()=='['||queue.getttop()=='{')) 
		   
		   ovs.push(0);	
	queue.deletequeue(ch);
	
optr.push(ch);break;
	
case 2:
optr.pop(op);
 optr.pop(op);
break;
	}}
v=ovs.gettop(); 
return(v);
	}//运算过程--------------------------------------------
	int main()
	{ 
	 Formoperate a;
	double aa;

	aa=a.expevaluation();
	
	cout<<aa<<endl;
	system("pause");
	}
 

⌨️ 快捷键说明

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