?? 課程設計1.cpp
字號:
}
}
cout<<"從文件讀取數據成功。"<<endl<<endl;
return head;
}
student *number_sort(student*head)//學號排序。選擇后,將鏈表全部讀入一個數組中,將數組按關鍵字從小到大排序,排序后重新存儲回鏈表中,再讀入文件。
{
student *p,stud1,stud[60];
int i,j,k,count=0;
p=new student;
for(p=head;p!=NULL;p=p->next,count++)
{
stud[count].number=p->number;
strcpy(stud[count].name,p->name);
strcpy(stud[count].sex,p->sex);
stud[count].birthday.year=p->birthday.year;stud[count].birthday.month=p->birthday.month;stud[count].birthday.day=p->birthday.day;
stud[count].phone_number=p->phone_number;
strcpy(stud[count].province,p->province);
}
for(i=0;i<count-1;i++)
{
k=i;
for(j=i+1;j<count;j++)
if(stud[j].number<stud[k].number)k=j;
stud1=stud[i];stud[i]=stud[k];stud[k]=stud1;
}
for(p=head,i=0;i<count;i++,p=p->next)
{
p->number=stud[i].number;
strcpy(p->name,stud[i].name);
strcpy(p->sex,stud[i].sex);
p->birthday.year=stud[i].birthday.year;p->birthday.month=stud[i].birthday.month;p->birthday.day=stud[i].birthday.day;
p->phone_number=stud[i].phone_number;
strcpy(p->province,stud[i].province);
}
cout<<"學號排序成功。"<<endl<<endl;
return head;
}
student *born_sort(student*head)//出生年月排序。
{
student *p,stud1,stud[60];
int i,j,k,count=0;
p=new student;
for(p=head;p!=NULL;p=p->next,count++)
{
stud[count].number=p->number;
strcpy(stud[count].name,p->name);
strcpy(stud[count].sex,p->sex);
stud[count].birthday.year=p->birthday.year;stud[count].birthday.month=p->birthday.month;stud[count].birthday.day=p->birthday.day;
stud[count].phone_number=p->phone_number;
strcpy(stud[count].province,p->province);
}
for(i=0;i<count-1;i++)
{
k=i;
for(j=i+1;j<count;j++)
{
if(stud[j].birthday.year<stud[k].birthday.year)k=j;
else if(stud[j].birthday.year==stud[k].birthday.year)
{
if(stud[j].birthday.month<stud[k].birthday.month)k=j;
else if(stud[j].birthday.month==stud[k].birthday.month)
{
if(stud[j].birthday.day<stud[k].birthday.day)k=j;
}
}
}
stud1=stud[i];stud[i]=stud[k];stud[k]=stud1;
}
for(p=head,i=0;i<count;i++,p=p->next)
{
p->number=stud[i].number;
strcpy(p->name,stud[i].name);
strcpy(p->sex,stud[i].sex);
p->birthday.year=stud[i].birthday.year;p->birthday.month=stud[i].birthday.month;p->birthday.day=stud[i].birthday.day;
p->phone_number=stud[i].phone_number;
strcpy(p->province,stud[i].province);
}
cout<<"出生年月排序成功."<<endl<<endl;
return head;
}
void change(student*head)
{
student *p;
p=new student;
long number1;
char c='y';
while(c=='y'||c=='Y')
{
cout<<"輸入你想修改信息的學生的學號:";
cin>>number1;
for(p=head;p!=NULL;p=p->next)
{
if(p->number==number1)
{
cout<<"找到改名學生."<<endl;
c='0';
break;
}
}
if(p==NULL)
{
cout<<"沒有這個學號。重新輸入請按y,取消輸入請按n:";
cin>>c;
}
}
if(c=='n'||c=='N')return;
else
{
cout<<"該學生的學號是:"<<p->number<<endl;
cout<<"該學生的姓名是:"<<p->name<<endl;
cout<<"該學生的性別是:"<<p->sex<<endl;
cout<<"該學生的出生日期是:"<<p->birthday.year<<":"<<p->birthday.month<<":"<<p->birthday.day<<endl;
cout<<"該學生的電話號碼是:"<<p->phone_number<<endl;
cout<<"該學生來自的省份是:"<<p->province<<endl<<endl;
cout<<"請輸入該學生的學號:";
cin>>p->number;
cout<<"請輸入該學生的姓名:";
cin>>p->name;
cout<<"請輸入該學生的性別:";
cin>>p->sex;
cout<<"請輸入該學生的出生日期:";
cin>>p->birthday.year>>p->birthday.month>>p->birthday.day;
if((p->birthday.month<1||p->birthday.month>12)||(p->birthday.day<1||p->birthday.day>31))
{
cout<<"輸入的出生年月格式有誤,請重新輸入。正確格式為 年 月 日."<<endl;
cin>>p->birthday.year>>p->birthday.month>>p->birthday.day;
}
cout<<"請輸入該學生的電話號碼:";
cin>>p->phone_number;
cout<<"請輸入該學生來自的省份:";
cin>>p->province;
cout<<endl;
}
}
void del(student*head)
{
student *p,*q;
q=p=new student;
long number1;
char c='y';
while(c=='y'||c=='Y')
{
cout<<"輸入你想刪除信息的學生的學號:";
cin>>number1;
for(p=head;p!=NULL;q=p,p=p->next)
{
if(p->number==number1)
{
cout<<"找到改名學生."<<endl;
c='0';
break;
}
}
if(p==NULL)
{
cout<<"沒有這個學號。重新輸入請按y,取消輸入請按n:";
cin>>c;
}
}
if(c=='n'||c=='N')return;
else
{
q->next=p->next;
delete p;
cout<<"已經成功刪除該學生的信息"<<endl<<endl;
}
}
int main()
{
fstream iofile("student.dat",ios::in|ios::out);
if(!iofile)
{
cerr<<"open error!";
exit(1);
}
int choice1,choice2,choice3;
while(1)
{
cout<<right<<setfill('-')<<setw(25)<<"主菜單"<<setw(25)<<'-'<<endl<<endl;
cout<<setfill(' ')<<setw(30)<<left<<"1:學生信息輸入.";
cout<<setw(30)<<"2:學生信息查詢."<<endl<<endl;
cout<<setw(30)<<"3:插入新學生的信息.";
cout<<setw(30)<<"4:學生信息排序."<<endl<<endl;
cout<<setw(30)<<"5:學生信息修改.";
cout<<setw(30)<<"6:學生信息刪除."<<endl<<endl;
cout<<setw(30)<<"7:在屏幕上顯示所有學生的信息.";
cout<<setw(30)<<"8:從文件中讀取數據到內存."<<endl<<endl;
cout<<setw(30)<<"9:將內存中的數據存入文件中.";
cout<<setw(30)<<"0:結束程序."<<endl<<endl;
cout<<setw(50)<<setfill('-')<<'-'<<endl;
cout<<"請選擇n:";
cin>>choice1;
choice2=1;
choice3=1;
switch(choice1)
{
case 0:return 0;
case 1:head=create(head);break;
case 2:while(choice2)
{
cout<<endl;
cout<<right<<setfill('-')<<setw(17)<<"查詢菜單"<<setfill('-')<<setw(17)<<"-"<<endl;
cout<<1<<"學號查詢。"<<endl;
cout<<setw(34)<<setfill('-')<<'-'<<endl;
cout<<2<<":姓名查詢."<<endl;
cout<<setw(34)<<setfill('-')<<'-'<<endl;
cout<<3<<":性別查詢."<<endl;
cout<<setw(34)<<setfill('-')<<'-'<<endl;
cout<<0<<":返回上一層."<<endl;
cout<<setw(34)<<setfill('-')<<'-'<<endl;
cout<<"請選擇n:";
cin>>choice2;
switch(choice2)
{
case 1:numbersearch(head);break;
case 2:namesearch(head);break;
case 3:sexsearch(head);break;
case 0:cout<<endl;break;
default:"data error.";break;
}
}break;
case 3:add(head);break;
case 4:while(choice3)
{
cout<<endl;
cout<<right<<setfill('-')<<setw(20)<<"排序菜單"<<setfill('-')<<setw(20)<<"-"<<endl;
cout<<1<<"學號排序。"<<endl;
cout<<setw(34)<<setfill('-')<<'-'<<endl;
cout<<2<<":出生年月排序."<<endl;
cout<<setw(34)<<setfill('-')<<'-'<<endl;
cout<<0<<":返回上一層."<<endl;
cout<<setw(34)<<setfill('-')<<'-'<<endl;
cout<<"請選擇n:";
cin>>choice3;
switch(choice3)
{
case 1:number_sort(head);break;
case 2:born_sort(head);break;
case 0:cout<<endl;break;
default:"data error.";break;
}
}break;
case 5:change(head);break;
case 6:del(head);break;
case 7:display(head);break;
case 8:head=getvalue(head,iofile);break;//為什么要用“head=getvalue(head,iofile);”,而用“getvalue(head,iofile);”就無法帶回改變后的head?畢竟head已經是全局變量了.
case 9: display(head,iofile);break;
default:"data error.";break;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -