📄 课程设计1.cpp
字号:
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std;
struct date
{
int year;
int month;
int day;
};
struct student
{
long number;
char name[20];
char sex[3];
date birthday;
string phone_number;
char province[20];
student *next;
};
student*head=NULL;
student *create(student*head)//创建动态链表。新增加的数据放在表头。
{
student *p;//当学号等于0时停止循环.
p=new student;
int a=1;
cout<<"请输入学生的学号(输入0时终止输入):";
while(a)//希望在输入格式出现错误时终止循环,不过好像没效。
{
try
{
cin>>p->number;
if(!cin)throw a;
else a=0;
}
catch(int)
{
cout<<"输入格式有误,请重新输入:";
}
}
if(p->number==0)//如果number为0,则立即推出返回菜单.
{cout<<endl;
return NULL;
}
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;
while(p->number!=0)
{
if(head==NULL)
{
head=p;
p->next=NULL;
}
else
{
p->next=head;
head=p;
}
p=new student;
cout<<"请输入该学生的学号(输入0时终止输入):";
cin>>p->number;
if(p->number==0)goto A;
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;
}
A:
cout<<endl;
delete p;
return(head);
}
void numbersearch(student*head)//按学号查找
{
student *p;
p=new student;
long number1;
cout<<"请输入该学生的学号(输入0时结束):";
cin>>number1;
if(number1==0)return;//考虑用户错误操作,一旦错误的进入号码查询,可以立刻输入结束符号0,来退回菜单。
for(p=head;p!=NULL;p=p->next)
if(p->number==number1)
{
cout<<"找到该学生."<<endl;
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;
break;//一有找到该学号的学生就退出循环。
}
if(p==NULL)cout<<"数据有误,没有该学生的资料."<<endl<<endl;
}
void namesearch(student*head)//按姓名查找
{
student *p;
p=new student;
char name1[20];
cout<<"请输入该学生的姓名(输入0时结束):";
cin>>name1;
if(name1=="0")return;
for(p=head;p!=NULL;p=p->next)
if(strcmp(p->name,name1)==0)
{
cout<<"找到该学生."<<endl;
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;
break;
}
if(p==NULL)cout<<"数据有误,没有该学生的资料."<<endl<<endl;
}
void sexsearch(student*head)//按性别查找。
{
student *p;
p=new student;
char sex1[3];
int count=0;//计算有多少个男生或者女生。
cout<<"请输入该学生的性别(输入0时结束):";
cin>>sex1;
if(strcmp(sex1,"0")==0)return;
for(p=head;p!=NULL;p=p->next)
if(strcmp(p->sex,sex1)==0)
{
count++;
cout<<"找到该学生."<<endl;
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;
break;
}
if(count==0)cout<<"数据有误,没有该性别的学生的资料."<<endl<<endl;
}
void add(student*head)//在链表末尾增加一个数据
{
student *p,*q;
q=new student;
cout<<"请输入该学生的学号(输入0时结束输入)"<<":";
cin>>q->number;
if(q->number==0){cout<<endl;delete q;return;}
cout<<"请输入该学生的姓名"<<":";
cin>>q->name;
cout<<"请输入该学生的性别"<<":";
cin>>q->sex;
cout<<"请输入该学生的出生日期:";
cin>>q->birthday.year>>q->birthday.month>>q->birthday.day;
if((q->birthday.month<1||q->birthday.month>12)||(q->birthday.day<1||q->birthday.day>31))
{
cout<<"输入的出生年月格式有误,请重新输入。正确格式为 年 月 日."<<endl;
cin>>q->birthday.year>>q->birthday.month>>q->birthday.day;
}
cout<<"请输入该学生的电话号码"<<":";
cin>>q->phone_number;
cout<<"请输入该学生来自的省份"<<":";
cin>>q->province;
q->next=NULL;
if(head==NULL){head=q;cout<<"增加数据成功."<<endl<<endl;}
else
{
for(p=head;p->next!=NULL;p=p->next);
p->next=q;
cout<<"增加数据成功."<<endl<<endl;
}
}
void display(student*head)
{
student*p;
int n=0;
p=head;
while(p!=NULL)
{
n++;
cout<<n<<"学生的学号是:"<<p->number<<endl;
cout<<n<<"学生的姓名是:"<<p->name<<endl;
cout<<n<<"学生的性别是:"<<p->sex<<endl;
cout<<n<<"学生的出生日期是:"<<p->birthday.year<<":"<<p->birthday.month<<":"<<p->birthday.day<<endl;
cout<<n<<"学生的电话号码是:"<<p->phone_number<<endl;
cout<<n<<"学生来自的省份是:"<<p->province<<endl<<endl;
p=p->next;
}
}
void display(student* head,fstream& ofile) //保存文件(在文件中显示),其实跟在屏幕上显示一样的。
{//重载display(),在文件中保存。
student* p;
p=head;
ofile.clear();
ofile.seekg(0);
while(p!=NULL)
{
ofile<<setw(15)<<left<<p->number;
ofile<<setw(10)<<left<<p->name;
ofile<<setw(5)<<left<<p->sex;
ofile<<setw(5)<<left<<p->birthday.year<<setw(3)<<left<<p->birthday.month<<setw(8)<<left<<p->birthday.day;
ofile<<setw(15)<<left<<p->phone_number;
ofile<<setw(15)<<left<<p->province<<endl<<endl;
p=p->next;
}
cout<<endl<<"保存数据到文件成功!"<<endl<<endl;
}
student *getvalue(student*head,fstream&infile)//从文件读取数据放到动态链表中。而且不明白为什么不用return head;就无法返回已经改变的head.虽然head已经是全局变量了。
{
student*p;
infile.seekg(0);
while(!infile.eof())
{
p=new student;
infile>>p->number;
if(p->number<0){delete p;cout<<"从文件读取数据成功。"<<endl;return head;}//不知为什么读完文件后还读多个莫名其妙的数据,只好用这种不高明的方法去掉这个数据。
infile>>p->name;
infile>>p->sex;
infile>>p->birthday.year>>p->birthday.month>>p->birthday.day;
infile>>p->phone_number;
infile>>p->province;
if(head==NULL)
{
head=p;
p->next=NULL;
}
else
{
p->next=head;
head=p;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -