C语言程序通讯录完美版

合集下载

C语言通讯录

C语言通讯录

C语言通讯录#include <stdio.h>#include <stdlib.h>#include <string.h>#include <conio.h>#include <windows.h>#define N 50struct Telephone{char name[20];char addrass[20];char mail[20];char telephone[20];struct Telephone *next;}student[N];struct Node{char name[20];char addrass[20];char mail[20];char telephone[20];}Node[N];typedef struct Telephone TEL;TEL *head = NULL;void Showmenu(); /*菜单*/void menu2(); /*菜单2*/void menu3(); /*菜单3*/void Add(); /*添加条目*/void Print(); /*输出条目*/void Findname(); /*查找条目(按姓名)*/ void Findtelephone(); /*查找条目(按电话)*/ void Alter(); /*修改条目*/void Del(); /*删除信息(按姓名)*/ void Del1(); /*删除信息(按手机号)*/ void sort(); /*按姓名排序*/void Saveandfree(); /*保存到文件*/void Open(); /*打开文件*/main(){char ch;Open(); /*打开文件*/while(1){Showmenu(); /*显示菜单*/scanf(" %c",&ch);switch(ch){case '1': Add(); /*添加条目*/system("cls");break;case '2': Print(); /*输出条目*/break;case '3':system("cls"); /*查找条目1.按姓名*/menu2();break;case '4': /*输出删除后的结果*/system("cls");menu3();break;case '5': Alter(); /*修改信息*/system("cls");break;case '6': /*sort();*/system("cls");break;case '0': Saveandfree(); /*保存并释放内存*/exit(0); /*退出*/break;default:printf("选择错误!");break;}}}/*菜单*/void Showmenu(){system("color 2b");printf("\n\n\n\n\t***************************通讯录*************************\n");printf("\t- \t1.输入信息-\n");printf("\t- \t2.输出信息-\n");printf("\t- \t3.查找信息-\n");printf("\t- \t4.删除信息-\n");printf("\t- \t5.修改信息-\n");printf("\t- \t6.排序-\n");printf("\t- \t0.保存并退出! -\n");printf("\t**********************************************************\n");printf(" \t请选择:\n");}/*菜单2*/void menu2(){int c1;system("color 3d");printf("\n***************************通讯录*************************\n");printf("- \t1.按姓名查找-\n");printf("- \t2.按手机号查找-\n");printf("**********************************************************\n");printf(" \t请选择:\n");scanf("%d", &c1);switch(c1){case 1: Findname();getch();system("cls");break;case 2: Findtelephone();getch();system("cls");break;default:printf("选择错误!");}}/*菜单3*/void menu3(){int c2;system("color 1b");printf("\n***************************通讯录*************************\n");printf("- \t1.按姓姓名删除-\n");printf("- \t2.按手机号删除-\n");printf("**********************************************************\n");printf(" \t请选择:\n");scanf("%d", &c2);switch(c2){case 1: Del();getch();system("cls");break;case 2: Del1();getch();system("cls");break;default:printf("选择错误!\n");}}/*添加条目*/void Add(){TEL * p1 = NULL,* p2 = NULL;p1 = (TEL *) malloc(sizeof(TEL)); /*申请结点*/printf("请输入姓名:\n"); /*添加信息*/scanf("%s", p1->name);printf("请输入地址:\n");scanf("%s", p1->addrass);printf("请输入邮箱:\n");scanf("%s", p1->mail);printf("请输入手机:\n");scanf("%s", p1->telephone);p1 -> next = NULL; /*保存到链表*/ if (head == NULL){head = (TEL *) malloc(sizeof(TEL)); /*申请空间*/head -> next = p1;}else{for (p2 = head; p2 -> next != NULL; p2 = p2 -> next); /*找到结点尾*/p2->next=p1;}printf("信息已被成功添加!");}/*输出学生信息*/void Print(){TEL *p = NULL;if (head == NULL){printf("此通讯录内无记录,请添加后使用此功能!\n");return;}printf("*************************用户信息************************\n"); /*输出信息*/for (p = head -> next; p != NULL; p=p -> next){printf("姓名:\t%s\n",p->name);printf("地址:\t%s\n",p->addrass);printf("邮箱:\t%s\n",p->mail);printf("手机:\t%s\n",p->telephone);}printf("********************************************************\n");getch();system("cls");}/*查找信息1.按姓名*/void Findname(){TEL *p;int flag;char findname[20];printf("请输入您想查找的姓名:\n");scanf("%s", findname);printf("**************************通讯录***************************\n");flag = 0;for (p = head -> next;p != NULL;p = p -> next){if(strcmp(p -> name, findname) == 0){flag ++;printf("姓名:\t%s\n", p -> name);printf("地址:\t%s\n", p -> addrass);printf("邮箱:\t%s\n", p -> mail);printf("电话:\t%s\n", p -> telephone);}}if (flag == 0){printf("无此记录!");}}/*查找信息2、按电话*/void Findtelephone(){TEL *p;int flag;char findname[20];printf("请输入您想查找的电话:\n");scanf("%s", findname);printf("**************************查找结果***************************\n");flag = 0;for (p = head -> next;p != NULL;p = p -> next){if(strcmp(p -> telephone, findname) == 0){flag ++;printf("姓名:\t%s\n", p -> name);printf("地址:\t%s\n", p -> addrass);printf("邮箱:\t%s\n", p -> mail);printf("电话:\t%s\n", p -> telephone);}}if (flag == 0){printf("无此记录!!");}}/*修改信息*/void Alter(){TEL *p1(TEL *head);{struct Telephone *p1;char flag = 0;char name[20];printf("请输入您想修改的姓名:\n");scanf("%s", name);p1 = head;while (p1 != NULL){if(strcmp(p1 -> name,name) == 0){printf("请输入新的姓名:\n"); /*添加新信息*/scanf("%s", p1 -> name);printf("请输入新的地址:\n");scanf("%s", p1 -> addrass);printf("请输入新的邮箱地址:\n");scanf("%s", p1 -> mail);printf("请输入新的手机号:\n");scanf("%s", p1 -> telephone);flag = 1;}p1 = p1 -> next;}}printf("修改成功!");}/*删除信息*/void Del(){char findname[20]; /*先查找后删除*/ TEL *p = head, *pr = head;printf("请输入您要删除人的姓名:\n");scanf(" %s", findname);if (head == NULL){printf("No node!\n");return;}while ((strcmp(p -> name, findname) != 0) && p -> next != NULL){pr = p;p = p -> next;}if(strcmp(p -> name, findname) == 0){if(p == head)head = p -> next; /*删除头结点*/elsepr -> next = p -> next; /*删除中间或尾结点*/free(p);}elseprintf("没有您想要删除的信息\n");printf("删除成功!");Print();}/*按手机号删除*/void Del1(){char findname[20]; /*先查找后删除*/ TEL *p = head, *pr = head;printf("请输入您要删除人的电话:\n");scanf(" %s", findname);if (head == NULL){printf("No node!\n");return;}while ((strcmp(p -> telephone, findname) != 0) && p -> next != NULL){pr = p;p = p -> next;}if(strcmp(p -> telephone, findname) == 0){if(p == head)head = p -> next; /*删除头结点*/elsepr -> next = p -> next; /*删除中间或尾结点*/free(p);}elseprintf("没有您想要删除的信息\n");printf("删除成功!");Print();}/*保存链表信息到文件并释放内存空间*/void Saveandfree(){TEL *p = NULL;FILE *fp;char *Book = "people.txt";if(head == NULL){printf("\n无记录\n");return;}elsep = head -> next;if((fp = fopen(Book,"w+")) == NULL){printf("\n无法打开文件!\n");return;}while(p != NULL) /*保存信息*/{fprintf(fp, "%s %s %s %s ", p -> name,p -> addrass, p -> mail, p -> telephone);p = p -> next;}printf("保存成功!");fclose(fp);/*释放链表空间*/for(; head -> next != NULL; ){p = head -> next;head -> next = head -> next -> next;free(p);}free(head);}/*文件信息输出到链表*/void Open(){FILE *fp;TEL *p1 = NULL, *p2 = NULL, *temp = NULL;if((fp = fopen("people.txt", "r+")) == NULL){printf("\n*************************通讯录************************\n");return;}head = (TEL *) malloc(sizeof(TEL));head -> next = NULL;temp = p2 = head;while(! feof(fp)) /*循环读取*/{p1 = (TEL *) malloc(sizeof(TEL));temp = p2;p2 -> next = p1;p2 = p1;fscanf(fp, "%s%s%s%s", p1 -> name, p1 -> addrass, p1 -> mail, p1 -> telephone);}temp -> next = NULL;fclose(fp); /*关闭文件*/}/*按姓名排序*//*void sort(){TEL *p1, *p2, *temp;temp = (TEL *)malloc(sizeof(TEL));p1 = head;for (;p1 -> next != NULL; p1 = p1 -> next){p2 = p1 -> next;for (;p2 -> next != NULL; p2 = p2 -> next){if (strcmp(p1 -> name, p2 -> name) > 0){strcpy(p1 -> name, temp -> name);strcpy(p1 -> addrass, temp -> addrass);strcpy(p1 -> mail, temp -> mail);strcpy(p1 -> telephone, temp -> telephone);strcpy(p2 -> name, p1 -> name);strcpy(p2 -> addrass, p1 -> addrass);strcpy(p2 -> mail, p1 -> mail);strcpy(p2 -> telephone, p1 -> telephone);strcpy(temp -> name, p2 -> name);strcpy(temp -> addrass, p2 -> addrass);strcpy(temp -> mail, p2 -> mail);strcpy(temp -> telephone, p2 -> telephone);}if (p2 -> next == NULL && (strcmp(p1 -> name, p2 -> name) > 0)){strcpy(p1 -> name, temp -> name);strcpy(p1 -> addrass, temp -> addrass);strcpy(p1 -> mail, temp -> mail);strcpy(p1 -> telephone, temp -> telephone);strcpy(p2 -> name, p1 -> name);strcpy(p2 -> addrass, p1 -> addrass);strcpy(p2 -> mail, p1 -> mail);strcpy(p2 -> telephone, p1 -> telephone);strcpy(temp -> name, p2 -> name);strcpy(temp -> addrass, p2 -> addrass);strcpy(temp -> mail, p2 -> mail);strcpy(temp -> telephone, p2 -> telephone);}}}printf("\n*************************排序后************************\n");Print();getch();}*/排序未实现。

c语言程序设计之通讯录

c语言程序设计之通讯录

