📄 课程设计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 + -