#include <iostream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
//#define student EType
typedef struct
{
int num;
float Chinese;
float Math;
float English;
float Sum;
}student;
student s[9]={{20020001,85.0,88,97.0,270.0},{20010002,92.5,91,95.0,278.5},{20010003,95.0,98,99.0,292.0},
{20010004,85.0,87,96.5,268.5},{20010005,96.0,93,100.0,289.0},{20010006,72.0,76,70.5,218.5},
{20010007,65.0,53,53.0,171.0},{20010008,88.0,94,90.5,272.5},{20010009,96.5,83,65.0,244.5}};//学生成绩
typedef struct
{
float key;
int n;
}EType;
struct BTNode
{
EType data;
BTNode *lchild;
BTNode *rchild;
};
typedef BTNode BinaryTree;
BinaryTree *BT,*p,*q;
BTNode *MakeNode(EType &x) //构造二叉树结点
{
BTNode *ptr;
ptr=new BTNode;
if(!ptr)
return NULL;
ptr ->data = x ;
ptr ->lchild=NULL;
ptr ->rchild=NULL;
return ptr;
}
void Display(BTNode *p)
{
for(int i=0;i<9;i++)
{
cout<<"学号 语文 数学 英语 总分 "<<endl;
cout<<s[i].num <<s[i].Chinese <<s[i].Math <<s[i].English <<s[i].Sum <<endl;
}
}
void InOrder(BTNode *BT) //中序遍历
{
if(BT)
{
InOrder(BT->lchild);
cout<<s[BT->data.n].num <<s[BT->data.n].Chinese <<s[BT->data.n ].Math <<s[BT->data.n ].English <<s[BT->data.n ].Sum <<endl;
InOrder(BT->rchild);
}
}
BTNode *SortBTInsert(BTNode *BT,EType &x)
{
BTNode *p=BT;
BTNode *parent=NULL;
while(p)
{
parent=p;
if(x.key <p->data.key);
p=p->lchild ;
if(x.key>p->data.key )
p=p->rchild ;
else
return p;
}
BTNode *q=new BTNode;
q->data = x;
q->lchild = NULL;
q->rchild = NULL;
if(BT) //原树非空
{
if(x.key<parent->data.key )
parent->lchild = q;
else
parent->rchild = q;
}
else
BT=q;
return BT;
}
bool SortBTSearch(BTNode *BT,EType &x, float &Searchkey)
{
BTNode *p=BT;
while(p)
{
if(Searchkey<p->data.key )
p=p->lchild ;
else if(Searchkey>p->data.key )
p=p->rchild ;
else
{
x=p->data ;
return true;
}
cout<<"查找学生成绩如下:"<<endl;
cout<<"学号 语文 数学 英语 总分 "<<endl;
cout<<s[x.n].num <<s[x.n].Chinese <<s[x.n].Math <<s[x.n].English <<s[x.n].Sum <<endl;
}
return false;
}
bool SortBTDelete(BTNode *BT,float &Searchkey)
{
BTNode *p=BT, *parent=NULL;
BTNode *s,*ps;
while(p&&p->data.key!=Searchkey)
{
parent = p;
if(Searchkey<p->data.key )
p=p->lchild ;
else
p=p->rchild ;
}
if(!p)
{
return false;
}
//对分类二叉树重构
if(p->lchild &&p->rchild )
{
s=p->lchild ;
ps=p;
while(s->rchild)
{
ps = s;
s =s->rchild;
}
p->data =s->data;
p = s;
parent = ps;
}
if(p->lchild )
s = p->lchild ;
else
s =p->rchild ;
if(p==BT)
BT = s;
else
{
if(p == parent->lchild )
parent->lchild = s;
else
parent->rchild = s;
}
delete p;
return true;
}
bool SearchMaxMin(BTNode *BT,EType x,EType y)
{
BTNode *p,*q;
p=q=BT;
while(p->lchild )
{
p=p->lchild ;
}
x=p->data ;
cout<<"最低分学生信息为:"<<endl;
cout<<"学号 语文 数学 英语 总分 "<<endl;
cout<<s[x.n].num <<s[x.n].Chinese <<s[x.n].Math <<s[x.n].English <<s[x.n].Sum <<endl;
while(q->rchild )
{
q=q->rchild ;
}
y=q->data ;
cout<<"最高分学生信息为:"<<endl;
cout<<"学号 语文 数学 英语 总分 "<<endl;
cout<<s[y.n].num <<s[y.n].Chinese <<s[y.n].Math <<s[y.n].English <<s[y.n].Sum <<endl;
return true;
}
//------------------------------------------------堆排序
struct MaxHeap
{
EType *heap;
int HeapSize;
int MaxSize;
} ;
MaxHeap H;
void Init(MaxHeap &H)//将堆中的数据初始化为一个最大堆
{
for(int i=H.HeapSize/2;i>=1;i--)
{
H.heap[0]=H.heap[i];
int son=2*i;
while(son<=H.HeapSize)
{
if(son<H.HeapSize &&H.heap[son].key <H.heap[son+1].key )
son++;
if(H.heap[0].key >= H.heap[son].key )
break;
H.heap[son/2] = H.heap[son];
son*=2;
}
H.heap[son/2] = H.heap[0];
}
}
bool DeleteMaxHeap(MaxHeap &H,EType &x)
{
if(H.HeapSize == 0)
return false;
x=H.heap[1];
H.heap[0] = H.heap[H.HeapSize--];
int i=1,son =2*i;
while(son<=H.HeapSize )
{
if(son<H.HeapSize && H.heap[son].key < H.heap[son+1].key )
son++;
if(H.heap[0].key >=H.heap[son].key )
break;
H.heap[i]=H.heap[son];
i=son;
son=i*2;
}
H.heap[i] = H.heap[0];
return true;
}
void Menu_name()
//作者信息
{
cout<<"\n\n\n\n\n\n\n";
cout<<" *************************************************\n";
cout<<" 利用分类二叉树查找及堆排序实现学生成绩管理\n\n";
cout<<" 制作:xxx\n";
cout<<" 班级:xxxxxxxx\n";
cout<<" 学号: xxxxxxxx\n";
cout<<" 指导老师: xxxx\n";
cout<<" **************************************************\n";
cout<<"\n\n\n\t\t";
}
void Menu() //菜单函数
{
cout <<"\n\t\t"<<"请选择以下一个功能:"<<endl;
cout <<"\t\t1.显示学生成绩(按学号排)." << endl;
cout <<"\t\t2.学生总分由低到高." << endl;
cout <<"\t\t3.最高分与最低分学生成绩查询."<<endl;
cout <<"\t\t4.学生总成绩堆排序 "<<endl;
cout <<"\t\t5.学生数学成绩堆排序"<<endl;
cout <<"\t\t0.退出.\n"<<endl;
cout <<"\t\t===============================\n"<<endl;
}
int main()
{
BTNode *BT;
EType x,y;
BT=NULL;
Menu_name();
for(int i=0;i<9;i++)
{
x.key =s[i].Sum ;
x.n =i;
BT=SortBTInsert(BT,x);
}//构建分类二叉树
char j;
while(1)
{
Menu();
cout<<"情选择功能序号: \n";
j=getchar();
getchar();
switch(j)
{
case 1:
cout<<"学号从低到高排序:" <<endl;
Display(p);
break;
case 2:
cout<<"按总分由低到高排序:"<<endl;
InOrder(BT);
break;
case 3:
cout<<"总分最高与最低学生信息:"<<endl;
SearchMaxMin(BT,x,y);
break;
case 4:
H.HeapSize =9;
H.MaxSize =10;
H.heap =new EType[H.MaxSize ];
for(int h=0;h<H.MaxSize ;h++)
{
H.heap[h+1].key =s[h].Sum ;
H.heap[h+1].n =h;
}
Init(H);
cout<<"按总分从高到低排序:"<<endl;
for(int h=0;h<9;h++)
{
cout<<"学号 语文 数学 英语 总分 "<<endl;
DeleteMaxHeap(H,x);
cout<<s[h].num <<s[h].Chinese <<s[h].Math <<s[h].English <<s[h].Sum <<endl;
}
break;
case 5:
H.HeapSize =9;
H.MaxSize =10;
H.heap =new EType[H.MaxSize ];
for(int h=0;h<H.MaxSize ;h++)
{
H.heap[h+1].key =s[h].Sum ;
H.heap[h+1].n =h;
}
Init(H);
cout<<"按数学成绩从高到低排序:"<<endl;
for(int h=0;h<9;h++)
{
cout<<"学号 语文 数学 英语 总分 "<<endl;
DeleteMaxHeap(H,x);
cout<<s[h].num <<s[h].Chinese <<s[h].Math <<s[h].English <<s[h].Sum <<endl;
}
break;
case 0:
break;
}
}
}
没有警告和报错,显示3221225477错误
了解一下说是存在野指针错误,可是大一新生搞不懂啊,求各位帮忙解惑啊,谢谢!
将Switch函数里的东西运行出来