#include<stdio.h>#include<string.h>#include<stdlib.h>int mimayanzheng(char a[],char b[]);void luru(int n);void xinxichaxun(struct txl_type mulu[],int n); int chazhao(struct txl_type mulu[],int n,int j); void xiugai(struct txl_type mulu[],int n);void shanchu(struct txl_type mulu[],int n);void paixu(struct txl_type mulu[],int n);void baocun(struct txl_type mulu[],int n);struct txl_type{ char name[15] ;char sex[5] ;char age[4] ;char phnum[15] ;char email[20];char adr[30] ;};void main(){char a[8]="1234567",b[8];int i,j;FILE *fp;for(i=0;i<3;i++){printf("请输入密码并按回车键确认\n");if(mimayanzheng(a,b)==1){printf("密码正确,成功");printf("进入系统\n");break;}elseprintf("密码错误请重新输入\n");}if(i==3){printf("退出程序\n");goto loop;}if((fp=fopen("tx1.txt","a"))==NULL)goto loop;fclose(fp);for(;;){struct txl_type mulu[500],*p;char ch;int n;FILE *fp;fp=fopen("tx1.txt","r");n=0;p=mulu;while((ch=fgetc(fp))!=EOF){fscanf(fp,"%s%s%s%s%s%s",p->name,p->sex,p->age,p->phnum,p->email,p->adr);p++;n++;};do{printf("1.信息录入 2.信息修改 3.信息查询4信息删除5信息排序6信息保存7退出\n");scanf("%d",&j);fflush(stdin);}while(j!=1&&j!=2&&j!=3&&j!=4&&j!=5&&j!=6&&j!=7);switch(j){case 1:printf("进入信息录入系统\n");luru(n);break;case 2:printf("进入信息修改系统\n");xiugai(mulu,n);break;case 3:printf("进入信息查询系统\n");xinxichaxun(mulu,n);break;case 4:printf("进入信息删除系统\n");shanchu(mulu,n);break;case 5:printf("进入信息排序系统\n");paixu(mulu,n);break;case 6:printf("进入信息保存系统\n");baocun(mulu,n);break;case 7:goto loop;}}loop:;}mimayanzheng(char a[],char b[]){int i;scanf("%7s",b);for(i=0;i<7;i++){ if(a[i]!=b[i])return 0;}return 1;}void luru(int n){struct txl_type t;int i;FILE *fp;fp=fopen("tx1.txt","a");i=1;while(i==1){fflush(stdin);printf("请输入联系人姓名\n");scanf("%s",);fflush(stdin);printf("请输入联系人性别\n");scanf("%s",t.sex);fflush(stdin);printf("请输入联系人年龄\n");scanf("%s",t.age);fflush(stdin);printf("请输入联系人手机号\n");scanf("%s",t.phnum);fflush(stdin);printf("请输入联系人邮箱\n");scanf("%s",t.email);fflush(stdin);printf("请输入联系人地址\n");scanf("%s",t.adr);fflush(stdin);fprintf(fp,"\n");fprintf(fp,"%12s %3s %4s %10s %6s %6s", ,t.sex ,t.age ,t.phnum ,t.email,t.a dr);printf("------------------------------------------------\n");n++;printf("新增联系人成功还可以添加%d个是否继续添加?\n",(500-n));printf(" 1.是 2. 否\n");scanf("%d",&i);fflush(stdin);}printf("已有%d名联系人\n",n);printf("还可以新增%d个联系人人\n",(500-n));fclose(fp);}void xinxichaxun(struct txl_type mulu[],int n){struct txl_type *p;int k=1,j;while(k==1){p=mulu;printf("请选择:1.用姓名查找 2.用电话号码查找\n");scanf("%d",&j);fflush(stdin);chazhao(p,n,j);/*调用查找程序*/printf("\n");printf(" 查找结束,是否继续查找?\n");printf(" 1.是 2.否\n");scanf("%d",&k);fflush(stdin);}}int chazhao(struct txl_type *p,int n,int j){int i,m[10],a=0,b;char keyName[10],keyphnum[15];if(j==1){printf("请输入姓名:\n");scanf("%s", keyName);fflush(stdin);printf("查找结果:\n");for(i=0;i<n;i++){if(strcmp(keyName,p->name)==0){printf("联系人%d: name:%s age:%s phnum:%s email:%s adr:%s\n",(a+1),p->name,p->age,p->phnum,p->email,p->adr);a++;m[a]=i;}p++;}}else if(j==2){printf("请输入电话号码:\n");scanf("%s", keyphnum);fflush(stdin);printf("查找结果:\n");for(i=0;i<n;i++){if(strcmp(keyphnum,p->phnum)==0){printf("联系人%d: name:%s age:%s phnum:%s email:%s adr:%s\n",(a+1),p->name,p->age,p->phnum,p->email,p->adr);a++;m[a]=i;}p++;}}if(a==0){printf(" 该联系人不存在!\n");return i;}else{printf("共找到以上%d个联系人\n",a);printf("请选择你要找的联系人(输入该联系人序号)\n");scanf("%d",&b);}return m[b];}void xiugai(struct txl_type mulu[],int n){struct txl_type *p,k;int m,i,t,j=1;FILE *fp;p=mulu;t=chazhao(p,n,j);/*调用查找程序*/if(t>=n);else{printf("请对该联系人资料进行修改进行修改\n");printf("请输入修改后联系人姓名\n");scanf("%s",);fflush(stdin);printf("请输入修改后联系人性别\n");scanf("%s",k.sex);fflush(stdin);printf("请输入修改后联系人年龄\n");scanf("%s",k.age);fflush(stdin);printf("请输入修改后联系人手机号\n");scanf("%s",k.phnum);fflush(stdin);printf("请输入修改后联系人邮箱\n");scanf("%s",k.email);fflush(stdin);printf("请输入修改后联系人地址\n");scanf("%s",k.adr);fflush(stdin);printf("修改前联系人资料:name:%s age:%s phnum:%s email:%s adr:%s\n",mulu[t].name,mulu[t].age,mulu[t].phnum,mulu[t].email,mulu[t].adr);printf("修改后联系人资料:name:%s age:%s phnum:%s email:%s adr:%s\n",,k.age,k.phnum,k.email,k.adr);printf("是否保存对联系人的修改? 1.是 2.否\n");scanf("%d",&m);if(m==1){mulu[t]=k;if((fp=fopen("tx1.txt","w"))!=NULL){for(i=0;i<n;i++){fprintf(fp,"\n");fprintf(fp,"%12s %3s %4s %10s %6s %6s",mulu[i].name ,mulu[i].sex ,mulu[i].age , mulu[i].phnum ,mulu[i].email,mulu[i].adr);}printf("已保存修改\n");}fclose(fp);}}}void shanchu(struct txl_type mulu[],int n){struct txl_type *p;int m,i,t,j=1;FILE *fp;p=mulu;t=chazhao(p,n,j);/*调用查找程序*/if(t>=n);else{printf(" 是否删除该联系人?\n");printf(" 1.是 2.否\n");scanf("%d",&m);fflush(stdin);if(m==1){for(i=t;i<n-1;i++)mulu[i]=mulu[i+1];if((fp=fopen("tx1.txt","w"))!=NULL){for(i=0;i<n-1;i++){fprintf(fp,"\n");fprintf(fp,"%12s %3s %4s %10s %6s %6s",mulu[i].name ,mulu[i].sex ,mulu[i].age , mulu[i].phnum ,mulu[i].email,mulu[i].adr);}}printf("该联系人已删除\n");fclose(fp);}}}void paixu(struct txl_type mulu[],int n){int i,j,m;struct txl_type t;FILE *fp;printf("请选择排序方式: 1.按姓名排序 2.按年龄排序\n");scanf("%d",&m);for(i=1;i<=n-1;i++){for(j=0;j<n-i;j++){if(m==1){if(strcmp(mulu[j].name,mulu[j+1].name)==1){t=mulu[j];mulu[j]=mulu[j+1];mulu[j+1]=t;}}else if(m==2){if(strcmp(mulu[j].age,mulu[j+1].age)==1){t=mulu[j];mulu[j]=mulu[j+1];mulu[j+1]=t;}}}}printf("排序后\n");if((fp=fopen("tx1.txt","w"))!=NULL){for(i=0;i<n;i++){fprintf(fp,"\n");fprintf(fp,"%12s %3s %4s %10s %6s %6s",mulu[i].name ,mulu[i].sex ,mulu[i].age , mulu[i].phnum ,mulu[i].email,mulu[i].adr);printf("name:%-12s sex:%-2s age:%-3s phnum:%-8s email:%-5s adr:%-5s\n",mulu[i].name,mulu[i].sex,mulu[i].age,mulu[i].phnum,mulu[i].email,mulu[i].adr);}}fclose(fp);printf("排序已完成,按回车键返回主菜单\n");fflush(stdin);if(m){char a;scanf("%c",&a);}}void baocun(struct txl_type mulu[],int n){int i,m;FILE *fp;printf(" 是否将联系人名单保存至lianxiren.txt?\n");printf(" 1.是 2.否\n");scanf("%d",&m);if(m==1){if((fp=fopen("lianxiren.txt","w"))!=NULL){for(i=0;i<n-1;i++){fprintf(fp,"\n");fprintf(fp,"姓名:%-10s 性别:%-2s 年龄:%-2s 电话号码:%-10s 邮箱:%-5s 地址:%-5s",mulu[i].name ,mulu[i].sex ,mulu[i].age ,mulu[i].phnum ,mulu[i].email,mulu[i].adr);}}fclose(fp);printf(" 保存成功,按回车键返回主菜单\n");}fflush(stdin);if(m!=2){char a;scanf("%c",&a);}}。

个人通讯录管理系统C语言源程序(优秀版)

个人通讯录管理系统C语言源程序(优秀版)

#include <stdio.h> /*头文件*/#include <stdlib.h> //包含最常用的系统函数#include <string.h> //关于字符数组的函数定义的头文件#include <conio.h> //控制台输入输出//定义结构体struct tongxunlu /*定义通讯录结构体变量*/{char xingming[20]; /*定义输入名字的数组*/char dianhua[20]; /*定义输入电话号码的数组*/char dizhi[40]; /*定义输入地址的数组*/} txl[100]; //默认100个数据int n=0;//记录数据联系人数量FILE *fp; /*定义文件*///程序用到的所有函数void zhucaidan(); /*主菜单函数*/void zengjia(); /*增加联系人函数*/void readfile(); /*文件中读入函数*/void writefile(); /*文件中写入函数*/void xiugai(); /*修改联系人函数*/void xiugai_xingming(); /*姓名修改*/void xiugai_dianhua(); /*电话号码修改*/void chazhao(); /*查找联系人函数*/void chazhao_xingming(); /*按姓名查找*/void chazhao_dianhua(); /*按号码查找*/void shanchu(); /*删除联系人函数*/void shanchu_quanbu(); /*全部删除*/void shanchu_dange(); /*单个删除*/void xianshi(); /*号码显示*///程序主函数模块六void main() /*主函数main*/{ readfile(); /*读入文件*/while(1) /* 循环(永远进行)*/{zhucaidan(); /*调用主菜单函数*/}}//读取文件函数部分开始模块七void readfile(){if((fp=fopen("c:\\通讯录.txt","r"))==NULL) /*以只读方式打开判定文件是否为空*/ {printf("\n\t\t\t 通讯录文件不存在"); /*判断结论*/if ((fp=fopen("同通讯录.txt","w"))==NULL) /*只写方式判断*/{printf("\n\t\t建立失败"); /*为空结论*/exit(0); /*退出*/}else /*不为空则执行else*/{printf("\n\t-----------------欢迎使用通讯录管理系统------------------");printf("\n\t 通讯录文件已建立");printf("\n\t 按任意键进入主菜单");printf("\n\t---------------------------------------------------------");getch();return ;}exit(0);}fseek(fp,0,2); /*文件位置指针移动到文件末尾*/if (ftell(fp)>0) /*文件不为空*/{rewind(fp); /*文件位置指针移动到文件开始位置*/for (n=0;!feof(fp) && fread(&txl[n],sizeof(struct tongxunlu),1,fp);n++);printf("\n\t----------欢迎使用通讯录管理系统1.0beat版----------------");printf("\n\t 文件导入成功");printf("\n\t 按任意键返回主菜单");printf("\n\t---------------------------------------------------------");getch();return;}printf("\n\t------------欢迎使用通讯录管理系统1.0beat版--------------");printf("\n\t 文件导入成功");printf("\n\t 通讯录文件中无任何纪录");printf("\n\t 按任意键返回主菜单");printf("\n\t---------------------------------------------------------");getch();return;}//读取文件函数结束//文件主菜单函数开始模块六void zhucaidan(){char c;system("cls");printf("\n\t\t|------- 欢迎您使用通讯录管理系统1.0beat版-----|");printf("\n\t\t| 1-添加联系人2-查询和显示联系人|");printf("\n\t\t| 3-删除联系人4-修改联系人|");printf("\n\t\t| 5-保存退出0-不保存退出|");printf("\n\t\t|------------------------------------------------|");printf("\n\t\t请选择您所所要的服务:");c=getch();switch (c) /*对于上边的选择项目*/{case '1':zengjia();break;case '2':chazhao();break;case '3':shanchu();break;case '4':xiugai();break;case '5':writefile();break;case '0':exit(0);default:zhucaidan(); /*如果没有则返回主菜单等待输入*/}}//文件主菜单函数结束//添加新联系人函数开始模块一void zengjia(){printf("\n\t\t------------------ 请输入联系人信息--------------------\n");printf("\n\t\t输入联系人姓名:");scanf("%s",txl[n].xingming); /*键盘输入*/printf("\n\t\t输入联系人电话号码:");scanf("%s",txl[n].dianhua);printf("\n\t\t输入联系人地址:");scanf("%s",txl[n].dizhi);n++; /*统计个数*/printf("\n\t\t是否继续添加联系人?(Y/N):"); /*是否继续输入联系人.*/ if (getch()=='y')zengjia();return;}//添加新联系人结束//写入文件函数开始模块七void writefile() /*联系人写入文件*/{int i;if ((fp=fopen("c:\\通讯录.txt","w"))==NULL){printf("\n\t\t文件打开失败");}for (i=0;i<n;i++){if (fwrite(&txl[i],sizeof(struct tongxunlu),1,fp)!=1){printf("\n\t\t写入文件错误!\n");}}fclose(fp); /*关闭文件*/printf("\n\t------------------------------------------------------"); /*输出提示信息*/printf("\n\t\t通讯录文件已保存");printf("\n\t\t谢谢使用,欢迎再次使用!\n");printf("如果对本软件有疑问,请到我们官网()提出您的问题,我们将为您解答.\n");printf("我们的软件有不足之处,欢迎到官网()反映,我们将努力改正!");printf("\n\t\t按任意键退出程序\n\t\t"); /*输出完毕*/exit(0);printf("\n\t------------------------------------------------------");}//写入文件函数结束///查询联系人函数开始模块四----------------------------------------------------void chazhao(){char c;system("cls");printf("\n\t\t------------------- 显示和查询联系人------------------");printf("\n\t\t| 1-逐个显示所有2-按姓名查询|");printf("\n\t\t| 3-按电话查询4-返回主菜单|");printf("\n\t\t|------------------------------------------------------");printf("\n\t\t请选择您所所要的服务:");c=getch();switch (c){case '1':xianshi();break; /*显示所有号码*/case '2':chazhao_xingming();break; /*调用按姓名查询函数*/case '3':chazhao_dianhua();break; /*调用按号码查询函数*/case '4':zhucaidan();break; /*主菜单*/}}void xianshi()//显示所有联系人函数{int i;system("cls");//清楚屏幕if(n!=0){printf("\n\t\t----------欢迎您查询通讯录所有联系人信息-------------");for (i=0;i<n;i++) /*依次序显示*/{printf("\n\t\t姓名:%s",txl[i].xingming);printf("\n\t\t电话:%s",txl[i].dianhua);printf("\n\t\t地址:%s",txl[i].dizhi);printf("\n\t\t--------------------------------------------------");if (i+1<n){printf("\n\t\t-----------------------");system("pause");//返回错误信息}}printf("\n\t\t---------------------------------------------");}else /*无联系人*/printf("\n\t\t通讯录中无任何纪录");printf("\n\t\t按任意键返回主菜单:");getch();return;}//按号码查询函数void chazhao_dianhua(){int mark=0;int i;char phone[15];printf("\n\t\t------------------- 按电话查找-------------------------");printf("\n\t\t请输入电话号码:");scanf("%s",phone);for(i=0;i<n;i++){if (strcmp(txl[i].dianhua,phone)==0)//对比查找函数{printf("\n\t\t------------ 以下是您查找的学生信息------------");printf("\n\t\t姓名:%s",txl[i].xingming);printf("\n\t\t电话:%s",txl[i].dianhua);printf("\n\t\t地址:%s",txl[i].dizhi);printf("\n\t\t------------------------------------------------");printf("\n\t\t按任意键返回主菜单:");mark++;getch();return;}}if (mark==0){printf("\n\t\t没有找到联系人的信息");printf("\n\t\t按任意键返回主菜单");getch();return;}}void chazhao_xingming()//按姓名查询{int mark=0;int i;char name[20];printf("\n\t\t----------------按姓名查找--------------------");printf("\n\t\t请输入您要查找的姓名:");scanf("%s",name);for(i=0;i<n;i++){if (strcmp(txl[i].xingming,name)==0){printf("\n\t\t------------ 以下是您查找的联系人信息---------------");printf("\n\t\t姓名:%s",txl[i].xingming);printf("\n\t\t电话:%s",txl[i].dianhua);printf("\n\t\t地址:%s",txl[i].dizhi);printf("\n\t\t---------------------------------------------------");mark++;getch();return;}}if (mark==0){printf("\n\t\t没有找到联系人的信息");printf("\n\t\t按任意键返回主菜单");getch();return;}}////查询联系人函数结束-----------------------------------------------------////删除联系人函数部分开始-------------------------------------------------void shanchu(){char c;if(n==0) /*如果通讯录中没有一个记录输出以下部分*/{printf("\n\t\t对不起,文件中无任何纪录");printf("\n\t\t按任意键返回主菜单");getch();return;}system("cls"); /*清屏*/printf("\n\t\t----------------- 删除菜单----------------------");printf("\n\t\t| 1-删除所有2-删除单个|");printf("\n\t\t| 3-返回主菜单|");printf("\n\t\t|-------------------------------------------------");printf("\n\t\t请选择您所所要的服务:");c=getch(); /*输入的赋予choice*/switch (c){case '1':shanchu_quanbu();break;case '2':shanchu_dange();break;case '3':zhucaidan();break;default:zhucaidan();break;}}void shanchu_quanbu()//删除所有联系人{printf("\n\t\t确认删除?(y/n)");if (getch()=='y'){fclose(fp);if ((fp=fopen("通讯录.txt","w"))==NULL) /*文件空输出*/{printf("\n\t\t不能打开文件,删除失败");readfile();}n=0;printf("\n\t\t纪录已删除,按任意键返回主菜单");getch();return;}elsereturn;}void shanchu_xingming()//按姓名删除联系人{int i,m,mark=0,a=0;char name[20];printf("\n\t\t请输入要删除联系人姓名:");scanf("%s",name); /*键盘输入姓名*/for (i=a;i<n;i++){if (strcmp(txl[i].xingming,name) == 0) //对比字符串查找到要删除的联系人{printf("\n\t\t以下是您要删除的联系人纪录:");printf("\n\t\t--------------------------------");printf("\n\t\t姓名:%s",txl[i].xingming);printf("\n\t\t电话:%s",txl[i].dianhua);printf("\n\t\t地址:%s",txl[i].dizhi);printf("\n\t\t--------------------------------");printf("\n\t\t是否删除?(y/n)");if (getch()=='y') // 实现删除功能{for (m=i;m<n-1;m++)txl[m]=txl[m+1]; /*将通讯录的想前移*/n--;mark++;printf("\n\t\t删除成功");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')shanchu_xingming(); /*继续调用删除函数*/return;}elsereturn;}continue;}if (mark==0){printf("\n\t\t没有该联系人的纪录");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')shanchu_xingming();return;}}void shanchu_dianhua()//按电话号码删除联系人{int i,m,mark=0; /*变量定义*/char phone[20];printf("\n\t\t请输入要删除联系人电话号码:");scanf("%s",phone);if(n==0){printf("\n\t\t对不起,文件中无任何纪录");printf("\n\t\t按任意键返回主菜单");getch();return;}for (i=0;i<n;i++){if (strcmp(txl[i].dianhua,phone)==0){printf("\n\t\t以下是您要删除的联系人纪录:");printf("\n\t\t姓名:%s",txl[i].xingming);printf("\n\t\t电话:%s",txl[i].dianhua);printf("\n\t\t地址:%s",txl[i].dizhi);printf("\n\t\t是否删除?(y/n)");if (getch()=='y'){for (m=i;m<n-1;m++) //递推替换实现删除txl[m]=txl[m+1];n--;mark++; //记录删除次数printf("\n\t\t删除成功");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')shanchu_dianhua();return;}elsereturn;}continue;}if (mark==0){printf("\n\t\t没有该联系人的纪录");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')return ;}}void shanchu_dange()//删除单个联系人{char c;printf("\n\t----------------------------------------------------------");printf("\n\t\t 1-按姓名删除2-按电话删除");printf("\n\t\t请选择您所所要的服务:");printf("\n\t----------------------------------------------------------");c=getch();switch (c){case '1':shanchu_xingming();break;case '2':shanchu_dianhua();break;}}///删除函数部分结束--------------------------------------------------------///修改联系人函数开始--------------------------------------------------void xiugai() /*修改函数*/{char c;if(n==0){printf("\n\t\t对不起,文件中无任何纪录");printf("\n\t\t按任意键返回主菜单");getch();return;}system("cls");printf("\n\t\t-----------------修改联系人菜单-----------------------");printf("\n\t\t| 1-按姓名修改2-按电话修改|");printf("\n\t\t| 任意键-按返回主菜单|");printf("\n\t\t------------------------------------------------------");c=getch();switch(c){case '1':xiugai_xingming();break;case '2':xiugai_dianhua();break;default:zhucaidan();break;}}void xiugai_xingming(){char c;int i,mark=0;char name[20];printf("\n\t\t请输入要修改的联系人姓名:");scanf("%s",name);if(n==0){printf("\n\t\t文件中无任何联系人");printf("\n\t\t按任意键返回主菜单");getch(); mark++;return;}for(i=0;i<n;i++){if(strcmp(txl[i].xingming,name)==0){printf("\n\t\t以下是您要修改的联系人信息");printf("\n\t\t姓名: %s",txl[i].xingming);printf("\n\t\t电话: %s",txl[i].dianhua);printf("\n\t\t地址: %s",txl[i].dizhi);printf("\n\t\t是否修改(y/n)");if(getch()=='y'){printf("\n\t------------请选择修改信息---------------");printf("\n\t 1-修改姓名2-修改电话");printf("\n\t 3-修改地址");printf("\n\t-----------------------------------------");printf("\n\t请选择您所所要的服务:");scanf("%s",&c);switch(c){case '1': printf("\n\t请输入新姓名:");scanf("%s",txl[i].xingming);break;case'2':printf("\n\t请输入新电话:%s");scanf("%s",txl[i].dianhua);break;case'3':printf("\n\t请输入新地址: ");scanf("%s",txl[i].dizhi);break;}}}}if(mark==0){printf("\n\t\t没有找到联系人信息");printf("\n\t是否继续修改?(Y/N):");if(getch()=='y')xiugai_xingming();return;}}void xiugai_dianhua(){char c,phone[15];int i,mark=0;printf("\n\t\t请输入要修改联系人的号码:");scanf("%s",phone);if(n==0){printf("\n\t\t文件中无任何联系人");printf("\n\t\t按任意键返回主菜单");getch();mark++;return;}for(i=0;i<n;i++){if(strcmp(txl[i].dianhua,phone)==0){printf("\n\t\t以下是您要修改的联系人信息");printf("\n\t\t姓名: %s",txl[i].xingming);printf("\n\t\t电话: %s",txl[i].dianhua);printf("\n\t\t地址: %s",txl[i].dizhi);printf("\n\t\t是否修改(y/n)");if(getch()=='y'){printf("\n\t------------请选择修改信息---------------");printf("\n\t 1-修改姓名2-修改电话");printf("\n\t 3-修改地址");printf("\n\t-----------------------------------------");printf("\n\t请选择您所所要的服务:");scanf("%s",&c);switch(c){case'1': printf("\n\t请输入新姓名:");scanf("%s",txl[i].xingming);break;case'2':printf("\n\t请输入新电话:");scanf("%s",txl[i].dianhua);break;case'3':printf("\n\t请输入新地址: ");scanf("%s",txl[i].dizhi);break;}}}}if(mark==0){printf("\n\t没有该联系人信息");printf("\n\t是否继续修改?(Y/N):");if(getch()=='y')xiugai_dianhua();return;}}其中专业理论知识内容包括:保安理论知识、消防业务知识、职业道德、法律常识、保安礼仪、救护知识。

