?? 課程設(shè)計(jì)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)//創(chuàng)建動態(tài)鏈表。新增加的數(shù)據(jù)放在表頭。
{
student *p;//當(dāng)學(xué)號等于0時(shí)停止循環(huán).
p=new student;
int a=1;
cout<<"請輸入學(xué)生的學(xué)號(輸入0時(shí)終止輸入):";
while(a)//希望在輸入格式出現(xiàn)錯誤時(shí)終止循環(huán),不過好像沒效。
{
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<<"請輸入該學(xué)生的姓名:";
cin>>p->name;
cout<<"請輸入該學(xué)生的性別:";
cin>>p->sex;
cout<<"請輸入該學(xué)生的出生日期:";
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<<"請輸入該學(xué)生的電話號碼:";
cin>>p->phone_number;
cout<<"請輸入該學(xué)生來自的省份:";
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<<"請輸入該學(xué)生的學(xué)號(輸入0時(shí)終止輸入):";
cin>>p->number;
if(p->number==0)goto A;
cout<<"請輸入該學(xué)生的姓名:";
cin>>p->name;
cout<<"請輸入該學(xué)生的性別:";
cin>>p->sex;
cout<<"請輸入該學(xué)生的出生日期:";
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<<"請輸入該學(xué)生的電話號碼:";
cin>>p->phone_number;
cout<<"請輸入該學(xué)生來自的省份:";
cin>>p->province;
cout<<endl;
}
A:
cout<<endl;
delete p;
return(head);
}
void numbersearch(student*head)//按學(xué)號查找
{
student *p;
p=new student;
long number1;
cout<<"請輸入該學(xué)生的學(xué)號(輸入0時(shí)結(jié)束):";
cin>>number1;
if(number1==0)return;//考慮用戶錯誤操作,一旦錯誤的進(jìn)入號碼查詢,可以立刻輸入結(jié)束符號0,來退回菜單。
for(p=head;p!=NULL;p=p->next)
if(p->number==number1)
{
cout<<"找到該學(xué)生."<<endl;
cout<<"該學(xué)生的學(xué)號是:"<<p->number<<endl;
cout<<"該學(xué)生的姓名是:"<<p->name<<endl;
cout<<"該學(xué)生的性別是:"<<p->sex<<endl;
cout<<"該學(xué)生的出生日期是:"<<p->birthday.year<<":"<<p->birthday.month<<":"<<p->birthday.day<<endl;
cout<<"該學(xué)生的電話號碼是:"<<p->phone_number<<endl;
cout<<"該學(xué)生來自的省份是:"<<p->province<<endl;
break;//一有找到該學(xué)號的學(xué)生就退出循環(huán)。
}
if(p==NULL)cout<<"數(shù)據(jù)有誤,沒有該學(xué)生的資料."<<endl<<endl;
}
void namesearch(student*head)//按姓名查找
{
student *p;
p=new student;
char name1[20];
cout<<"請輸入該學(xué)生的姓名(輸入0時(shí)結(jié)束):";
cin>>name1;
if(name1=="0")return;
for(p=head;p!=NULL;p=p->next)
if(strcmp(p->name,name1)==0)
{
cout<<"找到該學(xué)生."<<endl;
cout<<"該學(xué)生的學(xué)號是:"<<p->number<<endl;
cout<<"該學(xué)生的姓名是:"<<p->name<<endl;
cout<<"該學(xué)生的性別是:"<<p->sex<<endl;
cout<<"該學(xué)生的出生日期是:"<<p->birthday.year<<":"<<p->birthday.month<<":"<<p->birthday.day<<endl;
cout<<"該學(xué)生的電話號碼是:"<<p->phone_number<<endl;
cout<<"該學(xué)生來自的省份是:"<<p->province<<endl;
break;
}
if(p==NULL)cout<<"數(shù)據(jù)有誤,沒有該學(xué)生的資料."<<endl<<endl;
}
void sexsearch(student*head)//按性別查找。
{
student *p;
p=new student;
char sex1[3];
int count=0;//計(jì)算有多少個男生或者女生。
cout<<"請輸入該學(xué)生的性別(輸入0時(shí)結(jié)束):";
cin>>sex1;
if(strcmp(sex1,"0")==0)return;
for(p=head;p!=NULL;p=p->next)
if(strcmp(p->sex,sex1)==0)
{
count++;
cout<<"找到該學(xué)生."<<endl;
cout<<"該學(xué)生的學(xué)號是:"<<p->number<<endl;
cout<<"該學(xué)生的姓名是:"<<p->name<<endl;
cout<<"該學(xué)生的性別是:"<<p->sex<<endl;
cout<<"該學(xué)生的出生日期是:"<<p->birthday.year<<":"<<p->birthday.month<<":"<<p->birthday.day<<endl;
cout<<"該學(xué)生的電話號碼是:"<<p->phone_number<<endl;
cout<<"該學(xué)生來自的省份是:"<<p->province<<endl<<endl;
break;
}
if(count==0)cout<<"數(shù)據(jù)有誤,沒有該性別的學(xué)生的資料."<<endl<<endl;
}
void add(student*head)//在鏈表末尾增加一個數(shù)據(jù)
{
student *p,*q;
q=new student;
cout<<"請輸入該學(xué)生的學(xué)號(輸入0時(shí)結(jié)束輸入)"<<":";
cin>>q->number;
if(q->number==0){cout<<endl;delete q;return;}
cout<<"請輸入該學(xué)生的姓名"<<":";
cin>>q->name;
cout<<"請輸入該學(xué)生的性別"<<":";
cin>>q->sex;
cout<<"請輸入該學(xué)生的出生日期:";
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<<"請輸入該學(xué)生的電話號碼"<<":";
cin>>q->phone_number;
cout<<"請輸入該學(xué)生來自的省份"<<":";
cin>>q->province;
q->next=NULL;
if(head==NULL){head=q;cout<<"增加數(shù)據(jù)成功."<<endl<<endl;}
else
{
for(p=head;p->next!=NULL;p=p->next);
p->next=q;
cout<<"增加數(shù)據(jù)成功."<<endl<<endl;
}
}
void display(student*head)
{
student*p;
int n=0;
p=head;
while(p!=NULL)
{
n++;
cout<<n<<"學(xué)生的學(xué)號是:"<<p->number<<endl;
cout<<n<<"學(xué)生的姓名是:"<<p->name<<endl;
cout<<n<<"學(xué)生的性別是:"<<p->sex<<endl;
cout<<n<<"學(xué)生的出生日期是:"<<p->birthday.year<<":"<<p->birthday.month<<":"<<p->birthday.day<<endl;
cout<<n<<"學(xué)生的電話號碼是:"<<p->phone_number<<endl;
cout<<n<<"學(xué)生來自的省份是:"<<p->province<<endl<<endl;
p=p->next;
}
}
void display(student* head,fstream& ofile) //保存文件(在文件中顯示),其實(shí)跟在屏幕上顯示一樣的。
{//重載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<<"保存數(shù)據(jù)到文件成功!"<<endl<<endl;
}
student *getvalue(student*head,fstream&infile)//從文件讀取數(shù)據(jù)放到動態(tài)鏈表中。而且不明白為什么不用return head;就無法返回已經(jīng)改變的head.雖然head已經(jīng)是全局變量了。
{
student*p;
infile.seekg(0);
while(!infile.eof())
{
p=new student;
infile>>p->number;
if(p->number<0){delete p;cout<<"從文件讀取數(shù)據(jù)成功。"<<endl;return head;}//不知為什么讀完文件后還讀多個莫名其妙的數(shù)據(jù),只好用這種不高明的方法去掉這個數(shù)據(jù)。
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;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -