📄 二叉树.cpp
字号:
q[rear]=root;
}
while(front!=rear)
{
front=(front+1)%100;
p=q[front];
ofile<<setw(15)<<p->num<<setw(15)<<p->name<<setw(15)<<p->score<<setw(15)<<p->ch<<endl;
if(p->left!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->left;
}
if(p->right!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->right;
}
}
cout<<"保存成功"<<endl;
}
void back(struct tree *&root) //从文件恢复
{
ifstream ifile;
int i,j;
tree *p;
ifile.open("tree.txt");
if(!ifile)
{
cout<<"打开文件失败!"<<endl;
exit(-1);
}
ifile>>i;
root=NULL;
for(j=0;j<i;j++)
{
p=new tree;
ifile>>p->num>>p->name>>p->score>>p->ch;
root=insert(root,p);
}
ifile.close();
cout<<"恢复成功"<<endl;
cout<<setw(10)<<"学号"<<setw(15)<<"姓名"<<setw(15)<<"成绩"<<setw(10)<<"关键码"<<endl;
inorder(root);
}
void scoreall(struct tree *root) //关于成绩
{
int i;
float aver;
cout<<"输入您的选择:"<<endl;
cout<<"0.退出 1.成绩平均值 2.最高分 3.最低分 4.方差"<<endl;
cin>>i;
while (i!=0)
{
switch(i) {
case 0:
break;
case 1:
cout<<"所有成绩的平均值为:"<<Aver(root)<<endl;
break;
case 2:
cout<<"最高分为:"<<Max(root)<<endl;
break;
case 3:
cout<<"最低分为:"<<Min(root)<<endl;
break;
case 4:
aver=Aver(root);
cout<<"方差为:"<<fangcha(root,aver)<<endl;
break;
default:
break;
}
cout<<"输入您的选择:"<<endl;
cout<<"0.退出 1.成绩平均值 2.最高分 3.最低分 4.方差"<<endl;
cin>>i;
}
}
float Aver(struct tree *root) //平均值
{
float aver;
int i,j;
if(root->left==NULL&&root->right==NULL) aver=root->score;
else if(root->left==NULL&&root->right!=NULL)
{
i=ct(root->right);
aver=(Aver(root->right)*i+root->score)/(i+1);
}
else if(root->left!=NULL&&root->right==NULL)
{
i=ct(root->left);
aver=(Aver(root->left)*i+root->score)/(i+1);
}
else
{
i=ct(root->right);
j=ct(root->left);
aver=(Aver(root->right)*i+root->score+Aver(root->left)*j)/(i+j+1);
}
return aver;
}
int Max(struct tree *root) //最大值
{
int max;
if(root->right==NULL) max=root->score;
else max=Max(root->right);
return max;
}
int Min(struct tree *root) //最小值
{
int min;
if(root->left==NULL) min=root->score;
else min=Min(root->left);
return min;
}
float fangcha(struct tree *root,float aver) //方差计算
{
float fc;
int i,j;
if(root->left==NULL&&root->right==NULL)
{
fc=(root->score-aver)*(root->score-aver);
}
else if(root->left==NULL&&root->right!=NULL)
{
i=ct(root->right);
fc=(fangcha(root->right,aver)*i+(root->score-aver)*(root->score-aver))/(i+1);
}
else if(root->left!=NULL&&root->right==NULL)
{
i=ct(root->left);
fc=(fangcha(root->left,aver)*i+(root->score-aver)*(root->score-aver))/(i+1);
}
else
{
i=ct(root->right);
j=ct(root->left);
fc=(fangcha(root->right,aver)*i+fangcha(root->left,aver)*j+(root->score-aver)*(root->score-aver))/(i+j+1);
}
return fc;
}
void inorder1(struct tree *root) //层次遍历
{
struct tree *q[100];
int front=0,rear=0;
struct tree *p;
if(root!=NULL)
{
rear=(rear+1)%100;
q[rear]=root;
}
while (front!=rear)
{
front =(front+1)%100;
p=q[front];
cout<<setw(10)<<p->num<<setw(15)<<p->name<<setw(15)<<p->score<<setw(10)<<p->ch<<endl;
if(p->left!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->left;
}
if(p->right!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->right;
}
}
}
void search (struct tree *root) //查找
{
int i;
int num,score;
char name[20], ch[4];
cout<<"输入您的选择:"<<endl;
cout<<"0.退出 1.学号查找 2.姓名查找 3.分数查找 4.分数段查找"<<endl;
cin>>i;
while (i!=0)
{
switch(i)
{
case 0 :
break;
case 1:
cout<<"输入学号"<<endl;
cin>>num;
searchnum(root,num);
break;
case 2:
cout<<"输入姓名"<<endl;
cin>>name;
searchname(root,name);
break;
case 3:
cout<<"输入分数"<<endl;
cin>>score;
searchscore(root,score);
break;
case 4:
cout<<"输入关键码"<<endl;
cin>>ch;
searchch(root,ch);
break;
default:
break;
}
cout<<"输入您的选择:"<<endl;
cout<<"0.退出 1.学号查找 2.姓名查找 3.分数查找 4.分数段查找"<<endl;
cin>>i;
}
}
void searchnum(struct tree *root,int num) //学号查找
{
struct tree *q[100];
int front=0,rear=0;
struct tree *p;
if(root!=NULL)
{
rear=(rear+1)%100;
q[rear]=root;
}
while (front!=rear)
{
front =(front+1)%100;
p=q[front];
if(num==p->num)
{
cout<<setw(10)<<"学号"<<setw(15)<<"姓名"<<setw(15)<<"成绩"<<setw(10)<<"关键码"<<endl;
cout<<setw(10)<<p->num<<setw(15)<<p->name<<setw(15)<<p->score<<setw(10)<<p->ch<<endl;
}
if(p->left!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->left;
}
if(p->right!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->right;
}
}
}
void searchscore(struct tree *root,int score) //分数查找
{
struct tree *q[100];
int front=0,rear=0,i=0;
struct tree *p;
if(root!=NULL)
{
rear=(rear+1)%100;
q[rear]=root;
}
cout<<setw(10)<<"学号"<<setw(15)<<"姓名"<<setw(15)<<"成绩"<<setw(10)<<"关键码"<<endl;
while (front!=rear)
{
front =(front+1)%100;
p=q[front];
if(score==p->score)
{
i++;
cout<<setw(10)<<p->num<<setw(15)<<p->name<<setw(15)<<p->score<<setw(10)<<p->ch<<endl;
}
if(p->left!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->left;
}
if(p->right!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->right;
}
}
cout<<"此分数共有"<<i<<"人"<<endl;
}
void searchname(struct tree *root,char *name) //姓名查找
{
struct tree *q[100];
int front=0,rear=0;
struct tree *p;
if(root!=NULL)
{
rear=(rear+1)%100;
q[rear]=root;
}
cout<<setw(10)<<"学号"<<setw(15)<<"姓名"<<setw(15)<<"成绩"<<setw(10)<<"关键码"<<endl;
while (front!=rear)
{
front =(front+1)%100;
p=q[front];
if(strcmp(name,p->name)==0)
{
cout<<setw(10)<<p->num<<setw(15)<<p->name<<setw(15)<<p->score<<setw(10)<<p->ch<<endl;
}
if(p->left!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->left;
}
if(p->right!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->right;
}
}
}
void searchch(struct tree *root,char *ch) //分数段查找
{
struct tree *q[100];
int front=0,rear=0,i=0;
struct tree *p;
if(root!=NULL)
{
rear=(rear+1)%100;
q[rear]=root;
}
cout<<setw(10)<<"学号"<<setw(15)<<"姓名"<<setw(15)<<"成绩"<<setw(10)<<"关键码"<<endl;
while (front!=rear)
{
front =(front+1)%100;
p=q[front];
if(strcmp(ch,p->ch)==0)
{
i++;
cout<<setw(10)<<p->num<<setw(15)<<p->name<<setw(15)<<p->score<<setw(10)<<p->ch<<endl;
}
if(p->left!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->left;
}
if(p->right!=NULL)
{
rear=(rear+1)%100;
q[rear]=p->right;
}
}
cout<<"此成绩段共有"<<i<<"人"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -