| 
 #include<iostream.h> 
const int max=50; 
template<class T> 
class Stack 
{ 
public: 
void clear(); 
bool push(const T item); 
T & pop(); 
T & tops(); 
bool isEmpty(); 
bool isFull(); 
}; 
template<class T> 
class arrStack:public Stack<T> 
{ 
private: 
int mSize; 
int top; 
T *st; 
public: 
arrStack(int size) 
{ 
mSize=size; 
top=-1; 
st=new T[mSize]; 
} 
arrStack() 
{ 
top=-1; 
} 
~arrStack() 
{ 
delete[] st; 
} 
void clear() 
{ 
top=-1; 
} 
bool push(const T item) 
{ 
if(top==mSize-1) 
{ 
cout<<"栈满溢出"<<endl; 
return false; 
} 
else 
{ 
st[++top]=item; 
return true; 
} 
} 
T & pop() 
{ 
if(top==-1) 
{ 
cout<<"栈为空"<<endl; 
return st[top--]; 
} 
else 
{ 
T item=st[top--]; 
return item; 
} 
} 
T & tops()  
{ 
if(top==-1) 
{ 
cout<<"栈为空"<<endl; 
return st[top]; 
} 
else 
{ 
return st[top]; 
} 
} 
}; 
void main() 
{ 
char c,theta,l; 
int a,b; 
int p,q; 
arrStack<char> optr(max); 
optr.push('#'); 
arrStack<int> opnd(max); 
char x[7][7]; 
x[0][0]='>'; x[0][1]='>'; x[0][2]='<'; x[0][3]='<'; x[0][4]='<'; x[0][5]='>'; x[0][6]='>'; 
x[1][0]='>'; x[1][1]='>'; x[1][2]='<'; x[1][3]='<'; x[1][4]='<'; x[1][5]='>'; x[1][6]='>'; 
x[2][0]='>'; x[2][1]='>'; x[2][2]='>'; x[2][3]='>'; x[2][4]='<'; x[2][5]='>'; x[2][6]='>'; 
x[3][0]='>'; x[3][1]='>'; x[3][2]='>'; x[3][3]='>'; x[3][4]='<'; x[3][5]='>'; x[3][6]='>'; 
x[4][0]='<'; x[4][1]='<'; x[4][2]='<'; x[4][3]='<'; x[4][4]='<'; x[4][5]='='; x[4][6]=NULL; 
x[5][0]='>'; x[5][1]='>'; x[5][2]='>'; x[5][3]='>'; x[5][4]=NULL; x[5][5]='<'; x[5][6]='<'; 
x[6][0]='<'; x[6][1]='<'; x[6][2]='<'; x[6][3]='<'; x[6][4]='<'; x[6][5]=NULL; x[6][6]='='; 
cin>>c; 
while(c!='#'||optr.tops()!='#') 
{ 
if(c!='+'&&c!='-'&&c!='*'&&c!='/'&&c!='('&&c!=')'&&c!='#') 
{ 
opnd.push(c); 
cin>>c; 
} 
if(optr.tops()=='+') p=0;  
if(optr.tops()=='-') p=1; 
if(optr.tops()=='*') p=2; 
if(optr.tops()=='/') p=3; 
if(optr.tops()=='(') p=4; 
if(optr.tops()==')') p=5; 
if(optr.tops()=='#') p=6; 
        if(c=='+') q=0;  
if(c=='-') q=1; 
if(c=='*') q=2; 
if(c=='/') q=3; 
if(c=='(') q=4; 
if(c==')') q=5; 
if(c=='#') q=6; 
if(c=='#'||c=='+'||c=='-'||c=='/'||c=='*') 
{ 
switch(x[p][q]) 
{ 
case'<': 
optr.push(c); 
cin>>c; 
break; 
case'=': 
l=optr.pop(); 
cin>>c; 
break; 
case'>': 
theta=optr.pop(); 
b=opnd.pop(); 
a=opnd.pop(); 
if(theta=='+') 
opnd.push((a+b)); 
if(theta=='-') 
opnd.push((a-b)); 
if(theta=='*') 
opnd.push((a*b)); 
if(theta=='/') 
opnd.push((a/b)); 
break; 
} 
} 
} 
cout<<opnd.tops(); 
} 
 
高手帮帮忙,谢谢了  |