题目:
输入一串数值算术表达式(
算法:
共分为5个文件,4个头文件,一个主函数.
“jiedian.h”
using
const
const
//定义节点类
union
{
double
char
};
class
{
private:
zifu_shuzi
public:
jiedian
int
jiedian();
void
void
void
void
};
jiedian::jiedian()
{
j.shuzi=NULL;
flag=-1;
}
void
{
j.shuzi=j.zifu=NULL;
}
void
{
flag=1;
j.zifu=s;
}
void
{
flag=0;
j.shuzi=s;
}
void
{
if(flag==1)
cout<<j.zifu<<"
else
cout<<j.shuzi<<"
}
“tree.h”
#include"zhuanhuan.h"
class
{
public:
void
void
tree();
private:
jiedian
int
void
};
tree::tree()
{
num=0;
for(int
jd[i].flag=-1,jd[i].left=NULL,jd[i].right=NULL;
}
void
{
s=fun(s);
my_zhan
for(int
{
if(s[i]=='
continue;
else
{
double
while(s[i]>='0'&&s[i]<='9')
{
temp*=10;
temp+=(s[i]-'0');
i++;
}
if(s[i]=='.')
{
int
i++;
while
{
temp+=(double)s[i]/(lag*10);
lag*=10;
i++;
}
}
jd[num].create(temp);
Z.in((double)num);
num++;
}
else
{
jd[num].create(s[i]);
jd[num].right=&jd[(int)Z.out_shuzi().shuzi];
jd[num].left=&jd[(int)Z.out_shuzi().shuzi];
Z.in(double(num));
num++;
}
}
num--;//存储根节点
}
void
{
DFS(&jd[num]);
}
void
{
if(!m)
if(m->left!=NULL
{
m->show();
DFS(m->left);
DFS(m->right);
}
else
{
m->show();
DFS(m->right);
}
}
“zhan.h”
#include"jiedian.h"
//定义栈及栈操作
class
{
private:
jiedian
zifu_shuzi
int
int
public:
my_zhan();
void
void
void
jiedian
zifu_shuzi
zifu_shuzi
jiedian
int
};
my_zhan::my_zhan()
{
flag=0;
num=0;
memset(shuzi,NULL,sizeof(shuzi));
}
void
{
flag=1;
shuzi[num++].shuzi=p;
}
void
{
flag=1;
shuzi[num++].zifu=p;
}
void
{
flag=2;
j[num++]=p;
}
jiedian
{
jiedian
if(flag==2)
{
if(num!=0)
{
*p=j[--num];
j[num].dele();
return
}
else
return
}
else
return
}
zifu_shuzi
{
zifu_shuzi
p->zifu='#';
if(flag==1)
{
if(num!=0)
{
*p=shuzi[--num];
shuzi[num].zifu=NULL;
shuzi[num].shuzi=NULL;
return
}
else
return
}
else
return
}
zifu_shuzi
{
return
}
jiedian
{
return
}
int
{
return
}
“zhuanhuan.h”
#include"zhan.h"
//中缀转换成后缀,主函数:fun()
//增加求值功能
struct
{
char
char
};
pp
void
bool
int
double
void
{
pp
d.s1='#',d.s2=NULL;
a.s1='*',a.s2='/';
b.s1='+',b.s2='-';
c.s1='(',c.s2=NULL;
s[0]=d;
s[1]=b;
s[2]=a;
s[3]=c;
}
char*
{
create();
my_zhan
char
memset(ans,NULL,sizeof(ans));
int
bool
for(int
{
if((s[i]>='0'
{
if(flag==1)
ans[len++]=s[i];
}
else
continue;
else
{
Z.in(s[i]);
flag=1;
}
else
{
while(Z.pop_shuzi().zifu!='(')
{
ans[len++]='
ans[len++]=Z.out_shuzi().zifu;
}
ans[len++]='
Z.out_shuzi().zifu;
}
else
{
while(panduan(s[i],Z.pop_shuzi().zifu)==0)
{
ans[len++]='
ans[len++]=Z.out_shuzi().zifu;
}
Z.in(s[i]);
flag=1;
}
}
while(Z.get_num()!=0)
{
ans[len++]='
ans[len++]=Z.out_shuzi().zifu;
}
return
}
bool
{
if(help_panduan(s)>help_panduan(p))
return
else
return
}
int
{
for(int
{
if(s[i].s1==p
return
}
return
}
double
{
s=fun(s);
my_zhan
for(int
{
if(s[i]=='
continue;
else
{
double
while(s[i]>='0'&&s[i]<='9')
{
temp*=10;
temp+=(s[i]-'0');
i++;
}
if(s[i]=='.')
{
int
while
{
temp+=(double)s[i]/(lag*10);
lag*=10;
i++;
}
}
Z.in(temp);
}
else
{
char
double
double
switch
{
case
case
case
case
default:
break;
}
}
}
int
return
}
“main.cpp”
#include"tree.h"
int
{
while(1)
{
char
gets(s);
cout<<"表达式的值为:";
cout<<calc(s)<<endl;
cout<<"后缀形式:";
puts(fun(s));
tree
b.create(s);
cout<<"前缀形式:";
b.show();
cout<<endl;
}
}