C语言编程-通讯录

C语言编程-通讯录

C语言编程-通讯录#include<string.h> /*字符函数和字符串函数*/#include<conio.h> /*字符模式函数(调用getch()函数)*/#include<stdio.h> /*输入输出函数*/ int i;struct friend /*电子通讯录结构定义*/{char num[5]; /*编号*/char name[11]; /*名字*/char sex[2]; /*性别*/char phone[12]; /*电话*/char address[26]; /*地址*/char post_num[11]; /*邮编*/}friends[50];FILE *fp;void Data_load() /*数据载入函数*/{ int j;fp=fopen("friend.txt","r+"); /*打开文件friend.txt*/if(fp==NULL) /*判断是否存在friend.txt,如果不存在则创建*/ { fp=fopen("friend.txt","w");i=0;}else{ for(i=0;i<50;i++){ j=fgetc(fp);if(j==EOF)/*判断是否到了文件尾*/return;fseek(fp,i*sizeof(struct friend),0);fread(&friends[i],sizeof(struct friend),1,fp); /*从文件中读取一条记录*/}}}void Menu_Select() /*显示菜单界面*/{printf(" \n");printf(" \n");printf(" \n");printf(" Address Book Management System \n");printf(" \n");printf(" ======================\n"); printf(" 1.Address book data Entry\n"); /*数据输入*/printf(" 2.Address Book data Show\n"); /*数据显示*/printf(" 3.Address Book data Search\n"); /*数据查找*/printf(" 4.Address Book data Alter\n"); /*数据修改*/printf(" 5.Address Book data Insert\n"); /*数据插入*/printf(" 6.Address Book data Delete\n");/*数据删除*/printf(" 7.Address Book data Save\n"); /*数据保存*/printf(" 0.Exit\n"); /*退出*/printf(" ======================\n"); printf( " Please chioce 0-7 \n\n");}int Data_check(int k,char ID[5]) /*检查通讯录中是否存在相同ID或ID输入格式错误*/{int j;if(ID[0]=='0')k=1;for(j=0;ID[j]!='\0';j++){if(ID[j]>'9'||ID[j]<'0')k++;}for(j=0;j<i+1;j++){if(strcmp(ID,friends[j].num)==0)k++;}return k;}void Data_Input() /*数据输入函数*/{char yes_no='1';char ID[5];int k=0;if(i>=50)printf("\n\n Storage full!\n\n");if(i<50){printf("***************************************** *******************************\n");printf(" ******************* Add Address Book Information ***********************\n");printf("***************************************** *******************************\n");printf("***************************************** *******************************\n");}for(;i<50;i++){do{switch(yes_no){case '1': /*如果输入为'1',则进行数据输入*/printf(" ****** No.(4) Name(10) Sex(M/F) Phone(11) Address(25) Zip_Code(10)*******\n");scanf("%s%s%s%s%s%s",ID,friends[i].name,f riends[i].sex,friends[i].phone,friends[i].address,f riends[i].post_num);Data_check(k,ID);while(Data_check(k,ID)>0) /*检查输入的ID是否已存在,如果存在重新输入*/{printf("The No. already exists Or The No. isn't correct, please re-enter\n");scanf("%s",ID);Data_check(k,ID);}strcpy(friends[i].num,ID);if(i<49){printf("\n\n Whether to be continue? Y(1)/N(0) \n\n");yes_no=getch();}if(i>=49)printf("\n\nStorage full!\n\n");break;case '0':break;default:yes_no=getch();}}while(yes_no!='1'&&yes_no!='0'); /*如果输入的不是'1'或'0'则重新输入*/if(yes_no=='0')/*如果输入'0'则退出输入函数*/{i++;break;}}}int The_Size(char a[],char b[]) /*比较两个ID的大小*/{int j,t;if((j=strlen(a)-strlen(b))>0) t=1;if((j=strlen(a)-strlen(b))<0) t=-1;if((j=strlen(a)-strlen(b))==0){if(strcmp(a,b)>0) t=1;if(strcmp(a,b)<0) t=-1;if(strcmp(a,b)==0) t=0;}return t;}void Data_Show() /*数据输出函数*/{ int j,k;char m;struct friend t;printf("***************************************** ***************************\n");printf(" No. Name Sex Phone Address Zip_Code \n");printf("***************************************** ***************************\n");for(j=0;j<i-1;j++)/*对数据进行排序*/{ for(k=0;k<i-1-j;k++){if(The_Size(friends[k].num,friends[k+1].num)= =1){t=friends[k];friends[k]=friends[k+1];friends[k+1]=t;}}}for(j=0;j<i;j++)/*数据输出*/{printf("%4s %10s %4s %11s %20s %10s\n",fri ends[j].num,friends[j].name,friends[j].sex,friend s[j].phone,friends[j].address,friends[j].post_num );m=getch();if(m==' ')break;}printf("\n\n There are %d data in the book .\n\n",i);}void Data_Search() /*数据查找函数*/{char k;int j,a,b;char ID[5];char NAME[11];printf("Search by No. : 1\n");printf("Search by name : 2\n");k=getch();switch(k){case '1': /*按ID查找*/a=0;printf("\n\n Please enter the No.\n\n");scanf("%s",ID);for(j=0;j<i+1;j++){if(strcmp(ID,friends[j].num)==0){a++;printf(" No. Name Sex Phone Address Zip_Code \n");printf("%4s %10s %4s %11s %20s %10s\n",fri ends[j].num,friends[j].name,friends[j].sex,friend s[j].phone,friends[j].address,friends[j].post_num );}}if(a==0)printf(" Do not have to find the No.!\n\n");break;case '2': /*按姓名查找*/b=0;printf("\n\n Please enter the Name\n\n");scanf("%s",NAME);for(j=0;j<i+1;j++){if(strcmp(NAME,friends[j].name)==0){b++;printf(" No. Name Sex Phone Address Zip_Code \n");printf("%4s %10s %4s %11s %20s %10s\n",fri ends[j].num,friends[j].name,friends[j].sex,friend s[j].phone,friends[j].address,friends[j].post_num );}}if(b==0)printf(" Do not have to find the Name!\n\n");break;default:printf("\n\n Error!\n");break;}}void Data_Insert() /*数据插入函数*/{ if(i>=50)printf("\n\n Storage full!\n\n");if(i<50){char ID[5];int k=0;printf("Please enter the No. that you want to insert.\n");scanf("%s",ID);Data_check(k,ID);printf("%d",Data_check(k,ID)); /*检查所需插入的ID是否已存在,如果存在重新输入*/while(Data_check(k,ID)>0){printf("The No. already exists Or The No. isn't correct, please re-enter\n");scanf("%s",ID);Data_check(k,ID);}strcpy(friends[i].num,ID);/*插入数据*/printf(" ****** Name(10) Sex(M/F) Phone(11) Address(30) Zip_Code(10)*******\n");scanf("%s%s%s%s%s",friends[i].name,friends [i].sex,friends[i].phone,friends[i].address,friends [i].post_num);i++;}}void Data_Delete() /*数据删除函数*/{int k,t=0;char ID[5];printf("\n\n Please enter the No. need to be deleted\n\n");scanf("%s",ID);for(k=0;k<i;k++){if(strcmp(friends[k].num,ID)==0){t++;break;}}if(t==0)printf("\n\n The No. does not Exist!\n\n");if(t>0){for(;k<i;k++)/*删除位置前的元素顺序前移*/{friends[k]=friends[k+1];}printf("\n\n Delete success!\n\n");i=i-1;}}void Data_Alter() /*修改函数*/{int k,t=0;char ID[5];printf("\n\n Please enter the No. to be Amended\n\n");scanf("%s",ID);for(k=0;k<i;k++){if(strcmp(friends[k].num,ID)==0){t++;break;}}if(t==0)printf("\n\n The No. does not Exist!\n\n");if(t>0){strcpy(friends[k].num,ID);printf(" ****** Name(10) Sex(M/F) Phone(11) Address(30) Zip_Code(10)*******\n");scanf("%s%s%s%s%s",friends[k].name,friend s[k].sex,friends[k].phone,friends[k].address,frie nds[k].post_num);printf("\n\n Alter Success!\n\n");}}void Data_Save() /*数据保存函数*/{int j;fp=fopen("friend.txt","w");for(j=0;j<=i-1;j++){fwrite(&friends[j],sizeof(struct friend),1,fp);}printf("\n\n Save Success!\n\n");}void Exit() /*退出函数*/{char flag;printf(" Save or Not? (Y/N)\n"); /*判断是否保存*/do{flag=getch();switch(flag){case 'Y': /*保存*/Data_Save();break;case 'y': /*保存*/Data_Save();break;case 'N':break;case 'n':break;}}while(flag!='Y'&&flag!='y'&&flag!=' N'&&flag!='n');}void main(){ char choose,yes_no;Data_load();do{Menu_Select();choose=getch();switch(choose){case '1': Data_Input(); break; /*输入*/case '2': Data_Show(); break; /*显示*/case '3': Data_Search(); break; /*查找*/case '4': Data_Alter(); break; /*修改*/case '5': Data_Insert(); break; /*插入*/case '6': Data_Delete(); break; /*删除*/case '7': Data_Save(); break; /*保存*/case '0': Exit(); break; /*退出*/default : printf(" Error!\n");}if(choose=='0')break;printf(" Would you like to be continue? (Y/N)\n");do{yes_no=getch();}while(yes_no!='Y'&&yes_no!='y'&&yes _no!='N'&&yes_no!='n');if(yes_no=='N'||yes_no=='n'){Exit();break;}}while(yes_no!='Y'||yes_no!='y');。

C语言课程设计——通讯录软件源代码(完美版)

C语言课程设计——通讯录软件源代码(完美版)

C语言课程设计——通讯录软件源代码(完美版)通讯录完美版操作说明书编译环境:Visual C++ 6.0初始密码为:111111主菜单:选择编号进行相应的操作:选择“1”:输入编号:如“001”。

通讯录数据结构如下,只要输入的数据不超过允许的范围即可:struct callmember //定义数据结构{char code[8]; // 编号char name[20]; // 姓名char callnumber[16]; // 电话号码char address[30]; // 地址};输入完后“Enter”结束。

然后又回到“main menu”。

选择“2”显示通讯录信息,但此时刚输入的信息还没保存,选择“3”就可以将联系人信息保存在“通讯录.txt”文件中了,这个文件是自动生成的不需自己建立。

除此外还有查询、修改、删除功能。

当然功能有限,望有能之士加以修改,完善内容~本软件是一个值得收藏的软件,可以用于管理自己的通讯录~源程序:/***************************C语言课程设计*****************************//******************************************************************* **//******************************************************************* **//******************************************************************* **//***** 项目名称: 通讯录管理系统 *****/ /***** 作者: *****/ /*****专业: ***** *****/ /***** 班级: ***** *****/ /***** 学号: ********* *****//***** 指导老师: *****/ /***** 完成时间: 2010.7.12 *****/ /***** 联系电话: 150*******3 *****/ /***** 初始密码: 111111 *****//*********************************************************************/ /******************************************************************* **//******************************************************************* **//************************预处理***************************************/ #include<string.h> #include<stdio.h>#include<stdlib.h>#include<windows.h>#include<conio.h>#define N 301 //修改N的值,就可以改变记录个数,实际容纳人数为N-1 struct callmember //定义数据结构{char code[8]; // 编号char name[20]; // 姓名char callnumber[16]; // 电话号码char address[30]; // 地址};/***********************定义全局变量**********************************/FILE * fp; //文件指针struct callmember person[N]; //记录信息,N为通信录容量,有用信息为后N-1条int counter; //统计记录个数(联系人个数)/***********************函数声明*************************************/void creat(); //创建通讯录 OKvoid load(); //加载数据 OKvoid menu(); //界面菜单显示 OKvoid choose_1(int ); //选择功能 OKvoid insert(); //1、通讯信息的录入 OKvoid output(); //2、通讯信息的显示 OKvoid save(); //3、通讯信息的保存 OKvoid del(); //4、记录的删除 OKvoid modify(); //5、记录的修改 OKvoid found(); //6、记录的查找,三种方式 OKvoid choose_2(int ); //选择查询方式 OKvoid found_code(); //6.1、按编号查询 OK void found_name(); //6.2、按姓名查询 OK void found_callnumber(); //6.3、按电话号码查询 OK voidquit(); //7、退出系统 OK /********************** MAIN 函数***********************************/int main(){char m;char mima[10];char jiema[10];/*************密码文件的创建*****************/if((fp=fopen("mi_ma.dat","rb"))==NULL) //rb 只读(打开){fp=fopen("mi_ma.dat","wb");//wb 为读写建立一个新的文件(如果文件不存在,建立新文件)fprintf(fp,"%s","111111");fclose(fp);}/*************密码的读入*********************/fp=fopen("mi_ma.dat","rb");fscanf(fp,"%s",jiema);fclose(fp);/*************界面设置***********************/system("color 2f"); //设置界面颜色system("cls"); //清屏printf("\n\n\n\n\n\n\n\n\n\n\t\t\t请输入您的密码: "); //输入九位以内的密码gets(mima);if(!strcmp(mima,jiema)){printf("\n\n\t\t\t是否想修改您的密码 (Y/N) : "); scanf("%c",&m);if(m=='y'||m=='Y'){fp=fopen("mi_ma.dat","wb");printf("\n\t\t\t请输入您的新密码: ");scanf("%s",jiema);fprintf(fp,"%s",jiema);fclose(fp);printf("\n\t\t\t密码修改成功!");getchar();getchar();}system("cls"); //清屏system("color 2f"); //设置界面颜色creat(); //为程序创建文件(如果文件不存在)fp=fopen("counter.txt","r"); //r 只读(打开文件)fscanf(fp,"%d",&counter); //将磁盘中的数据赋给counter fclose(fp); //关闭打开的文件load(); //数据装载到内存中menu();return 0;}else{printf("\n\n\t\t\t输入密码错误!");getchar();printf("\t\t\t");return 0;}}/************************创建通讯录函数****************************/ void creat(){if((fp=fopen("通讯录.txt","r"))==NULL)//r 只读(打开){fp=fopen("通讯录.txt","w");//w 为读写建立一个新的文件(如果文件不存在,建立新文件)fprintf(fp,"%-10s%-20s%-20s%-30s\n","编号","姓名","电话号码","地址"); //写入基本信息person[0]fclose(fp);}if((fp=fopen("counter.txt","r"))==NULL) //r 只读(打开){fp=fopen("counter.txt","w"); //w 为只写建立一个新的文件fprintf(fp,"%d",0); //写入初值(自有文件新建时才有)fclose(fp);}}void load(){int i;if((fp=fopen("通讯录.txt","r"))!=NULL) //r 只读(打开),打开文件成功for(i=0;i<=counter;i++)fscanf(fp,"%s%s%s%s",person[i].code,person[i].name,person[i].callnum ber,person[i].address);//将文件中的信息加载到内存中,供操作使用elseprintf("打开文件失败!\n");fclose(fp);}/************************界面菜单显示函数**************************/ void menu(){int k;printf("\n");printf("\n");printf("\t ********************************************\n");printf("\t ********** 设计者: ***********\n");printf("\t ********** 设计时间: 2010.7.13 ***********\n");printf("\t ********************************************\n\n");printf("\t ********************************************\n");printf("\t **************** MENU ********************\n");printf("\t ********************************************\n");printf("\t ********* 1、通讯信息的录入: *************\n");printf("\t ********* 2、通讯信息的显示: *************\n");printf("\t ********* 3、通讯信息的保存: *************\n");printf("\t ********* 4、通信记录的删除: *************\n");printf("\t ********* 5、通信记录的修改: *************\n");printf("\t ********* 6、通信记录的查找: *************\n");printf("\t ********* 0、退出系统: *************\n");printf("\t ********************************************\n");printf("\t Now Please Enter your choose(0-6): ");scanf("%d",&k);choose_1(k);}/************************函数功能选择函数************************/ void choose_1(int k){switch(k){case 1 : insert();break;case 2 : output();break;case 3 : save();system("cls");printf("\n\n\n\n\n\n\n\n\n\n\t\t\t\t 保存成功!");getchar();getchar();system("cls");menu();break;case 4 : del();break;case 5 : modify();break;case 6 : found();break;case 0 : quit();break;default: ;break;}}/****************************OK***********************************/ /************************通讯信息录入函数*************************/ /***************将数据写入内存中,由person[N]数组保存着***********/ void insert(){struct callmember * p;system("cls");p=(struct callmember *)malloc(sizeof(struct callmember ));//将动态分配的内存首地址赋给pprintf("\n\n\n\n\n\n");printf("\t *********************************\n\n");printf("\t 编号: ");scanf("%s",p->code);strcpy(person[counter+1].code,p->code);printf("\t 姓名: ");scanf("%s",p->name);strcpy(person[counter+1].name,p->name);printf("\t 电话: ");scanf("%s",p->callnumber);strcpy(person[counter+1].callnumber,p->callnumber);printf("\t 地址: ");scanf("%s",p->address);strcpy(person[counter+1].address,p->address);printf("\n\t *********************************");counter++;free(p); //释放空间getchar();getchar();system("cls");menu();}/************************通讯信息显示函数************************/ /************从内存中将数据显示在屏幕上,不用打开文件************/ void output(){int i=1;system("cls");if(counter){for(i=0;i<=counter;i++)printf("\t%-8s%-20s%-16s%-30s",person[i].code,person[i].name,person[i].callnumber,person[i].address);printf("\t");}elseprintf("\n\n\n\n\n\t\t记录为空,请输入联系人信息!");getchar();getchar();system("cls");menu();}/*************保存、修改、删除只针对内存中的数组进行操作*********/ /************************通讯信息保存函数************************/ void save() {int i;system("cls");if((fp=fopen("counter.txt","w"))==NULL) //w 只写覆盖原来的数据{printf("\n\n\n\t\t\t操作失败.");exit(1);}fprintf(fp,"%d",counter); //往磁盘写入现在的值,即保存fclose(fp);if((fp=fopen("通讯录.txt","w"))!=NULL)fprintf(fp,"%s","\0");fclose(fp);if((fp=fopen("通讯录.txt","a"))!=NULL)for(i=0;i<=counter;i++)fprintf(fp,"%-10s%-20s%-20s%-30s\n",person[i].code,person[i].name,person[i].callnumber,p erson[i].address);fclose(fp);system("cls");}/************************信息删除函数****************************/ void del() //根据姓名删除,根据显示选择要删的内容 {int k=1;int c=0;char m;char namekey[20];system("cls");printf("\n\n\n\n\n\n\t\t\t请输入要删除的姓名:");scanf("%s",namekey);while(k<=counter){if(!strcmp(namekey,person[k++].name)){c=k-1;break;}}if(c){printf("\t\t\t已查到,记录为:\n\n\n");printf("\t%-10s%-20s%-20s%-30s\n",person[0].code,person[0].name,person[0].callnumber,p erson[0].address);printf("\t%-10s%-20s%-20s%-30s\n",person[c].code,person[c].name,person[c].callnumber,pe rson[c].address);printf("\n\n\t\t\t确定要删除吗? (Y/N) : ");scanf("%s",&m);if(m=='y'||m=='Y'){if(c==counter){strcpy(person[counter].code,"\0");strcpy(person[counter].name,"\0");strcpy(person[counter].callnumber,"\0");strcpy(person[counter].address,"\0");counter--;}else{for(k=c;k<=counter;k++) //后面的信息将前面的信息覆盖掉{strcpy(person[k].code,person[k+1].code);strcpy(person[k].name,person[k+1].name);strcpy(person[k].callnumber,person[k+1].callnumber); strcpy(person[k].address,person[k+1].address);}strcpy(person[counter].code,"\0"); //最后面的信息为空strcpy(person[counter].name,"\0");strcpy(person[counter].callnumber,"\0");strcpy(person[counter].address,"\0");counter--;}printf("\t\t\t删除成功!");getchar();getchar();system("cls");save(); //用save()函数将删除后的信息从新保存menu();}else{system("cls");menu();}}else{printf("\n\t\t\t对不起,没有此人记录!");getchar();getchar();system("cls");menu();}}/************************信息修改函数****************************/ //可以设定编号唯一,通过查找编号来确定记录位置,进而进行修改 void modify(){int k=1;char m;char codekey[20];system("cls");printf("\n\n\t\t\t 请输入要修改的编号: ");scanf("%s",codekey);while(k<=counter){if(!strcmp(codekey,person[k].code))break;k++;}if(k<=counter){printf("\n\t\t\t 已查到,记录为:\n");printf("\n\t\t%-10s%-20s%-20s%-30s\n","编号","姓名","电话号码","地址");printf("\t\t%-10s%-20s%-20s%-30s\n",person[k].code,person[k].name,person[k].callnumber, person[k].address);printf("\t\t\t 确定要修改吗? (Y/N) : ");scanf("%s",&m);if(m=='y'||m=='Y'){printf("\n\t\t\t 请输入新的信息:\n\n");printf("\t *********************************\n\n");printf("\t 编号: ");scanf("%s",person[k].code);printf("\t 姓名: ");scanf("%s",person[k].name);printf("\t 电话: ");scanf("%s",person[k].callnumber);printf("\t 地址: ");scanf("%s",person[k].address);printf("\n\t *********************************"); printf("\n\n\t\t\t 修改成功!");getchar();getchar();system("cls");save(); //修改完后立马保存menu();}else{system("cls");menu();}}else{printf("\n\n\t\t\t对不起,没有此人记录!");getchar();getchar();system("cls");menu();}}/************************信息查询函数****************************/ void found() //调试合适{int k;system("cls");printf("\n");printf("\n");printf("\n\t\t******************************************\n"); printf("\n\t\t 1、按编号查询:\n");printf("\n\t\t 2、按姓名查询:\n");printf("\n\t\t 3、按电话号码查询:\n");printf("\n\t\t 0、返回:\n");printf("\n\t\t******************************************\n\n"); printf("\t\tPlease Enter Your Choose now: ");scanf("%d",&k);choose_2(k);}void choose_2(int k){switch(k){case 1 : found_code(); break;case 2 : found_name(); break;case 3 : found_callnumber(); break;case 0 : system("cls");menu(); break;default : system("cls");menu();break;}system("cls");menu();}//按编号查询void found_code(){int k=0;char codekey[8];struct callmember * p;p=(struct callmember *)malloc(sizeof(struct callmember)); printf("\n\t\t请输入要查询的编号:");scanf("%s",codekey);if((fp=fopen("通讯录.txt","r"))==NULL){printf("\n不能打开通讯录!\n");exit(0);}while(!feof(fp)) //当文件结束时,feof()返回值为1{fscanf(fp,"%s%s%s%s\n",p->code,p->name,p->callnumber,p->address);if(!strcmp(codekey,p->code)){k=1;break; //k=1,说明已经查到}}if(k){printf("\n\t\t%-10s%-20s%-20s%-30s\n","编号","姓名","电话号码","地址");printf("\t\t%-10s%-20s%-20s%-30s",p->code,p->name,p->callnumber,p->address);}elseprintf("\n\t\t您查询的编号不存在!");fclose(fp);getchar();getchar();system("cls");}//按姓名查询void found_name(){int k=0;char namekey[8];struct callmember * p;p=(struct callmember *)malloc(sizeof(struct callmember));printf("\n\t\t请输入要查询的姓名:");scanf("%s",namekey);if((fp=fopen("通讯录.txt","r"))==NULL){printf("\n不能打开通讯录!\n");exit(0);}while(!feof(fp)) //当文件结束时,feof()返回值为1.{fscanf(fp,"%s%s%s%s\n",p->code,p->name,p->callnumber,p->address); if(!strcmp(namekey,p->name)){k=1;break; //k=1,说明已经查到}}if(k){printf("\n\t\t%-10s%-20s%-20s%-30s\n","编号","姓名","电话号码","地址");printf("\t\t%-10s%-20s%-20s%-30s",p->code,p->name,p->callnumber,p->address);}elseprintf("\n\t\t您查询的姓名不存在!");fclose(fp);getchar();getchar();system("cls");}//按电话号码查询void found_callnumber(){int k=0;char callnumberkey[8];struct callmember * p;p=(struct callmember *)malloc(sizeof(struct callmember));printf("\n\t\t请输入要查询的电话号:");scanf("%s",callnumberkey);if((fp=fopen("通讯录.txt","r"))==NULL){printf("\n不能打开通讯录!\n");exit(0);}while(!feof(fp)) //当文件结束时,feof()返回值为1.{fscanf(fp,"%s%s%s%s\n",p->code,p->name,p->callnumber,p->address);if(!strcmp(callnumberkey,p->callnumber)){k=1;break; //k=1,说明已经查到}}if(k){printf("\n\t\t%-10s%-20s%-20s%-30s\n","编号","姓名","电话号码","地址");printf("\t\t%-10s%-20s%-20s%-30s",p->code,p->name,p->callnumber,p->address);}elseprintf("\n\t\t您查询的号码不存在!");fclose(fp);getchar();getchar();system("cls"); }/************************退出系统函数******************************/void quit() //调试合适 { printf("\t\t");exit(0);}。

