(利用堆栈的基本操作来实现中缀表达式的计算.doc
文本预览下载声明
#include cstdio
#include iostream
#include cstdlib
#include cstring
using namespace std;
typedef struct
{
double *data;
int top;
}nstack;
typedef struct
{
char *data;
int top;
}cstack;
nstack num;
cstack op;
char suffix[1010],s[1010];
double num1,num2,num3;
void init(nstack s)
{
s.data=new double [100];
s.top=-1;
}
void init(cstack s)
{
s.data=new char [100];
s.top=-1;
}
bool empty(nstack s)
{
if(s.top==-1)
return 1;
return 0;
}
bool empty(cstack s)
{
if(s.top==-1)
return 1;
return 0;
}
bool top(nstack s,int e)
{
if(s.top==-1)
return 0;
e=s.data[s.top];
return 1;
}
bool top(cstack s,char e)
{
if(s.top==-1)
return 0;
e=s.data[s.top];
return 1;
}
bool pop(cstack s,char e)
{
if(s.top==-1)
return 0;
e=s.data[s.top--];
return 1;
}
bool pop(nstack s,double e)
{
if(s.top==-1)
return 0;
e=s.data[s.top--];
return 1;
}
void push(cstack s,char e)
{
s.data[++s.top]=e;
}
void push(nstack s,double e)
{
s.data[++s.top]=e;
}
int opmember(char c)
{
if(c==+||c==-||c==*||c==/)
return 1;
if(c==()
return 2;
if(c==))
return 3;
return 0;
}
int getpre(char c)
{
if(c==#) return -1;
if(c==() return 0;
if(c==+||c==-) return 1;
if(c==)) return 2;
if(c==*||c==/) return 2;
}
bool compare(char s1,char s2)
{
if(getpre(s1)=getpre(s2))
return 1;
return 0;
}
bool isnum(char c)
{
if(c=0c=9) return 1;
return 0;
}
bool islogic(char *s)
{
int len=strlen(s);
int l=0,r=0;
if(opmember(s[0]==1||opmember(s[0])==3)) return 0;
if(opmember(s[len-1])==1||opmember(s[len-1])==2) return 0;
for(int i=0;ilen-1;i++)
{
if(s[i]==() l++;
if(s[
显示全部