int main()
{
LinkQueue Q;
initAddressBook(Q);
int flag=0;
while(flag!=8)
{
printf("※※※※※主※※※※※菜※※※※※单※※※※※\n");
printf("※1、增加联系人 2、删除联系人 ※\n");
printf("※3、修改联系人 4、清空通讯录 ※\n");
printf("※5、查找联系人 6、按组显示 ※\n");
printf("※7、显示全部联系人 8、退出 ※\n");
printf("※※※※※※※※※※※※※※※※※※※※※※※\n");
printf("请输入编号(1-8):\n");
scanf("%d",&flag);
if(flag<1||flag>8)
{
printf("输入错误请重新输入!\n");
continue;
}
else
{
switch(flag){
case 1:
if(enAddressBook(Q))
printf("增加联系人成功!\n");
else
printf("增加联系人失败!\n");
break;
case 2:
if(delPerson(Q))
printf("删除联系人成功!\n");
else
printf("联系人删除失败!\n");
break;
case 3:
if(update(Q))
printf("修改联系人成功!\n");
else
printf("修改联系人失败!\n");
break;
case 4:
if(clearAddressBook(Q))
printf("通讯录已清空!\n");
else
printf("通讯录清空失败!\n");
break;
case 5:
findByName(Q);
break;
case 6:
findByGroup(Q);
break;
case 7:
disAll(Q);
break;
case 8:
system("cls");
break;
}
}
}
printf("※※※※※感谢您使用本系统!※※※※※※\n");
return 0;
}
//初始化通讯录
void initAddressBook(LinkQueue &Q)
{
Q.front=Q.rear=(QNode *)malloc(sizeof(QNode));
if(!Q.front)
exit(1);
else
Q.front->next=NULL;
}
//清空通讯录
bool clearAddressBook(LinkQueue &Q)
{
system("cls");
while(addressBookLength(Q)!=0)
{
QNode *p;
p=Q.front->next;
Q.front->next=p->next;
if(p==Q.rear)
Q.rear=Q.front;
free(p);
}
return true;
}
//通讯录当前人数
int addressBookLength(LinkQueue &Q)
{
int i=0;
QNode *p;
p=Q.front;
if(Q.front==Q.rear)
i=0;
else
{
while(p!=Q.rear)
{
p=p->next;
i++;
}
}
return i;
}
//删除联系人
bool delPerson(LinkQueue &Q)
{
char _name[20];
system("cls");
printf("请输入要删除的联系人的姓名:\n");
scanf("%s",_name);
QNode *p,*q;
if(Q.front==Q.rear)
{
printf("通讯录为空!\n");
return false;
}
else
{
p=Q.front;
q=Q.front->next;
while(q!=Q.rear){
if(strcmp(q->name,_name)==0)
{
p->next=q->next;
free(q);//删除节点
return true;
}
else
{
p=p->next;
q=q->next;
}
}
if((strcmp(q->name,_name)==0))
{
free(q);
Q.front=Q.rear;
return true;
}
else
{
printf("无此联系人!\n");
return false;
}
}
}
//查找联系人
bool findByName(LinkQueue &Q)
{
system("cls");
if(Q.front==Q.rear)
{
printf("手机中无联系人!\n");
return false;
}
else
{
char _name[20];
printf("请输入要查询的联系人姓名:\n");
scanf("%s",_name);
QNode *p;
p=Q.front->next;
while(p->next!=NULL)
{
if(strcmp(p->name,_name)==0)
{
printf("姓名:%s 号码:%s 分组:%s\n",p->name,p->tel,p->group);
return true;
}
p=p->next;
}
if(strcmp(p->name,_name)==0){
printf("姓名:%s 号码:%s 分组:%s\n",p->name,p->tel,p->group);
return true;
}
else
{
printf("查无此人!\n");
return false;
}
}
}
//按组显示联系人
bool findByGroup(LinkQueue &Q)
{
system("cls");
if(Q.front==Q.rear)
{
printf("手机中无联系人!\n");
return false;
}
else
{
char _group[20];
printf("请输入要查询的分组:\n");
scanf("%s",_group);
QNode *p;
p=Q.front->next;
while(p->next!=NULL)
{
if(strcmp(p->group,_group)==0)
{
printf("姓名:%s 号码:%s \n",p->name,p->tel);
}
p=p->next;
}
if(strcmp(p->group,_group)==0){
printf("姓名:%s 号码:%s \n",p->name,p->tel);
return true;
}
else
{
printf("无此分组!\n");
return false;
}
}
}
//增加联系人
bool enAddressBook(LinkQueue &Q)
{
system("cls");
QNode *p;
p=(QNode *)malloc(sizeof(QNode));
if(!p)
return false;
else
{
printf("请输入联系人姓名:\n");
scanf("%s",p->name);
printf("请输入联系人号码:\n");
scanf("%s",&p->tel);
printf("请输入联系人分组:\n");
scanf("%s",p->group);
if(Q.front->next==NULL)
{
Q.front->next=p;
Q.rear=p;
Q.rear->next=NULL;
}
else
{
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
}
return true;
}
}
//修改联系人
bool update(LinkQueue &Q)
{
system("cls");
if(Q.front==Q.rear)
{
printf("手机中无联系人!\n");
return false;
}
else
{
char _name[20];
printf("请输入要修改的联系人姓名:\n");
scanf("%s",_name);
QNode *p;
p=Q.front->next;
while(p->next!=NULL)
{
if(strcmp(p->name,_name)==0)
{
printf("请输入新联系人姓名:\n");
scanf("%s",p->name);
printf("请输入新联系人号码:\n");
scanf("%s",&p->tel);
printf("请输入新联系人分组:\n");
scanf("%s",p->group);
return true;
}
p=p->next;
}
if(strcmp(p->name,_name)==0){
printf("请输入新联系人姓名:\n");
scanf("%s",p->name);
printf("请输入新联系人号码:\n");
scanf("%s",&p->tel);
printf("请输入新联系人分组:\n");
scanf("%s",p->group);
return true;
}
else
{
printf("查无此人!\n");
return false;
}
}
}
//显示所有联系人
void disAll(LinkQueue &Q)
{
system("cls");
int i=0;
QNode *p;
p=Q.front->next;
if(Q.front==Q.rear)
printf("无联系人!\n");
else
{
printf(" 姓名 号码 分组\n");
while(p!=NULL){
printf("%15s %15s %15s\n",p->name,p->tel,p->group);
p=p->next;
}
}
}
谭浩强的《C程序设计》是国内最好的C语言入门书籍。。 多练,多思考是最重要的。 为什么这个题目这样写算法,标准答案跟我写的答案有么区别,它的算法比我的好还是差,好在哪里。。 这个题目除了此解,还有没有其他解。
这个是一个简单的系统,你可以改的完善一点,代码如下:
#include
#include
//person结构定义
struct person
{
char name[8];
char tel[15];
char addr[50];
};//结束定义
char filename[20];
FILE *fp;
void creat();
void ndelete();
void output();
void search();
void search1();
void search2();
void append();
main( )
{
int m;
creat();
while(1)
{
printf("\n\t\t***********欢迎使用电话查询系统**********\n\n");
printf("\n\t\t添加, 请按1");
printf("\n\t\t按姓名查找,请按2");
printf("\n\t\t按号码查找, 请按3");
printf("\n\t\t输出, 请按4");
printf("\n\t\t退出, 请按0\n");
printf("\n\t\t********************************************\n\n");
printf("Please select(0--4):");
scanf("%d",&m);
if(m>=0&&m<=4)
{
switch(m)
{
case 1: append();
break;
case 2: search();
break;
case 3: search1();
break;
case 4: output();
break;
case 0: exit();
}
printf("\n\n操作完毕,请再次选择!");
}
else
printf("\n\n选择错误,请再次选择!");
}
}
/*输入模块creat( ): 电话薄的子函数。*/
void creat()
{
printf("\n请确定电话薄文件名:");
scanf("%s",filename);
if((fp=fopen(filename,"at+"))==NULL)
{
printf("\n不能建电话薄录!");
exit();
}
fprintf(fp,"%-10s%-20s%-50s\n","姓名","电话号码","住址");
fclose(fp);
}
/*输出模块output( ):输出电话薄中联系人的个人信息的子函数*/
void output()
{
struct person one;
if((fp=fopen(filename,"r"))==NULL)
{
printf("\n不能打开通讯录!");
exit();
}
printf("\n\n%20s\n","通 讯 录");
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
printf("%-10s%-20s%-50s",one.name,one.tel,one.addr);
}
fclose(fp);
}
/*添加模块append( ):向电话薄中添加某人的个人信息的子函数*/
void append()
{
struct person one;
search2();
if((fp=fopen(filename,"a"))==NULL)
{
printf("\n不能打开通讯录!");
exit();
}
printf("\n请输入添加的姓名、电话号码及住址\n");
scanf("%s%s%s",one.name,one.tel,one.addr);
fprintf(fp,"%-10s%-20s%-50s\n",one.name,one.tel,one.addr);
fclose(fp);
}
/*查找模块search( ):在电话薄中按姓名查找某人的个人信息的子函数*/
void search()
{
int k=0;
char namekey[8];
struct person one;
printf("\n请输入姓名:");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打开电话薄!");
exit();
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
printf("\n\n已查到,记录为:");
printf("\n%-10s%-20s%-50s",one.name,one.tel,one.addr);
k=1;
}
}
if(!k)
printf("\n\n对不起,电话薄中没有此人的记录。");
fclose(fp);
}
/*查找模块search1( ):在电话薄中按电话号码查找某人的个人信息的子函数*/
void search1()
{
int k=0;
char telkey[15];
struct person one;
printf("\n请输入电话号码:");
scanf("%s",telkey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打开电话薄!");
exit();
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(telkey,one.tel))
{
printf("\n\n已查到,记录为:");
printf("\n%-10s%-2s%-50s",one.name,one.tel,one.addr);
k=1;
}
}
if(!k)
printf("\n\n对不起,电话薄中没有此人的记录。");
fclose(fp);
}
/*查找模块search( ):在电话薄中按姓名查找某人的个人信息的子函数*/
void search2()
{
int k=0;
char namekey[8];
struct person one;
printf("\n请输入添加的姓名:\n");
scanf("%s",namekey);
if((fp=fopen(filename,"rb"))==NULL)
{
printf("\n不能打开电话薄!");
exit();
}
while(!feof(fp))
{
fscanf(fp,"%s%s%s\n",one.name,one.tel,one.addr);
if(!strcmp(namekey,one.name))
{
printf("\n\n查到有同名记录为:");
printf("\n%-10s",one.name);
k=1;
printf("\n\n请改名字后继续完成添加\n");
}
}
fclose(fp);
}
#include
#include
#include
#define NUM 100
struct phoneinfor
{
char name[15];
char sex[8];
char num[15];
char job[15];
char address[30];
}phoneinfor[NUM];
char Initialization()/*界面初始化*/
{
char ch;
printf(" ********Phone Information System********\n1.Show all the phone information\n2.Add phone information\n3.Delete phone information\n4.Search phone information\n5.Exit\nPlease select:");
do
{
ch=getch();
if(ch>='1'&&ch<='5')
{
printf("%c\n",ch);
getchar();
return ch;
}
}while(1);
}
void showall()/*显示所有*/
{
int i=0;
FILE *fp;
system("cls");
if((fp=fopen("C:\\phoneinfo.txt","r"))==NULL)
{
printf("ERROR:cannot open file\n");
getchar();
return;
}
printf("name sex phonenum job address\n");
while(!feof(fp))
{fread(&phoneinfor[i],sizeof(struct phoneinfor),1,fp);
printf("%-15s %-6s %-9s %-15s %-s\n",phoneinfor[i].name,phoneinfor[i].sex,phoneinfor[i].num,phoneinfor[i].job,phoneinfor[i].address);
i++;
if(!i%20)
{
printf("Press any key to continue...");
getch();
}
}
fclose(fp);
printf("Press any key to return");
getch();
return;
}
void addinfor()/*增加数据*/
{
int i=0,b=0;
char ch;
FILE *fp;
do
{
system("cls");
printf("name:");
gets(phoneinfor[i].name);
printf("sex:");
gets(phoneinfor[i].sex);
printf("phonenum:");
gets(phoneinfor[i].num);
printf("job:");
gets(phoneinfor[i].job);
printf("address:");
gets(phoneinfor[i].address);
printf("Saved!Continue?(Y/N)");
do
{
ch=getch();
if(ch=='Y'||ch=='y'||ch=='N'||ch=='n')
{
printf("%c\n",ch);
getchar();
break;
}
}while(1);
i++;
}while(ch=='Y'||ch=='y');
if((fp=fopen("C:\\phoneinfo.txt","a+"))==NULL)
{
printf("ERROR:cannot open file\n");
return;
}
for(b=0;bif(fwrite(&phoneinfor[b],sizeof(struct phoneinfor),1,fp)!=1)
printf("file write error\n");
fclose(fp);
return;
}
void delinfor()/*删除信息*/
{
int i=0,b=0,k;
char delnum[9];
FILE *fp;
system("cls");
printf("Enter the Delete phonenum:");
gets(delnum);
if((fp=fopen("C:\\phoneinfo.txt","r"))==NULL)
{
printf("ERROR:cannot open file\n");
return;
}
while(!feof(fp))
{
fread(&phoneinfor[i],sizeof(struct phoneinfor),1,fp);
i++;
}
fclose(fp);
for(b=0;bif(!strcmp(delnum,phoneinfor[b].num))
{
for(k=b;k
if((fp=fopen("C:\\phoneinfo.txt","w"))==NULL)
{
printf("ERROR:cannot open file\n");
return;
}
for(i=0;i
printf("file write error\n");
fclose(fp);
printf("Delete!Press any key to return...");
getchar();
return;
}
printf("Not Found!Press any key to return...");
getchar();
return;
}
void searchinfor()/*查找信息*/
{
char ch;
char searchinfor[20];
int i=0;
FILE *fp;
system("cls");
printf("search mode:\n1.By name\n2.By phonenum\nChoose:");
do
{
ch=getch();
if(ch=='1'||ch=='2')
{
printf("%c\n",ch);
getchar();
break;
}
}while(1);
if((fp=fopen("C:\\phoneinfo.txt","r"))==NULL)
{
printf("ERROR:cannot open file\n");
return;
}
if(ch=='1')
{
printf("Enter the name:");
gets(searchinfor);
printf("name sex phonenum job address\n");
while(!feof(fp))
{
fread(&phoneinfor[i],sizeof(struct phoneinfor),1,fp);
if(!strcmp(searchinfor,phoneinfor[i].name))
{
printf("%-15s %-6s %-9s %-15s %-s\n",phoneinfor[i].name,phoneinfor[i].sex,phoneinfor[i].num,phoneinfor[i].job,phoneinfor[i].address);
printf("Press any key to return...");
getchar();
fclose(fp);
return;
}
i++;
}
}
if(ch=='2')
{
printf("Enter the phonenum:");
gets(searchinfor);
printf("name sex phonenum job address\n");
while(!feof(fp))
{
fread(&phoneinfor[i],sizeof(struct phoneinfor),1,fp);
if(!strcmp(searchinfor,phoneinfor[i].num))
{
printf("%-15s %-6s %-9s %-15s %-s\n",phoneinfor[i].name,phoneinfor[i].sex,phoneinfor[i].num,phoneinfor[i].job,phoneinfor[i].address);
printf("Press any key to return...");
getchar();
fclose(fp);
return;
}
i++;
}
}
system("cls");
printf("Not Found!Press any key to return...");
fclose(fp);
getchar();
return;
}
void main()
{
char ch;
do
{
system("cls");
ch=Initialization();
if(ch=='1') showall();
if(ch=='2') addinfor();
if(ch=='3') delinfor();
if(ch=='4') searchinfor();
if(ch=='5') break;
}while(1);
system("cls");
printf("Thank you for your use!");
getchar();
}