📄 mainfunctions.h
字号:
//操作函数
#pragma once
#include "StdAfx.h"
#include "TreeStruct.h"
#include "BiTree.h"
#include "DataOperate.h"
#include "conio.h"
bool FuncBuild(TreeNode *&T)
{
TStuData stu;
char cc[100];
int total=1;
system("cls");
cout<<"#### 1 手动建立学生数据"<<endl<<endl;
T = NULL;
while (1)
{
cout<<"输入第"<<total<<"位学生数据,学号处输入F终止输入"<<endl;
cout<<"学号:";
cin>>cc;
if (!strcmp(cc,"F")) break;
stu.StuNumber=cc;
cout<<"姓名:";
cin>>cc;
stu.StuName=cc;
cout<<"分数:";
cin>>stu.StuScore;
total++;
InsertNode(T,stu);
}
cout<<"输入完成,按任意键返回菜单...";
getch();
return true;
}
bool FuncInsert(TreeNode *&T)
{
system("cls");
cout<<"#### 2 插入一个学生 "<<endl<<endl;
char cc[100];
TStuData stu;
cout<<"输入要插入的学生数据,学号处输入F取消插入"<<endl;
cout<<"学号:";
cin>>cc;
if (!strcmp(cc,"F")) return false;
stu.StuNumber=cc;
cout<<"姓名:";
cin>>cc;
stu.StuName=cc;
cout<<"分数:";
cin>>stu.StuScore;
if (InsertNode(T,stu))
{
cout<<endl<<"插入完成,按任意键返回菜单...";
getch();
return true;
}
else
{
cout<<endl<<"插入不成功,按任意键返回菜单...";
getch();
return false;
}
return true;
}
bool FuncDeleteStu(TreeNode *&T)
{
string stunum;
char cc[100];
system("cls");
cout<<"#### 3 删除一个学生(按学号)"<<endl<<endl;
cout<<"输入要删除的学生学号,输入F取消删除"<<endl;
cout<<"学号:";
cin>>cc;
if (!strcmp(cc,"F")) return false;
stunum = cc;
FLAG_ShowStuInfo = true;
FLAG_ShowStuInfo = false;
if (DeleteNode(T,stunum))
{
cout<<endl<<"删除完成,按任意键返回菜单...";
getch();
return true;
}
else
{
cout<<endl<<"删除不成功,按任意键返回菜单...";
getch();
return false;
}
return true;
}
bool FuncDeleteNode(TreeNode *&T)
{
system("cls");
cout<<"#### 4 删除一个分数段结点"<<endl<<endl;
string st;
int seg;
char cc[100];
cout<<"输入分数段名,F取消:";
cin>>cc;
st=cc;
if (!strcmp(cc,"F")) return false;
seg = String2Segment(st);
while (!seg)
{
cout<<"输入错误,重新输入:";
cin>>cc;
if (!strcmp(cc,"F")) return false;
st=cc;
seg = String2Segment(st);
}
if (DeleteNode(T,seg))
{
cout<<endl<<"删除完成,按任意键返回菜单...";
getch();
return true;
}
else
{
cout<<endl<<"删除不成功,按任意键返回菜单...";
getch();
return false;
}
return true;
}
bool Stats(TreeNode *p)
{
if (p)
{
for (int i=0;i<p->data.amount;i++)
{
STAT_Total++;
STAT_Sum+=p->data.stu[i].StuScore;
STAT_SqrSum+=p->data.stu[i].StuScore*p->data.stu[i].StuScore;
if (p->data.stu[i].StuScore>STAT_Max) STAT_Max=p->data.stu[i].StuScore;
if (p->data.stu[i].StuScore<STAT_Min) STAT_Min=p->data.stu[i].StuScore;
}
}
return true;
}
bool FuncStatistic(TreeNode *&T, TreeNode *&Thrt)
{
system("cls");
cout<<"#### 5 统计学生总数/均值/最值/方差"<<endl<<endl;
STAT_Total=0;
STAT_Sum=0;
STAT_SqrSum=0;
STAT_Max=0;
STAT_Min=999;
InOrderTraverse(T,Thrt,Stats);
cout<<"学生总数:"<<STAT_Total<<endl;
cout<<"平均分:"<<((double)STAT_Sum)/STAT_Total<<endl;
cout<<"最高分:"<<STAT_Max<<endl;
cout<<"最低分:"<<STAT_Min<<endl;
cout<<"方差:"<<double(STAT_SqrSum)/STAT_Total-(double(STAT_Sum)/STAT_Total)*(double(STAT_Sum)/STAT_Total)<<endl;
cout<<"按任意键返回菜单...";
getch();
return true;
}
bool FuncInOrder(TreeNode *&T,TreeNode *&Thrt)
{
system("cls");
cout<<"#### 6 中序遍历"<<endl<<endl;
FLAG_ShowNode = true;
char cc=' ';
while ((cc!='Y')&&(cc!='N'))
{
cout<<"遍历时是否显示详细学生数据?[Y/N]"<<endl;
cc=getch();
if (cc=='y') cc='Y';
if (cc=='n') cc='N';
}
if (cc=='Y') FLAG_ShowStuInfo = true;
else FLAG_ShowStuInfo = false;
InOrderTraverse(T,Thrt,ShowNode);
cout<<endl<<"遍历完成,按任意键返回菜单...";
getch();
return true;
}
bool FuncLayer(TreeNode *&T)
{
system("cls");
cout<<"#### 7 层次遍历"<<endl<<endl;
LayerTraverse(T);
cout<<endl<<"遍历完成,按任意键返回菜单...";
getch();
return true;
}
bool Search_StuNumber(TreeNode *p)
{
for (int i=0;i<p->data.amount;i++)
if (!strcmp(p->data.stu[i].StuNumber.c_str(),SearchKeyString.c_str()))
{
STAT_Total++;
ShowStuInfo(p->data.stu[i]);
}
return true;
}
bool Search_StuName(TreeNode *p)
{
for (int i=0;i<p->data.amount;i++)
if (!strcmp(p->data.stu[i].StuName.c_str(),SearchKeyString.c_str()))
{
STAT_Total++;
ShowStuInfo(p->data.stu[i]);
}
return true;
}
bool Search_StuScore(TreeNode *p)
{
for (int i=0;i<p->data.amount;i++)
if (p->data.stu[i].StuScore==SearchKeyNum)
{
STAT_Total++;
ShowStuInfo(p->data.stu[i]);
}
return true;
}
bool FuncSearch(TreeNode *&T,TreeNode *&Thrt)
{
system("cls");
cout<<"#### 8 学生检索"<<endl<<endl;
while (1)
{
cout<<"选择关键字类型,Q返回主菜单:"<<endl;
cout<<" 1 学号"<<endl;
cout<<" 2 姓名"<<endl;
cout<<" 3 成绩"<<endl;
char ch=' ';
while (ch!='1'&&ch!='2'&&ch!='3'&&ch!='q')
{
ch=getch();
if (ch=='Q') ch='q';
}
STAT_Total = 0;
char cc[100];
FLAG_ShowStuInfo=true;
switch (ch)
{
case '1':
cout<<"输入学号:";
cin>>cc;
SearchKeyString=cc;
InOrderTraverse(T,Thrt,Search_StuNumber);
cout<<"总数:"<<STAT_Total<<endl;break;
case '2':
cout<<"输入学号:";
cin>>cc;
SearchKeyString=cc;
InOrderTraverse(T,Thrt,Search_StuName);
cout<<"总数:"<<STAT_Total<<endl;break;
case '3':
cout<<"输入分数:";
cin>>SearchKeyNum;
InOrderTraverse(T,Thrt,Search_StuScore);
cout<<"总数:"<<STAT_Total<<endl;break;
case 'q':cout<<endl<<"按任意键返回菜单...";
getch();return true;
}
cout<<"任意键继续..."<<endl;
getch();
}
return true;
}
bool Search_Range(TreeNode *p)
{
for (int i=0;i<p->data.amount;i++)
if ((p->data.stu[i].StuScore>=Range_Low)&&(p->data.stu[i].StuScore<=Range_High))
{
STAT_Total++;
ShowStuInfo(p->data.stu[i]);
}
return true;
}
bool FuncSegment(TreeNode *&T,TreeNode *&Thrt)
{
system("cls");
cout<<"#### 9 指定检索分数段"<<endl<<endl;
cout<<"输入分数下限";
cin>>Range_Low;
cout<<"输入分数上限";
cin>>Range_High;
STAT_Total=0;
FLAG_ShowStuInfo=true;
InOrderTraverse(T,Thrt,Search_Range);
cout<<"总数:"<<STAT_Total<<endl;
cout<<endl<<"按任意键返回菜单...";
getch();return true;
return true;
}
bool FuncSave(TreeNode *&T,TreeNode *&Thrt)
{
system("cls");
cout<<"#### S 保存全部数据到文件"<<endl<<endl;
char cc[100];
string st;
cout<<"输入文件名:";
cin>>cc;
st=cc;
if (SaveTree(T,Thrt,st))
{
cout<<endl<<"保存成功,按任意键返回菜单...";
getch();return true;
}
else
{
cout<<endl<<"保存失败,按任意键返回菜单...";
getch();return false;
}
return true;
}
bool FuncRecover(TreeNode *&T,TreeNode *&Thrt)
{
system("cls");
cout<<"#### R 从文件恢复全部数据"<<endl<<endl;
char cc[100];
string st;
cout<<"输入文件名:";
cin>>cc;
st=cc;
if (RebuildTree(T,Thrt,st))
{
cout<<endl<<"恢复成功,按任意键返回菜单...";
getch();return true;
}
else
{
cout<<endl<<"恢复失败,按任意键返回菜单...";
getch();return false;
}
return true;
}
bool menu(bool &wrong,TreeNode *&T,TreeNode *&Thrt)
{
system("cls");
cout<<"####################功能选择####################"<<endl;
cout<<"# #"<<endl;
cout<<"# 1 手动建立学生数据 #"<<endl;
cout<<"# 2 插入一个学生 #"<<endl;
cout<<"# 3 删除一个学生(按学号) #"<<endl;
cout<<"# 4 删除一个分数段结点 #"<<endl;
cout<<"# 5 统计学生总数/均值/最值/方差 #"<<endl;
cout<<"# 6 中序遍历 #"<<endl;
cout<<"# 7 层次遍历 #"<<endl;
cout<<"# 8 学生检索 #"<<endl;
cout<<"# 9 指定检索分数段 #"<<endl;
cout<<"# #"<<endl;
cout<<"# S 保存全部数据到文件 #"<<endl;
cout<<"# R 从文件恢复全部数据 #"<<endl;
cout<<"# #"<<endl;
cout<<"# 0 退出 #"<<endl;
cout<<"# #"<<endl;
cout<<"################################################"<<endl;
if (wrong) cout<<"输入错误,重新选择: ";
else cout<<"选择: ";
wrong = false;
fflush(stdin);
char c=getch();
if (c=='S') c='s';
if (c=='R') c='r';
switch (c)
{
case '0':cout<<"成功退出,任意键继续...";getch();return false;
case '1':FuncBuild(T);break;
case '2':FuncInsert(T);break;
case '3':FuncDeleteStu(T);break;
case '4':FuncDeleteNode(T);break;
case '5':FuncStatistic(T,Thrt);break;
case '6':FuncInOrder(T,Thrt);break;
case '7':FuncLayer(T);break;
case '8':FuncSearch(T,Thrt);break;
case '9':FuncSegment(T,Thrt);break;
case 's':FuncSave(T,Thrt);break;
case 'r':FuncRecover(T,Thrt);break;
default:wrong=true;break;
}
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -