?? 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 + -