📄 task_deeds.cpp
字号:
}
void StudentAdd_Delete(student *&headOfStudent,courselist *&headOfCourse)
{
short i;
cout<<" 1. 增加学生纪录"<<endl
<<" 2. 删除学生纪录"<<endl
<<" 0. 返回上一级菜单"<<endl
<<"请选择: ";
cin>>i;
while (cin.fail() || (i>2 || i<0))
{
cout<<char(7);
cout<<"\n输入错误 !"<<endl
<<"输入菜单中您要选择的项目前的序号即可,即0-2的数字"<<endl
<<"请选择: ";
cin.clear();
cin.ignore(100,'\n');
cin>>i;
}
switch (i)
{
case 1: student *temp;
courseOfStudent *course,*endcourse,*p;
courselist *courses,*q;
char coursename[20];
bool flag;
do
{
cout<<"\n请输入添加的学生记录,如果添加结束,请输入 * "<<endl;
temp=new student;
if (temp==NULL)
{
cout<<char(7);
cout<<"\n申请内存空间失败,添加学生记录失败!"<<endl;
return;
}
cout<<"请输入学生的姓名:";
cin>>temp->studentName;
if (temp->studentName[0]=='*')
return;
cout<<"请输入学生的学号:";
cin>>temp->studentNumber;
temp->courses=NULL;
temp->pNameLc=NULL;
temp->pNameRc=NULL;
temp->pNumberLc=NULL;
temp->pNumberRc=NULL;
temp->pScoreLc=NULL;
temp->pScoreRc=NULL;
cout<<"请输入该学生的各门课程及相应分数,如果该生未选这门课程,输入-1"<<endl;
course=NULL;endcourse=NULL;
courses=headOfCourse;
while (courses)
{
cout<<courses->courseName<<':';
p=new courseOfStudent;
if (p==NULL)
{
cout<<char(7);
cout<<"\n申请内存失败!不能添加学生课程信息!"<<endl;
break;
}
strcpy(p->courseName,courses->courseName);
p->next=NULL;
cin>>p->score;
while (cin.fail())
{
cout<<char(7);
cout<<"\n输入错误!学生课程成绩应该是 0.0-100.0 的数!"<<endl
<<"请重新输入"<<temp->studentName<<"的"<<p->courseName<<"的成绩:";
cin.clear();
cin.ignore(100,'\n');
cin>>p->score;
}
if (course==NULL)
{
course=p;
endcourse=p;
}
else
{
endcourse->next=p;
endcourse=p;
}
courses=courses->next;
}
temp->courses=course;
if (headOfCourse)
cout<<"\n如果该学生还有其他课程,请按照“ 课程名 该课学分 该生成绩 ”的格式输入!退出请输入 * "<<endl;
else
cout<<"\n请按照“ 课程名 该课学分 该生成绩 ”的格式输入!退出请输入 * "<<endl;
courses=headOfCourse;
if (courses)
while (courses->next) courses=courses->next;
flag=false;
while (cin>>coursename)
{
if (coursename[0]=='*') break;
flag=true;
q=new courselist;
if (q==NULL)
{
cout<<char(7);
cout<<"\n申请内存空间失败!不能添加课程信息!"<<endl;
break;
}
strcpy(q->courseName,coursename);
cin>>q->credits;
q->next=NULL;
if (courses)
{
courses->next=q;
courses=q;
}
else
{
courses=q;
headOfCourse=q;
}
p=new courseOfStudent;
if (p==NULL)
{
cout<<char(7);
cout<<"\n申请内存失败!不能添加学生课程信息!"<<endl;
break;
}
strcpy(p->courseName,coursename);
p->next=NULL;
cin>>p->score;
if (course==NULL)
{
course=p;
endcourse=p;
}
else
{
endcourse->next=p;
endcourse=p;
}
}
if (flag)
{
if (headOfStudent)
AddCourses(headOfStudent,headOfCourse);
}
StudentAdd_Name(headOfStudent,temp);
StudentAdd_Number(headOfStudent,temp);
}while (1);
break;
case 2: StudentDelete(headOfStudent);break;
}
}
void AddCourses(student *&headOfStudent,courselist *&headOfCourse)
{
courseOfStudent *course=headOfStudent->courses,*p;
courselist *courseslist=headOfCourse;
if (course)
{
while (course->next)
{
course=course->next;
courseslist=courseslist->next;
}
courseslist=courseslist->next;
}
while (courseslist)
{
p=new courseOfStudent;
if (p==NULL)
{
cout<<char(7);
cout<<"\n申请内存空间失败!不能添加课程信息!"<<endl;
return;
}
strcpy(p->courseName,courseslist->courseName);
p->score=-1;
p->next=NULL;
if (course)
{
course->next=p;
course=p;
}
else
{
course=p;
}
courseslist=courseslist->next;
}
if (headOfStudent->pNameLc)
AddCourses(headOfStudent->pNameLc,headOfCourse);
if (headOfStudent->pNameRc)
AddCourses(headOfStudent->pNameRc,headOfCourse);
}
void StudentAdd_Name(student *&head,student *&temp)
{
if (head==NULL)
{
head=temp;
return;
}
char i=strcmp(head->studentName,temp->studentName);
if (i>0)
StudentAdd_Name(head->pNameLc,temp);
else
StudentAdd_Name(head->pNameRc,temp);
}
void StudentAdd_Number(student *&head,student *&temp)
{
if (head==NULL)
{
head=temp;
return;
}
char i=strcmp(head->studentNumber,temp->studentNumber);
if (i>0)
StudentAdd_Number(head->pNumberLc,temp);
else
if (i<0)
StudentAdd_Number(head->pNumberRc,temp);
}
void StudentDelete(student *&head)
{
short i;
cout<<" 1. 输入要删除学生的名字"<<endl
<<" 2. 输入要删除学生的学号"<<endl
<<"请选择:";
cin>>i;
while (cin.fail() || (i!=1 && i!=2))
{
cout<<char(7);
cout<<"\n输入错误 !"<<endl
<<"输入菜单中您要选择的项目前的序号即可,即输入 1 或 2 "<<endl
<<"请选择: ";
cin.clear();
cin.ignore(100,'\n');
cin>>i;
}
char name[25],number[20];
student *temp,*father,*p;
switch (i)
{
case 1: cout<<"\n请输入要删除的学生的名字:";
cin>>name;
father=head;
temp=SearchByName(head,name,father);
p=temp;
if (temp==NULL)
{
cout<<char(7);
cout<<"\n没有该学生的记录!删除无效!"<<endl;
return;
}
strcpy(number,temp->studentNumber);
break;
case 2: cout<<"\n请输入要删除的学生的学号:";
cin>>number;
father=head;
temp=SearchByNumber(head,number,father);
p=temp;
if (temp==NULL)
{
cout<<char(7);
cout<<"\n没有该学生的记录!删除无效!"<<endl;
return;
}
strcpy(name,temp->studentName);
father=head;
temp=SearchByName(head,name,father);
break;
}
if (temp==father) //要删除根节点
{
if (temp->pNameRc==NULL)
{
head=head->pNameLc;
}
else //左子树加到右子树的最左下
{
temp=temp->pNameRc;
while (temp->pNameLc)
temp=temp->pNameLc;
temp->pNameLc=father->pNameLc;
head=father->pNameRc;
}
}
else //删除非根节点
{
if (temp==father->pNameLc) //删除节点为左节点
{
if (temp->pNameRc==NULL) //右子树为空
{
father->pNameLc=temp->pNameLc;
}
else //右子树不为空,先将左子树加到右子树的最左下
{
temp=temp->pNameRc;
while (temp->pNameLc)
temp=temp->pNameLc;
temp->pNameLc=father->pNameLc->pNameLc;
father->pNameLc=father->pNameLc->pNameRc;
}
}
else //删除节点为右节点
{
if (temp->pNameRc==NULL) //右子树为空
{
father->pNameRc=temp->pNameLc;
}
else //右子树不为空,先将左子树加到右子树的最左下
{
temp=temp->pNameRc;
while (temp->pNameLc)
temp=temp->pNameLc;
temp->pNameLc=father->pNameRc->pNameLc;
father->pNameRc=father->pNameRc->pNameRc;
}
}
}
father=head;
temp=SearchByNumber(head,number,father);
if (temp==father) //要删除根节点
{
if (temp->pNumberRc==NULL)
{
head=head->pNumberLc;
}
else //左子树加到右子树的最左下
{
temp=temp->pNumberRc;
while (temp->pNumberLc)
temp=temp->pNumberLc;
temp->pNumberLc=father->pNumberLc;
head=father->pNumberRc;
}
}
else //删除非根节点
{
if (temp==father->pNumberLc) //删除节点为左节点
{
if (temp->pNumberRc==NULL) //右子树为空
{
father->pNumberLc=temp->pNumberLc;
}
else //右子树不为空,先将左子树加到右子树的最左下
{
temp=temp->pNumberRc;
while (temp->pNumberLc)
temp=temp->pNumberLc;
temp->pNumberLc=father->pNumberLc->pNumberLc;
father->pNumberLc=father->pNumberLc->pNumberRc;
}
}
else //删除节点为右节点
{
if (temp->pNumberRc==NULL) //右子树为空
{
father->pNumberRc=temp->pNumberLc;
}
else //右子树不为空,先将左子树加到右子树的最左下
{
temp=temp->pNumberRc;
while (temp->pNumberLc)
temp=temp->pNumberLc;
temp->pNumberLc=father->pNumberRc->pNumberLc;
father->pNumberRc=father->pNumberRc->pNumberRc;
}
}
}
if (i==1)
cout<<"\n学生"<<p->studentName<<"的成绩记录已被删除!"<<endl;
else
cout<<"\n学生"<<p->studentNumber<<"的成绩记录已被删除!"<<endl;
delete p;
}
void Search(student *&head)
{
short i;
char s[25];
student *temp=NULL,*code;
while (1)
{
cout<<" 1. 按姓名查找学生成绩"<<endl
<<" 2. 按学号查找学生成绩"<<endl
<<" 0. 返回上一级菜单"<<endl
<<"请选择: ";
cin>>i;
while (cin.fail() || (i>2 || i<0) )
{
cout<<char(7);
cout<<"\n输入错误 !"<<endl
<<"输入菜单中您要选择的项目前的序号即可,即0-2的数字"<<endl
<<"请选择: ";
cin.clear();
cin.ignore(100,'\n');
cin>>i;
}
switch (i)
{
case 1: do
{
cout<<"\n请输入您要查询的学生的名字,如果查询结束,请输入一个 * "<<endl;
cin>>s;
if (s[0]=='*') break;
if (head)
temp=SearchByName(head,s,code);
else
temp=NULL;
if (temp==NULL)
{
cout<<char(7);
cout<<"\n没有该学生的记录!查询失败!"<<endl;
}
else
{
PrintSingleStudent(temp);
}
}while (1);
break;
case 2: do
{
cout<<"\n请输入您要查询的学生的学号,如果查询结束,请输入一个 * "<<endl;
cin>>s;
if (s[0]=='*') break;
if (head)
temp=SearchByNumber(head,s,code);
else
temp=NULL;
if (temp==NULL)
{
cout<<char(7);
cout<<"\n没有该学生的记录!查询失败!"<<endl;
}
else
{
PrintSingleStudent(temp);
}
}while (1);
break;
case 0:return;
}
}
}
student *SearchByName(student *&head,char name[],student *&code)
{
short i=strcmp(head->studentName,name);
if (i==0) return head;
code=head;
if (i>0)
{
if (head->pNameLc)
return SearchByName(head->pNameLc,name,code);
else
return NULL;
}
else
{
if (head->pNameRc)
return SearchByName(head->pNameRc,name,code);
else
return NULL;
}
}
student *SearchByNumber(student *&head,char number[],student *&code)
{
short i=strcmp(head->studentNumber,number);
if (i==0) return head;
code=head;
if (i>0)
{
if (head->pNumberLc)
return SearchByNumber(head->pNumberLc,number,code);
else
return NULL;
}
else
{
if (head->pNumberRc)
return SearchByNumber(head->pNumberRc,number,code);
else
return NULL;
}
}
void Sort(student *&headOfStudent,courselist *&headOfCourse)
{
short i;
cout<<" 1. 按照姓名排序"<<endl
<<" 2. 按照学号排序"<<endl
<<" 3. 按照某门成绩排序"<<endl
<<" 0. 返回上一级菜单"<<endl
<<"请选择:";
cin>>i;
while (cin.fail() || (i>3 || i<0))
{
cout<<char(7);
cout<<"\n输入错误 !"<<endl
<<"输入菜单中您要选择的项目前的序号即可,即0-2的数字"<<endl
<<"请选择: ";
cin.clear();
cin.ignore(100,'\n');
cin>>i;
}
if (i!=0 && headOfStudent==NULL)
{
cout<<char(7);
cout<<"\n没有学生记录!不能进行排序!"<<endl;
return;
}
char coursename[20];
courselist *temp;
switch (i)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -