C语言,编写一个简单的通讯录管理系统??
{
char name[10];
char address[30];
int ems;
int tel;
struct Txl *next;
};
struct Txl *findTxl(struct Txl * head,char name) //查找
{
struct Txl *q,*p;
if(head == NULL)
printf("找不到该该用户!\n");
else if(head->name == name)
{
printf("姓名:%s\n",head->name);
printf("地址::%s\n",head->address);
printf("邮编:%d\n",head->ems);
printf("联系电话:%d\n",head->tel);
head = head->next;
}
else
{
p = head;
while( (p->name != name) && (p->next != NULL) ) //若都成立,则p到最后一个结点,若不成立,则找到p结点
{
q = p;
p = p->next;
}
if(p->name != name)
printf("找不到该用户!\n");
else
{
printf("姓名:%s\n",head->name);
printf("地址::%s\n",head->address);
printf("邮编:%d\n",head->ems);
printf("联系电话:%d\n",head->tel);
head = head->next;
}
}
return (head);
}
int main(void)
{
int j=1;
char name1[20];
struct Txl*fristTxl,*p;
while(j>0)
{
printf("--------------------------------\n");
printf("1.输入用户信息\n");
printf("2.查找用户\n");
printf("3.列表用户信息\n");
scanf("%d",&j);
if(j==1)
fristTxl = create();
else if(j==2)
{
printf("所查找的用户姓名:");
scanf("%s",&name1);
fristTxl= findTxl(fristTxl,name1);
}
其余略 求大神指点怎么查找??? 展开
#include <string.h>
#include <stdio.h>
#include<stdlib.h>
int cntAddr=0,i,j;
char in[12];
const char * strFmt="\n%d\t%s\t%s\t%s\t%s\n";
typedef struct ADDRESSD {
int ID;
char name[12];
char workPlace[12];
char teleOff[12];
char teleHome[12];
}ADDRESS;
ADDRESS addrTmp[100],adTmp;
int load(ADDRESS adr[]){
FILE *fp;
if((fp=fopen("c:\\txl.txt","r"))!=NULL){
cntAddr=0;
while(!feof(fp)){
fscanf(fp,"%d %s %s %s %s ",&(adr[cntAddr].ID),adr[cntAddr].name,adr[cntAddr].workPlace,adr[cntAddr].teleOff,adr[cntAddr].teleHome);
++cntAddr;
}
}
fclose(fp);
return cntAddr;
};
void list(ADDRESS addr[],int length){
for(i=0;i<length;++i)printf(strFmt,addr[i].ID,addr[i].name,addr[i].workPlace,addr[i].teleOff,addr[i].teleHome);
}
void display(ADDRESS addr[]){
for(i=0;i<cntAddr;++i)addrTmp[i]=addr[i];
for(i=0;i<cntAddr-1;++i)for(j=i+1;j<cntAddr;++j)if(addrTmp[i].ID>addrTmp[j].ID){adTmp=addrTmp[i];addrTmp[i]=addrTmp[j];addrTmp[j]=adTmp;}
for(i=0;i<cntAddr;++i)printf(strFmt,addrTmp[i].ID,addrTmp[i].name,addrTmp[i].workPlace,addrTmp[i].teleOff,addrTmp[i].teleHome);
}
void namesort(ADDRESS addr[],int length){
for(i=0;i<length;++i)addrTmp[i]=addr[i];
for(i=0;i<length-1;++i)for(j=i+1;j<length;++j)if(strcmp(addrTmp[i].name,addrTmp[j].name)>0){adTmp=addrTmp[i];addrTmp[i]=addrTmp[j];addrTmp[j]=adTmp;}
for(i=0;i<length;++i)printf(strFmt,addrTmp[i].ID,addrTmp[i].name,addrTmp[i].workPlace,addrTmp[i].teleOff,addrTmp[i].teleHome);
};
void save(ADDRESS adr[],int length){
FILE *fp;
fp=fopen("c:\\txl.txt","w");
for(i=0;i<cntAddr;++i)fprintf(fp,"%d %s %s %s %s ",adr[i].ID,adr[i].name,adr[i].workPlace,adr[i].teleOff,adr[i].teleHome);
fclose(fp);
};
void unitssort(ADDRESS addr[],int length){
for(i=0;i<length;++i)addrTmp[i]=addr[i];
for(i=0;i<length-1;++i)for(j=i+1;j<length;++j)if(strcmp(addrTmp[i].workPlace,addrTmp[j].workPlace)>0){adTmp=addrTmp[i];addrTmp[i]=addrTmp[j];addrTmp[j]=adTmp;}
for(i=0;i<length;++i)printf(strFmt,addrTmp[i].ID,addrTmp[i].name,addrTmp[i].workPlace,addrTmp[i].teleOff,addrTmp[i].teleHome);
};
void namesearch(ADDRESS addr[],int length){
printf("请输入姓名:\n");
scanf("%s",&in);
for(i=0;i<length;++i)if(strcmp(addr[i].name,in)==0)printf(strFmt,addr[i].ID,addr[i].name,addr[i].workPlace,addr[i].teleOff,addr[i].teleHome);
};
void unitssearch(ADDRESS adr[],int length){
printf("请输入工作单位:\n");
scanf("%s",&in);
for(i=0;i<length;++i)if(strcmp(adr[i].workPlace,in)==0)printf(strFmt,adr[i].ID,adr[i].name,adr[i].workPlace,adr[i].teleOff,adr[i].teleHome);
};
void officenumbersearch(ADDRESS adr[],int length){
printf("请输入办公电话:\n");
scanf("%s",&in);
for(i=0;i<length;++i)if(strcmp(adr[i].teleOff,in)==0)printf(strFmt,adr[i].ID,adr[i].name,adr[i].workPlace,adr[i].teleOff,adr[i].teleHome);
};
void homenumbersearch(ADDRESS adr[],int length){
printf("请输入家庭电话:\n");
scanf("%s",&in);
for(i=0;i<length;++i)if(strcmp(adr[i].teleHome,in)==0)printf(strFmt,adr[i].ID,adr[i].name,adr[i].workPlace,adr[i].teleOff,adr[i].teleHome);
};
int delet(ADDRESS adr[],int length){
printf("要修改第几条记录:\n");
scanf("%d",&j);
for(i=j;i<length-1;++i)adr[i]=adr[i+1];
return --length;
};
void rewrite(ADDRESS adr[],int length){
printf("要修改第几条记录:\n");
scanf("%d",&j);
printf("请输入ID:\n");
scanf("%d",&i);
adr[j].ID=i;
printf("请输入姓名:\n");
scanf("%s",in);
strcpy(adr[j].name,in);
printf("请输入工作单位:\n");
scanf("%s",in);
strcpy(adr[j].workPlace,in);
printf("请输入办公电话:\n");
scanf("%s",in);
strcpy(adr[j].teleOff,in);
printf("请输入家庭电话:\n");
scanf("%s",in);
strcpy(adr[j].teleHome,in);
};
int add(ADDRESS adr[],int length){
printf("请输入ID:\n");
scanf("%d",&i);
adr[length].ID=i;
printf("请输入姓名:\n");
scanf("%s",in);
strcpy(adr[length].name,in);
printf("请输入工作单位:\n");
scanf("%s",in);
strcpy(adr[length].workPlace,in);
printf("请输入办公电话:\n");
scanf("%s",in);
strcpy(adr[length].teleOff,in);
printf("请输入家庭电话:\n");
scanf("%s",in);
strcpy(adr[length].teleHome,in);
return ++cntAddr;
};
void copy(){
FILE *fp,*fc;
printf("请输入新文件名(路径中的反斜杠用\\表示)\n");
scanf("%s",&in);
if((fp=fopen("c:\\txl.txt","r"))!=NULL){
fc=fopen(in,"w");
while(!feof(fp)){
fscanf(fp,"%d %s %s %s %s ",&(adTmp.ID),adTmp.name,adTmp.workPlace,adTmp.teleOff,adTmp.teleHome);
fprintf(fc,"%d %s %s %s %s ",adTmp.ID,adTmp.name,adTmp.workPlace,adTmp.teleOff,adTmp.teleHome);
}
fclose(fc);
}
fclose(fp);
};
int enter(ADDRESS adr[]){
cntAddr=0;
printf("请按次序输入数据,结束按CTRL+z:\n");
while(1){
printf("请输入ID:\n");
if(scanf("%d",&i)==EOF)break;
adr[cntAddr].ID=i;
printf("请输入姓名:\n");
if(scanf("%s",in)==EOF)break;
strcpy(adr[cntAddr].name,in);
printf("请输入工作单位:\n");
if(scanf("%s",in)==EOF)break;
strcpy(adr[cntAddr].workPlace,in);
printf("请输入办公电话:\n");
if(scanf("%s",in)==EOF)break;
strcpy(adr[cntAddr].teleOff,in);
printf("请输入家庭电话:\n");
if(scanf("%s",in)==EOF)break;
strcpy(adr[cntAddr].teleHome,in);
++cntAddr;
}
return cntAddr;
};
void showpaixu(ADDRESS adr[],int length)
{
int choice;
menu1:
system("cls");
printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n");
printf(" 1. 全部显示通讯录信息\n\n");
printf(" 2. 按序号显示通讯录信息\n\n");
printf(" 3. 按姓名排序\n\n");
printf(" 4. 按工作单位排序\n\n");
printf(" 0. 返回上一级菜单\n\n");
printf(" ********************************************************************\n");
printf(" 请选择(0~4)\n ");
scanf("%d",&choice);
switch(choice)
{
case 1:
list(adr,length);
printf(" 请按任意键返回...\n");
system("pause");
break; /*显示全部记录*/
case 2:
display(adr);
printf(" 请按任意键返回...\n");
system("pause");break; /*按序号显示记录*/
case 3:
namesort(adr,length);
printf(" 请按任意键返回...\n");
system("pause");
break; /*按姓名排序*/
case 4:
unitssort(adr,length);
printf(" 请按任意键返回...\n");
system("pause");
break; /*按工作单位排序*/
case 0: return;
}
goto menu1;
}
/*通讯录信息查找功能*/
void chazhao(ADDRESS adr[],int length)
{
int choice;
menu2:
system("cls");
printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n");
printf(" 1. 通过姓名查找\n\n");
printf(" 2. 通过工作单位查找\n\n");
printf(" 3. 通过办公电话查找\n\n");
printf(" 4. 通过家庭电话查找\n\n");
printf(" 0. 返回上一级菜单\n\n");
printf(" ********************************************************************\n");
printf(" 请选择(0~4)\n ");
scanf("%d",&choice);
switch(choice)
{
case 1:
namesearch(adr,length);
printf(" 请按任意键返回...\n");
system("pause");
break; /*按姓名查找记录*/
case 2:
unitssearch(adr,length);
printf(" 请按任意键返回...\n");
system("pause");
break; /*按工作单位查找记录*/
case 3:
officenumbersearch(adr,length);
printf(" 请按任意键返回...\n");
system("pause");
break; /*按办公电话查找记录*/
case 4:
homenumbersearch(adr,length);
printf(" 请按任意键返回...\n");
system("pause");
break; /*按住宅电话查找记录*/
case 0: return;
}
goto menu2;
}
/*通讯录信息变更功能*/
void biangeng(ADDRESS adr[],int length)
{
int choice;
menu3:
system("cls");
printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n");
printf(" 1. 删除记录\n\n");
printf(" 2. 修改记录\n\n");
printf(" 3. 添加记录\n\n");
printf(" 0. 返回上一级菜单\n\n");
printf(" ********************************************************************\n");
printf(" 请选择(0~3)\n ");
scanf("%d",&choice);
switch(choice)
{
case 1:
length=delet(adr,length);
save(adr,length);
printf(" 请按任意键返回...\n");
getchar();
break; /*删除记录*/
case 2:
rewrite(adr,length);
save(adr,length);
printf(" 请按任意键返回...\n");
getchar();
break; /*修改记录*/
case 3:
length=add(adr,length);
save(adr,length);
printf(" 请按任意键返回...\n");
getchar();
break; /*插入记录*/
case 0: return;
}
goto menu3;
}
int wenjian(ADDRESS adr[],int length)
{
int choice;
menu4:
system("cls");
printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n");
printf(" 1. 导入文件到通讯录\n\n");
printf(" 2. 把当前文件复制到新文件上\n\n");
printf(" 0. 返回上一级菜单\n\n");
printf(" ********************************************************************\n");
printf(" 请选择(0~2)\n ");
scanf("%d",&choice);
switch(choice)
{
case 1: length=load(adr);
printf("您已经成功的导入文件到通讯录!\n");
printf(" 请按任意键返回...\n");
system("pause");break;
case 2: copy();
printf("复制成功!\n");
printf(" 请按任意键返回...\n");
system("pause");break;
case 0: return length;
}
goto menu4;
}
/*关于*/
void about()
{
system("color 87");
system("cls");
printf("\n\n\n\n 《通讯录信息管理系统》\n\n\n");
printf(" *****************************************\n\n\n");
printf(" 版权所有(C) 2009-2010 DIY软件\n\n\n");
printf(" 制作人:西北民族大学计算机科学与信息09级计算机3班 王元波\n\n\n");
printf(" *****************************************\n\n\n");
printf(" 请按任意键返回主菜单...");
system("pause");
}
/******主函数开始*******/
void main()
{
int choice;
int length; /*保存记录长度*/
ADDRESS adr[100] ; /*定义结构体数组*/
menu:
system("color f9");
system("cls");
printf("\n\n\n\n *************************通讯录信息管理系统*************************\n\n");
printf(" 请选择以下通讯录管理功能:\n\n");
printf(" 1. 通讯录信息输入\n\n");
printf(" 2. 通讯录信息浏览及排序\n\n");
printf(" 3. 通讯录信息查询\n\n");
printf(" 4. 通讯录信息更改\n\n");
printf(" 5. 通讯录及文件操作\n\n");
printf(" 6. 关于通讯录信息管理系统\n\n");
printf(" 0. 退出通讯录管理系统\n\n");
printf(" ********************************************************************\n");
printf(" 请选择(0~6)\n ");
scanf("%d",&choice);
switch(choice)
{
case 1: length=enter(adr);
save(adr,length);
printf("\n 请按任意键返回...\n");
system("pause");break;/*输入记录*/
case 2: showpaixu(adr,length);break;
case 3: chazhao(adr,length);break;
case 4: biangeng(adr,length);break;
case 5: length=wenjian(adr,length);break;
case 6: about();break;/*关于*/
case 0: return; /*如返回值为则程序结束*/
}
goto menu;
}
拓展资料
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。
尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
#define maxlen 15
struct persons
{
int num;
char name[20];
char e_addr[20];
char tel_no[15];
char sim_no;
char arch;
}persons[maxlen];
typedef struct lnode{
int num;
char name[20];
char e_addr[20];
char tel_no[15];
char sim_no;
char arch;
struct lnode *next;
}listnode,*linklist;
linklist head=NULL,r=NULL;
listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7,*p8,*p9;
int i;
char name1[10],ch;char tel_no1[15];char arch1;char sim_no1;char e_addr1[20];
char s1[20];
FILE *fp;
void creat()
{
int j;
long k;
fp=fopen("数据文件.txt","r t");
if(fp!=NULL)
{for(i=0;i<=maxlen;i++ )
{ j=fgetc(fp);
if(j==EOF)
return;
k=i;
fseek(fp,k*sizeof(struct persons),0);
fread(&persons[i],sizeof(struct persons),1,fp);
s=(linklist)malloc(sizeof(listnode));
s->num=persons[i].num;
strcpy(s->name,persons[i].name);
strcpy(s->e_addr,persons[i].e_addr);
strcpy(s->tel_no,persons[i].tel_no);
s->sim_no=persons[i].sim_no;
s->arch=persons[i].arch;
if(head==NULL)
{head=s;r=head;head->next=NULL;}
else
{r->next=s;
r=s;r->next=NULL;
}}fclose(fp);}
else
{ fp=fopen("数据文件.txt","w");
i=1;
}
}
void ShowA()
{p1=head;
while(p1!=NULL)
{if(p1->arch=='A')
{
cout<<endl;
cout<<p1->name<<endl;
cout<<p1->tel_no<<endl;
cout<<p1->sim_no<<endl;}
p1=p1->next;continue;}
}
void ShowB()
{p1=head;
while(p1!=NULL)
{if(p1->arch=='B')
{
cout<<endl;
cout<<p1->name<<endl;
cout<<p1->tel_no<<endl;
cout<<p1->sim_no<<endl;}
p1=p1->next;continue;}
}
void ShowC()
{p1=head;
while(p1!=NULL)
{if(p1->arch=='C')
{
cout<<endl;
cout<<p1->name<<endl;
cout<<p1->tel_no<<endl;
cout<<p1->sim_no<<endl;}
p1=p1->next;continue;}
}
void Show()
{char ch1;
printf(" Which arch do you want to show?\n");
cout<<" A:同学同事"<<"B:家人"<<"C:其他"<<endl;
cout<<"请选择:"<<endl;
cin>>ch1;
switch(ch1)
{case'A':ShowA();break;
case'B':ShowB();break;
case'C':ShowC();break;
default:cout<<"error"<<endl;
}
}
void Delete()
{ printf("\n\n\t 请输入要删除用户的姓名:");
cin>>name1;
p4=head;
if(strcmp(p4->name,name1)==0)
{ p4=p4->next;
head=p4;
}
else
{ while(strcmp(p4->next->name,name1)!=0)
p4=p4->next;
p5=p4->next;
p4->next=p5->next;
free(p5);
}
}
void Input()
{ s=(linklist)malloc(sizeof(listnode));
printf("\n\n\t 请输入该用户的信息:");
cout<<"name:";
cin>>s->name;
cout<<"tel_no:";
cin>>s->tel_no;
cout<<"sim_no:";
cin>>s->sim_no;
cout<<"e_addr:";
cin>>s->e_addr;
cout<<"arch:";
cin>>s->arch;
if(head==NULL)printf("\n\n");
else
{p8=head;
while(p8!=NULL&&strcmp(s->name,p8->name)!=0&&strcmp(s->tel_no,p8->tel_no)!=0)
p8=p8->next;
if(p8!=NULL)
{printf(" 您添加的用户已存在!");
free(s);}}
if(head==NULL)
{head=s;head->next=NULL;}
else
{r->next=s;
r=s;r->next=NULL;}
}
void Alter()
{
printf("\n\n\t 请输入姓名:");
cin>>name1;
p3=head;
while(p3!=NULL&&strcmp(name1,p3->name)!=0)
p3=p3->next;
if(p3==NULL)
printf("\n\n\t 您选择的用户不存在!");
else
{ printf("\n\n\t 请输入该用户的新信息!");
cout<<"name:";
cin>>name1;
cout<<"tel_no:";
cin>>tel_no1;
cout<<"e_addr:";
cin>>e_addr1;
cout<<"sim_no:";
cin>>sim_no1;
cout<<"arch:";
cin>>arch1;
while(p9!=NULL&&strcmp(name1,p9->name)!=0&&strcmp(tel_no1,p9->tel_no)!=0)
p9=p9->next;
if(p9==NULL)
{strcpy(p3->name,name1);
strcpy(p3->tel_no,tel_no1);
strcpy(p3->e_addr,e_addr1);
p3->sim_no=sim_no1;
p3->arch=arch1;
}
else
printf(" 您添加的用户已存在!");
}
}
void Dial()
{int a;char b;
cout<<"1.拨叫用户:"<<endl;
cout<<"2.使用单键拨号:"<<endl;
cin>>a;
p0=head;
if(a==1)
{cout<<" 请输入拨叫用户的姓名:";
cin>>name1;
while(strcmp(name1,p0->name)!=0&&p0!=NULL)
p0=p0->next;
if(p0==NULL)
cout<<" 您选择的用户不存在!";
else
/*for(c=0;c<15;c++)
{cout<<endl;
cout<<(*p0).tel_no[c];}*/
{cout<<endl;
cout<<p0->tel_no;
cout<<endl;}
}
else
{cout<<"请输入单键号码:";
cin>>b;
while(p0!=NULL&&b!=p0->sim_no)
p0=p0->next;
if(p0==NULL)
cout<<" 您输入的单键号码不存在!";
else
{cout<<endl;
cout<<p0->name<<endl;
cout<<p0->tel_no;
cout<<endl;}
}
}
void Save()
{ int j;
fp=fopen("数据文件.txt","w");
for(p2=head,j=0;p2!=NULL;j++ ,p2=p2->next)
{
strcpy(persons[j].name,p2->name);
strcpy(persons[j].tel_no,p2->tel_no);
persons[j].sim_no=p2->sim_no;
strcpy(persons[j].e_addr,p2->e_addr);
persons[j].arch=p2->arch;
fwrite(&persons[j],sizeof(struct persons),1,fp);
}
}
void main()
{ creat();
do
{
printf(" **********************************************************");
printf("\n\n\n\n\t 欢迎来到通讯录!");
printf("\n\n\t\t 请选择操作:");
printf("\n\t\t1. 显示通讯录");
printf("\n\t\t2. 删除通讯录");
printf("\n\t\t3. 添加通讯录");
printf("\n\t\t4. 编辑通讯录");
printf("\n\t\t5. 拨号功能");
printf("\n\t\t6. 保存刚才的操作并退出\n");
printf(" **********************************************************");
printf("\n\n\n");
printf("\t 选择您想要的操作:");
cin>>ch;
switch(ch)
{ case '1': Show();
break;
case '2': Delete();
break;
case '3': Input();
break;
case '4': Alter();
break;
case '5': Dial();
break;
case '6': Save();
fclose(fp);
exit(0);
break;
default:
printf("\n\t*********************************\n");
printf("\n\t The num should 1-6!!! \n");
printf("\n\t**********************************");
break;
}
}while(1);
}
1、运行后,先输入用户信息(1回车,然后输入一条用户记录),我猜会有提示,按提示输入姓名、地址、邮编、联系电话
2、(重复若干次1步骤,添加若干用户记录后),再查找一条用户记录(2回车,然后输入一个刚才输入过的用户姓名)