//文件communication.h
//类Communication的定义
#ifndef _COMMUNICATION_H_
#define _COMMUNICATION_H_
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<string.h>
#include<fstream.h>
class Communication{
public:
Communication(); //默认的构造函数
~Communication();
void set(); //输入 一个学生的所有信息
void lookup(); //输出 学生的信息
void getName(); //输出学生的姓名
int findout(char *p); //查询,是否有符合要求的信息
void chang(); //修改同学的资料
int saveToFile(ostream &outFile);
int loadFromFile(FILE *fptr);
private:
char *number;
char *name;
char *sex;
char *birthday;
char *school;
char *classs;
char *telephone;
char *postcode;
char *QQ;
char *address;
};
struct list
{
Communication *pstud;
list * next;
};
list *create(); //创建链表读入数据,保存到文件
void save(const list *head); //保存,将 链表中 的信息保存到文件中去
void show(list *head); //输出链表中的学生信息
list *cleanUp(list *head); //请空通讯录
void outputAllNames(list *head); //输出所有成员名单
void findOut(list *head); //查询成员
list *addOne(list *head); //增加一个同学的资料
list *addHead(list *head);
list *modifyOne(list *head); //修改一个同学日资料
list *minusOne(list *head); //删除一位成员的资料
list *load(); //从文件中读数据,并创建链表
#endif
//文件communication.cpp
//类communication的实现
#include"communication.h"
Communication::Communication()
{
number=NULL;
name=NULL;
sex=NULL;
birthday=NULL;
school=NULL;
classs=NULL;
telephone=NULL;
postcode=NULL;
QQ=NULL;
address=NULL;
}
Communication::~Communication()
{
delete number;
delete name;
delete sex;
delete birthday;
delete school;
delete classs;
delete telephone;
delete postcode;
delete QQ;
delete address;
}
void Communication::set()
{
number=new char[20];
name=new char[20];
sex=new char[20];
birthday=new char[20];
school=new char[20];
classs=new char[20];
telephone=new char[20];
postcode=new char[20];
QQ=new char[20];
address=new char[20];
cout<<"输入学号、姓名、性别(man或woman):";
cin>>number>>name>>sex;
while(strcmp(sex,"man")!=0&&strcmp(sex,"woman")!=0)
{
delete sex;
sex=new char[20];
cout<<"你的性别错误,请新输入:";
cin>>sex;
}
cout<<"输入出生年月(如199010)、学校、班级、:";
cin>>birthday>>school>>classs;
cout<<"输入电话号码、邮政编码、qq号:";
cin>>telephone>>postcode>>QQ;
cout<<"输入家庭住址:";
cin>>address;
}
void Communication::lookup()
{
cout<<"学号:/t"<<number<<endl;
cout<<"姓名:/t"<<name<<endl;
cout<<"性别:/t"<<sex<<endl;
cout<<"生日:/t"<<birthday<<endl;
cout<<"学校:/t"<<school<<endl;
cout<<"班级:/t"<<classs<<endl;
cout<<"电话:/t"<<telephone<<endl;
cout<<"邮编:/t"<<postcode<<endl;
cout<<"QQ号:/t"<<QQ<<endl;
cout<<"住址:/t"<<address<<endl;
}
void Communication::getName()
{
cout<<name<<" ";
}
int Communication::findout(char *p)
{
char *string[10];
for(int j=0;j<10;j++)
string[j]=new char[20];
strcpy(string[0],number);
strcpy(string[1],name);
strcpy(string[2],sex);
strcpy(string[3],birthday);
strcpy(string[4],school);
strcpy(string[5],classs);
strcpy(string[6],telephone);
strcpy(string[7],postcode);
strcpy(string[8],QQ);
strcpy(string[9],address);
int num;
for(int i=0;i<10;i++)
{
num=strcmp(string[i],p);
if(num==0)
return 1;
}
if(num<0||num>0)
return 0;
}
void Communication::chang()
{
cout<<" 请问你想要修改什么?"<<endl;
cout<<"1.学号 2.姓名 3.性别"<<endl;
cout<<"4.生日 5.学校 6.班级"<<endl;
cout<<"7.电话 8.邮编 9.QQ号"<<endl;
cout<<"10.住址"<<endl;
cout<<"请输入你的选择: ";
int num;
cin>>num;
switch(num)
{
case 1:
delete number;
number=new char[20];
cout<<"请输入修改后的学号:";
cin>>number;
break;
case 2:
delete name;
name=new char[20];
cout<<"请输入修改后的姓名:";
cin>>name;
break;
case 3:
delete sex;
sex=new char[20];
cout<<"请输入修改后的姓别:";
cin>>sex;
break;
case 4:
delete birthday;
birthday=new char[20];
cout<<"请输入修改后的生日:";
cin>>birthday;
break;
case 5:
delete school;
school=new char[20];
cout<<"请输入修改后的学校:";
cin>>school;
break;
case 6:
delete classs;
classs=new char[20];
cout<<"请输入修改后的班级:";
cin>>classs;
break;
case 7:
delete telephone;
telephone=new char[20];
cout<<"请输入修改后的电话:";
cin>>telephone;
break;
case 8:
delete postcode;
postcode=new char[20];
cout<<"请输入修改后的邮编:";
cin>>postcode;
break;
case 9:
delete QQ;
QQ=new char[20];
cout<<"请输入修改后的QQ号:";
cin>>QQ;
break;
case 10:
delete address;
address=new char[20];
cout<<"请输入修改后的住址:";
cin>>address;
break;
default:
break;
}
}
int Communication::saveToFile(ostream &outFile)
{
if(!outFile){
cerr<<"No file found or create file error!";
return 0;
}
outFile<<number<<" "<<name<<" ";
outFile<<sex<<" "<<birthday<<" "<<school<<" ";
outFile<<classs<<" "<<telephone<<" ";
outFile<<postcode<<" "<<QQ<<" "<<address<<" ";
return 1;
}
list *create()
{
int choose;
cout<<"!!! 注意:您可能保存有通讯录,可选择第9项服务查看..."<<endl;
cout<<"提示:重新创建则原来的将被删除,此操作不可恢复。"<<endl;
cout<<"/n是否真要创建新通讯录并删除原有的?"<<endl;
cout<<"1.是 2.否"<<endl;
cout<<"请选择: ";
cin>>choose;
switch(choose)
{
case 1:
break;
case 2:
return NULL;
default:
return NULL;
}
list *temp;
list *head=NULL,*tail=NULL;
int sum;
cout<<"/n输入你要保存信息的学生数量:";
cin>>sum;
if(sum<=0){
cout<<"输入错误!学生数量自动设为1。/n";
sum=1;
}
head=new list;
if(head==NULL)
{
cout<<"没有可以利用的内存!";
return NULL;
}
else{
head->pstud=new Communication;
cout<<"第1个学生:"<<endl;
head->pstud->set();
head->next=NULL;
tail=head;
}
cout<<endl;
for(int i=0;i<sum-1;i++)
{
temp=new list;
if(temp==NULL)
{
cout<<"没有可以利用的内存!";
return head;
}
else{
temp->pstud=new Communication;
cout<<"第"<<i+2<<"个学生:"<<endl;
temp->pstud->set();
temp->next=NULL;
tail->next=temp;
tail=temp;
}
cout<<endl;
}
save(head);
return head;
}
void save(const list *head)
{
ofstream fout;
fout.open("aaa.txt",ios::out);
if(!fout)cerr<<"Open file or create file error."<<endl;
else{
const list *curNode;
curNode=head;
while(curNode!=NULL){
int num;
num=curNode->pstud->saveToFile(fout);
if(num==1)cout<<" *保存成功."<<endl;
curNode=curNode->next;
}
fout<<" zzz";
}
fout.close();
cout<<endl;
}
void show(list *head)
{
try{
list *curNode=head;
if(curNode==NULL||curNode->pstud==NULL)throw 2*3;
int number=1;
while(curNode!=NULL){
cout<<"第"<<number<<"个同学的信息:"<<endl;
curNode->pstud->lookup();
curNode=curNode->next;
number++;
}
}
catch(int e)
{
cout<<" *************************************************"<<endl;
cout<<" ** !!! 请暂时不要选择第"<<e<<"项服务。 **"<<endl;
cout<<" ** 通讯录已经被清空或未创建,请先创建, **"<<endl;
cout<<" ** 或者选择第九项服务,查看是否创建过通讯录。 **"<<endl;
cout<<" *************************************************"<<endl;
return;
}
cout<<endl;
}
list *cleanUp(list *head)
{
list *cur=head;
while(cur)
{
list *temp=cur;
cur=cur->next;
delete temp;
}
ofstream of("aaa.txt",ios::trunc|ios::out);
of<<"zzz";
of.close();
return NULL;
}
void outputAllNames(list *head)
{
try{
list *curNode;
curNode=head;
if(curNode==NULL||curNode->pstud==NULL)throw 2;
cout<<"通讯录中的同学有:"<<endl<<" ";
int number=1;
while(curNode!=NULL){
curNode->pstud->getName();
curNode=curNode->next;
number++;
if(number%5==0)
{
cout<<" "<<endl;
}
}
}
catch(int e)
{
cout<<" *************************************************"<<endl;
cout<<" ** !!! 请暂时不要选择第"<<e<<"项服务。 **"<<endl;
cout<<" ** 通讯录已经被清空或未创建,请先创建, **"<<endl;
cout<<" ** 或者选择第九项服务,查看是否创建过通讯录。 **"<<endl;
cout<<" *************************************************"<<endl;
return;
}
cout<<endl;
}
void findOut(list *head)
{
char *temp=new char[20];
cout<<"请输入你要查询的信息:";
cin>>temp;
list *curNode=head;
int num=0;
while(curNode!=NULL)
{
num=(curNode->pstud->findout(temp));
if(num==1)
{
cout<<" 找到了这个学生:"<<endl;
curNode->pstud->lookup();
break;
}
curNode=curNode->next;
}
if(num==0)
{
cout<<" 不好意思,没有找到这个学生。"<<endl;
cout<<" * 可能通讯录中没有这个学生。"<<endl;
cout<<" * 通讯录已经被清空或未创建。"<<endl;
cout<<" * 请选择第2或第6项服务,检查你是否创建了通讯录。"<<endl;
cout<<" * 或者选择第9项服务,查看文件中是否保存有通讯录。"<<endl;
}
}
list *addHead(list *head)
{
if(head==NULL)
{
head=new list;
if(head==NULL)
{
cout<<"没有可以利用的内存!";
return NULL;
}
else
{
head->pstud=new Communication;
head->pstud->set();
head->next=NULL;
}
return head;
}
list *curNode=new list;
if(curNode==NULL)
{
cout<<"没有可以利用的内存!";
return head;
}
else
{
curNode->pstud=new Communication;
curNode->pstud->set();
curNode->next=head;
head=curNode;
}
return head;
}
list *addTail(list *head)
{
if(head==NULL)
{
head=new list;
if(head==NULL)
{
cout<<"没有可以利用的内存!";
return NULL;
}
else
{
head->pstud=new Communication;
head->pstud->set();
head->next=NULL;
}
return head;
}
list *curNode=head;
list *preNode=NULL;
while(curNode!=NULL)
{
preNode=curNode;
curNode=curNode->next;
}
list *tail=preNode;
curNode=new list;
if(curNode==NULL)
{
cout<<"没有可以利用的内存!";
return head;
}
else
{
curNode->pstud=new Communication;
curNode->pstud->set();
tail->next=curNode;
tail=curNode;
tail->next=NULL;
}
return head;
}
list *addOne(list *head)
{
list *p;
int choose;
cout<<" 请问你想把该同学保存到什么位置?"<<endl;
cout<<" 1. 通讯录首 2.通讯录尾 "<<endl;
cout<<" 请输入你的选择: ";
cin>>choose;
switch(choose)
{
case 1:
p=addHead(head);
break;
case 2:
p=addTail(head);
break;
default:
p=head;
break;
}
save(p);
return p;
}
list *modifyOne(list *head)
{
char *temp=new char[20];
cout<<"请先输入你要修改的同学的信息:";
cin>>temp;
list *curNode=head;
int num=0;
while(curNode!=NULL)
{
num=(curNode->pstud->findout(temp));
if(num==1)
{
cout<<"************************"<<endl;
cout<<"** 找到了这个学生。 **"<<endl;
cout<<"************************"<<endl;
curNode->pstud->chang();
cout<<"修改后:"<<endl;
curNode->pstud->lookup();
break;
}
curNode=curNode->next;
}
if(num==0)
{
cout<<" 不好意思,没有找到这个学生。"<<endl;
cout<<" * 可能通讯录中没有这个学生。"<<endl;
cout<<" * 通讯录已经被清空或未创建。"<<endl;
cout<<" * 请选择第2或第6项服务,检查你是否创建了通讯录。"<<endl;
cout<<" * 或者选择第9项服务,查看文件中是否保存有通讯录。"<<endl;
}
save(head);
return head;
}
list *minusOne(list *head)
{
char *temp=new char[20];
cout<<"请先输入你要删除的同学的一个信息:";
cin>>temp;
list *preNode=NULL;
list *curNode=head;
int num=0;
while(curNode!=NULL)
{
num=(curNode->pstud->findout(temp));
if(num==1)
{
cout<<"************************"<<endl;
cout<<"** 找到了这个学生。 **"<<endl;
cout<<"************************"<<endl;
curNode->pstud->lookup();
if(preNode==NULL)
head=head->next;
else{
preNode->next=curNode->next;
}
break;
}
preNode=curNode;
curNode=curNode->next;
}
if(num==0)
{
cout<<" 不好意思,没有找到这个学生。"<<endl;
cout<<" * 可能通讯录中没有这个学生。"<<endl;
cout<<" * 通讯录已经被清空或未创建。"<<endl;
cout<<" * 请选择第2或第6项服务,检查你是否创建了通讯录。"<<endl;
cout<<" * 或者选择第9项服务,查看文件中是否保存有通讯录。"<<endl;
}
delete curNode;
save(head);
return head;
}
int Communication::loadFromFile(FILE *fptr)
{
number=new char[20];
name=new char[20];
sex=new char[20];
birthday=new char[20];
school=new char[20];
classs=new char[20];
telephone=new char[20];
postcode=new char[20];
QQ=new char[20];
address=new char[20];
fscanf(fptr,"%s",number);
if((strcmp(number,"zzz"))==0)
return 0;
fscanf(fptr,"%s",name);
fscanf(fptr,"%s",sex);
fscanf(fptr,"%s",birthday);
fscanf(fptr,"%s",school);
fscanf(fptr,"%s",classs);
fscanf(fptr,"%s",telephone);
fscanf(fptr,"%s",postcode);
fscanf(fptr,"%s",QQ);
fscanf(fptr,"%s",address);
return 1;
}
list *load()
{
FILE *fptr;
if((fptr=fopen("aaa.txt","r"))!=NULL)
{
list *temp;
list *head=NULL,*tail=NULL;
head=new list;
if(head==NULL)
{
cout<<"没有可以利用的内存!";
return NULL;
}
else{
int um;
head->pstud=new Communication;
um=head->pstud->loadFromFile(fptr);
if(um==1)cout<<" *读取成功。"<<endl;
else{
cout<<"文件中没有通讯录。"<<endl;
delete head;
return NULL;
}
head->next=NULL;
tail=head;
}
while(!feof(fptr))
{
temp=new list;
if(temp==NULL)
{
cout<<"没有可以利用的内存!";
return head;
}
else{
int um;
temp->pstud=new Communication;
um=temp->pstud->loadFromFile(fptr);
if(um==1)cout<<" *读取成功。"<<endl;
else{
delete temp;
break;
}
temp->next=NULL;
tail->next=temp;
tail=temp;
}
}
return head;
}
else{
cout<<"打开文件失败。"<<endl;
return NULL;
}
}
//文件“测试函数.cpp”
//通讯录管理系统的测试
#include"communication.h"
void main()
{
int flag=1;
list *commun=NULL;
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
cout<<"/n/n/n/n/n 欢迎使用我的学生通讯录管理系统!"<<endl;
cout<<"/n/n/n 0.进入"<<endl;
int nnn;
cin>>nnn;
switch(nnn)
{
case 0:
break;
default:
break;
}
system("cls");
while(flag==1){
int num;
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
cout<<"********************** 学生通讯录管理系统 **************************"<<endl;
cout<<"**********************************************************************"<<endl;
cout<<"**** 1.创建通讯录,保存 ****"<<endl;
cout<<"**** 2.查看所有成员的名单 ****"<<endl;
cout<<"**** 3.增加一位成员的资料 ****"<<endl;
cout<<"**** 4.删除一位成员的资料 ****"<<endl;
cout<<"**** 5.修改一位成员的资料 ****"<<endl;
cout<<"**** 6.查看所有成员资料 ****"<<endl;
cout<<"**** 7.查询是否有某位同学的资料 ****"<<endl;
cout<<"**** 8.清空通讯录 ****"<<endl;
cout<<"**** 9.查看已有通讯录 ****"<<endl;
cout<<"**** 0.退出系统 ****"<<endl;
cout<<"**********************************************************************"<<endl;
cout<<"**** 请输入你选择的服务: ";
cin>>num;
cout<<endl;
switch(num){
case 1:
system("cls"); //#include<stdlib.h>
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
commun=create();
break;
case 2:
system("cls");
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
cout<<"输出所有成员的名单:"<<endl;
outputAllNames(commun);
break;
case 3:
{
system("cls");
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
list *p=addOne(commun);
commun=p;
}
break;
case 4:
{
system("cls");
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
list *p=minusOne(commun);
commun=p;
}
break;
case 5:
system("cls");
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
modifyOne(commun);
break;
case 6:
system("cls");
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
cout<<"查看所有成员的资料:"<<endl;
show(commun);
break;
case 7:
system("cls");
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
cout<<"查询:"<<endl;
findOut(commun);
break;
case 8:
system("cls");
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
commun=cleanUp(commun);
cout<<"通讯录已清空。"<<endl<<endl;
break;
case 9:
system("cls");
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
commun=load();
break;
case 0:
system("cls");
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
break;
default:
system("cls");
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
cout<<"你可能输入错误."<<endl;
break;
}
cout<<endl<<endl;
do{
cout<<" ***********************"<<endl;
cout<<" **** 需再次服务? ****"<<endl;
cout<<" ***********************"<<endl;
cout<<" **** 1.是 2.否 ****"<<endl;
cout<<" ***********************"<<endl;
cout<<" **** 请选择: ";
cin>>flag;
if(flag!=1&&flag!=2)
{
flag=3;
cout<<"/n !!! 输入错误,请重新选择。"<<endl;
}
}while(flag==3);
system("cls");
}
cout<<" 08级计算机系 计科四班 周子照 学号2008021406 "<<endl;
cout<<"**********************************************************************"<<endl;
}