各位帮我看看出了什么问题,程序一直运行不出来。明天就要交作业了,急死我了!
这是关于算术表达式的程序
#include<string>
#include<iostream>
using namespace std;
template <class Telem>
class SqStack
{
private:
Telem *elem;
int top;
int maxlen;
public:
SqStack(Telem a[],int n,int maxsz=100):maxlen(maxsz)
{
elem=new Telem[maxlen];
for(int i=n-1;i>=0;i--)elem[i]=a[i];
top=n-1;
};
~SqStack(){delete[] elem;};
void init(){top = -1;};
int leng(){ return top+1;};
bool push (Telem& el)
{ if (top==maxlen-1) return(false);
else { top=top+1;
elem[top]=el;
return(true);
};
};
Telem pop( )
{
if (top==-1) return NULL;
else return(elem[top--]);
};
Telem gettop( )
{
if (top==-1) return NULL;
else return(elem[top]);
};
bool full(){return top==maxlen-1;};
bool empt(){return top==-1;};
char rela(char symb1,char symb2)
{
char r[7][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
};
char ch[2];int in[2];
ch[0]=symb1;ch[1]=symb2;
for(int i=0;i<2;i++)
switch(ch[i])
{
case'+':in[i]=0;break;
case'-':in[i]=1;break;
case'*':in[i]=2;break;
case'/':in[i]=3;break;
case'(':in[i]=4;break;
case')':in[i]=5;break;
case'#':in[i]=6;
};
return r[in[0]][in[1]];
};
int oprt(int a,int b,char op)
{
int re=0;
switch(op)
{
case'+':re=a+b;break;
case'-':re=a-b;break;
case'*':re=a*b;break;
case'/':re=a/b;break;
};
return re;
};
bool comp(string st,int &v)
{
SqStack<int>sopnd;
SqStack<char>soprt;
char ch,symb,op; int a,b,i;
soprt.push('#');
symb=st[2]; i=3;
while (symb!='.')
{
if (( symb!='+')&&( symb!='-')&& (symb!='*')&& (symb!='/')&&( symb!='(')&&(symb!=')')&& (symb!='#'))
{
sopnd.push(int (symb));
symb=st[i]; i++;
}
else {
ch= soprt.gettop();
switch(rela(ch,symb))
{
case'<':
soprt.push(symb);
symb=st[i];i++;break;
case'=':
soprt.pop();
symb=st[i];i++;break;
case'>':
op=soprt.pop();
b=sopnd.pop(); a=sopnd.pop();
sopnd.push(oprt(a,b,op));
}
}
}
if (soprt.empt())
{
v=sopnd.gettop();
return true;
}
else return false;
} ;
};
void main()
{
int v;
string s;
cout<<"请输入算术表达式:";
cin>>s;
cout<<s;
comp(s,v);
cout<<"结果为"<<v;
}