?? change.cpp
字號:
#include"head.h"
#include<iostream.h>
#include<stdlib.h>
#include<string.h>
extern void DisplayBasic(Student* &);
void Change(Student* &head)
{
Student* pGuard;
Student* pS;
Student* ptr;
char choice;
char Name[20];
int Class;
int Number;
cout<<"已記錄的學生如下:"<<endl;
DisplayBasic(head);
cout<<"說明:本程序提供①根據學生的學號"
<<" ②根據學生姓名和所在的班級"
<<" 兩種方式來查找并修改學生的信息."
<<endl;
status0:
cout<<"請選擇修改方式(1/2):";
cin>>choice;
if(choice=='1')
{
status1:
cout<<"請輸入要修改的學生的學號:"<<endl;
cin>>Number;
for(pGuard=head;pGuard!=NULL;pGuard=pGuard->next)
{
if(pGuard->Number==Number)
{
pS=new Student;
for(int i=0;i<MAX;i++)
{
pS->Score[i]=pGuard->Score[i]; //把已記錄的成績也保存下來
}
status2:
system("cls");
cout<<"找到該學生,已記錄的信息如下:"<<endl;
cout<<" 學生姓名: "<<pGuard->Name <<endl;
cout<<" 班級: "<<pGuard->Class <<endl;
cout<<" 學號: "<<pGuard->Number <<endl;
cout<<" 性別: ";
if(pGuard->Sex==1)
cout<<"男"<<endl;
else
cout<<"女"<<endl;
cout<<" 年齡: "<<pGuard->Age <<endl;
cout<<" 宿舍號: "<<pGuard->Room <<endl;
cout<<" 電話號碼: "<<pGuard->Tel <<endl<<endl<<endl;
cout<<"現在請輸入新的學生信息(如果沒有變化請按照原來的數值輸入):"<<endl;
cout<<"請輸入學生所在的班級: ";
cin>>pS->Class;
cout<<endl<<"請輸入學生的學號: ";
cin>>pS->Number;
cout<<endl<<"請輸入學生的姓名: ";
cin>>pS->Name;
status3:
cout<<endl<<"請輸入學生的性別(男用1表示,女用2表示): ";
cin>>pS->Sex;
if(pS->Sex!=1&&pS->Sex!=2)
{
cout<<"您鍵入的內容有錯,請重新選擇."<<endl;
goto status3;
}
cout<<endl<<"請輸入學生的年齡: ";
cin>>pS->Age;
cout<<endl<<"請輸入學生的宿舍號: ";
cin>>pS->Room;
cout<<endl<<"請輸入學生的電話號碼: ";
cin>>pS->Tel;
status4:
cout<<"請核對以上信息,輸入內容完全無誤嗎? (y/n):"<<endl;
cin>>choice;
if(choice=='N'||choice=='n')
{
cout<<"那么請重新輸入吧."<<endl;
system("pause");
system("cls");
goto status2;
}
else if(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n')
{
cout<<"您鍵入的內容有錯,請重新選擇."<<endl;
system("pause");
system("cls");
goto status4;
}
else
{
if(pS->Class==pGuard->Class) //判斷學生的班別是否有變化
{
if(pGuard==head) //先判斷pGuard的情況,否則會使下面的ptr出錯
{
head=pS;
pS->next=pGuard->next;
delete pGuard;
}
else
{
for(ptr=head;ptr->next!=pGuard;ptr=ptr->next)
; //使ptr指向pGuard的前一個節點
ptr->next=pS;
pS->next=pGuard->next;
delete pGuard; //以上3句把pGuard和pS交換,刪除pGuard
}
}
else //如果學生的班級有了變化
{
if(pGuard==head)
{
head=pS;
pS->next=pGuard->next;
delete pGuard;
}
else
{
for(ptr=head;ptr->next!=pGuard;ptr=ptr->next)
; //同上,使ptr指向pGuard的前一個節點
ptr=pGuard->next;
delete pGuard; //以上兩句把無效了的pGuard刪除
//現在是插入新的節點,即pS
for(pGuard=head;pGuard->next!=NULL&&pGuard->next->Class<pS->Class;pGuard=pGuard->next)
;
if(pGuard==head&&pGuard->Class>pS->Class)
{
head=pS;
pS->next=pGuard;
}
else if(pGuard==head&&pGuard->Class<pS->Class)
{
pS->next=head->next;
head->next=pS;
}
else
{
pS->next=pGuard->next;
pGuard->next=pS;
}
}
}
}
cout<<"已成功修改該學生的信息!"<<endl;
return;
}
}
cout<<"沒有找到該學生的信息!"<<endl;
status5:
cout<<"要重新輸入學生的學號嗎?(y/n):";
cin>>choice;
if(choice=='y'||choice=='Y')
{
cout<<"那么請重新輸入吧!"<<endl;
system("pause");
goto status1;
}
else if(choice!='y'&&choice!='Y'&&choice!='n'&&choice!='N')
{
cout<<"您鍵入的內容有誤,請重新選擇."<<endl;
goto status5;
}
}
else if(choice=='2')
{
status10:
cout<<"請輸入要修改的學生的姓名:";
cin>>Name;
cout<<"請輸入學生所在的班級:";
cin>>Class;
for(pGuard=head;pGuard!=NULL;pGuard=pGuard->next)
{
if(pGuard->Class==Class&&strcmp(pGuard->Name,Name)==0)
{
pS=new Student;
for(int i=0;i<MAX;i++)
{
pS->Score[i]=pGuard->Score[i]; //把已記錄的成績也保存下來
}
status6:
cout<<"找到該學生,已記錄的信息如下:"<<endl;
cout<<" 學生姓名: "<<pGuard->Name <<endl;
cout<<" 班級: "<<pGuard->Class <<endl;
cout<<" 學號: "<<pGuard->Number <<endl;
cout<<" 性別: ";
if(pGuard->Sex==1)
cout<<"男"<<endl;
else
cout<<"女"<<endl;
cout<<" 年齡: "<<pGuard->Age <<endl;
cout<<" 宿舍號: "<<pGuard->Room <<endl;
cout<<" 電話號碼: "<<pGuard->Tel <<endl<<endl<<endl;
cout<<"現在請輸入新的學生信息(如果沒有變化請按照原來的數值輸入):"<<endl;
cout<<"請輸入學生所在的班級: ";
cin>>pS->Class;
cout<<endl<<"請輸入學生的學號: ";
cin>>pS->Number;
cout<<endl<<"請輸入學生的姓名: ";
cin>>pS->Name;
status7:
cout<<endl<<"請輸入學生的性別(男用1表示,女用2表示): ";
cin>>pS->Sex;
if(pS->Sex!=1&&pS->Sex!=2)
{
cout<<"您鍵入的內容有錯,請重新選擇."<<endl;
goto status7;
}
cout<<endl<<"請輸入學生的年齡: ";
cin>>pS->Age;
cout<<endl<<"請輸入學生的宿舍號: ";
cin>>pS->Room;
cout<<endl<<"請輸入學生的電話號碼: ";
cin>>pS->Tel;
status8:
cout<<"請核對以上信息,輸入內容完全無誤嗎? (y/n):"<<endl;
cin>>choice;
if(choice=='N'||choice=='n')
{
cout<<"那么請重新輸入吧."<<endl;
system("pause");
system("cls");
goto status6;
}
else if(choice!='Y'&&choice!='y'&&choice!='N'&&choice!='n')
{
cout<<"您鍵入的內容有錯,請重新選擇."<<endl;
system("pause");
system("cls");
goto status8;
}
else
{
if(pS->Class==pGuard->Class) //同上,判斷學生的班別是否有變化
{
if(pGuard==head)
{
head=pS;
pS->next=pGuard->next;
delete pGuard;
}
else
{
for(ptr=head;ptr->next!=pGuard;ptr=ptr->next)
; //使ptr指向pGuard的前一個節點
ptr->next=pS;
pS->next=pGuard->next;
delete pGuard; //以上3句把pGuard和pS交換,刪除pGuard
}
}
else //如果學生的班級有了變化
{
if(pGuard==head)
{
head=pS;
pS->next=pGuard->next;
delete pGuard;
}
else
{
for(ptr=head;ptr->next!=pGuard;ptr=ptr->next)
; //同上,使ptr指向pGuard的前一個節點
ptr=pGuard->next;
delete pGuard; //以上兩句把無效了的pGuard刪除
//現在是插入新的節點,即pS
for(pGuard=head;pGuard->next!=NULL&&pGuard->next->Class<pS->Class;pGuard=pGuard->next)
;
if(pGuard==head&&pGuard->Class>pS->Class)
{
head=pS;
pS->next=pGuard;
}
else if(pGuard==head&&pGuard->Class<pS->Class)
{
pS->next=head->next;
head->next=pS;
}
else
{
pS->next=pGuard->next;
pGuard->next=pS;
}
}
}
}
cout<<"已成功修改該學生的基本信息."<<endl;
return;
}
}
cout<<"沒有找到該學生的信息!"<<endl;
status9:
cout<<"要重新輸入學生的班級和姓名嗎?(y/n):";
cin>>choice;
if(choice=='y'||choice=='Y')
{
cout<<"那么請重新輸入吧!"<<endl;
system("pause");
goto status10;
}
else if(choice!='y'&&choice!='Y'&&choice!='n'&&choice!='N')
{
cout<<"您鍵入的內容有誤,請重新選擇."<<endl;
goto status9;
}
}
else if(choice!='1'&&choice!='2')
{
cout<<"您鍵入的內容有錯,請重新選擇."<<endl;
goto status0;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -