同學通信錄中學生信息包括學號、姓名、聯繫電話、通訊地址、郵編,請設計程序統計同學錄人數功能

//同學通信錄中學生信息包括學號、姓名、聯繫電話、通訊地址、郵編,請設計程序統計同學錄人數功能。
#include <iostream>
#include <string>
using namespace std;
typedef struct student
{
string stnumber;
string name;
string phonenumber;
string address;
string  Postcode;
struct student *next;
}student;


int init(student** head)
{
*head = new student;
if(*head)
{
(*head)->next = NULL;
return 1;
}
else
return 0;


}
int insert(student* head,student node)
{
student *temp = new student;
if(temp!=NULL)
{
temp->stnumber = node.stnumber;
temp->name = node.name;
temp->phonenumber = node.phonenumber;
temp->address = node.address;
temp->Postcode = node.Postcode;
temp->next = head->next;
head->next = temp;        
return 1;
}
else
{
cout<<"錄入失敗"<<endl;
return 0;
}
}
void show(student *head)
{
student *temp = head->next;
int count = 0;
if(temp)
cout<<"學號"<<"\t"<<"姓名"<<"\t"<<"電話"<<"\t"<<"地址"<<"\t"<<"郵編"<<endl;
else
cout<<"未錄入任何學生"<<endl;
while(temp)
{
cout<<temp->stnumber<<"\t"
<<temp->name<<"\t"
<<temp->phonenumber<<"\t"
<<temp->address<<"\t"
<<temp->Postcode<<endl;
temp = temp->next;
count++;
}
cout<<"學生總數爲"<<count<<endl;
}
void freeList(student *head)
{
student *temp = head->next;
while(temp!=NULL)
{
head->next = temp->next;
delete temp;
temp = head->next;
}
delete head;
}
int main()
{
student *head =NULL;
if(!init(&head))
{
cout<<"初始化失敗!"<<endl;
return -1;
}
student temp;
cout<<"請輸入學生信息(按CTRL+Z停止輸入):\n學號\t姓名\t電話\t地址\t郵編 "<<endl;
while(cin>>temp.stnumber && cin>>temp.name && cin>>temp.phonenumber && cin>>temp.address && cin>>temp.Postcode)
{
if(insert(head,temp))
continue;
else
{
cout<<"輸入失敗!"<<endl;
return -1;
}
}
show(head);
freeList(head);
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章