通讯录C语言源程序

通讯录C语言源程序
程序1:
#include<stdio.h>
#include<string.h>
struct friends_list{
char name[10];
int age;
char telephone[13];
};
int Count=0;
void new_friend(struct friends_list friends[]);
{
int k,y=0;
int t,j=1,g=0;
struct friends_list x;
if(Count==0){
printf("通讯录是空的!\n");
return;
}
/*首先,判断要修改的联系人中有没有相同的名字*/
for(k=0;k<Count;k++){
if(strcmp(name,friends[k].name)==0)
printf("电话:%13s\n",friends[k].telephone);
j++;
}
}
/*选择需要修改的联系人的序号*/
printf("请选择所要修改的联系人序号:\n");
scanf("%d",&t);
for(k=0;k<Count;k++){
if(strcmp(name,friends[k].name)==0){
ADDRESS;/******以下是函数原型*******/
int enter(ADDRESS t[]); /*输入记录*/

C语言通讯录程序

C语言通讯录程序

C语言通讯录程序#include <stdio.h>#include <malloc.h> //得到指向大小为Size的内存区域的首字节的指针// #include <string.h>#include <stdlib.h> //标准库函数//#define NULL 0#define LEN sizeof(struct address_list) //计算字节//int n;struct address_list{char name[30]; //名字char work[30]; //职业char handset[30]; //手机char email[30]; //电子邮件char address[30]; //通讯地址struct address_list *next;};struct address_list *shifang(struct address_list *head); // 释放内存函数声明//创建函数,不带头结点的链表struct address_list *creat(void){struct address_list *head,*p1,*p2;char name[20];n=0;p1=(struct address_list *)malloc(LEN);p2=p1; //强制内存转换printf("请输入通讯录的内容!\n姓名输入为0时表示创建完毕!\n");printf("请输入姓名:");gets(name);if(strcmp(name,"0")!=0){strcpy(p1->name,name);printf("请输入职业:"); gets(p1->work);printf("请输入手机:"); gets(p1->handset);printf("请输入电子邮件:"); gets(p1->email);printf("请输入通讯地址:"); gets(p1->address);head=NULL;while(1){n=n+1; //记录通讯录人数个数if(n==1)head=p1;elsep2->next=p1;p2=p1;printf("请输入姓名:");gets(name);if(strcmp(name,"0")==0){break;}else{p1=(struct address_list *)malloc(LEN);strcpy(p1->name,name);printf("请输入职业:"); gets(p1->work);printf("请输入手机:"); gets(p1->handset);printf("请输入电子邮件:"); gets(p1->email);printf("请输入通讯地址:"); gets(p1->address);}}p2->next=NULL;return head;}elsereturn 0;}//输出函数void print(struct address_list *head){struct address_list *p;if(head!=NULL){p=head;printf("本通讯录现在共有%d人:\n",n);printf("---姓名-----------------职业--------------手机-------Email-------------通讯地址\n");printf("************************************************************ *******************\n");do{printf(" %s",p->name); printf(" ");printf("%s",p->work); printf(" ");printf("%s",p->handset); printf(" ");printf("%s",p->email); printf(" ");printf("%s",p->address); printf(" \n");p=p->next;}while(p!=NULL);printf("************************************************************ *******************\n");}elseprintf("通讯录为空,无法输出!\n");}//增加函数struct address_list *insert(struct address_list *head){struct address_list *p0,*p1,*p2;char name[20];p1=head;printf("请输入增加的内容:\n");printf("请输入姓名:"); gets(name);if(strcmp(name,"0")==0){printf("姓名不能为0,增加失败!\n");return(head);}else{p0=(struct address_list *)malloc(LEN);strcpy(p0->name,name);printf("请输入职业:"); gets(p0->work);printf("请输入手机:"); gets(p0->handset);printf("请输入电子邮件:"); gets(p0->email);printf("请输入通讯地址:"); gets(p0->address);n=n+1;if(head==NULL){head=p0;p0->next=NULL;return head;}else{while(strcmp(p0->name,p1->name)>0&&(p1->next!=NULL)){p2=p1;p1=p1->next;}if(strcmp(p0->name,p1->name)<0 || strcmp(p0->name,p1->name)==0){if(head==p1){head=p0;}else{p2->next=p0;}p0->next=p1;}else{p1->next=p0;p0->next=NULL;}return head;}}}struct address_list* delete_txl(struct address_list *head){struct address_list *p,*q;char name[30];if(head==NULL){printf("通讯录为空,无法显示!\n");return head;}p=head;printf("请输入需要删除的人的姓名:");gets(name);if(strcmp(head->name,name)==0){head=head->next;free(p);printf("删除操作成功!\n");return head;}else{q=head,p=head->next;while(p!=NULL){if(strcmp(p->name,name)==0){q->next=p->next;free(p);printf("删除操作成功!\n");return head;}p=p->next;q=q->next;}}}//显示函数struct address_list *display(struct address_list *head) {struct address_list *p1,*p2;char name[30];int m;if(head==NULL){printf("通讯录为空,无法显示!\n");return head;}p1=head;printf("请输入需要显示人的姓名:");gets(name);while(p1!=NULL){while((strcmp(p1->name,name))!=0 && p1->next!=NULL){p2=p1;p1=p1->next;}if(strcmp(p1->name,name)==0){m++;printf("%s的通讯内容如下:\n",name);printf("---姓名-----------------职业--------------手机-------Email-------------通讯地址\n");printf("************************************************************ *******************\n");printf(" %s",p1->name);printf(" ");printf("%s",p1->work);printf(" ");printf("%s",p1->handset);printf(" ");printf("%s",p1->email);printf(" ");printf("%s",p1->address); printf(" \n");printf("************************************************************ *******************\n");}p1=p1->next;}if(m==0){printf("此人未在本通讯录中!\n");}return(head);}//排序函数struct address_list *paixu(struct address_list *head){struct address_list *p1,*p2;struct address_list1{char name[30];char work[30];char handset[30];char email[30];char address[30];};struct address_list1 px[200];struct address_list1 temp;if(head==NULL){printf("通讯录为空,无法排序!\n");return(head);}p1=head;for(i=0;i<n,p1!=NULL;i++){strcpy(px[i].name,p1->name);strcpy(px[i].work,p1->work);strcpy(px[i].handset,p1->handset);strcpy(px[i].email,p1->email);strcpy(px[i].address,p1->address);p2=p1;p1=p1->next;}head=shifang(head);for(j=0;j<n-1;j++){for(i=j+1;i<n;i++){if(strcmp(px[i].name,px[j].name)<0){temp=px[i];px[i]=px[j];px[j]=temp;}}}p1=(struct address_list *)malloc(LEN);p2=p1;strcpy(p1->name,px[0].name);strcpy(p1->work,px[0].work);strcpy(p1->handset,px[0].handset);strcpy(p1->email,px[0].email);strcpy(p1->address,px[0].address);head=p1;for(i=1;i<n;i++){p1=(struct address_list *)malloc(LEN);strcpy(p1->name,px[i].name);strcpy(p1->work,px[i].work);strcpy(p1->handset,px[i].handset);strcpy(p1->email,px[i].email);strcpy(p1->address,px[i].address);p2->next=p1;p2=p1;}p2->next=NULL;printf("按姓名排序后为:\n");print(head);return(head);}//姓名查找函数struct address_list *search(struct address_list *head){struct address_list *p1,*p2;int m;char name[30];if(head==NULL){printf("通讯录为空,无法分类查找!\n");return(head);}p1=head;//printf("********************\n");printf("请输入需要查找的姓名:");// printf("********************\n");m=0;gets(name);while(p1!=NULL){while(strcmp(p1->name,name)!=0&&p1->next!=NULL){p2=p1;p1=p1->next;}if(strcmp(p1->name,name)==0){m++;printf("你查找的内容是:\n");printf("************************************************************ *******************\n");printf("++ %s %s %s %s %s\n",p1->name,p1-> work,p1->handset,p1->email,p1->address);printf("************************************************************ *******************\n");}p1=p1->next;if(m==0){printf("此人未在本通讯录中!\n");}break;}return(head);}//释放内存函数struct address_list *shifang(struct address_list *head){struct address_list *p1;while(head!=NULL){p1=head;head=head->next;free(p1);}return(head);}//文件写入函数void save(struct address_list *head){FILE *fp;struct address_list *p1;char tong[30];if(head==NULL){printf("通讯录为空,无法存储!\n");return;}printf("请输入保存后的文件名:");gets(tong);fp=fopen("(tong).txt","w");if(fp==NULL){printf("cannot open file\n");return;}p1=head;fprintf(fp,"姓名职业手机Email 通讯地址\n");for(;p1!=NULL;){fprintf(fp,"%s %s %s %s %s\n",p1->name,p1-> work,p1->handset,p1->email,p1->address);p1=p1->next;}printf("保存完毕!\n");fclose(fp);}//文件读出函数struct address_list *load(struct address_list *head){FILE *fp;char tong[30];struct address_list *p1,*p2;printf("请输入要输出的文件名:");gets(tong);fp=fopen("(tong).txt","r");if(fp==NULL){printf("此通讯录名不存在,无法输出!\n");return(head);}else{head=shifang(head);}p1=(struct address_list *)malloc(LEN);fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p 1->address);if(feof(fp)!=0){printf("文件为空,无法打开!\n");return(head);}else{rewind(fp);p2=p1;head=p1;n=0;while(feof(fp)==0){fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->work,&p1->handset,&p1->email,&p 1->address);if(feof(fp)!=0)break;p2->next=p1;p2=p1;p1=(struct address_list *)malloc(LEN);n=n+1;}p2->next=NULL;p1=head;head=head->next;n=n-1;free(p1);print(head);printf("打开完毕!\n");return(head);}fclose(fp);}//综合操作函数struct address_list *menu(struct address_list *head){char num[10];while(1){printf("\t\t\t*********************************************\n");printf("\t\t\t*** 1 姓名查找****\n");printf("\t\t\t*** 2 单个显示****\n");printf("\t\t\t*** 3 增加****\n");printf("\t\t\t*** 4 退出****\n");printf("\t\t\t*********************************************\n");printf("请输入您选择的操作:");gets(num);switch(*num){case '1':{head=search(head); //姓名查找print(head);}break;case '2':{head=display(head); //显示}break;case '3':{head=insert(head); //增加print(head);}break;case '4':return head;default:printf("操作错误,此项不存在!\n");break;}if(strcmp(num,"6")==0)break;}return head;}//主函数void main(){struct address_list *head=NULL;char num[10];printf("\t\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");printf("\t\t\t*=* 通讯录*=*\n");printf("\t\t\t*=* 请及时保存创建完毕的通讯录内容! *=*\n");printf("\t\t\t*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*\n");while(1){printf("\t\t\t*********************************************\n");printf("\t\t\t*** 1 创建通讯录****\n");printf("\t\t\t*** 2 按名字排序****\n");printf("\t\t\t*** 3 综合操作****\n");printf("\t\t\t*** 4 保存通讯录****\n");printf("\t\t\t*** 5 打开通讯录****\n");printf("\t\t\t*** 6 删除****\n");printf("\t\t\t*** 7 退出****\n");printf("\t\t\t*********************************************\n");printf("请输入您选择的操作:");gets(num);switch(*num){case '1':{if(head==NULL){head=creat(); //创建print(head);}else{head=shifang(head);head=creat(); //重新创建print(head);}}break;case '2':{head=paixu(head); //排序}break;case '3':{head=menu(head); //综合操作}break;case '4':{save(head); //文件保存print(head);}break;case '5':{head=load(head); //文件输出}break;case '6':{head=delete_txl(head); //删除print(head);}break;case '7':head=shifang(head);break;default:printf("操作错误,此项不存在!\n");break;}if(strcmp(num,"7")==0)break;}}。

c语言学生通讯录设计

c语言学生通讯录设计

下面是一个简单的C语言学生通讯录设计示例:#include <stdio.h>#include <string.h>struct student {char name[20];int age;char gender;char phone[15];};int main() {struct student students[100];int n, i;printf("请输入学生人数:");scanf("%d", &n);for (i = 0; i < n; i++) {printf("请输入第%d个学生的姓名、年龄、性别和电话号码:", i+1);scanf("%s %d %c %s", students[i].name, &students[i].age, &students[i].gender, students[i].phone);}printf("\n学生通讯录\n");printf("姓名\t年龄\t性别\t电话号码\n");for (i = 0; i < n; i++) {printf("%s\t%d\t%c\t%s\n", students[i].name, students[i].age, students[i].gender, students[i].phone);}return 0;}该程序定义了一个名为`student`的结构体,用于存储学生的姓名、年龄、性别和电话号码。

在主函数中,首先通过`scanf`函数从用户输入中读取学生人数`n`,然后使用一个循环来依次读取每个学生的信息,并将它们存储到`students`数组中。

最后,使用另一个循环将所有学生的信息打印出来。

该程序可以根据需要添加其他功能,例如根据姓名或电话号码查询学生信息、添加或删除学生信息等。

通讯录(C语言版)(请下载观看,无乱码)

通讯录(C语言版)(请下载观看,无乱码)

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<conio.h>typedef struct personnode {char name[8];char tel[16];char add[30];}personnode;FILE *fp; //定义文件型指针变量/*以下创建通讯录,若通讯录不存在,刚提示创建,若存在则不创建*/void creat(){personnode *person;person=(personnode *)malloc(sizeof(personnode));if((fp=fopen("通讯录.dat","r"))==NULL){ //r 只读(打开)printf("通讯录不存在!\n");fp=fopen("通讯录.dat","w"); //w 只写(建立)fprintf(fp,"%-10s%-20s%-50s\n","姓名","电话号码","地址");printf("\n建立通讯录成功!\n");}elseprintf("通讯录已存在,不用建立!\n");fclose(fp);} //creat()void output(){personnode *person;person=(personnode *)malloc(sizeof(personnode));if((fp=fopen("通讯录.dat","r"))==NULL){printf("\n不能打开通讯录!\n");exit(0);}printf("\n**************************************\n");printf("%24s\n","通讯录");while(!feof(fp)){ //如果没到文件末尾,则不停循环输出fscanf(fp,"%s%s%s\n",person->name,person->tel,person->add);printf("%-10s%-20s%-50s\n",person->name,person->tel,person->add);}fclose(fp);printf("\n**************************************\n");} //output()/*以下为添加模块*/void append(){personnode *person;person=(personnode *)malloc(sizeof(personnode));if((fp=fopen("通讯录.dat","a"))==NULL){ //a 追加printf("\n不能打开通讯录!\n");exit(0);}printf("\n请输入添加的姓名、电话号码及地址\n");scanf("%s%s%s",person->name,person->tel,person->add);fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->tel,person->add);fclose(fp);printf("\n添加成功!\n");} //append()/*以下为查找模块*/void search(){int k=0,h;char namekey[8],telkey[16];personnode *person;person=(personnode *)malloc(sizeof(personnode));printf("\n*********************************\n");printf("* \t1 -- 按号码查找 \t*\n");printf("* \t2 -- 按姓名查找 \t*\n");printf("*********************************\n");printf("\n请选择操作 (1 ~ 2) :");scanf("%d",&h);fp=fopen("通讯录.dat","r");if(h==1){printf("\n请输入你查找的号码!\n");scanf("%s",telkey);while(!feof(fp)){fscanf(fp,"%s%s%s\n",person->name,person->tel,person->add);if(!strcmp(telkey,person->tel)){printf("\n\n已查到,记录为:\n");printf("%-10s%-20s%-50s\n",person->name,person->tel,person->add);k=1;} //if}}else if(h==2){printf("\n请输入要查找的姓名:");scanf("%s",namekey);while(!feof(fp)){fscanf(fp,"%s%s%s\n",person->name,person->tel,person->add);if(!strcmp(namekey,person->name)){printf("\n\n已查到,记录为:\n");printf("%-10s%-20s%-50s\n",person->name,person->tel,person->add);k=1;} //if} //while*/if(!k)printf("对不起,没有此人记录!\n");fclose(fp);}} //search()/*以下为修改模块*/void modify(){int k=0;long offset;char namekey[8];personnode *person;person=(personnode *)malloc(sizeof(personnode));printf("\n请输入要修改的姓名:");scanf("%s",namekey);if((fp=fopen("通讯录.dat","r+"))==NULL){ // r+ 读写printf("\n不能打开通讯录!\n");exit(0);}while(!feof(fp)){offset=ftell(fp);fscanf(fp,"%s%s%s\n",person->name,person->tel,person->add);if(!strcmp(namekey,person->name)){k=1;break;}}if(k){printf("\n已查到,记录为:\n");printf("%-10s%-20s%-50s\n",person->name,person->tel,person->add);printf("\n请输入新姓名、电话号码及地址\n");scanf("%s%s%s",person->name,person->tel,person->add);fseek(fp,offset,SEEK_SET); //从该条记录的开始点进行写操作 fprintf(fp,"%-10s%-20s%-50s\n",person->name,person->tel,person->add);printf("\n修改成功!\n");}else printf("\n对不起,没有此人记录!\n");fclose(fp);}/*以下为删除模块*/void delrecord(){int k=0;int sum=1;char m;long offset1;char namekey[8];personnode *person;person=(personnode *)malloc(sizeof(personnode));printf("\n请输入要删除的姓名:");scanf("%s",namekey);if((fp=fopen("通讯录.dat","r+"))==NULL){printf("\n不能打开通讯录!\n");exit(0);}while(!feof(fp)){offset1=ftell(fp); //文件位置指针当前值fscanf(fp,"%s%s%s\n",person->name,person->tel,person->add);sum+=1;if(!strcmp(namekey,person->name)){k=1;break;}} //while()if(k){printf("\n已查到,记录为:\n");printf("%-10s%-20s%-50s\n",person->name,person->tel,person->add);printf("\n确定要删除吗? (Y/N) ");scanf("%s",&m);if(m=='y'||m=='Y'){rewind(fp);fseek(fp,sum*80L,1);fprintf(fp,"%-10s%-20s%-50s\n","","","");printf("\n删除成功!\n");} //if}else printf("\n对不起,没有此人记录!\n");fclose(fp);} //delcord()void main(){int m,flag=1;system("color 2f"); //调用颜色命令,包含在stdlib.h头文件中printf("\n\n******************************************************************** ***********\n\n");printf("\t\t┏━━━━━━━━━━━━━━━━━━━━━┓\n");printf("\t\t┃∩__∩ Random 编写♂┃\n");printf("\t\t┃欢迎使用Random的通讯录┃\n");printf("\t\t┃ QQ:747187822 ┃\n");printf("\t\t┣━━━━━━━━━━┳━━━━━━━━━━┫\n");printf("\t\t┃ 1 -- 添加┃ 2 -- 查找┃\n");printf("\t\t┃┃┃\n");printf("\t\t┃ 3 -- 修改┃ 4 -- 删除┃\n");printf("\t\t┃┃┃\n");printf("\t\t┃ 5 -- 打印┃ 0 -- 退出┃\n");printf("\t\t┗━━━━━━━━━━┻━━━━━━━━━━┛\n");printf("\n********************************************************************** *********\n\n");creat();while(flag){ //用死循环实现printf("\n请选择操作 (0 ~ 5) :");scanf("%d",&m);switch(m){case 1: append();break;case 2: search();break;case 3: modify();break;case 4: delrecord();break;case 5: output();break;case 0: printf("\n\n==========>感谢使用Random的通讯录!<==========\n");exit(0);} //switch()} //while()}/*本程序编写By:Random*/。

(完整word版)c语言程序设计通讯录管理系统

(完整word版)c语言程序设计通讯录管理系统

1.需求分析根据题目要求,通讯录保存在文件中,要提供文件的输入和输出功能;还有就是要提供通讯录添加(个人信息包括姓名、学号、年龄、住址、电子邮件等)、删除、显示、修改等基本功能。

2。

概要设计系统总体设计是信息的输入、信息的编辑和退出.系统功能模块如下:1.添加通讯录;2.输出通讯录;3.查找通讯录;4.修改通讯录;5.删除通讯录;6.退出通讯录;3.详细设计1.通讯录里无信息,所以要添加信息.用键盘逐个输入通讯录里联系人的信息.2.输出通讯录用于让使用者观看通讯录里所有的信息.3。

查找通讯录是让使用者可以通过电话或姓名来查找通讯录里的联系人.4。

修改通讯录是让使用者通过姓名查找到通讯录里的联系人,然后其作出修改。

5.删除通讯录是用来按照联系人的姓名或电话来单独删除通讯录中一些不想要的联系人。

还可以集体删除通讯录中的联系人.6.退出通讯录系统。

4.主要源程序代码#include 〈stdio。

h>#include 〈stdlib.h〉#include 〈string.h>#include 〈conio。

h〉struct record{char name[20];char age[20];char phone[20];char adress[40];char number[30];char e_mail[20];}contact[500];int NO=0;FILE *fp;void mainmenu();void newrecord();void browsemenu();void searchmenu();void searchname();void searchphone();void modifymenu();void deletemenu();void deleteall();void deletesingle();void deletename();void deletephone();int loadfile();void savefile();void main(){printf(”\n\t\t :)欢迎使用通讯录管理系统beta”);printf(”\n\t\t文件导入中……请稍后……\n\n\n\n\n");system(”cls”);loadfile();while (1){mainmenu();}}void mainmenu(){char choice;system(”cls");printf("\n\t\t******************** 主菜单********************");printf("\n\t\t****************** 1-新建菜单******************”);printf(”\n\t\t******************2—浏览菜单******************”);printf(”\n\t\t****************** 3—查找菜单******************”);printf("\n\t\t******************4—修改菜单******************”);printf(”\n\t\t******************5-删除菜单******************”);printf(”\n\t\t****************** 6-退出******************");printf("\n\t\t************************************************”); printf(”\n\t\t请选择:");choice=getch();switch (choice){case '1':newrecord();break;case ’2’:browsemenu();break;case ’3’:searchmenu();;break;case '4’:modifymenu();break;case ’5’:deletemenu();break;case '6’:exit(0);default:mainmenu();}}void newrecord(){printf(”\n\t\t****************请输入联系人信息****************\n”);printf(”\n\t\t输入姓名:”);scanf(”%s”,&contact[NO]。

通讯录系统C语言程序_附完整代码

通讯录系统C语言程序_附完整代码
通讯录系统 C 语言程序_完整代码
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<memory.h> struct student { char names[20]; int year; int month; int day; int sex; //sex=1 表示男生,2 表示女生 char tel[20]; char department[20]; }s[50]; //一共能够登记 50 个人 int cnt=0; void display(){ printf("| 请输入 0-4 |\n"); printf("| 1.注册(增加)|\n"); printf("| 2.删减用户 |\n"); printf("| 3.修改用户 |\n"); printf("| 4.查询功能 |\n"); printf("| 0.退出系统 |\n"); } void in(){ printf("请输入姓名:"); scanf("%s",&s[cnt].names); printf("\n"); printf("请输入出生年月日,中间用空格隔开:"); scanf("%d %d %d",&s[cnt].year,&s[cnt].month,&s[cnt].day); printf("\n"); printf("请输入性别,1 表示男生,2 表示女生:"); scanf("%d",&s[cnt].sex); printf("\n"); printf("请输入电话:"); scanf("%s",&s[cnt].tel); printf("\n"); printf("请输入职位:"); scanf("%s",&s[cnt].department); printf("\n 注册完毕\n"); cnt++; return ;

通讯录的C源程序

通讯录的C源程序

#include"stdio.h"#include <stdlib.h>#include"string.h"#define header1 "|--------------电话簿------------------|\n"#define header2 "| name | phonenum | adress | \n" #define header3 "|----------|----------------|----------|\n"#define end "|-----------------结束------------------|"typedef struct _CStudent{char szName[20]; /* 学生姓名*/double number; /*电话*/char adress[100]; /*地址*/}CStudent;void menu();int duqu(int *len);int zengjia(int *k,int *len);int chaxun(int *len);int xiugai(int *len);int shanchu(int *len);int input(CStudent* p); /* 从键盘输入*/int output(int *len); /* 向屏幕输出*/int search(CStudent p,int *j,CStudent c[3],int *len);int sname(CStudent p,int *len); /*姓名查询*/int snumber(CStudent p,int *len); /*电话查询*/void xname(CStudent q[100],int i); /*姓名修改*/void xnumber(CStudent q[100],int i); /*号码修改*/void xadress(CStudent q[100],int i); /*地址修改*/int sput(int *len,CStudent q[100]);/*删除的处理模块*/main( ){static int len=1;int i,j=0,k=0;while(j==0){menu();do{printf("请选择你需要的服务:\n");scanf("%d",&i);}while(i<0||i>5);//system("cls");switch(i){case 1:{j=zengjia(&k,&len);break;}/*增加*/case 2:{j=duqu(&len);break;} /*读取*/case 3:{j=chaxun(&len);break;} /*查找*/case 4:{j=xiugai(&len);break;} /*修改*/case 5:{j=shanchu(&len);break;}/*删除*/default:{printf(" |**欢迎下次使用电话薄管理系统!**|\n");j=1;break;}}}}int duqu(int *len){CStudent p[100];int i;FILE *fp;system("cls");printf("\n\t\t|* **** 欢迎进入读取联系人界面! **** *|\n\n");if((fp=fopen("e://file3","r"))==NULL){printf("error!\n");return -1;}printf("%s%s%s",header1,header2,header3);for(i=0;i<*len;i++){fscanf(fp,"%s%lf%s ",p[i].szName,&p[i].number,p[i].adress);printf("| %s | %.0lf | %s |\n",p[i].szName,p[i].number,p[i].adress);}printf("%s\n",end);system("pause");return 0;}void menu(){system("cls");system("color 1F");printf("\n\n\n"); /*输出主菜单*/printf("\t\t|* **** 欢迎进入通讯录管理系统! **** *|\n");printf("\t\t|********************menu********************|\n");printf("\t\t| |\n");printf("\t\t| 1 增加 2 读取|\n");printf("\t\t| |\n");printf("\t\t| 3 查找 4 修改|\n");printf("\t\t| |\n");printf("\t\t| 5 删除0 退出|\n");printf("\t\t| |\n");printf("\t\t|********************************************|\n");}int zengjia(int *k,int *len){CStudent p[100];int i,j;FILE *fp;system("cls");printf("\n\t\t|* **** 欢迎进入增加联系人界面! **** *|\n\n");printf("请输入增加的个数:\n");scanf("%d",&j);k=&j;(*len)+=j;//system("cls");if((fp=fopen("e://file3","a+"))==NULL){printf("error!\n");return -1;}for(i=0;i<j;i++){printf("姓名:");scanf("%s",&p[i].szName);printf("电话:");scanf("%lf",&p[i].number);printf("地址:");scanf("%s",p[i].adress);}for(i=0;i<j;i++)fprintf(fp,"\n%s %lf %s ",p[i].szName,p[i].number,p[i].adress);fclose(fp);output(len);return 0;}int chaxun(int *len){int i;CStudent aa,cc;system("cls");printf("\n\t\t|* **** 欢迎进入查询联系人界面! **** *|\n\n");do{printf(" |-------查找功能------|\n");printf(" | |\n");printf(" | 1 按电话号码查找|\n");printf(" | |\n");printf(" | 2 按姓名查找|\n");printf(" | |\n");printf(" | 0 退出|\n");printf(" | |\n");printf("请输入:\n");scanf("%d",&i);}while(i>2||i<0);if(i==2){printf("请输入姓名:\n");scanf("%s",&aa.szName);sname(aa,len);}else if(i==1){printf("请输入号码:\n");scanf("%lf",&cc.number);snumber(cc,len);}elseprintf("欢迎下次使用电话薄管理系统!\n");return 0;}int xiugai(int *len){CStudent p[100],q[100],a;int i,j,b;FILE *fp;system("cls");printf("\n\t\t|* **** 欢迎进入修改联系人界面! **** *|\n\n");printf("请输入修改人的姓名:\n");scanf("%s",a.szName);if((fp=fopen("e://file3","r+"))==NULL){printf("error!\n");return -1;}for(i=0;i<*len;i++)fscanf(fp,"%s%lf%s",p[i].szName,&p[i].number,p[i].adress);for(i=0;i<*len;i++){strcpy(q[i].szName,p[i].szName);strcpy(q[i].adress,p[i].adress);q[i].number=p[i].number;}for(i=0;i<*len;i++){if(strcmp(p[i].szName,a.szName)==0){j=1;break;}}if(j==1){printf(" |-------修改功能------|\n");printf(" | |\n");printf(" | 1 修改姓名|\n");printf(" | |\n");printf(" | 2 修改号码|\n");printf(" | |\n");printf(" | 3 修改地址|\n");printf(" | |\n");do{printf("请选择修改内容:\n");scanf("%d",&b);if(b==1)xname(q,i);if(b==2)xnumber(q,i);if(b==3)xadress(q,i);}while(b>3||b<1);printf("修改的联系人重新保存!\n");sput(len,q);}else{printf(" |*****通讯录中没有该人的信息!******|\n");system("pause");fclose(fp);output(len);return 0;}void xname(CStudent q[100],int i){printf("姓名重置:");scanf("%s",q[i].szName);}void xnumber(CStudent q[100],int i) /*号码修改*/{printf("电话重置:");scanf("%lf",&q[i].number);}void xadress(CStudent q[100],int i) /*地址修改*/{printf("地址重置:");scanf("%s",q[i].adress);}int shanchu(int *len){CStudent p[100],q[100],a;int i,j;FILE *fp;system("cls");printf("\n\t\t|* **** 欢迎进入删除联系人界面! **** *|\n\n");printf("请输入删除人的姓名:\n");scanf("%s",a.szName);if((fp=fopen("e://file3","r+"))==NULL){printf("error!\n");return -1;}for(i=0;i<*len;i++)fscanf(fp,"%s%lf%s",p[i].szName,&p[i].number,p[i].adress);for(i=0;i<*len;i++){strcpy(q[i].szName,p[i].szName);strcpy(q[i].adress,p[i].adress);q[i].number=p[i].number;}for(i=0;i<*len;i++)if(strcmp(p[i].szName,a.szName)==0){j=1;break;}}if(j==1){for(;i<*len-1;i++){strcpy(q[i].szName,q[i+1].szName);strcpy(q[i].adress,p[i].adress);q[i].number=q[i+1].number;}printf("删除成功!\n");(*len)--;sput(len,q);for(i=0;i<*len;i++)fscanf(fp,"%s%lf%s",q[i].szName,&q[i].number,q[i].adress);output(len);}elseprintf(" |*****通讯录中没有该人的信息!******|\n");fclose(fp);return 0;}int sput(int *len,CStudent q[100]){int i;FILE *fp;if((fp=fopen("e://file3","w"))==NULL){printf("error!\n");return -1;}for(i=0;i<*len;i++)fprintf(fp,"%s %lf %s ",q[i].szName,q[i].number,q[i].adress);fclose(fp);return 0;}search(CStudent p,int *j,CStudent c[100],int *len) /*查询*/{FILE *fp;int i,flag=0;if((fp=fopen("e:\\file3","r"))==NULL){printf("cannot write file\n");return -1;}for(i=0;i<*len;i++){fscanf(fp,"%s%lf%s",c[i].szName,&c[i].number,c[i].adress);if(strcmp(c[i].szName,p.szName)==0){flag=1;break;}if(c[i].number==p.number){flag=2;break;}}*j=i;fclose(fp);return flag;}int sname(CStudent p,int *len){CStudent c[100];int i,flag;flag=search(p,&i,c,len);if (flag==1){printf("%s%s%s",header1,header2,header3);printf("| %s | %.0lf | %s |\n",c[i].szName,c[i].number,c[i].adress);printf("%s",end);printf("\n");}elseprintf("Not Found\n");system("pause");return 0;}int snumber(CStudent p,int *len){CStudent c[100];int i,flag;flag=search(p,&i,c,len);if (flag==2){printf("%s%s%s",header1,header2,header3);printf("| %s | %.0lf | %s |\n",c[i].szName,c[i].number,c[i].adress);printf("%s",end);printf("\n");}elseprintf("Not Found\n");system("pause");return 0;}int output(int *len) /*向屏幕输出*/{CStudent p[100];int i;FILE *fp;if((fp=fopen("e://file3","r"))==NULL){printf("error!\n");return -1;}printf("%s%s%s",header1,header2,header3);for(i=0;i<*len;i++){fscanf(fp,"%s%lf%s",p[i].szName,&p[i].number,p[i].adress);printf("| %s | %.0lf | %s |\n",p[i].szName,p[i].number,p[i].adress);}printf("%s",end);printf("\n");system("pause");return 0;}。

c语言编写通讯录程序源代码

c语言编写通讯录程序源代码

c语言编写通讯录程序源代码以下是一个简单的 C 语言编写的通讯录程序示例,包含了添加、查找、修改和删除联系人的功能:```c#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX_CONTACTS 100// 联系人结构体struct Contact {char name[50];char number[20];};// 通讯录数组struct Contact contacts[MAX_CONTACTS];// 通讯录大小int size = 0;// 添加联系人void addContact() {if (size >= MAX_CONTACTS) {printf("通讯录已满,无法添加更多联系人。

\n");return;}printf("请输入联系人姓名:");scanf("%s", contacts[size].name);printf("请输入联系人号码:");scanf("%s", contacts[size].number);size++;printf("联系人已添加。

\n");}// 查找联系人void findContact() {char name[50];printf("请输入要查找的联系人姓名:");scanf("%s", name);for (int i = 0; i < size; i++) {if (strcmp(contacts[i].name, name) == 0) {printf("姓名:%s\n", contacts[i].name); printf("号码:%s\n", contacts[i].number); return;}}printf("未找到该联系人。

C语言_通讯录

C语言_通讯录

/*10.3.2源程序*//******头文件(.h)***********/#include "stdio.h" /*I/O函数*/#include "stdlib.h" /*标准库函数*/#include "string.h"/*字符串函数*/#include "ctype.h" /*字符操作函数*/#define M 50 /*定义常数表示记录数*/typedef struct /*定义数据结构*/{char name[20]; /*姓名*/char units[30]; /*单位*/char tele[10]; /*电话*/}ADDRESS;/******以下是函数原型*******/int enter(ADDRESS t[]); /*输入记录*/void list(ADDRESS t[],int n); /*显示记录*/void search(ADDRESS t[],int n); /*按姓名查找显示记录*/int delete(ADDRESS t[],int n); /*删除记录*/int add(ADDRESS t[],int n); /*插入记录*/void save(ADDRESS t[],int n); /*记录保存为文件*/int load(ADDRESS t[]); /*从文件中读记录*/void display(ADDRESS t[]); /*按序号查找显示记录*/void sort(ADDRESS t[],int n); /*按姓名排序*/void qseek(ADDRESS t[],int n); /*快速查找记录*/void copy(); /*文件复制*/void print(ADDRESS temp); /*显示单条记录*/int find(ADDRESS t[],int n,char *s) ; /*查找函数*/int menu_select(); /*主菜单函数*//******主函数开始*******/main(){int i;ADDRESS adr[M]; /*定义结构体数组*/int length; /*保存记录长度*/clrscr(); /*清屏*/for(;;)/*无限循环*/{switch(menu_select()) /*调用主菜单函数,返回值整数作开关语句的条件*/{case 0:length=enter(adr);break;/*输入记录*/case 1:list(adr,length);break; /*显示全部记录*/case 2:search(adr,length);break; /*查找记录*/case 3:length=delete(adr,length);break; /*删除记录*/case 4:length=add(adr,length); break; /*插入记录*/ case 5:save(adr,length);break; /*保存文件*/case 6:length=load(adr); break; /*读文件*/case 7:display(adr);break; /*按序号显示记录*/case 8:sort(adr,length);break; /*按姓名排序*/case 9:qseek(adr,length);break; /*快速查找记录*/case 10:copy();break; /*复制文件*/case 11:exit(0); /*如返回值为11则程序结束*/}}}/*菜单函数,函数返回值为整数,代表所选的菜单项*/menu_select(){char s[80];int c;gotoxy(1,25);/*将光标定为在第25行,第1列*/printf("press any key enter menu......\n");/*提示压任意键继续*/ getch(); /*读入任意字符*/clrscr(); /*清屏*/gotoxy(1,1);printf("********************MENU*********************\n\n"); printf(" 0. Enter record\n");printf(" 1. List the file\n");printf(" 2. Search record on name\n");printf(" 3. Delete a record\n");printf(" 4. add record \n");printf(" 5. Save the file\n");printf(" 6. Load the file\n");printf(" 7. display record on order\n");printf(" 8. sort to make new file\n");printf(" 9. Quick seek record\n");printf(" 10. copy the file to new file\n");printf(" 11. Quit\n");printf("***********************************************\n"); do{printf("\n Enter you choice(0~11):"); /*提示输入选项*/ scanf("%s",s); /*输入选择项*/c=atoi(s); /*将输入的字符串转化为整型数*/}while(c<0||c>11); /*选择项不在0~11之间重输*/return c; /*返回选择项,主程序根据该数调用相应的函数*/}/***输入记录,形参为结构体数组,函数值返回类型为整型表示记录长度*/int enter(ADDRESS t[]){int i,n;char *s;clrscr(); /*清屏*/printf("\nplease input num \n"); /*提示信息*/scanf("%d",&n); /*输入记录数*/printf("please input record \n"); /*提示输入记录*/printf("name unit tele phone\n");printf("------------------------------------------------\n"); for(i=0;i<n;i++){scanf("%s%s%s",t[i].name,t[i].units,t[i].tele); /*输入记录*/printf("----------------------------------------------\n"); }return n; /*返回记录条数*/}/*显示记录,参数为记录数组和记录条数*/void list(ADDRESS t[],int n){int i;clrscr();printf("\n\n*******************ADDRESS******************\n"); printf("name unit te lephone\n");printf("------------------------------------------------\n"); for(i=0;i<n;i++)printf("%-20s%-30s%-10s\n",t[i].name,t[i].units,t[i].tele);if((i+1)%10==0) /*判断输出是否达到10条记录*/{printf("Press any key continue...\n"); /*提示信息*/getch(); /*压任意键继续*/}printf("************************end*******************\n");}/*查找记录*/void search(ADDRESS t[],int n){char s[20]; /*保存待查找姓名字符串*/int i; /*保存查找到结点的序号*/clrscr(); /*清屏*/printf("please search name\n");scanf("%s",s); /*输入待查找姓名*/i=find(t,n,s); /*调用find函数,得到一个整数*/if(i>n-1) /*如果整数i值大于n-1,说明没找到*/printf("not found\n");elseprint(t[i]); /*找到,调用显示函数显示记录*/}/*显示指定的一条记录*/void print(ADDRESS temp){clrscr();printf("\n\n********************************************\n"); printf("name unit telephone\n");printf("------------------------------------------------\n"); printf("%-20s%-30s%-10s\n",,temp.units,temp.tele);printf("**********************end***********************\n"); }/*查找函数,参数为记录数组和记录条数以及姓名s */int find(ADDRESS t[],int n,char *s){int i;for(i=0;i<n;i++)/*从第一条记录开始,直到最后一条*/{if(strcmp(s,t[i].name)==0) /*记录中的姓名和待比较的姓名是否相等*/return i; /*相等,则返回该记录的下标号,程序提前结结束*/}return i; /*返回i值*/}/*删除函数,参数为记录数组和记录条数*/int delete(ADDRESS t[],int n){char s[20]; /*要删除记录的姓名*/int ch=0;int i,j;printf("please deleted name\n"); /*提示信息*/scanf("%s",s);/*输入姓名*/i=find(t,n,s); /*调用find函数*/if(i>n-1) /*如果i>n-1超过了数组的长度*/printf("no found not deleted\n"); /*显示没找到要删除的记录*/ else{print(t[i]); /*调用输出函数显示该条记录信息*/printf("Are you sure delete it(1/0)\n"); /*确认是否要删除*/scanf("%d",&ch); /*输入一个整数0或1*/if(ch==1) /*如果确认删除整数为1*/{for(j=i+1;j<n;j++) /*删除该记录,实际后续记录前移*/{strcpy(t[j-1].name,t[j].name); /*将后一条记录的姓名拷贝到前一条*/strcpy(t[j-1].units,t[j].units); /*将后一条记录的单位拷贝到前一条*/strcpy(t[j-1].tele,t[j].tele); /*将后一条记录的电话拷贝到前一条*/}n--; /*记录数减1*/}}return n; /*返回记录数*/}/*插入记录函数,参数为结构体数组和记录数*/int add(ADDRESS t[],int n)/*插入函数,参数为结构体数组和记录数*/{ADDRESS temp; /*新插入记录信息*/int i,j;char s[20]; /*确定插入在哪个记录之前*/printf("please input record\n");printf("************************************************\n"); printf("name unit telephone\n");printf("--------------------------------------------------\n"); scanf("%s%s%s",,temp.units,temp.tele); /*输入插入信息*/printf("------------------------------------------------\n"); printf("please input locate name \n");scanf("%s",s); /*输入插入位置的姓名*/i=find(t,n,s); /*调用find,确定插入位置*/for(j=n-1;j>=i;j--) /*从最后一个结点开始向后移动一条*/{strcpy(t[j+1].name,t[j].name); /*当前记录的姓名拷贝到后一条*/strcpy(t[j+1].units,t[j].units); /*当前记录的单位拷贝到后一条*/strcpy(t[j+1].tele,t[j].tele); /*当前记录的电话拷贝到后一条*/}strcpy(t[i].name,); /*将新插入记录的姓名拷贝到第i个位置*/strcpy(t[i].units,temp.units); /*将新插入记录的单位拷贝到第i 个位置*/strcpy(t[i].tele,temp.tele); /*将新插入记录的电话拷贝到第i个位置*/n++; /*记录数加1*/return n; /*返回记录数*/}/*保存函数,参数为结构体数组和记录数*/void save(ADDRESS t[],int n){int i;FILE *fp; /*指向文件的指针*/if((fp=fopen("record.txt","wb"))==NULL) /*打开文件,并判断打开是否正常*/{printf("can not open file\n");/*没打开*/exit(1); /*退出*/}printf("\nSaving file\n"); /*输出提示信息*/fprintf(fp,"%d",n); /*将记录数写入文件*/fprintf(fp,"\r\n"); /*将换行符号写入文件*/for(i=0;i<n;i++){fprintf(fp,"%-20s%-30s%-10s",t[i].name,t[i].units,t[i].tele );/*格式写入记录*/fprintf(fp,"\r\n"); /*将换行符号写入文件*/}fclose(fp);/*关闭文件*/printf("****save success***\n"); /*显示保存成功*/}/*读入函数,参数为结构体数组*/int load(ADDRESS t[]){int i,n;FILE *fp; /*指向文件的指针*/if((fp=fopen("record.txt","rb"))==NULL)/*打开文件*/{printf("can not open file\n"); /*不能打开*/exit(1); /*退出*/}fscanf(fp,"%d",&n); /*读入记录数*/for(i=0;i<n;i++)fscanf(fp,"%20s%30s%10s",t[i].name,t[i].units,t[i].tele); /*按格式读入记录*/fclose(fp); /*关闭文件*/printf("You have success read data from file!!!\n"); /*显示保存成功*/return n; /*返回记录数*/}/*按序号显示记录函数*/void display(ADDRESS t[]){int id,n;FILE *fp; /*指向文件的指针*/if((fp=fopen("record.txt","rb"))==NULL) /*打开文件*/{printf("can not open file\n"); /*不能打开文件*/exit(1); /*退出*/}printf("Enter order number...\n"); /*显示信息*/scanf("%d",&id); /*输入序号*/fscanf(fp,"%d",&n); /*从文件读入记录数*/if(id>=0&&id<n) /*判断序号是否在记录范围内*/{fseek(fp,(id-1)*sizeof(ADDRESS),1); /*移动文件指针到该记录位置*/print(t[id]); /*调用输出函数显示该记录*/printf("\r\n");}elseprintf("no %d number record!!!\n ",id); /*如果序号不合理显示信息*/fclose(fp); /*关闭文件*/}/*排序函数,参数为结构体数组和记录数*/void sort(ADDRESS t[],int n){int i,j,flag;ADDRESS temp; /*临时变量做交换数据用*/for(i=0;i<n;i++){flag=0; /*设标志判断是否发生过交换*/for(j=0;j<n-1;j++)if((strcmp(t[j].name,t[j+1].name))>0) /*比较大小*/{flag=1;strcpy(,t[j].name); /*交换记录*/strcpy(temp.units,t[j].units);strcpy(temp.tele,t[j].tele);strcpy(t[j].name,t[j+1].name);strcpy(t[j].units,t[j+1].units);strcpy(t[j].tele,t[j+1].tele);strcpy(t[j+1].name,);strcpy(t[j+1].units,temp.units);strcpy(t[j+1].tele,temp.tele);}if(flag==0)break; /*如果标志为0,说明没有发生过交换循环结束*/}printf("sort sucess!!!\n"); /*显示排序成功*/}/*快速查找,参数为结构体数组和记录数*/void qseek(ADDRESS t[],int n){char s[20];int l,r,m;printf("\nPlease sort before qseek!\n"); /*提示确认在查找之前,记录是否已排序*/printf("please enter name for qseek\n"); /*提示输入*/scanf("%s",s); /*输入待查找的姓名*/l=0;r=n-1; /*设置左边界与右边界的初值*/while(l<=r) /*当左边界<=右边界时*/{m=(l+r)/2; /*计算中间位置*/if(strcmp(t[m].name,s)==0) /*与中间结点姓名字段做比较判是否相等*/{print(t[m]); /*如果相等,则调用print函数显示记录信息*/return ; /*返回*/}if(strcmp(t[m].name,s)<0) /*如果中间结点小*/l=m+1; /*修改左边界*/elser=m-1; /*否则,中间结点大,修改右边界*/}if(l>r) /*如果左边界大于右边界时*/printf("not found\n"); /*显示没找到*/}/*复制文件*/void copy(){char outfile[20]; /*目标文件名*/int i,n;ADDRESS temp[M]; /*定义临时变量*/FILE *sfp,*tfp; /*定义指向文件的指针*/clrscr();/*清屏*/if((sfp=fopen("record.txt","rb"))==NULL) /*打开记录文件*/{printf("can not open file\n"); /*显示不能打开文件信息*/exit(1); /*退出*/}printf("Enter outfile name,for example c:\\f1\\te.txt:\n"); /*提示信息*/scanf("%s",outfile); /*输入目标文件名*/if((tfp=fopen(outfile,"wb"))==NULL) /*打开目标文件*/{printf("can not open file\n"); /*显示不能打开文件信息*/exit(1); /*退出*/}fscanf(sfp,"%d",&n); /*读出文件记录数*/fprintf(tfp,"%d",n);/*写入目标文件数*/fprintf(tfp,"\r\n"); /*写入换行符*/for(i=0;i<n;i++){fscanf(sfp,"%20s%30s%10s\n",temp[i].name,temp[i].units, temp[i].tele); /*读入记录*/fprintf(tfp,"%-20s%-30s%-10s\n",temp[i].name,temp[i].units,temp[i].tele); /*写入记录*/fprintf(tfp,"\r\n"); /*写入换行符*/}fclose(sfp); /*关闭源文件*/fclose(tfp); /*关闭目标文件*/printf("you have success copy file!!!\n"); /*显示复制成功*/}1:插入新的记录:2:删除记录的姓名:3:输入记录:4:查找函数:5读入函数:6主函数:7菜单函数:8:复制文件:9:按序号显示记录函数:10:显示记录:11:显示指定的某一条记录:12:快速查找:13保存函数:14:查找记录;15:排序函数:。

通讯录C语言程序

通讯录C语言程序

#include <stdio.h>#include <stdlib.h>#include <ctype.h>#define MAX_WORD_LENGTH 20#define MAX_WORD_COUNT 20intcpstring( char* desstr, char* str1, char* str2 ){if ( str1 >= str2 ){return -1;}while ( str1 != str2 ){*desstr = *str1;str1++;desstr++;}*desstr = '\0';return 0;}intstrcompare( char* str1, char* str2 ){while ( str1!='\0' && str2!='\0' &&isalpha(*str1) &&isalpha(*str2) && *str1==*str2 ) {str1++;str2++;}if ( (str1=='\0' && str2=='\0')||!isalpha(*str1)||!isalpha(*str2) ){return 0;}else{return -1;}}char* get1stword( char* string ){char* pend = string;while ( isalpha(*pend) ){pend++;}return pend;}char* getwordfrommainstr( char* mainstr, char* substr ){char* pm = mainstr;char* ps = substr;char* pend = 0;if ( *pm == '\0' ){return NULL;}while ( 1 ){if ( strcompare( pm, ps ) == 0 ){return pm;}else{pend = get1stword( pm );if ( *pend == '\0' ){return NULL;}pm = ++pend;}}}intischecked( char** checkedword, char* str ){intwordcount;char* p;for ( wordcount=0; wordcount<MAX_WORD_COUNT; wordcount++ ){if ( *(checkedword+wordcount*MAX_WORD_LENGTH)=='\0' ){return -1;}p = getwordfrommainstr(checkedword+wordcount*MAX_WORD_LENGTH, str );if ( p != NULL ){if ( strcompare( p, str ) == 0 ){return 0;}}}if ( p == NULL ){return -1;}}intsavetochecked( char** checkedword, char* str ){intwordcount;for(wordcount=0;wordcount<MAX_WORD_COUNT&&*(checkedword+wordcount*MAX_WORD_ LENGTH)!='\0'; wordcount++ ){}intwordlength;char* p1 = str;char* p2 = checkedword+wordcount*MAX_WORD_LENGTH;for ( wordlength=0; wordlength<MAX_WORD_LENGTH && (*p1)!='\0'; wordlength++,p1++,p2++ ) {*p2 = *p1;}*p2 = '\0';return 0;}intshowcheckedword( char** checkedword ){intwordcount = 0;printf( "-----\n" );while ( wordcount< MAX_WORD_COUNT*MAX_WORD_LENGTH ){wordcount++;}printf( "-----\n" );return 0;}int main(){char s1[]="this is a test, test";char s2[]="it is a great test";charcheckedword[MAX_WORD_COUNT][MAX_WORD_LENGTH];memset(checkedword, '\0', MAX_WORD_COUNT*MAX_WORD_LENGTH );char* p1 = s1;char word[MAX_WORD_LENGTH];while ( *p1 != '\0' ){memset( word, '\0', MAX_WORD_LENGTH );char* pend;pend = get1stword( p1 );cpstring( word, p1, pend );if ( 0 == ischecked( checkedword, word ) ){p1 = ++pend;continue;}char* p = getwordfrommainstr( s2, word );if ( p == NULL ){printf( "=======没有找到========\n%s\n", word );}else{printf( "=======已经找到========\n%s\n", word );}p1 = ++pend;}return 0;}。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
if(strcmp(t->xm,mt)!=0)
{
printf("您输入的人名不存在!\001\n");
xuanxiang();
}
else xuanxiang();
}
void main()
{
printf("欢迎使用通讯录\n");
counter1=yuanyour();
xuanxiang();
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct txl
{
char xm[11];
char dz[11];
char yb[11];
char dh[11];
}A;
FILE *fp;
int rs;
int counter1=0;
else printf("请输入:");
for(i=0;;i++)
{
scanf("%s",&a[i].xm);
scanf("%s",&a[i].dz);
scanf("%s",&a[i].yb);
scanf("%s",&a[i].dh);
if(strcmp(a[i].xm,"0")==0)break;
if((fp=fopen("f:\\tongxunlu.dat","ab+"))==NULL)
{
printf("can not open this file\n");
exit(0);
}
printf("如果现在想退出输入,请输0继续1\n");
scanf("%d",&l);
if(l==0)xuanxiang();
{
printf("can not open this file\n");
exit(0);
}
printf("原通讯录中人数有:%d人!!!\n",counter1);
fread(&b,sizeof(A),1,fp);
printf("姓名\t\t邮编\t\t地址\t\t电话\n");
for(i=0;!feof(fp);i++)
if(m==2)shuchu();
if(m==3)chazhaom();
if(m==0)
{
printf("谢谢使用!欢迎再用!再见!\001\n");
}
if(m!=1&&m!=2&&m!=3&&m!=4&&m!=5&&m!=6&&m!=0)
{
printf("输入错误,请重输!!!\n");
printf("输入(1)、返回主菜单(2)输出全部(3)查找(4)直接退出(0)\001\n");
void shuru();
void shuchu();
int yuanyour();
void chazhaom();
void xuanxiang()
{
int m;
printf("请输入选项:\n(1)输入(2)输出全部(3)按姓名查找(0)直接退出\001\n");
scanf("%d",&m);
if(m==1)shuru();
xuanxiang();
}
}
int yuanyour()
{
if((fp=fopen("f:\\tongxunlu.dat","ab+"))==NULL)
{
printf("can not open this file\n");
exit(0);
}
while(!feof(fp))
{
fgetc(fp);
counter1++;
else counter++;
}
for(i=0;i<counter;i++)
fwrite(&a[i],sizeof(A),1,fp);
fclose(fp);
rs=counter+counter1;
counter1=rs;
xuanxiang();
}
void shuchu()
{
A b;
int i;
if((fp=fopen("f:\\tongxunlu.dat","rb+"))==NULL)
scanf("%s",mt);
while(!feof(fp))
{
fread(t,sizeof(A),1,fp);
if(strcmp(t->xm,mt)==0)
{
printf("%11s%11s%11s%11s\n",t->xm,t->dz,t->yb,t->dh);
break;
}
}
fclose(fp);
}
fclose(fp);
counter1--;
counter1/=sizeof(A);
return counter1;
}
void shuru()
{
int i,counter=0,l;
A a[100];
printf("原通讯录中人数有:%d人!!!\n",counter1);
printf("请输入你的通讯录,例如:\t姓名通讯地址邮编联系电话\n\n最后输入“0 0 0 0回车”后结束!\n\n");
{
printf("no!");
exit(0);
}
if((fp=fopen("f:\\tongxunlu.dat","rb+"))==NULL)
{
printf("can not open this file\n&#r mt[11];
printf("请输入您要查找的人名:\n");
{
printf("%s\t\t%s%\t\ts%\t\t%s\n",b.xm,b.dz,b.yb,b.dh);
fread(&b,sizeof(A),1,fp);
}
fclose(fp);
xuanxiang();
}
void chazhaom()
{
A *t;
if(!(t=(A *)malloc(sizeof(A))))
相关文档
最新文档