?? 學生通訊錄.cpp
字號:
#include<iostream.h>
#include<stdio.h>
#include"student.h"
#include<string.h>
#include<fstream.h>
Link::Link(void)
{
head=new LinkNode;
head->next=NULL;
}//////////////////////// 函數功能:將鏈頭指針初始化為帶表頭結點的空鏈
Link::~Link(void)
{
struct LinkNode *p;
while(head!=NULL)
{
p=head;
head=head->next;
delete p;
}
}///////////////// 函數功能:將鏈表所有結點的空間釋放(包括表頭結點)
void Link::HeadCreate(int n)
{
struct LinkNode *p, *h=NULL;
for(int i=0;i<n;i++)
{
p=new LinkNode;
p->data=InputStuInfo();
p->next=h;
h=p;
};
head->next=h;
}/////////////// 函數功能1:用頭插入建立鏈表方法生成一個具有n個結點的鏈表
int Link::Insert(int i ,DataType x)
{
struct LinkNode *temp;
struct LinkNode *p,*q;
if ( i<=0)
{
cout<<"\n the position is error\n\n";
return 0; // error
}
int j=1;
q=head; p=head->next;
while(j <i && p!=NULL)
{
q=p; p=p->next;
j++;
}
temp=new LinkNode;
temp->data=x;
temp->next=p;
q->next=temp;
return 1; // ok
}///////////////////////////// 函數功能:將一個新值插入到某個位置的結點之后上,如果超過總結點個數,則做尾插入
DataType InputStuInfo(void)
{
DataType x;
cout<<"\n姓名:";
cin>>x.stname;
cout<<"\n學號:";
cin>>x.stno;
cout<<"\n出生日期:";
cin>>x.birthday;
cout<<"\n性別(0=男,1=女):";
cin>>x.sex;
cout<<"\n聯系電話:" ;
cin>>x.tel;
cout<<"\n地址:";
cin>>x.address;
return x;
}//////////// 函數功能2:從鍵盤中接受用戶輸入學生的信息
int Link::Delete(int i)
{
struct LinkNode *q,*p;
int j=1;
if ( i<=0)
{
cout<<"\n the position is error\n\n";
return 0;
}
q=head; p=head->next;
while(j <i && p!=NULL)
{
q=p;
p=p->next;
j++;
}
if (p==NULL)
{
cout<<"the position is out of range\n\n";
return 0;
}
q->next=p->next;
delete p;
return 1;
}//////////////////////// 函數功能3:將鏈表中某個位置上的結點刪除
void Link::ReadFile(void)
{
ifstream fp;
DataType x;
LinkNode *p;
fp.open("student.dat",ios::in|ios::binary);
head=new LinkNode;
head->next=NULL;
while(fp.read((char*)&x,sizeof(x)))
{
p=new LinkNode;
p->data=x;
p->next=head->next;
head->next=p;
}
fp.close();
}//////////////////函數功能4:從文件中讀取所有的學生信息并用頭插入方法建立鏈表
void Link::WriteFile(void)
{
LinkNode *p;
ofstream fp;
DataType x;
fp.open("student.dat",ios::out|ios::binary);
p=head->next;
while(p!=NULL)
{
x=p->data;
fp.write((char *) &x,sizeof(x));
p=p->next;
}
fp.close();
}///////////////函數功能5:將所有的學生信息保存到文件中
LinkNode *Link::Locate(void)
{
char StuNo[10];
LinkNode *p;
cout<<endl<<"please input the NO to search:";
cin>>StuNo;
p=head->next;
while(p!=NULL)
{
if (strcmp(p->data.stno,StuNo)==0)
return p;
else
p=p->next;
}
return NULL;
}/////////////函數功能6:按學號進行查找
void Link::Print(void)
{
struct LinkNode *p=head->next;
cout<<"\n The data in the link are:\n";
while ( p!=NULL)
{
OutputStuInfo(p->data);
p=p->next;
}
cout<<"\n\n\n";
}/////////////// 函數功能7:從頭到尾的將鏈表中的所有結點一個一個輸出來(不包括表頭結點)
void OutputStuInfo(DataType x)
{
cout<<"name:"<<x.stname;
cout<<" No:"<<x.stno;
cout<<" birthday:"<<x.birthday;
cout<<" sex(0=boy,1=girl):"<<x.sex;
cout<<" tel:"<<x.tel;
cout<<" address:"<<x.address<<endl;
}///////////// 函數功能:將學生信息輸出到計算機屏幕上
void main()
{
ofstream fop("student.dat");
Link link1;
int finished=0;
int choice , n ,pos;
DataType x;
while ( !finished )
{
cout<<"\n\n*********Menu*********\n";
cout<<"1:新建學生通訊錄\n";
cout<<"2:插入學生信息\n";
cout<<"3:刪除學生信息\n";
cout<<"4:讀取通訊錄信息\n";
cout<<"5:向文件寫入學生通訊錄信息\n";
cout<<"6:查詢\n";
cout<<"7:在屏幕中輸出全部學生信息\n";
cout<<"8:退出\n";
cout<<"請選擇(1-8):";
cin>>choice;
switch(choice)
{
case 1:
cout<<"\nplease enter the number of nodes(insert in head):"; //調用頭插入建立鏈表
cin>>n;
link1.HeadCreate(n);
link1.Print();
break;
case 2:
cout<<"\nplease enter the position to insert:"; //調用鏈表插入函數
cin>>pos;
cout<<"\n please enter the data to insert\n";
x=InputStuInfo();
if ( link1.Insert(pos,x)==1)
link1.Print();
break;
case 3:
cout<<"\nplease enter the position to delete:"; //調用鏈表刪除函數
cin>>pos;
if ( link1.Delete(pos)==1 )
link1.Print();
break;
case 4:
link1.ReadFile(); //調用文件讀取函數
break;
case 5:
link1.WriteFile(); //調用文件寫入函數
break;
case 6:
LinkNode *p;
p=link1.Locate(); //調用按學號查找函數
if (p==NULL)
cout<<"不存在該學號的學生信息"<<endl;
else
OutputStuInfo(p->data);
break;
case 7:
link1.Print(); //調用鏈表輸出函數
break;
case 8:
finished=1; //結束程序
break;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -