#include<iostream.h>
//---------class numstack------
class numstack
{private:
int *data;
int top;
public:
numstack();
~numstack();
int numstackempty();
void numpush(int);
int numpop();
int numgettop();
int numtop();
};
//--------class flostack------
class flostack
{private:
float *data;
int top;
public:
flostack();
~flostack();
void flopush(float);
float flopop();
int flogettop();
float flotop();
};
//--------class numstack------
numstack::numstack()
{
data=new int[31];
top=-1;
}
numstack::~numstack()
{
delete data;
}
int numstack::numstackempty()
{
if(top==-1) return 1;
else return 0;
}
void numstack::numpush(int i)
{
if(top==30) top=-1;
top++;
data[top]=i;
}
int numstack::numpop()
{
top--;
return data[top+1];
}
int numstack::numgettop()
{ return top; }
int numstack::numtop()
{ return data[top];}
//------------class flostack-----
flostack::flostack()
{
data=new float[31];
top=-1;
}
flostack::~flostack()
{
delete data;
}
void flostack::flopush(float i)
{
if(top==30) top=-1;
top++;
data[top]=i;
}
float flostack::flopop()
{
top--;
return data[top+1];
}
int flostack::flogettop()
{ return top; }
float flostack::flotop()
{ return data[top];}
//----------------------------------------
void main()
{ flostack B;
numstack zhi;
int n,m,cou=0,v;
float i,a,b,an;
char c;
cout<<"请输入表达式,以“#”结束"<<endl;
zhi.numpush(0);
cin>>i>>c;
while(c!='#')
{ B.flopush(i);
switch(c)
{ case '+': n=2; break;
case '-': n=3; break;
case '*': n=4; break;
case '/': n=5; break;
}
m=zhi.numtop();
if(n>=m)
{ zhi.numpush(n); }
while(n<m)
{ b=B.flopop();
a=B.flopop();
v=zhi.numpop();
switch(v)
{ case 2: an=a+b; break;
case 3: an=a-b; break;
case 4: an=a*b; break;
case 5: an=a/b; break;
}
B.flopush(an);
m=zhi.numtop();
cou++;
}
if(cou!=0)
{ zhi.numpush(n); }
cin>>i>>c;
}
B.flopush(i);
while(zhi.numtop()!=0)
{ b=B.flopop();
a=B.flopop();
m=zhi.numpop();
switch(m)
{ case 2: an=a+b; break;
case 3: an=a-b; break;
case 4: an=a*b; break;
case 5: an=a/b; break;
}
B.flopush(an);
}
cout<<"结果:"<<B.flopop()<<endl;
}
这是我的程序