数据结构通讯录源代码
通讯录源代码
源代码:#include <iostream>#include <string>#include <iomanip>#include <fstream>using namespace std;class One //定义一个类{public:static int num;static int compare_times;One(){num++;}~One(){num--;}void input();void print();int c_name(string t);int c_address(string t);int c_tel(string t);int c_postcode(string t);int c_Email(string t);int c_all(string t);void read_in(ifstream &in);void write_out(ofstream &out);friend void sort(One *p[]);private:string name;string address;string tel;string postcode;string Email;};int One::num=0;int One::compare_times=0;void One::input() //“输入”函数{cout<<endl<<"输入列表中第"<<One::num<<"个人的信息:"<<endl<<"姓名(限个字符) 地址(限个字符,1个汉字占个字符) 电话邮编E-mail"<<endl;cin>>name>>address>>tel>>postcode>>Email;}void One::print() //“输出”函数{cout<<setw(8)<<name<<setw(22)<<address<<setw(13)<<tel<<setw(8)<<postcode<<" "<<Email<<endl;}int compare(string a,string b);int get();int One::c_name(string t) //定义友员函数{return compare(name,t);}int One::c_address(string t){return compare(address,t);}int One::c_tel(string t){return compare(tel,t);}int One::c_postcode(string t){return compare(postcode,t);}int One::c_Email(string t){return compare(Email,t);}int compare(string a,string b) //“比较”函数{if(a<b)return -1;if(a==b){One::compare_times++;return 0;}elsereturn 1;}int One::c_all(string t){return(c_name(t)&&c_address(t)&&c_tel(t)&&c_postcode(t)&&c_Email(t)); }void One::read_in(ifstream &in) //“读取文件”函数{in>>name>>address>>tel>>postcode>>Email;}void One::write_out(ofstream &out) //“储存文件”函数{out<<' '<<name<<' '<<address<<' '<<tel<<' '<<postcode<<' '<<Email<<endl; }//function follow:void add(One *p[]);void seek(One *p[]);void sort(One *p[]);void del(One *p[]);void show(One *p[]);void read(One *p[]);void save(One *p[]);void error();int main() //主函数{One *p[51];bool exit=false;int key;do{system("cls");cout<<"1.添加2.查询3.排序4.删除5.查看6.读取7.储存0.退出"<<endl;key=get();switch(key){case 1:add(p);break;case 2:seek(p);break;case 3:sort(p);break;case 4:del(p);break;case 5:show(p);break;case 6:read(p);break;case 7:save(p);break;case 0:exit=true;break;default:error();}}while(!exit);return 0;}void add(One *p[]) //“添加”函数{int n,base_num=One::num;cout<<"你想添加几个人呢?(莫超过"<<50-One::num<<"个哦!)"<<endl;cin>>n;system("cls");if(n<=49-One::num)for(int i=0;i<n;i++){int t=base_num+i;p[t]=new One;p[t]->input();}}void seek(One *p[]) //“查询”函数{bool back=false;int key;system("cls");cout<<"查询:"<<endl<<"1.按名称2.按地址3.按电话4.按邮编5.按E-mail 6.全部"<<endl;key=get();string t;cout<<"请输入关键字:"<<endl;cin>>t;cout<<"查询结果:"<<endl;for(int i=0;i<One::num;i++){switch(key){case 1:if(!p[i]->c_name(t))p[i]->print();break;case 2:if(!p[i]->c_address(t))p[i]->print();break;case 3:if(!p[i]->c_tel(t))p[i]->print();break;case 4:if(!p[i]->c_postcode(t))p[i]->print();break;case 5:if(!p[i]->c_Email(t))p[i]->print();break;case 6:if(!p[i]->c_all(t))p[i]->print();break;default:error();}}if(One::compare_times==0)cout<<" 没有找到所需结果,请检查输入!"<<endl;One::compare_times=0;system("pause");}void exchange(One *&p1,One *&p2);void sort(One *p[]) //“排序”函数{bool back=false;int key;system("cls");cout<<"排序:"<<endl<<"1.按名称2.按地址3.按电话4.按邮编5.按E-mail"<<endl;key=get();for(int i=0;i<One::num-1;i++)for(int j=i+1;j<One::num;j++){switch(key){case 1:if(p[i]->c_name(p[j]->name)==1)exchange(p[i],p[j]);break;case 2:if(p[i]->c_address(p[j]->address)==1)exchange(p[i],p[j]);break;case 3:if(p[i]->c_tel(p[j]->tel)==1)exchange(p[i],p[j]);break;case 4:if(p[i]->c_postcode(p[j]->postcode)==1)exchange(p[i],p[j]);break;case 5:if(p[i]->c_Email(p[j]->Email)==1)exchange(p[i],p[j]);break;default:error();}}show(p);system("pause");}void exchange(One *&p1,One *&p2){One *temp;temp=p1;p1=p2;p2=temp;}void del_p(One *p[],int i);void del(One *p[]) //“删除”函数{bool back=false;int key,n=One::num;system("cls");cout<<"删除:"<<endl<<"1.按名称2.按地址3.按电话4.按邮编5.按E-mail 6.全部"<<endl;key=get();string t;cout<<"请输入关键字:"<<endl;cin>>t;cout<<"指定内容已被删除:"<<endl;for(int i=0;i<n;i++){switch(key){case 1:if(!p[i]->c_name(t)){p[i]->print();del_p(p,i);}break;case 2:if(!p[i]->c_address(t)){p[i]->print();del_p(p,i);}break;case 3:if(!p[i]->c_tel(t)){p[i]->print();del_p(p,i);}break;case 4:if(!p[i]->c_postcode(t)){p[i]->print();del_p(p,i);}break;case 5:if(!p[i]->c_Email(t)){p[i]->print();del_p(p,i);}break;case 6:if(!p[i]->c_all(t)){p[i]->print();del_p(p,i);}break;default:error();}}if(One::compare_times==0)cout<<" fail to find,please check up your input!"<<endl;One::compare_times=0;system("pause");}void del_p(One *p[],int i)int n=One::num;delete p[i];for(int j=i+1;j<n;j++){p[j-1]=p[j];}}void show(One *p[]){int t=One::num;for(int i=0;i<t;i++){p[i]->print();}system("pause");}void read(One *p[]) //“读取”函数{ifstream in("de.txt",ios::in);if(!in)cout<<"打开错误!"<<endl;elsefor(int i=0;i<6;i++){p[i]=new One;p[i]->read_in(in);}cout<<"读取成功!"<<endl;system("pause");}void save(One *p[]) //“储存”函数{ofstream out("de_out.txt",ios::out);if(!out)cout<<"打开文件错误!"<<endl;elsefor(int i=0;i<One::num;i++)p[i]->write_out(out);cout<<"打开文件成功!"<<endl;system("pause");}void error()cout<<"error"<<endl;system("pause");}int get(){int key;if(cin>>key)return key;else{error();cin.clear();return 0;}}。
通讯录源代码
#include<stdio.h>#include<string.h> //字符串处理//#include<conio.h>#define MY1 printf("\n姓名地址邮政编码电话\t")#define MY2 printf("\n%s %4s %s %s\n",s[i].name,s[i].address,s[i].code,s[i] .tel)#define MAX 20int i;int sum;/*时间结构体类型*/struct s_message{char name[20];char address[50];char code[15];char tel[15];}s[MAX];/*保存通讯录信息*/void save_message(int sum){int i;FILE *fp; //声明fp是指针,用来指向FILE类型的对象//if((fp=fopen("D:\\s.txt","wb"))==NULL){printf("文件有误!\n");return;}for (i=0;i<sum;i++){if(fwrite(&s[i],sizeof(struct s_message),1,fp)!=1) //fwite写一个数据块//printf("writing file is wrong!\n");}fclose(fp);}/*读取通讯录信息*/int read_message(){FILE *fp;int i=0;if((fp=fopen("D:\\s.txt","rb"))==NULL){printf("\n\n***********暂时无任何库存信息,按回车键进入主菜单选择基本信息的录入!***********\n");return 0;}while(feof(fp)==0) /*feof(fp)有两个返回值:如果遇到文件结束,函数feof(fp)的值为1,否则为0。
通讯录-源程序
#include<stdio.h> #include<malloc.h> #include<stdlib.h> #include<string.h> #define NULL 0#define LEN sizeof(struct student) struct student {char num[20]; char name[10]; char age[5]; char tel[20]; char adress[50]; char email[20]; stru ct student *next;};int n; struct student *creat(void) {char string[]={"结束"}; struct student *head; struct student *p1,*p2; n=0;p1=p2=(struct student *)malloc(LEN); printf("请输入编号\n"); scanf("%s",&p1->num); printf("请输入姓名\n"); scanf("%s",&p1->name); printf("请输入年龄\n"); scanf("%s",&p1->age);printf("请输入电话号码\n"); scanf("%s",&p1->tel);printf("请输入通讯地址\n"); scanf("%s",&p1->adress); printf("请输入电子邮箱\n"); scanf("%s",&p1->email); head=NULL;while(strcmp(p1->num,string)!=0) {n=n+1;if(n==1) head=p1; else p2->next=p1; p2=p1;p1=(struct student *)malloc(LEN); printf("请输入编号\n"); scanf("%s",&p1->num); printf("请输入姓名\n"); scanf("%s",&p1->name);printf("请输入年龄\n");scanf("%s",&p1->age);printf("请输入电话号码\n"); scanf("%s",&p1->tel);printf("请输入通讯地址\n"); scanf("%s",&p1->adress); printf("请输入电子邮箱\n"); scanf("%s",&p1->email); }p2->next=NULL; return(head);}struct student *del( struct student *head, char num[20]) {struct student *p1,*p2; if(head==NULL) {printf("\n list null!\n");return head;} p1=head;while(strcmp(p1->num,num)!=0&&p1->next!=NULL) {p2=p1;p1=p1->next; }if(strcmp(p1->num,num)==0) { if(p1==head) head=p1->next; else p2->next=p1->next;n=n-1;}else printf("%s not been found!\n",num); return (head);}struct student *insert(struct student *head,struct student *stud) {struct student *p0,*p1,*p2;p0=p1=p2=(struct student *)malloc(LEN); p1=head; p0=stud;if(head==NULL){head=p0;p0->next=NULL;} else{while((p0->num>p1->num)&&(p1->next!=NULL)) {p2=p1;p1=p1->next;} if(p0->num<=p1-> num){if(head==p1) head=p0;else p2->next=p0; p0->next=p1; }else {p1->next=p0;p0->next=NULL;} } n=n+1; return(head);}struct student *namesearch(struct student *head) {struct student *p;p=(struct student *)malloc(LEN); char name[10]; p=head;printf("请输入姓名\n");scanf("%s",name);while(p!=NULL&&strcmp(p->name,name)!=0) p=p->next;printf("编号姓名年龄电话号码通讯地址电子邮箱\n");printf("%s %s %s %s %s %s\n",p->num,p->name,p->age,p->tel,p->adress,p->email); return( head); }struct student *numsearch( struct student *head) {struct student *p;p=(struct student *)malloc(LEN); char num[20]; p=head; printf("请输入编号\n"); scanf("%s",num);while(p!=NULL&&strcmp(p->num,num)!=0) p=p->next;printf("编号姓名年龄电话号码通讯地址电子邮箱\n");printf("%s %s %s %s %s %s\n",p->num,p->name,p->age,p->tel,p->adress,p->email); retur n(head); }struct student *messagechange( struct student *head) {struct student *p;p=(struct student *)malloc(LEN); char name[10]; p=head;printf("请输入姓名\n");scanf("%s",name); while(p!=NULL&&strcmp(p->name,name)!=0) p=p->next;printf("请输入编号\n"); scanf("%s",&p->num); printf("请输入姓名\n"); scanf("%s",&p->name); printf("请输入年龄\n"); scanf("%s",&p->age);printf("请输入电话号码\n"); scanf("%s",&p->tel);printf("请输入通讯地址\n"); scanf("%s",&p->adress); printf("请输入电子邮箱\n");scanf("%s",&p->email);return(head); }void print(struct student*head) {struct student *p; p=head;printf("编号姓名年龄电话通讯地址电子邮箱\n"); if(head!=NULL) do{printf("%s %s %s %s %s %s\n",&p->num,&p->name,&p->age,&p->tel,&p->adress,&p->ema il); p=p->next;} while(p!=NULL);}void save(struct student *head) { FILE *fp; int i,n=0;struct student *p;if((fp=fopen("stu_list","wb"))==NULL) {printf("\n\t\t打开文件失败!!!\n");return; }p=head->next;if(p!=NULL){ while (p!=NULL) { p=p->next; n++; }fprintf(fp,"%d",n); fprintf(fp,"\r\n");p=head->next; for(i=0;i<n;i++) {fprintf(fp,"\n\t\t%s,%s,%s,%s,%s,%s",p->num,p->name,p->age,p->tel,p->adress,p->email); fpri ntf(fp,"\t\t\r\n"); p=p->next;}}else printf("Please make sure the data is not NULL!!"); fclose(fp);printf("\n\t\t存盘成功!!!\n");}void quit(void) { exit(0); }void main() {struct student *head,*stud; int flage=1; for(;flage==1;) {int ch;printf("\n\t\t通讯录系统:\n");printf("\t\t=================================\n"); printf("\t\t 输入记录-----------------0\n"); printf("\t\t 显示所有记录-------------1\n"); printf("\t\t 按编号查询记录-----------2\n");printf("\t\t 按姓名查询记录-----------3\n");printf("\t\t 添加信息-----------4\n"); printf("\t\t 修改记录-------------5\n"); printf("\t\t 删除记录-----------------6\n"); printf("\t\t存盘---------------------7\n");printf("\t\t 退出---------------------8\n");printf("\t\t=================================\n");printf("\t\t请输入数字(0~8):");scanf("%d",&ch); switch(ch){case 0:head=creat(); printf("是否继续;1/0\n"); scanf("%d",&flage);break;case 1:print (head);break; case 2:head=numsearch(head); printf("是否继续;1/0\n"); scanf("%d",&flage);break; case 3:head=namesearch(head); printf("是否继续;1/0\n");scanf("%d",&flage);break;case 4:stud=(struct student *) malloc(LEN); head=insert(head,stud); printf("请输入编号\n"); scanf("%s",&stud->num); printf("请输入姓名\n"); scanf("%s",&stud->name); printf("请输入年龄\n"); scanf("%s",&stud->age); printf("请输入电话号码\n"); scanf("%s",&stud->tel); printf("请输入通讯地址\n"); scanf("%s",&stud->adress);printf("请输入电子邮箱\n");scanf("%s",&stud->email); printf("是否继续;1/0\n");scanf("%d",&flage);break;case 5: head=messagechange(head); printf("是否继续;1/0\n"); scanf("%d",&flage);break; case 6:char num[20];printf("请输入编号\n");scanf("%s",num); head=del(head,num); printf("是否继续;1/0\n"); scanf("%d",&flage);break;case 7:save(head);break; case 8:quit();break;default:printf("\n\t\t警告!输入错误,请输入数字0-8!");}}}。
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);}。
手机通讯录源代码
手机通讯录源代码手机通讯录源代码#include#include#include#include#define N 15 //最大容量为15typedef struct //定义一个结构{char name[10];char haoma[15];char leibie[10];char dizhi[20];}ren;//菜单void menu() //目录{system("cls"); //清屏printf(" #########欢迎使用手机通讯录#########\n");printf("\n");printf("================================= ========================================= ====\n");printf("1.~~号码查询~~ \n");printf("2.~~号码添加~~ \n");printf("3.~~号码拨号~~ \n");printf("4.~~号码修改~~ \n");printf("5.~~号码删除~~ \n");printf("0. ~~退出~~ \n");printf("================================= ========================================= ====\n");printf("\n");printf("个人通讯录\n");printf("\n");printf(" ~~请选择相应的功能~~:");}void input() //查询{int help=0; //帮助指令,如果查找所找的类别中没有数据,显示ren ry;FILE *fp;char leibie[10];system("cls");printf("\n 输入要查人员的类别:");scanf("%s",leibie);//要查询的类别查找失败if((fp=fopen("ren.dat","rb"))==NULL){printf("can't open file!\n");exit(0);}fread(&ry,sizeof(ren),1,fp);if(!feof(fp)){ printf(" 人员的信息如下:\n");printf("说明:(A办公类B个人类C商务类)");printf("\n%10s %15s %10s %20s \n","姓名","号码","类别","邮箱");while(!feof(fp)){if(strcmp(ry.leibie,leibie)==0) //若为该类别则输出{ help++;printf("%10s %15s %10s %20s\n",/doc/8f1229847.html,,ry.haoma,ry. leibie,ry.dizhi);}fread(&ry,sizeof(ren),1,fp);}if(help==0){printf(" 查找失败");}。
手机通讯录源代码
手机通讯录源代码#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <string.h>#define N 15 //最大容量为15typedef struct //定义一个结构{char name[10];char haoma[15];char leibie[10];char dizhi[20];}ren;//菜单void menu() //目录{system("cls"); //清屏printf(" #########欢迎使用手机通讯录#########\n");printf("\n");printf("================================= ========================================= ====\n");printf("1.~~号码查询~~ \n");printf("2.~~号码添加~~ \n");printf("3.~~号码拨号~~ \n");printf("4.~~号码修改~~ \n");printf("5.~~号码删除~~ \n");printf("0. ~~退出~~ \n");printf("================================= ========================================= ====\n");printf("\n");printf("个人通讯录\n");printf("\n");printf(" ~~请选择相应的功能~~:");}void input() //查询{int help=0; //帮助指令,如果查找所找的类别中没有数据,显示ren ry;FILE *fp;char leibie[10];system("cls");printf("\n 输入要查人员的类别:");scanf("%s",leibie);//要查询的类别查找失败if((fp=fopen("ren.dat","rb"))==NULL){printf("can't open file!\n");exit(0);}fread(&ry,sizeof(ren),1,fp);if(!feof(fp)){ printf(" 人员的信息如下:\n");printf("说明:(A办公类B个人类C商务类)");printf("\n%10s %15s %10s %20s \n","姓名","号码","类别","邮箱");while(!feof(fp)){if(strcmp(ry.leibie,leibie)==0) //若为该类别则输出{ help++;printf("%10s %15s %10s %20s\n",,ry.haoma,ry.leibie,ry.dizhi);}fread(&ry,sizeof(ren),1,fp);}if(help==0){printf(" 查找失败!!!");}}// else printf(" 查找失败!!!");fclose(fp);getch();}//修改:输入人员的姓名,查找该人员,若找到则修改该人员的信息,并显示修改前后的结果;//号码添加void search(){int n;FILE *fp; //定义文件指针ren ry,s; // 定义两个ren结构体的量if((fp=fopen("ren.dat","ab+"))==NULL){printf("can't open file!\n");exit(0);}printf("\n录入格式姓名号码类别邮箱。
手机通讯录开发源代码
手机通讯录开发源代码设计开发源代码1.AddContactsActivity类package .demo.pr3;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.EditText;import android.widget.Toast;import .demo.pr3.datax.ContactsTable;import/doc/10ce63b27ed184254b35eefdc8d376 eeafaa17c4.html er;/*增加记录操作界面*/public class AddContactsActivity extends Activity { private EditText nameEditT ext; //输入框private EditText mobileEditText; //手机输入框private EditText qqEditText; //qqprivate EditText danweiEditText; //单位private EditText addressEditText; //地址Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(/doc/10ce63b27ed18 4254b35eefdc8d376eeafaa17c4.html yout.edit);setTitle("添加联系人");//从已设置的页面布局查找对应的控件nameEditText=(EditText)findViewById(/doc/10ce63b27ed184254b35eefdc8d376eeafaa17c4.html );mobileEditText=(EditText)findViewById(R.id.mobile); danweiEditText=(EditText)findViewById(R.id.danwei); qqEditText=(EditText)findViewById(R.id.qq);addressEditText=(EditText)findViewById(R.id.address); }/*创建菜单 */public boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE,1, Menu.NONE, "保存");menu.add(Menu.NONE,2, Menu.NONE, "返回");return super.onCreateOptionsMenu(menu);}/* 菜单事件*/public boolean onOptionsItemSelected(MenuItem item){ // TODO Auto-generated method stubswitch (item.getItemId()) {case 1://保存if(!nameEditText.getText().toString().equals("")){User user=new User();user.setName(nameEditText.getText().toString());user.setMoblie(mobileEditText.getText().toString());user.setDanwei(danweiEditText.getT ext().toString());user.setQq(qqEditT ext.getText().toString());user.setAddress(addressEditText.getText().toString());ContactsTable ct=new ContactsTable(AddContactsActivity.this);if(ct.addData(user)){Toast.makeText(AddContactsActivity.this, "添加成功!",Toast.LENGTH_SHORT).show();finish();}else{Toast.makeText(AddContactsActivity.this, "添加失败!", Toast.LENGTH_SHORT).show();}}else{Toast.makeText(AddContactsActivity.this, "请先输入数据!", Toast.LENGTH_SHORT).show();}break;case 2://返回finish();break;default:break;}return super.onOptionsItemSelected(item);}}2. ContactsMessageActivity类package .demo.pr3;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.TextView;import .demo.pr3.datax.ContactsTable;import/doc/10ce63b27ed184254b35eefdc8d376 eeafaa17c4.html er;/*显示联系人界面 */public class ContactsMessageActivity extends Activity { private TextView nameTextView; //输入框private TextView mobileTextView; //手机输入框private TextView qqTextView; //qqprivate TextView danweiTextView; //单位private TextView addressTextView; //地址private User user; //修改的联系人Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(/doc/10ce63b27ed18 4254b35eefdc8d376eeafaa17c4.html yout.message);setTitle("联系人信息");//从已设置的页面布局查找对应的控件nameTextView=(TextView)findViewById(http://www.doczj.co m/doc/10ce63b27ed184254b35eefdc8d376eeafaa17c4.html );mobileTextView=(T extView)findViewById(R.id.mobile); danweiTextView=(TextView)findViewById(R.id.danwei); qqTextView=(TextView)findViewById(R.id.qq);addressTextView=(TextView)findViewById(R.id.address);//将要修改的联系人数据付值到用户界面显示Bundle localBundle = getIntent().getExtras();int id=localBundle.getInt("user_ID");ContactsTable ct=new ContactsTable(this);user =ct.getUserByID(id);nameTextView.setT ext(":"+user.getName());mobileTextView.setText(":"+user.getMoblie());qqTextView.setText("QQ:"+user.getQq()); danweiTextView.setText("单位:"+user.getDanwei()); addressTextView.setText("地址:"+user.getAddress());}/*创建菜单*/public boolean onCreateOptionsMenu(Menu menu) { menu.add(Menu.NONE, 1, Menu.NONE, "返回");return super.onCreateOptionsMenu(menu);}/* 菜单事件 */public boolean onOptionsItemSelected(MenuItem item){ // TODO Auto-generated method stubswitch (item.getItemId()) {case 1://返回finish();break;default:break;}return super.onOptionsItemSelected(item);}}3. MyContactsActivity类package .demo.pr3;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.AlertDialog.Builder;import android.content.ContentUris;import android.content.ContentValues;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.graphics.Color;import/doc/10ce63b27ed184254b35eefdc8d376 eeafaa17c4.html .Uri;import android.os.Bundle;import android.provider.ContactsContract.RawContacts;import/doc/10ce63b27ed184254b35eefdc8d376 eeafaa17c4.html monDataKinds.Phone;import/doc/10ce63b27ed184254b35eefdc8d376 eeafaa17c4.html monDataKinds.StructuredName; import android.provider.ContactsContract.Contacts.Data;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;import .demo.pr3.datax.ContactsTable;import/doc/10ce63b27ed184254b35eefdc8d376 eeafaa17c4.html er;/*主界面*/public class MyContactsActivity extends Activity {private ListView listView; //结果列表private BaseAdapter listViewAdapter; //ListView 列表适配器private User users[];//通讯录用户private int selecteItem=0; //当前选择Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(/doc/10ce63b27ed18 4254b35eefdc8d376eeafaa17c4.html yout.main);setTitle("通讯录");listView = (ListView) findViewById(R.id.listView);loadContacts();}/*加载联系人列表*/private void loadContacts(){//获取所以通讯录联系人ContactsTable ct=new ContactsTable(this);users=ct.getAllUser();//listView列表现实适配器listViewAdapter=new BaseAdapter() {Overridepublic View getView(int position,View convertView, ViewGroup parent) {if(convertView==null){TextView textView =new TextView(MyContactsActivity.this); textView.setTextSize(22);convertView=textView;}String moblie=users[position].getMoblie()==null?"" :users[position].getMoblie();((TextView)convertView).setText(users[position].getName()+"---"+moblie);if(position==selecteItem){convertView.setBackgroundColor(Color.YELLOW); }else{convertView.setBackgroundColor(0);}return convertView;}Overridepublic long getItemId(int position) {return position;}Overridepublic Object getItem(int position) {return users[position];}Overridepublic int getCount() {return users.length;}};//设置listView控件的适配器listView.setAdapter(listViewAdapter);listView.setOnItemClickListener(new OnItemClickListener() { Overridepublic void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3) {// TODO Auto-generated method stub//记录点击列selecteItem=arg2;//刷新列表listViewAdapter.notifyDataSetChanged();}});}/*创建菜单*/public boolean onCreateOptionsMenu(Menu menu) {menu.add(Menu.NONE, 1, Menu.NONE, "添加");menu.add(Menu.NONE, 2, Menu.NONE, "编辑");menu.add(Menu.NONE, 3, Menu.NONE, "查看信息");menu.add(Menu.NONE, 4, Menu.NONE, "删除");menu.add(Menu.NONE, 5, Menu.NONE, "查询");menu.add(Menu.NONE, 6, Menu.NONE, "导入到手机薄");menu.add(Menu.NONE, 7, Menu.NONE, "退出");return super.onCreateOptionsMenu(menu);}/*菜单事件*/public boolean onOptionsItemSelected(MenuItem item){// TODO Auto-generated method stubswitch (item.getItemId()) {case 1://添加Intent intent = new Intent(MyContactsActivity.this,AddContactsActivity.class);startActivity(intent);break;case 2://编辑if(users[selecteItem].getId_DB()>0)//根据数据库ID判断当前记录是否可以操作{intent = new Intent(MyContactsActivity.this,UpdateContactsActivity.class);intent.putExtra("user_ID", users[selecteItem].getId_DB());startActivity(intent);}else{Toast.makeText(this, "无结果记录,无法操作!",Toast.LENGTH_SHORT).show();}break;case 3://查看信息if(users[selecteItem].getId_DB()>0){intent = new Intent(MyContactsActivity.this,ContactsMessageActivity.class);intent.putExtra("user_ID", users[selecteItem].getId_DB());startActivity(intent);}else{Toast.makeText(this, "无结果记录,无法操作!",Toast.LENGTH_SHORT).show();}break;case 4://删除if(users[selecteItem].getId_DB()>0){delete();}else{Toast.makeText(this, "无结果记录,无法操。
通讯录管理系统源代码
源代码:#include "stdio.h"#include "stdlib.h"#include "string.h"#include "conio.h"#include "stdlib.h"#define null 0struct record{char name[20];char phone[20];char adress[40];char postcode[10];char e_mail[30];}student[500];struct LinkList{struct record US;struct LinkList *next;}a;struct LinkList *head=null;int num=0;FILE *fp;int menu_select();int adduser();int list();int search();int display();int add();int listbyname();int dele();int save();int exit();void main(){system("cls");for(;;){switch(menu_select()){case 0:adduser();break;case 1:list();break;case 2:search();break;case 3:display();break;case 4:add();break;case 5:listbyname();break;case 6:dele();break;case 7:save();break;case 8:exit(0);}}}menu_select(){char s[80];int a;printf("*_* press any key enter menu! *_* \n");getch();system("cls");printf("\t\t********************MENU*********************\n\n"); printf("\t\t 0. 输入记录\n");printf("\t\t 1. 显示记录 \n");printf("\t\t 2. 按查找\n");printf("\t\t 3. 按查找\n");printf("\t\t 4. 插入记录 \n");printf("\t\t 5. 按排序\n");printf("\t\t 6. 删除记录\n");printf("\t\t 7. 记录保存文件\n");printf("\t\t 8. Quit\n");printf("\t\t***********************************************\n"); do{printf("\n Enter you choice(0~11):");scanf("%s",s);a=atoi(s);}while (a<0||a>11);return a;}adduser(){printf("\n\t\t**************** 请输入用户信息 ****************\n"); printf("\n\t\t输入:");scanf("%s",&student[num].name);printf("\n\t\t输入:");scanf("%s",&student[num].phone);printf("\n\t\t输入地址:");scanf("%s",&student[num].adress);printf("\n\t\t输入邮编:");scanf("%s",&student[num].postcode);printf("\n\t\t输入:");scanf("%s",&student[num].e_mail);num++;printf("\n\t\t是否继续添加?(Y/N):");if (getch()=='y')adduser();return(0);}list(){int i;system("cls");if(num!=0){printf("\n\t\t*************** 以下为通讯录所有信息************"); for (i=0;i<num;i++){printf("\n\t\t: %s",student[i].name);printf("\n\t\t: %s",student[i].phone);printf("\n\t\t地址: %s",student[i].adress);printf("\n\t\t邮编: %s",student[i].postcode);printf("\n\t\t:%s",student[i].e_mail);printf("\t\t");if (i+1<num){printf("\n\t\t__________________________");system("pause");}}printf("\n\t\t************************************************"); }elseprintf("\n\t\t通讯录中无任何纪录");printf("\n\t\t按任意键返回主菜单:");getch();return(0);}search(){int mark=0;int i;int a=0;printf("\n\t\t***************** 按查找 *******************");char name[20];printf("\n\t\t请输入:");scanf("%s",name);for(i=a;i<num;i++){if (strcmp(student[i].name,name)==0){printf("\n\t\t************* 以下是您查找的用户信息 ***********"); printf("\n\t\t: %s",student[i].name);printf("\n\t\t: %s",student[i].phone);printf("\n\t\t地址: %s",student[i].adress);printf("\n\t\t:%s",student[i].e_mail);printf("\n\t\t************************************************"); mark++;if ((i+1)<num){printf("\n\t\t是否继续查找相同名字的用户信息:(y/n)");if (getch()=='y'){a=i;continue;}elsereturn(0);}else{printf("\n\t\t按任意键返回主菜单");getch();return(0);}}}if(mark!=0){printf("\n\t\t没有相同的用户纪录");printf("\n\t\t按任意键返回主菜单");getch();return(0);}else{printf("\n\t\t没有相同的用户纪录");printf("\n\t\t按任意键返回主菜单");getch();return(0);}}display(){int mark=0;int i;int a=0;printf("\n\t\t****************** 按查找 ******************");char phone[10];printf("\n\t\t请输入:");scanf("%s",phone);for(i=0;i<num;i++){if (strcmp(student[i].phone,phone)==0){printf("\n\t\t************** 以下是您查找的用户信息 **********"); printf("\n\t\t: %s",student[i].name);printf("\n\t\t: %s",student[i].phone);printf("\n\t\t地址: %s",student[i].adress);printf("\n\t\t:%s",student[i].e_mail);printf("\n\t\t************************************************"); printf("\n\t\t按任意键返回主菜单:");mark++;getch();return(0);}}if (mark==0){printf("\n\t\t没有改用户的信息");printf("\n\t\t按任意键返回主菜单");getch();return(0);}return(0);}add(){int i;if ((fp=fopen("student.bin","wb"))==NULL){printf("\n\t\t文件打开失败");}for (i=0;i<num;i++){if (fwrite(&student[i],sizeof(struct record),1,fp)!=1){printf("\n\t\t写入文件错误!\n");}printf("\n\t\t**************** 请输入用户信息 ****************\n"); printf("\n\t\t输入:");scanf("%s",&student[num].name);printf("\n\t\t输入:");scanf("%s",&student[num].phone);printf("\n\t\t输入地址:");scanf("%s",&student[num].adress);printf("\n\t\t输入邮编:");scanf("%s",&student[num].postcode);printf("\n\t\t输入:");scanf("%s",&student[num].e_mail);num++;printf("\n\t\t是否继续添加?(Y/N):");if (getch()=='y')adduser();return(0);}fclose(fp);printf("\n\t\t通讯录文件已保存");printf("\n\t\t按任意键退出程序\n\t\t");exit(0);return(0);}void deletebyphone(){int i,j;int deletemark=0;char phone[20];printf("\n\t\t请输入要删除用户:");scanf("%s",phone);if(num==0){printf("\n\t\t对不起,文件中无任何纪录");printf("\n\t\t按任意键返回主菜单");getch();return;}for (i=0;i<num;i++){if (strcmp(student[i].phone,phone)==NULL){printf("\n\t\t以下是您要删除的用户纪录:"); printf("\n\t\t: %s",student[i].name);printf("\n\t\t: %s",student[i].phone);printf("\n\t\t地址: %s",student[i].adress); printf("\n\t\t:%s",student[i].e_mail);printf("\n\t\t是否删除?(y/n)");if (getch()=='y'){for (j=i;j<num-1;j++)student[j]=student[j+1];num--;deletemark++;printf("\n\t\t删除成功");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')deletebyphone();return;}elsereturn;}continue;}if (deletemark==0){printf("\n\t\t没有该用户的纪录");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')deletebyphone();return;}}void deletebyname(){int a=0;int findmark=0;int j;int deletemark=0;int i;char name[20];printf("\n\t\t请输入要删除用户:");scanf("%s",name);for (i=a;i<num;i++){if (strcmp(student[i].name,name)==NULL){printf("\n\t\t以下是您要删除的用户纪录:");findmark++;printf("\n\t\t________________________________"); printf("\n\t\t: %s",student[i].name);printf("\n\t\t: %s",student[i].phone);printf("\n\t\t地址: %s",student[i].adress);printf("\n\t\t:%s",student[i].e_mail);printf("\n\t\t________________________________"); printf("\n\t\t是否删除?(y/n)");if (getch()=='y'){for (j=i;j<num-1;j++)student[j]=student[j+1];num--;deletemark++;printf("\n\t\t删除成功");if((i+1)<num){printf("\n\t\t是否继续删除相同的用户信息?(y/n)"); if (getch()=='y'){a=i;continue;}}printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')deletebyname();return;}if((i+1)<num){printf("\n\t\t是否继续删除相同的用户信息?(y/n)"); if (getch()=='y'){a=i;continue;}}}elsecontinue;}if ((deletemark==0)&&(findmark==0)){printf("\n\t\t没有该用户的纪录");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')deletebyphone();return;return;}else if (findmark!=0){printf("\n\t\t没有重名信息");printf("\n\t\t没有该用户的纪录");printf("\n\t\t是否继续删除?(y/n)");if (getch()=='y')deletebyphone();return;return;}}dele(){char choic;printf("\n\t\t1-按删除 2-按删除");printf("\n\t\t请选择:");choic=getch();switch (choic){case '1':deletebyphone();break;case '2':deletebyname();break;}return(0);}listbyname(){int i,j;struct record tmp;for (i=1;i<num;i++){if(strcmp(student[i].name,student[i-1].name)<0){tmp=student[i];j=i-1;do{student[j+1]=student[j];j--;}while ((strcmp(,student[j].name)<0&&j>=0)); student[j+1]=tmp;}}printf("\n\t\t排序成功,是否显示?(y/n)");if (getch()=='y')list();return(0);}save(){int j;FILE*fp;fp=fopen("student.txt","w");if (fp==NULL)printf("can't open the file.");if(num!=0){for(j=0;j<num;j++){fwrite(student,sizeof(student),1,fp);}}printf("保存成功!");fclose(fp);return(0);}。
通讯录源代码,直接可用
#include <stdio.h>#include <windows.h>#include <stdlib.h>#include <conio.h>#include <string.h>/********************结构体定义******************************/ struct Contact{char NAME[20];char Phone[12];char QQ[12];};struct Node{Contact person;Node *next;};Node *head=NULL;/**************************函数申明*************************/ void AddPerson(Contact); //添加联系人Contact *FindPerson(char *name); //查找联系人void DelPerson(char *name); //删除联系人void ShowAll(); //显示所有联系人void function_6(); //分类显示联系人void SaveData(); //保存联系人信息void LoadData(); //载入联系人信息void face(); //界面int CheckPhone(char *num); //检验手机号是否合法int JudgePhone(char *num); //判断电话号码的用户类型/**********************主函数部分****************************/ int main(){system("title 迷你通讯录BY 马恒");system("color f2");LoadData();int op;face();while(1){printf("请选择功能:");fflush(stdin);if(0==scanf("%d",&op)){printf("\n输入不合法,请重新输入!\n");continue;}switch(op){case 0:{printf("感谢您的使用,下次再见!\n");getchar();exit(0);}break;case 1:{Contact per;fflush(stdin);printf("姓名:");scanf("%s",);do{printf("联系电话:");scanf("%s",per.Phone);}while(!CheckPhone(per.Phone));printf("QQ:");scanf("%s",per.QQ);AddPerson(per);SaveData();printf("添加联系人成功!\n");}break;case 2:{char name[15];printf("输入查找姓名:");scanf("%s",name);Contact *per=FindPerson(name);if(NULL!=per){printf("%s %s %s",per->NAME,per->Phone,per->QQ);}else{printf("%s联系人不存在,查询失败!",name);}}break;case 3:{char name[15];printf("输入需要修改的联系人:\n");scanf("%s",name);Contact *per=FindPerson(name);if(NULL==per){printf("%s联系人不存在,修改失败!",name);}else{printf("新姓名:");scanf("%s",per->NAME);printf("\n新号码:");scanf("%s",per->Phone);printf("\n新QQ号:");scanf("%s",per->QQ);SaveData();printf("更新联系人成功!\n");}}break;case 4:{char name[20];printf("输入需要删除的联系人:");scanf("%s",name);Contact *per=FindPerson(name);if(per==NULL){printf("%s联系人不存在,删除失败!",name);}else{DelPerson(name);SaveData();printf("\n成功删除联系人!\n");}}break;case 5:{ShowAll();}break;case 6:{system("cls");for(int i=1;i<=80;i++){printf("@");}printf("\t\t\t\t1.显示全部移动用户\n");//2printf("\t\t\t\t2.显示全部联通用户\n");//1printf("\t\t\t\t3.显示全部电信用户\n");//3printf("\t\t\t\t0.返回主菜单\n");for(i=1;i<=80;i++){printf("@");}int m=1,op;while(m){Node *p=head;printf("请输入指令:");fflush(stdin);if(0==scanf("%d",&op)){printf("您输入的指令无效,重新输入:");continue;}switch(op){case 0:{system("cls");face();m=0;}break;case 1:{while(NULL!=p){if(2==JudgePhone(p->person.Phone)){printf("%s %s %s \n",p->,p->person.Phone,p->person.QQ);}p=p->next;}}break;case 2:{while(NULL!=p){if(1==JudgePhone(p->person.Phone)){printf("%s %s %s \n",p->,p->person.Phone,p->person.QQ);}p=p->next;}}break;case 3:{while(NULL!=p){if(3==JudgePhone(p->person.Phone)){printf("%s %s %s \n",p->,p->person.Phone,p->person.QQ);}p=p->next;}}break;default:{printf("输入不合法,重新输入:");}}}}break;default:{printf("您输入的内容不合法,请重新输入!\n");}}}return 0;}void AddPerson(Contact person) //添加联系人Node *pNewNode;pNewNode=(Node*)malloc(sizeof(Node));pNewNode->person=person;pNewNode->next=NULL;if(NULL==head){head=pNewNode;}else{Node *pNode=head;while(NULL!=pNode->next){pNode=pNode->next;}pNode->next=pNewNode;}}void ShowAll() //显示所有联系人{Node *pNode=head;int icnt=0;while(NULL!=pNode){printf("%s %s %s \n",pNode->,pNode->person.Phone,pNode->person.QQ);pNode=pNode->next;icnt++;}printf("一共有%d个联系人\n",icnt);}Contact *FindPerson(char *name) //查找联系人{Node *pNode=head;while(NULL!=pNode){if(strcmp(pNode->,name)==0){break;}elsepNode=pNode->next;}return pNode?(&(pNode->person)):NULL;}void DelPerson(char *name){if(strcmp(head->,name)==0){Node *pNewHead=head->next;free(head);head=pNewHead;return;}Node *p1=head;while(strcmp(p1->next->,name)!=0) {p1=p1->next;}Node *p2=p1->next->next;free(p1->next);p1->next=p2;}void LoadData(){FILE *fp=fopen("data","rb");if(NULL==fp){return;}Contact ct;while(fread(&ct,sizeof(Contact),1,fp)){AddPerson(ct);}fclose(fp);}void SaveData(){FILE *fp=fopen("data","wb"); //以二进制写入Node *pNode=head;while(NULL!=pNode){fwrite(&(pNode->person),sizeof(Contact),1,fp);pNode=pNode->next;}fclose(fp);}void face(){for(int i=1;i<=80;i++)printf("=");for(i=1;i<=30;i++)printf("=");printf("通讯录管理系统v1.0");for(i=0;i<=30;i++)printf("=");for(i=1;i<=80;i++)printf("=");printf("\t\t\t\t1.添加联系人\n");printf("\t\t\t\t2.查找联系人\n");printf("\t\t\t\t3.修改联系人\n");printf("\t\t\t\t4.删除联系人\n");printf("\t\t\t\t5.显示所有联系人\n");printf("\t\t\t\t6.分类显示联系人\n");printf("\t\t\t\t0.退出通讯录\n");for(i=1;i<=80;i++){printf("=");}for(i=1;i<=80;i++){printf("=");}}int CheckPhone(char *num){if(11!=strlen(num)){return 0;}int i;for(i=0;i<11;i++){if(num[i]<'0'||num[i]>'9'){return 0;}}return 1;}int JudgePhone(char *num) //判断电话号码的用户类型{if('1'!=num[0]){return 0;}if('1'==num[0]){if('3'==num[1]){switch(num[2]){case '0':case '1':case '2':return 1;break;case '3':return 3;break;case '4':case '5':case '6':case '7':case '8':case '9':return 2;break;default:return 0;}}else if('5'==num[1]){switch(num[2]){case '0':case '1':case '2':case '7':case '8':case '9':return 2;break;case '5':case '6':return 1;break;case '3':return 3;break;default:return 0;}}else if('8'==num[1]){switch(num[2]){case'5':case'6':return 1;break;case'0':case'1':case'2':case'7':case'8':case'9':return 2;break;case'3':return 3;break;default:return 0;break;}}else{return 0;}}return 0;}。
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语言通讯录源代码
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <conio.h>struct person{char name[15];char sex[2];int nianling;long int youbian;char addr[50];char celephone[22];char homephone[22];char company[20];char email[40];long int QQ;}per[500];int num=0;//*外部变量num为文件中的纪录数*//FILE *fp;void mainmenu();//*主菜单*//void newrecord();//新添纪录*//void xiugaimenu();//*修改菜单*//void xiugaibyname();//*按姓名修改*//void xiugaibycelephone();//*按手机号码修改*//void xiugaixiangmu(int a);//*修改项目*//void searchmenu();//*查询菜单*//void searchbyname();//*按姓名查询*//void searchbycelephone();//*按手机号码查询*//void searchbyhomephone();//*按家庭电话号码查询*// void searchbyqq();//*按QQ查询*//void deletemenu();//*删除菜单*//void deleteall();//*删除所有信息*//void deleteone();//*删除单个*//void showall();//*输出全部信息*//void readfromfile();//*读取文件*//void writetofile();//*写入文件*// /void deletebycelephone(); //*按手机号码删除*//void deletebyname();//*按姓名删除*/void listbycelephone();//*按手机号码排序*//void listbyname();//*按姓名排序*//void listmenu();//*排序菜单*//void main()//*主函数*//{system("cls");readfromfile();while (1){mainmenu();}} //*主函数结束*//void mainmenu()//*主菜单函数*//{int a;system("cls");system("color 2a");printf("\n\t 您好! 欢迎使用通讯录!\n");printf(" 1: 添加个人信息\n");printf(" 2: 查询个人信息\n");printf(" 3: 删除个人信息\n");printf(" 4: 修改个人信息\n");printf(" 5: 信息排序\n");printf(" 6: 输出全部信息\n");printf(" 7: 写入文件\n");printf(" 8: 读取文件\n");printf(" 9: 删除所有\n");printf(" 0: 退出\n");printf(" 请输入您的数字选择: ");loop1: scanf("%d",&a);switch (a){case 1:newrecord();break;case 2:searchmenu();break;case 3:deletemenu();break;case 4:xiugaimenu();break;case 5:listmenu();break;case 6:showall();break;case 7:writetofile();break;case 8:readfromfile();break;case 9:deleteall();break;case 0:exit(0);default:printf("对不起!您的输入有误!请重新输入: ");goto loop1;}}void newrecord()//*添加纪录*//{int a;system("cls");system("color 2a");loop: printf("\n\n\n\n\t\t 请输入您要添加的联系人的信息:\n");printf("\n请输入姓名:\n");name: scanf("%s",&per[num].name);if(strlen(per[num].name)>15){printf("对不起!您的输入有误!请重新输入");goto name;}printf("\n请输入性别:\n");sex: scanf("%s",&per[num].sex);if(strlen(per[num].sex)==0){printf("对不起!您的输入有误!请重新输入");goto sex;}printf("\n请输入年龄:");nianling: scanf("%d",&per[num].nianling);if(per[num].nianling<10||per[num].nianling>110){printf("对不起!您的输入有误!请重新输入");goto nianling;}printf("\n请输入邮编\n");youbian: scanf("%d",&per[num].youbian);if(per[num].youbian<10000||per[num].youbian>999999){printf("对不起!您的输入有误!请重新输入");goto youbian;}printf("\n请输入地址\n");addr: scanf("%s",&per[num].addr);if(strlen(per[num].addr)>50||strlen(per[num].addr)<4){printf("对不起!您的输入有误!请重新输入\n");goto addr;}printf("\n请输入手机号码:\n");celephone: scanf("%s",&per[num].celephone);if(strlen(per[num].celephone)!=11){printf("对不起!您的输入有误!请重新输入\n");goto celephone;}printf("\n请输入家庭电话号码:\n");homephone: scanf("%s",&per[num].homephone);if(strlen(per[num].homephone)!=11){printf("对不起!您的输入有误!请重新输入\n");goto homephone;}printf("\n请输入公司名称:");company: scanf("%s",&per[num].company);if(strlen(per[num].company)>20||strlen(per[num].company)<10){printf("对不起!您的输入有误!请重新输入");goto company;}printf("\n输入电子邮箱:");email: scanf("%s",&per[num].email);if(strlen(per[num].email)>30||strlen(per[num].email)<4){printf("对不起!您的输入有误!请重新输入");goto email;}printf("\n输入QQ号码:");QQ: scanf("%ld",&per[num].QQ);if(per[num].QQ>00||per[num].QQ<){printf("对不起!您的输入有误!请重新输入");goto QQ;}num++;printf("\n是否继续添加?\n");printf("\n请按1和2(1代表继续;2代表不继续)\n");printf("请输入您的选择");scanf("%d",&a);if(a==1){goto loop;}else{return;}}void searchmenu()//*查询菜单*//{int a;system("cls");system("color 2a");printf("\n\t\t 欢迎使用查询菜单");printf("\n\t\t 1-按手机号码查询");printf("\n\t\t 2-按姓名查询");printf("\n\t\t 3-按家庭电话号码查询");printf("\n\t\t 4-按QQ查询");printf("\n\t\t 5-返回主菜单");printf("\n\t\t");printf("请输入您的选择");scanf("%d",&a);switch (a){case 1:searchbycelephone();break;case 2:searchbyname();break;case 3:searchbyhomephone();break;case 4:searchbyqq();break;case 5:mainmenu();break;}}void searchbycelephone()//*按手机号码查询*//{int mark=0;int i,j;int a=0;char celephone[11];printf("\n 按手机号码查找\n");printf("\n请输入手机号码:\n");scanf("%s",celephone);for(i=0;i<num;i++){if (strcmp(per[i].celephone,celephone)==0){printf("\n 以下是您查找的联系人的信息");printf("\n________________________________");printf("\n名字:%s",per[i].name);printf("\n性别:%s",per[i].sex);printf("\n年龄:%d",per[i].nianling);printf("\n邮编:%ld",per[i].youbian);printf("\n地址:%s",per[i].addr);printf("\n手机号码:%s",per[i].celephone);printf("\n家庭电话号码:%s",per[i].homephone);printf("\n公司名称:%s",per[i].company);printf("\n电子邮件:%s",per[i].email);printf("\nQQ号码:%ld",per[i].QQ);printf("\n________________________________");printf("\n请按任意数字键返回主菜单");mark++;scanf("%d",&j);return;}}if (mark==0){printf("\n对不起!没有该联系人的信息!");printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}}void searchbyname()//*按姓名查询*//{int mark=0;int i,j,findmark=0;int a=0;char name[15];printf("\n 按姓名查找");printf("\n请输入姓名:");scanf("%s",name);for(i=a;i<num;i++){if (strcmp(per[i].name,name)==0){findmark++;printf("\n\t\t 以下是您查找的联系人的信息");printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");if ((i+1)<num){printf("\n是否继续查找相同名字的联系人的信息?(1代表继续;2代表不继续)");scanf("%d",&j);if (j==1){a=i;mark++;continue;}elsebreak;}else{printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}}}if(mark!=0){printf("\n对不起! 没有相同名字的该联系人的信息!!");printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}else if(findmark==0){printf("\n对不起!没有该联系人的信息!");printf("\n请按任意数字键返回主菜单");scanf("%d",&j);return;}}void searchbyhomephone()//*按家庭电话号码查询*//{int mark=0;int i,j;int a=0;char homephone[11];printf("\n\t\t 按家庭电话号码查找");printf("\n请输入家庭电话号码:");scanf("%s",homephone);for(i=0;i<num;i++){if (strcmp(per[i].homephone,homephone)==0){printf("\n\t\t 以下是您要查找的联系人的信息");printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t请按任意数字键返回主菜单");mark++;scanf("%d",&j);return;}}if (mark==0){printf("\n\t\t对不起!没有该联系人的信息!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}}void searchbyqq()//*按QQ查询*//{int mark=0;int i,j;long int QQ;printf("\n\t\t 按QQ号码查找\n");printf("\n请输入QQ号码:");scanf("%ld",&QQ);for(i=0;i<num;i++){if(per[i].QQ==QQ){printf("\n\t\t 以下是您查找的联系人的信息");printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t请按任意数字键返回主菜单");mark++;scanf("%d",&j);return;}}if (mark==0){printf("\n\t\t对不起!没有该联系人的信息!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}}void deletemenu()//*删除菜单*//{int i,j;system("cls");system("color 3b");if(num==0){printf("\n\t\t对不起!文件中无任何纪录!");printf("\n\t\t请先添加个人信息!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}else{printf("\n\t\t 删除菜单");printf("\n\t\t 1-删除单个");printf("\n\t\t 2-返回主菜单");printf("\n\t\t ");printf("\n\t\t请输入您的数字选择: ");loop4: scanf("%d",&i);switch (i){case 1:deleteone();break;case 2:mainmenu();break;default:printf("对不起!您的输入有误!请重新输入: ");goto loop4;}}}void deleteone()//*删除单个*//{int i;printf("\n\t\t1-按手机号码删除2-按姓名删除");printf("\n\t\t请输入您的选择:");loop5:scanf("%d",&i);;switch (i){case 1:deletebycelephone();break;case 2:deletebyname();break;default:printf("对不起!您的输入有误!请重新输入: ");goto loop5;}}void deletebyname()//姓名删除*//{int a=0;int findmark=0;int i,j,k;int deletemark=0;char name[15];printf("\n\t\t请输入您要删除的联系人的姓名:");scanf("%s",name);for (i=a;i<num;i++){if (strcmp(per[i].name,name)==0){printf("\n\t\t以下是您要删除的联系人的纪录:");findmark++;printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t是否删除?(1代表删除;2代表不删除)\n");printf("\n\t\t请输入您的数字选择: ");scanf("%d",&k);if(k==1){for (j=i;j<num-1;j++) //*纪录移动,从per数组中删除之*//per[j]=per[j+1];num--;deletemark++;printf("\n\t\t删除成功");if((i+1)<num){printf("\n\t\t是否继续删除相同姓名的联系人的信息?(1代表删除;2代表不删除)");printf("\n\t\t请输入您的数字选择: ");scanf("%d",&k);if(k=1){a=i;findmark++;continue;}}printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)");printf("\n\t\t请输入您的数字选择: ");scanf("%d",&k);if(k==1)deleteone();elsebreak;}}elsecontinue;}if((deletemark==0)&&(findmark==0)){printf("\n\t\t对不起!没有该联系人的纪录!");printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&k);if(k==1)deleteone();}else if(findmark!=0){printf("\n\t\t对不起!没有重名信息!");printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&k);if(k==1)deleteone();}}void deletebycelephone()//*按手机号码删除*//{int i,j,h,m;int deletemark=0;char celephone[11];printf("\n\t\t请输入要删除的联系人的手机号码:");scanf("%s",celephone);for(i=0;i<num;i++){if(strcmp(per[i].celephone,celephone)==0){deletemark++;printf("\n\t\t以下是您要删除的联系人的纪录:");printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t是否删除?(1代表删除;2代表不删除)\n");printf(" 请输入您的数字选择: ");scanf("%d",&h);if(h==1){for (j=i;j<num-1;j++) /*纪录移动,从per数组中删除之*/per[j]=per[j+1];num--;printf("\n\t\t删除成功");printf("\n是否继续删除?(1代表继续;2代表不继续)\n");printf(" 请输入您的数字选择: ");scanf("%d",&h);if(h==1)deletebycelephone();}}}if(deletemark==0){printf("\n\t\t对不起!没有该联系人的纪录!");printf("\n\t\t是否继续删除?(1代表继续;2代表不继续)\n");printf(" 请输入您的数字选择: ");scanf("%d",&m);if(m==1)deletebycelephone();}}void xiugaimenu()//*修改菜单*//{int i,j;system("cls");system("color 3f");if(num==0){printf("\n\t\t对不起!文件中无任何纪录!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}else{printf("\n\t\t 1-按姓名修改2-按手机号码修改\n");printf(" 请输入您的数字选择: ");loop6:scanf("%d",&i);switch (i){case 1:xiugaibyname();break;case 2:xiugaibycelephone();break;default:printf("对不起!您的输入有误!请重新输入: ");goto loop6;}}}void xiugaibyname()//*按姓名修改*//{int a=0;int findmark=0;int xiugaimark=0;int i,j;char name[15];printf("\n\t\t请输入要修改联系人的姓名:");scanf("%s",name);for (i=a;i<num;i++){if (strcmp(per[i].name,name)==0){printf("\n\t\t以下是您要修改的联系人的纪录:");findmark++;printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t是否修改?(1代表修改;2代表不修改)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1){xiugaixiangmu(i);xiugaimark++;printf("\n\t\t修改成功");if((i+1)<num){printf("\n\t\t是否继续修改相同姓名的联系人的信息?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1){a=i;findmark++;continue;}}printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibyname();return;}}}if ((xiugaimark==0)&&(findmark==0)){printf("\n\t\t没有该联系人的纪录");printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibyname();return;}else if (findmark!=0){printf("\n\t\t没有重名信息");printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibyname();return;}}void xiugaibycelephone()//*按手机号码修改*//{int findmark=0;int i,j;char celephone[11];printf("\n\t\t请输入要修改联系人的手机号码:");scanf("%s",celephone);for (i=0;i<num;i++){if (strcmp(per[i].celephone,celephone)==0){printf("\n\t\t以下是您要修改的联系人的纪录:");findmark++;printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\n\t\t是否修改?(1代表修改;2代表不修改)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1){xiugaixiangmu(i);printf("\n\t\t修改成功");printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);printf(" 请输入您的数字选择: ");if (j==1)xiugaixiangmu(i);break;}}}if (findmark==0){printf("\n\t\t对不起!没有该联系人的纪录!");printf("\n\t\t是否继续修改?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&j);if (j==1)xiugaibycelephone();return;}}void xiugaixiangmu(int a)//*修改项目*//{int i;system("cls");system("color c0");printf("\n\t\t 请输入您要修改的项目");printf("\n\t\t\n");printf("\n\t\t1-手机号码\n");printf("\n\t\t2-姓名\n");printf("\n\t\t3-家庭电话号码\n");printf("\n\t\t4-QQ号码\n");printf("\n\t\t5-地址\n");printf("\n\t\t6-邮编\n");printf("\n\t\t7-电子邮件\n");printf("\n\t\t8-回主菜单\n");printf("\n\t\t\n");printf(" 请输入您的数字选择: ");scanf("%d",&i);loop9: switch (i){case 1:printf("请输入手机号码:");scanf("%s",&per[a].celephone);break;case 2:printf("请输入姓名:");scanf("%s",&per[a].name);break;case 3:printf("请输入家庭电话号码:");scanf("%s",&per[a].homephone);break;case 4:printf("请输入QQ号码:");scanf("%s",&per[a].QQ);break;case 5:printf("请输入地址:");scanf("%s",&per[a].addr);break;case 6:printf("请输入邮编:");scanf("%s",&per[a].youbian);break;case 7:printf("请输入email:");scanf("%s",&per[a].email);break;case 8:mainmenu();break;default: printf("对不起!您的输入有误!请重新输入: ");goto loop9;}}void listmenu()//*排序菜单*//{int i,j;system("cls");system("color b0");if(num==0){printf("\n\t\t对不起,文件中无任何纪录");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}else{system("cls");system("color 12");printf("\n\t\t 排序菜单");printf("\n\t\t 1-按姓名排序");printf("\n\t\t 2-按手机号码排序");printf("\n\t\t 3-返回主菜单");printf(" 请输入您的数字选择: ");loop10: scanf("%d",&i);switch(i){case 1:listbyname();break;case 2:listbycelephone();break;case 3:mainmenu();break;default:printf("对不起!您的输入有误!请重新输入: ");goto loop10;}}}void listbyname()//*按姓名排序*//{int i,j,k;struct person tmp;for (i=1;i<num;i++){if(strcmp(per[i].name,per[i-1].name)<0){tmp=per[i];j=i-1;do{per[j+1]=per[j];j--;}while ((strcmp(,per[j].name)<0&&j>=0));per[j+1]=tmp;}}printf("\n\t\t排序成功,是否显示?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&k);if (k==1)showall();return;}void listbycelephone()//*按手机号码排序*//{int i,j,k;struct person tmp;for (i=1;i<num;i++){if(strcmp(per[i].celephone,per[i-1].celephone)<0){tmp=per[i];j=i-1;do{per[j+1]=per[j];j--;}while ((strcmp(tmp.celephone,per[j].celephone)<0)&&j>=0);per[j+1]=tmp;}}printf("\n\t\t排序成功,是否显示?(1代表继续;2代表不继续)");printf(" 请输入您的数字选择: ");scanf("%d",&k);if (k==1)showall();return;}void showall()//*输出全部信息*//{int i,j;system("cls");system("color 3b");if(num!=0){printf("\n\t\t*************** 以下为通讯录所有信息************");for (i=0;i<num;i++){printf("\n\t\t________________________________");printf("\n\t\t名字:%s",per[i].name);printf("\n\t\t性别:%s",per[i].sex);printf("\n\t\t年龄:%d",per[i].nianling);printf("\n\t\t邮编:%ld",per[i].youbian);printf("\n\t\t地址:%s",per[i].addr);printf("\n\t\t手机号码:%s",per[i].celephone);printf("\n\t\t家庭电话号码:%s",per[i].homephone);printf("\n\t\t公司名称:%s",per[i].company);printf("\n\t\t电子邮件:%s",per[i].email);printf("\n\t\tQQ号码:%ld",per[i].QQ);printf("\n\t\t________________________________");printf("\t\t");if (i+1<num){printf("\n\t\t__________________________");system("pause");}}printf("\n\t\t************************************************");}elseprintf("\n\t\t对不起!通讯录中无任何纪录!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}void writetofile()//*写入文件*//{int i,k;system("cls");system("color 84");if ((fp=fopen("per.bin","wb"))==NULL){printf("\n\t\t文件打开失败");}for (i=0;i<num;i++){if (fwrite(&per[i],sizeof(struct person),1,fp)!=1){printf("\n\t\t写入文件错误!\n");}}fclose(fp);printf("\n\t\t通讯录文件已保存");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&k);return;}void readfromfile()//*读取文件*//{int i,j,k;system("cls");system("color 2b");if((fp=fopen("per.bin","rb"))==NULL){printf("\n\t\t****************************");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t* 通讯录文件不存在! *");if ((fp=fopen("per.bin","wb"))==NULL){printf("\n* 建立失败! *");printf("\n* *");printf("\n* *");printf("\n****************************");exit(0);}else{printf("\n\t\t* 通讯录文件已建立! *");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t****************************");printf("\n\t\t 按任意键进入主菜单");printf("\n\t\t 请输入您的数字选择: ");scanf("%d",&k);return;}exit(0);}fseek(fp,0,2); //*文件位置指针移动到文件末尾*//if (ftell(fp)>0) //*文件不为空*//{rewind(fp); //*文件位置指针移动到文件开始位置*//for (num=0;!feof(fp) && fread(&per[num],sizeof(struct person),1,fp);num++);printf("\n\t\t****************************");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t* 文件导入成功! *");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t****************************");printf("\n\t\t 按1显示所有信息,按2回主菜单!");printf("\t\t 请输入您的数字选择: ");scanf("%d",&j);if(j==1)showall();}else{printf("\n\t\t****************************");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t* 文件导入成功! *");printf("\n\t\t* 通讯录文件中无任何纪录! *");printf("\n\t\t* *");printf("\n\t\t* *");printf("\n\t\t****************************");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&i);return;}}void deleteall()//*删除所有信息*//{int i,j;system("cls");system("color 50");printf("\n\t\t确认删除?\n");printf("请按1和2(1代表确认;2代表不删除)\n ");scanf("%d",&i);if (i==1){fclose(fp);if ((fp=fopen("per.bin","wb"))==NULL){printf("\n\t\t不能打开文件,删除失败");readfromfile();}num=0;printf("\n\t\t纪录已删除!");printf("\n\t\t请按任意数字键返回主菜单");scanf("%d",&j);return;}}。
通讯录源代码
#include"stdio.h"#include"stdlib.h"#include"string.h"typedefstructContent//结构体{charname[20];longnumber;chardress[100];structContent*next;}ML;//ML*head=NULL;ML*Found(){ML*p;p=(ML*)malloc(sizeof(ML));//指针申请空间if(p==NULL){printf("创建失败");exit(0);}p->name;p->number=0;p->dress;p->next=NULL;returnp;}voidImput(ML*head)//联.系人增添{ML*p=Found();p->next=head->next;head->next=p;printf("\n请输入联系人姓名:");getchar();gets(p->name);printf("\n请输入电话号码::");scanf("%ld",&p->number);printf("\n请输入联系人地址:");getchar();gets(p->dress);}voidRevise(ML*head)//修改{chars[10];intch;longnu;ML*p=Found();printf("输入需要修改的联系人姓名:");getchar();gets(s);p=head->next;while(strcmp(p->name,s)){p=p->next;}printf("\n\n");printf("|**********请输入操作编号********** *|\n");printf("| |\n");printf("|1、联系人姓名|\n");printf("|2、联系人电话|\n");printf("|3、联系人地址|\n");printf("|0、退出|\n");printf("|printf("||\n");printf("请选择需要修改的内容:");scanf("%d",&ch);switch(ch){case1: |\n");{printf("\n请输入名字:");getchar();gets(p->name);printf("修改成功!\n");break;}case 2:{printf("\n请输入电话:");scanf("%ld",&nu);p->number=nu;printf("修改成功!\n");break;}case 3:{printf("\n请输入地址:");getchar();gets(p->dress);printf("修改成功!\n");break;}default:break;}}voidDelete(ML*head)//.删.除{chars[10];ML*p,*d;p=Found();d=Found();p=head->next;head->next=p;printf("输入要删除的联系人姓名:\n");getchar();gets(s);d=head;do{if(!strcmp(p->name,s)){d->next=p->next;free(p);printf("删除成功!\n");return;}d=p;p=p->next;}while(p!=NULL);printf("该联系人不存在\n");}voidRes(ML*head)//查找{chars[10];ML*p=Found();p=head->next;printf("输入要查找的联系人姓名\n");getchar();gets(s);for(;p!=NULL;p=p->next){if(!strcmp(p->name,s)){printf("\n名字:%s",p->name);printf("\n电话:%ld",p->number);printf("\n地址:%s\n",p->dress);return;}}printf("该联系人不存在\n");}voidBrowse(ML*head)//浏.览ML*p=Found();printf("\nif(head->next!=NULL){printf("\n\n");printf("名字\t电话\t地址\t\n");for(p=head->next;p!=NULL;p=p->next){printf("%s\t%ld\t%s\t\n",p->name,p->number,p->dress);}elseprintf("\n通讯录为空\n");printf("\n\n");}}intMenu(ML*head)//菜单 {intch;printf(" \n");printf("|**********请输入操作编号********** *|\n"); printf("||\n"); printf("|1、 添加联系人 |\n"); printf("|2、 修改联系人 |\n"); printf("|3、 删除联系人 |\n"); printf("|4、 查找联系人 |\n"); printf("|5、 浏览联系人 |\n"); printf("|0、 退出|\n"); printf("||\n");printf("||\n");printf("请选择:");scanf("%d",&ch);switch(ch) { case 1:Imput(head); break; case 2:Revise(head); break; case 3:Delete(head); break; case 4:Res(head); break; case 5:Browse(head); break; default:break; } returnch;voidmain()// 主函数{ML*head=Found();printf(" \n");printf("|*** *** |\n");printf("|** ** |\n");printf("|** ** |\n");printf("|** **** |\n");printf("|*** 通讯录.*** |\n");printf("| |\n");printf("|*** *** |\n");printf("|* ** |\n");printf("|** *---XXX* *|\n");|\n");printf("|while(Menu(head));。
通讯录源代码
通讯录源代码#include <stdio.h>#include <stdlib.h>#include <string.h>#include<conio.h>#include <malloc.h>#define LEN sizeof(struct record) //计算字计//#define NULL 0int num;//通计计文件中的计系人计器数struct record{char name[100];char gender[100];char tel[100];char city[100];char eip[100];struct record *next;};struct record *head;void menu();//计示英文提示计计函数void creat();//计建新的通计计void add();//将某计系人增加到通计计中void find();//计合计计某计系人的通计信息void alter();//修改通计计中某计系人的信息void deleteb();//计除通计计中某计系人的信息void list();//计示通计计中的所有计计voidsave();//用于计通计计的更新保存到文件中将void load();//计建新的通计计void quit(int *f);//退出计计void shifang();// 计放存函明内数声void main(){int choic;head=NULL;int flag=1;printf("使用计明:在通计计中存在信息计每次使用通计计前计先加计通计计文件当!\n");while(flag){menu();scanf("%d",&choic);switch(choic){case 1:{if(head==NULL)creat(); //计建else{shifang();creat(); //重新计建}}break;case 2:{ add();save();//将追加的计计保存到文件中}break;case 3:find();printf("\t");system("pause"); //使系计计停break;case 4:{ alter();save();//将修改后的计计保存到文件中}printf("\t");system("pause");break;case 5:deleteb();printf("\t");system("pause");break;case 6:load();break;case 7:quit(&flag);break;case 8:list();printf("\t");system("pause");break;}}}void menu(){system("cls");printf( "\t\t\n"); ??????????????????????????printf( "\t\t \n");※※printf( "\t\t ※计迎使用通计计※\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.※计除某计系人信息 6.加计通计计文件※\n");printf( "\t\t \n"); ※※printf( "\t\t 7.※退出计计 8.计示通计计中所有计计※\n");printf( "\t\t \n"); ※※printf( "\t\t\n\n");?????????????????????????? printf("\n\t\t计计入计计的操作您:");}void creat() /*函数void creat()计建计系人的人信息个*/ {struct record *p1,*p2;char name[100];num=0;p1=head=(struct record *)malloc(LEN);p2=p1;system("cls");printf("计计入姓名~性计~计计~城市及计计(计束操作计按0号计)\n"); getchar();printf("计入姓名:");gets(name);if(strcmp(name,"0")!=0&&name){strcpy(p1->name,name);printf("计入性计(男 or 女):");gets(p1->gender);printf("计入8位计计计或号11位手机号(0-9):");gets(p1->tel);printf("计入所在城市:");gets(p1->city);printf("计入计计(0-9):");gets(p1->eip);p1->next=NULL;while(1){num=num+1;//通计计文件中的计系人计器增数1if(num==1)head=p1;elsep2->next=p1;p2=p1;printf("计计入姓名:");gets(name);if(strcmp(name,"0")==0)//当计入姓名计0计计表示此次计计计的计入计束{break;}else//计计计入{p1=(struct record *)malloc(LEN);strcpy(p1->name,name);printf("计入性计(男 or 女):");gets(p1->gender);printf("计入8位计计计或号11位手机号(0-9):");gets( p1->tel);printf("计入所在城市:");gets(p1->city);printf("计入计计(0-9):");gets(p1->eip);p1->next=NULL;}}p2->next=NULL;save();return;}elsereturn;}void list()//用于计出通计计中的所有计系人信息的函数{struct record *p;if(head!=NULL)//通计计非空计~列出所有计系人信息{ system("cls");p=head;printf("本通计计计在共有%d人:\n",num);printf("---姓名-------性计------手机--------城市---------计计\n"); while(p!=NULL){printf("== %s",p->name); printf(" ");printf("%s",p ->gender); printf(" ");printf("%s",p->tel); printf(" ");printf("%s",p->city); printf(" ");printf("%s",p->eip); printf(" \n");p=p->next;}printf("==================================\n");}else //当通计计计空计~计出提示信息printf("通计计计空,无法计出!");}void add()//增加计计函数{struct record *p0,*p1,*p2;system("cls");//计行系计清屏char name[100],ch;p1=head;printf("计计入所要增加的计系人的各计信息:\n");getchar();//吸收回计符或空格printf("计计入姓名:");gets(name);if((strcmp(name,"0")==0)&&name){printf("姓名不能计0,增加失计!\n");return;}else{p0=(struct record *)malloc(LEN);strcpy(p0->name,name);printf("计入性计(男 or 女):");gets(p0->gender);printf("计入8位计计计或号11位手机号(0-9):");gets(p0->tel);printf("计入所在城市:");gets(p0->city);printf("计入计计(0-9):");gets(p0->eip);num=num+1;if(head==NULL){head=p0;p0->next=NULL;return;}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;}printf("\n是否计计添加?(Y/N):");ch=getchar();if((ch=='y')||(ch=='Y'))add();return;}}}void deleteb() //计除计计{system("cls");struct record *p,*q;char sname[100],ch;p=head;if(head==NULL){printf("通计计计空!无法计除!\n");return;}printf("计计入欲计除计计的姓名:");scanf("%s",sname);while(strcmp(p->name,sname)!=0 && p->next!=NULL) //计计点计除计计点找并{q=p;p=p->next;}if(strcmp(p->name,sname)==0){printf("%s的通计容如下内:\n",sname);printf("---姓名-------性计------手机--------城市---------计计\n"); printf("==================================\n");printf("== %s",p->name);printf(" ");printf("%s",p->gender);printf(" ");printf("%s",p->tel);printf(" ");printf("%s",p->city);printf(" ");printf("%s",p->eip); printf(" \n");printf("==================================\n");getchar();//吸收回计计和空格printf("您确计要计除此人的通计计计计?(Y/N):");ch=getchar();if((ch=='Y')||(ch=='y')){if(p==head) //当条通计计中只有一计计计head=p->next;elseq->next=p->next;num=num-1;//通计计文件中的计系人计器数减1printf("计除成功!\n");save();//保存到文件中}else if((ch=='N')||(ch=='n'))return;}else{printf("通计计中无此人信息,\n");}return; }void quit(int *f)//退出计计{*f=0 ;printf("计计本次的使用您!\n");}void save()//将写数通计计入文件的函{FILE *fp;struct record *p1;char tong[100];if(head==NULL) {fp=fopen("tongxun.txt","w");fp=NULL;//将清通计计文件空return;}getchar();//吸收回计计和空格printf("计计入要保存通计计的文件名:");gets(tong); fp=fopen("tongxun.txt","w");if(fp==NULL){printf("打计通计计文件失计!\n");return;}p1=head;fprintf(fp,"姓名性计手机城市计计\n");for(;p1!=NULL;){fprintf(fp,"%s %s %s %s %s\n",p1->name,p1->gender,p1->tel,p1->city,p1->eip);p1=p1->next;}printf("保存完计!\n");fclose(fp);}void load()//文件加计函数{FILE *fp;char tong[100];struct record *p1,*p2;system("cls");getchar();printf("正在加计通计计文件!\n");getchar();printf("计先计入要加计的通计计信息所在的文件名称:");gets(tong); if((fp=fopen("tongxun.txt","r"))==NULL){ printf("此通计计名不存在,无法计出\n");return;}else{shifang();//计放存内}p1=(struct record *)malloc(LEN);fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->gender,&p1->tel,&p1->city,&p1->eip);if(feof(fp)!=0){printf("文件计空,无法打计!\n");return;}else{rewind(fp);p2=p1;head=p1;num=0;while(feof(fp)==0){fscanf(fp,"%s%s%s%s%s",&p1->name,&p1->gender,&p1->tel,&p1->city,&p1->eip);if(feof(fp)!=0)break;p2->next=p1;p2=p1;p1=(struct record *)malloc(LEN); num=num+1;}p2->next=NULL;p1=head;head=head->next;num=num-1;free(p1);list();//计示所有计计printf("加计完计!\n");return;}fclose(fp);}void shifang()//计放存函内数{struct record *p1;while(head!=NULL){ p1=head;head=head->next;free(p1);}return;}void findbyname()//按姓名计计某计系人的通计计信息{struct record *p1,*p2;char name[100];int m;system("cls");if(head==NULL){printf("通计计计空,无法计示!\n");return;}p1=head;getchar();printf("计计入需要计人的姓名找:");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("%s的通计容如下内:\n",name);printf("---姓名-------性计------手机--------城市---------计计\n"); printf("==================================\n");printf("== %s",p1->name);printf(" ");printf("%s",p1->gender);printf(" ");printf("%s",p1->tel);printf(" ");printf("%s",p1->city);printf(" ");printf("%s",p1->eip); printf(" \n");printf("==================================\n");printf("\n是否计计计某计系人的姓名信息找(Y/N):");if((getchar()=='y')||(getchar()=='Y'))findbyname();break;}p1=p1->next;}if(m==0)printf("此人未在本通计计中!\n");return;}void findbytel()//按计计计计某计系人的通计计信息{struct record *p1,*p2;char tel[100];int m;system("cls");if(head==NULL){printf("通计计计空,无法计示!\n");return;}p1=head;getchar();printf("计计入需要计人的计计找:");m=0;gets(tel);while(p1!=NULL){while((strcmp(p1->tel,tel))!=0&&p1->next!=NULL) {p2=p1;p1=p1->next;}if(strcmp(p1->tel,tel)==0){m++;printf("%s的通计容如下内:\n",p1->name);printf("---姓名-------性计------手机--------城市---------计计\n"); printf("==================================\n");printf("== %s",p1->name);printf(" ");printf("%s",p1->gender);printf(" ");printf("%s",p1->tel);printf(" ");printf("%s",p1->city);printf(" ");printf("%s",p1->eip); printf(" \n");printf("==================================\n");printf("\n是否计计计某计系人的计计信息找(Y/N):");if((getchar()=='y')||(getchar()=='Y'))findbytel();break;}p1=p1->next;}if(m==0)printf("此人未在本通计计中!\n");return;}void find(){int m;printf("\n计计计计计方式:\n");printf("\t 1------姓名\n");printf("\t 2------计计\n");printf("\t 3------返回\n");printf("计计计:");scanf("%d",&m);while(m!=1&&m!=2&&m!=3){printf("计入计计~计重新计计:");scanf("%d",&m);}if(m==1)findbyname();if(m==2)findbytel();}void altertel() //修改计计计计 {struct record *p;int a = 0;char name[100];system("cls");getchar();printf("计计入需要修改的计系人的姓名:");gets(name);for(p=head;p!=NULL;p=p->next){if(strcmp(p->name,name)==0){printf("%s的通计容如下内:\n",name);printf("---姓名-------性计------手机--------城市---------计计\n"); printf("==================================\n");printf("== %s",p->name);printf(" ");printf("%s",p->gender);printf(" ");printf("%s",p->tel);printf(" ");printf("%s",p->city);printf(" ");printf("%s",p->eip); printf(" \n");printf("==================================\n");printf("计计入新计计:");gets(p->tel);printf("修改成功!\n");a=1;break;}}if(a != 1)printf("无此通计计,\n");return;}void altereip(struct record *p1)//修改计计信息{printf("计计入新计计:");gets(p1->eip);}void altercity() //修改城市信息{struct record *p;int a = 0;char name[100];system("cls");getchar();printf("计计入需要修改的计系人的姓名:");gets(name);for(p=head;p!=NULL;p=p->next){if(strcmp(p->name,name)==0){printf("%s的通计容如下内:\n",name);printf("---姓名-------性计------手机--------城市---------计计\n"); printf("==================================\n");printf("== %s",p->name);printf(" ");printf("%s",p->gender);printf(" ");printf("%s",p->tel);printf(" ");printf("%s",p->city);printf(" ");printf("%s",p->eip); printf(" \n");printf("==================================\n"); printf("计计入计系人目前所在城市:");gets(p->city);altereip(p);printf("修改成功,\n");a = 1;break;}}if(a != 1)printf("无此通计计,\n");return;}void alter(){int m;system("cls");if(head==NULL){printf("通计计计空!无法修改!\n");return;}printf("\n计计计修改方式:\n"); printf("\t 1------计计\n"); printf("\t 2------城市\n"); printf("\t 3------返回\n"); printf("计计计:");scanf("%d",&m);while(m!=1&&m!=2&&m!=3){printf("计入计计~计重新计计:"); scanf("%d",&m);}if(m==1)altertel();if(m==2)altercity();}。
通讯录源代码
#include <iostream>#include <stdlib.h>#include <string>using namespace std;typedef struct //定义个人信息结构体{char name[15]; //存放名字char position[20]; //存放职务char title[20]; //存放职称char number[12]; //存放电话号码char email[20]; //存放电子邮件} Person;Person per[100];/*******************菜单显示函数******************/int Menu(){char str;while(str < '0' || str > '6'){system("cls"); /************清屏**********/cout << "\t\t *****通讯录***** \n";cout << "\t\t┌───────┐\n";cout << "\t\t│1. 添加信息│\n";cout << "\t\t│2. 显示信息│\n";cout << "\t\t│3. 删除信息│\n";cout << "\t\t│4. 查询信息│\n";cout << "\t\t│5. 修改信息│\n";cout << "\t\t│6. 姓名排序│\n";cout << "\t\t│0. 退出程序│\n";cout << "\t\t└───────┘\n";cout << "\t\t请您选择(0-6):";str = getchar();}return (str-'0'); /**********返回数字**********/ }/********************信息添加函数******************/int Input(Person per[], int n){int i=0;char s;while(s != 'n' && s != 'N') //判断是否继续添加信息{cout << "\t姓名:";cin >> per[n+i].name;cout << "\t职务:";cin >> per[n+i].position;cout << "\t职称:";cin >> per[n+i].title;cout << "\t电话:";cin >> per[n+i].number;cout << "\t邮箱:";cin >> per[n+i].email;cout << "\n\t是否继续添加?(Y/N)";cin >> s;i++;}return n; //返回i值到主函数}/*********************显示函数**********************/void Display(Person per[], int n){int i;cout << "\t****************************************************\n";cout << "\t" << "姓名" ;cout << "\t" << "职务" ;cout << "\t" << "职称" ;cout << "\t" << "电话" ;cout << "\t" << "邮箱\n";cout << "\t****************************************************\n";for(i = 0; i <= n; i++) //循环函数,显示个人信息{cout << "\t" << per[i].name ;cout << "\t" << per[i].position ;cout << "\t" << per[i].title ;cout << "\t" << per[i].number ;cout << "\t" << per[i].email <<endl;if(i>1&&i%10==0){printf("\t---------------------------------\n");printf("\t");system("pause");printf("\t---------------------------------\n");}}cout << "\t****************************************************\n";}/****************************删除函数*****************************/int Delete(Person per[], int n){char str[15];int i = 0;int j;cout << "\t请输入想删除记录中的名字: ";cin >> str;while(strcmp(per[i].name, str) != 0 && i < n) /*******字符串比较*******/ i++;if(i == n){cout << "\t通讯录中没有此人!\n";}for(j = i; j < n-1; j++){strcpy(per[j].name, per[j+1].name);strcpy(per[j].position, per[j+1].position); /************将后一条记录拷贝到前一条**********/strcpy(per[j].title, per[j+1].title);strcpy(per[j].number , per[j+1].number);strcpy(per[j].email, per[j+1].email);}cout << "\t已经成功删除!\n";return n-1;}/********************************查询函数*******************************/void Find(Person per[], int n){int m;cout << "\t请选择查询方式:\n";cout << "\t┌──────┐\n";cout << "\t│1------姓名│\n";cout << "\t│2------职务│\n";cout << "\t│3------职称│\n"; //显示查询方式的目录cout << "\t│4------电话│\n";cout << "\t│5------邮箱│\n";cout << "\t│6------返回│\n";cout << "\t└──────┘\n";cout << "\t请选择序号: ";cin >> m;while(m != 1 && m != 2 && m != 3 && m != 4 && m != 5){cout << "\t输入错误,请重新选择: ";cin >> m;}if(m == 1) //按姓名查询{char str[15];int i = 0;cout << "\t请输入想查询的姓名: ";cin >> str;while(strcmp(per[i].name, str) != 0 && i < n)i++;if(i == n){cout << "\t通讯录中没有此人! ";return ;}cout << "\t此人职务:\n" << " \t " << per[i].position <<endl;cout << "\t此人职称:\n" << " \t " << per[i].title <<endl;cout << "\t电话号码:\n" << " \t " << per[i].number <<endl;cout << "\t此人邮箱:\n" << " \t " << per[i].email <<endl;if(m == 2) //按职务查询{char str[20];int i = 0;cout << "\t请输入想查询的职务: ";cin >> str;while(strcmp(per[i].position, str) != 0 && i < n)i++;if(i == n){cout<< "\t通讯录中没有此人! ";return ;}cout << "\t此人姓名:\n" << " \t " << per[i].name <<endl;cout << "\t此人职称:\n" << " \t " << per[i].title <<endl;cout << "\t电话号码:\n" << " \t " << per[i].number <<endl;cout << "\t此人邮箱:\n" << " \t " << per[i].email <<endl; };if(m == 3) //按职称查询{char str[20];int i = 0;cout << "\t请输入想查询的职称: ";cin >> str;while(strcmp(per[i].title, str) != 0 && i < n)i++;if(i == n){cout << "\t通讯录中没有此人! ";return ;}cout << "\t此人姓名:\n" << " \t " << per[i].name <<endl;cout << "\t此人职务:\n" << " \t " << per[i].position <<endl;cout << "\t电话号码:\n" << " \t " << per[i].number <<endl;cout << "\t此人邮箱:\n" << " \t " << per[i].email <<endl;if(m == 4) //按号码查询{char l[12];int i = 0;cout << "\t请输入想查询的电话: ";cin >> l;while((per[i].number, l) != 0 && i < n)i++;if(i == n){cout << "\t通讯录中没有此人! ";return ;}cout << "\t此人姓名:\n" << " \t " << per[i].name <<endl;cout << "\t此人职务:\n" << " \t " << per[i].position <<endl;cout << "\t此人职称:\n" << " \t " << per[i].title <<endl;cout << "\t此人邮箱:\n" << " \t " << per[i].email <<endl; }if(m == 5) //按邮箱查询{char str[20];int i = 0;cout << "\t请输入想查询的邮箱: ";cin >> str;while(strcmp(per[i].email, str) != 0 && i < n)i++;if(i == n){cout << "\t通讯录中没有此人! ";return ;}cout << "\t此人姓名:\n" << " \t " << per[i].name <<endl;cout << "\t此人职务:\n" << " \t " << per[i].position <<endl;cout << "\t此人职称:\n" << " \t " << per[i].title <<endl;cout << "\t电话号码:\n" << " \t " << per[i].number <<endl; };}/**********************修改函数************************/void Modify(Person per[], int n) /*修改函数*/{char str[15];int i=0;cout << "\t想修改的名字:\t ";cin >> str;while(strcmp(per[i].name, str) != 0 && i < n)i++;if(i == n){cout << "\t没有此人!\t";return ;}cout << "\t输入新姓名: ";gets(per[i].name);cout << "\t输入新职务: ";gets(per[i].position);cout << "\t输入新职称: ";gets(per[i].title);cout << "\t输入新号码: ";gets(per[i].number);cout << "\t输入新邮箱: ";gets(per[i].email);cout << "\t修改成功\n\t";}/*****************排序函数***************/void Sort (Person per[], int n){int i;int j;char temp[15];for (i = 0; i < n-1; i++)for (j = i; j < n; j++)if (strcmp(per[i].name , per[j].name) > 0){strcpy(temp, per[i].name );strcpy(per[i].name , per[j].name);strcpy(per[j].name, temp);strcpy(temp, per[i].position );strcpy(per[i].position , per[j].position);strcpy(per[j].position, temp);strcpy(temp, per[i].title );strcpy(per[i].title , per[j].title);strcpy(per[j].title, temp);strcpy(temp, per[i].number );strcpy(per[i].number , per[j].number);strcpy(per[j].number, temp);strcpy(temp, per[i].email );strcpy(per[i].email , per[j].email);strcpy(per[j].email, temp);}Display(per, n);return ;}/******************主函数开始******************/void main(){int n = 0;for(n = 0; n <= 6; n++){switch (Menu()) /*调用主菜单函数,返回值整数作开关语句的条件*/{case 1:cout << "\t添加记录到通讯录\n";n = Input(per, n);cout << "\t";system("pause");break;case 2:cout << "\t通讯录显示\n";Display(per, n);cout << "\t";system("pause");break;case 3:cout << "\t从通讯录中删除记录\n";n = Delete(per, n);cout << "\t";system("pause");break;case 4:cout << "\t通讯录中查找\n";Find(per, n);cout << "\t";system("pause");break;case 5:cout << "\t修改记录\n";Modify(per, n);cout << "\t";system("pause");break;case 6:cout << "\t按姓名排序\n";Sort(per, n);cout << "\t";system("pause");break;case 0: /*如返回值为0则程序结束*/ cout << "\tOVER !\n";default :cout<< "\n\t";exit(0);}}}。
通讯录源代码
#include "stdio.h"#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 telephone\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 telephone\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"); /*显示复制成功*/}。
数据结构通讯录系统源代码
头文件:/* 单链表中每个结点的联系人结构体定义*/typedef struct Node/* 每个联系人结构体*/{char name[10];//姓名char telnum[20];//电话号码char address[256];//地址struct Node *next;}SLNode;void Initiate(SLNode ** head)/* 初始化单链表*/{if((*head=(SLNode *)malloc(sizeof(SLNode)))== NULL){printf("内存空间不足!\n");exit(1) ;}(*head)->next=NULL;/* 置链尾标记NULL */}int Length(SLNode *head)//求当前通讯录中的联系人数{SLNode *p;/* p指向头结点*/int size;/* size初始值为0 */p = head->next;size = 0 ;while(p != NULL && head != NULL)/* 循环计数*/{k = k->next;size++;}return size;/* 返回其大小*/}void DataWrite(SLNode *head){SLNode *p;int i;FILE *fp;fp = fopen("Address list.dat","wb+");//打开文件,覆盖以前的文件p = head->next;for( i = 0;i < Length(head);i++){fwrite(p,sizeof(SLNode),1,fp);p =p->next;}fclose(fp);//关闭文件}void Load(SLNode *head)/* 初始化*/{SLNode *q,*p,*m;FILE *fp;int count = 0 ;fp = fopen("Address list.dat","rb");//以读的方式打开二进制文件//rewind(fp);if(fp == NULL){fp = fopen("Address list.dat","wb+");if(fp != NULL){printf("通讯录打开失败!创建新的通讯录!\n");}else{printf("通讯录打开失败!创建新的通讯录失败!\n");exit(0);}}else{q = (SLNode *)malloc(sizeof(SLNode ));while(fread(q,sizeof(SLNode),1,fp)){//fread(q,sizeof(SLNode),1,fp);if(ferror(fp)){printf("error!\n");break;}p = head;m = (SLNode *)malloc(sizeof(SLNode));strcpy(m->name,q->name);strcpy(m->telnum,q->telnum);strcpy(m->address,q->address);count++;m->next=p->next;/* 将新结点插入队头结点*/p->next=m;memset(q,'\0',sizeof(SLNode));}}fclose(fp);printf("\t\t\t\t 通讯录总联系人数:%d \n",Length(head));}int PrintAll(SLNode *head)/* 打印所有出所有联系人信息*/{int i;SLNode *q;q = head->next;if(q == NULL){printf("通讯录为空!\n");return -1;}else{for(i = 0 ; i< Length(head);i++)//逐个打印联系人信息{printf("编号:%d\t姓名:%s\t号码:%s\t地址:%s\n",i+1,q->name,q->telnum,q->address);q = q->next;}return 0;}}void ANParam(SLNode *head)/* 在带头结点的单链表head的第i(0《i《size)个结点前插入一个存放数据元素x的结点*//* 插入成功则返回1,失败则返回0 */SLNode *p,*q;q=(SLNode*)malloc(sizeof(SLNode));/* 生成新结点由指针p指示*/printf("请输入新联系人姓名:");fflush(stdin);gets(q->name);printf("请输入新联系人的电话号码:");fflush(stdin);gets(q->telnum);printf("请输入新联系人的地址:");fflush(stdin);gets(q->address);p = head;q->next=p->next;/* 将新结点插入队头结点*/p->next=q;DataWrite(head);//将信息以覆盖方式重新写入通讯录中printf("添加成功!\n");}void AWParam(SLNode *head,char telnum[11],char name[10],char address[256])//带形参的添加函数/* 在带头结点的单链表head的第i(0《i《size)个结点前插入一个存放数据元素x的结点*//* 插入成功则返回1,失败则返回0 */{SLNode *p,*q;q=(SLNode*)malloc(sizeof(SLNode));/* 生成新结点由指针p指示*/strcpy(q->name,name);strcpy(q->telnum,telnum);strcpy(q->address,address);p = head;q->next=p->next;/* 将新结点插入队头结点*/p->next=q;DataWrite(head);printf("添加成功!\n");}void Delete(SLNode *head)/* 删除带头结点的单链表head的第i(1~n,n为结点个数)个结点/* 删除结点的元素有x带回,删除成功返回1,否则返回0 */{SLNode *p,*s;int choice,i,m,ret;if(PrintAll(head))//如果通讯录为空,则无法删除,退出返回主菜单{return ;}m = Length(head);//求当前通讯录人数printf("请输入你要删除联系人的前面编号:");ret = scanf("%d",&choice);while(!ret || choice > m || choice < 1)//判断输入是否为数字,是否为,若否,则重新输入{printf("请正确输入你要删除联系人的前面编号!\n");fflush(stdin);ret = scanf("%d",&choice);}p=head;i = 0;//由于我们打印出来时是由1开始,所以此处定义为0while(p->next != NULL && p->next->next != NULL && i < (choice-1) )//查找所选择编号前一个结点{p = p->next;i++;}s = p->next;p->next = p->next->next;free(s);DataWrite(head);//将更新的数据重新写入文件中printf("删除成功!");}void SBName(SLNode *head)/* 查找函数,如果不存在,可添加*/{SLNode *p;int choice;char telnum[11], name[10], address[256];p=head->next;printf("请输入你要查找联系人的姓名:");fflush(stdin);gets(name);while(p != NULL){if(!strcmp(p->name,name)){printf("姓名:%s\t电话:%s\t地址:%s\n",p->name,p->telnum,p->address);break;}p=p->next;}if(p == NULL){printf("错误!所查找联系人不在通讯录,是否将该联系人添加至通讯录?\n");printf("请按0确认添加,否则不添加\n");scanf("%d",&choice);//判断输入是否为数字,且数字范围是1~m之间,否则重新输入if(!choice){printf("该联系人姓名:%s\n",name);printf("请输入该联系人的电话:\n");fflush(stdin);gets(telnum);printf("请输入该联系人的地址:\n");fflush(stdin);gets(address);AWParam(head,telnum,name,address);}}}void SBTell(SLNode *head)/* 查找函数,如果不存在,可添加*/{SLNode *p;int choice;char telnum[20], name[10], address[256];p=head->next;printf("请输入你要查找联系人的号码:");fflush(stdin);gets(telnum);while(p != NULL){if(!strcmp(p->telnum,telnum)){printf("姓名:%s\t电话:%s\t地址:%s\n",p->name,p->telnum,p->address);break;}p=p->next;}if(p == NULL){printf("错误!所查找联系人不在通讯录,是否将该联系人添加至通讯录?\n");printf("请按0确认添加,否则不添加\n");scanf("%d",&choice);if(!choice){printf("该联系人电话号码:%s\n",telnum);printf("请输入该联系人的姓名:\n");fflush(stdin);gets(name);printf("请输入该联系人的地址:\n");fflush(stdin);gets(address);AWParam(head,telnum,name,address);}}}void Destroy(SLNode **head)/* 因为单链表的结点空间是在程序运行中申请的,而系统只负责自动收回程序中静态分配的内存空间,所以在程序退出前释放动态申请的内存空间*/{SLNode *p,*p1;p= *head;while(p!=NULL)/* 通过循环释放所有的动态开辟的内存空间*/{p1=p;p=p->next;free(p1);}*head=NULL;}void Alter(SLNode *head){SLNode *p;int choice,num,i,m;if(PrintAll(head))//通讯录为空无法进行修改,退出返回主菜单{return ;}printf("请输入你要更改的信息人的前面编号:");fflush(stdin);m = Length(head);//获取当前通讯录人数i = scanf("%d",&num);//判断是否输入数字while( !i || num > m || num < 1 )//判断输入是否为数字,若否,则重新输入{printf("请正确输入你要更改的信息人的前面编号!\n");fflush(stdin);}p = head->next;//p指向第一个联系人for(i = 0 ; i< Length(head);i++){if((i+1) == num){printf("姓名:%s\t号码:%s\t地址:%s\n",p->name,p->telnum,p->address); lable1: printf("请输入你要更改的信息:\n");printf("1、姓名\t2、号码\t3、地址\t0、全部修改\n");m = scanf("%d",&choice);while(!m || choice > 3 || choice < 0)//判断输入是否为数字,若否,则重新输入{printf("请正确输入选项!\n");fflush(stdin);m = scanf("%d",&choice);}switch(choice){case 1:printf("请输入新姓名:");fflush(stdin);gets(p->name);break;case 2:printf("请输入新号码:");fflush(stdin);gets(p->telnum);break;case 3:printf("请输入新地址:");fflush(stdin);gets(p->address);break;default:printf("请输入新姓名:");fflush(stdin);gets(p->name);printf("请输入新号码:");fflush(stdin);gets(p->telnum);printf("请输入新地址:");fflush(stdin);gets(p->address);break;}printf("以下是刚更改的信息,请确认:");printf("姓名:%s\t号码:%s\t地址:%s\n",p->name,p->telnum,p->address);printf("是否继续更改该联系人?\n");printf("继续修改请按0,否则保存修改!\n");printf("输入:");scanf("%d",&choice);if(!choice){goto lable1;}else{DataWrite(head);//将修改后的文件全部写入通讯录中}}p = p->next;}}int menu(){int ret,i;printf("\t\t******************通讯录******************\n\n");printf("\t\t 1、查找联系人\t");printf("2、添加联系人\n\n");printf("\t\t 3、显示所有联系人信息\t");printf("4、删除联系人\n\n");printf("\t\t 5、更改联系人信息\t");printf("6、退出\n\n");printf("\t\t**************请选择相应功能**************\n");printf("\t\t输入对应功能编号:");i = scanf("%d",&ret);while(!i || ret > 6 || ret <1)//判断输入是否为数字,若否,则重新输入{printf("\t\t请正确输入功能编号!\n");fflush(stdin);printf("\t\t输入对应功能编号:");i = scanf("%d",&ret);}return ret;}void DeleteAll(SLNode *head){FILE *fp;fp = fopen("Address list.dat","wb+");//已创建新的文件覆盖以前的文件,一达到清除目的Destroy(&head);Initiate(&head);//初始化单链表,创建新的头结点printf("清除成功!\n");}测试主函数:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <windows.h>#include "LinkList.h"int main(){int choice = 0 ,ret;SLNode *head;char s[500] = "\t\t**谢谢使用**\n",a[] = "\t\t华中农业大学楚天学院通讯录系统\n";system("title 华中农业大学楚天学院通讯录系统");system("color 30");while(choice < strlen(a)){putchar(a[choice]);Sleep(80);choice++;}Initiate(&head);Load(head);while(1){switch(menu()){case 1:system("cls");if(head->next == NULL){printf("通讯录为空!\n");break ;}else{printf("请选择查找方式:");printf("1、姓名\t 2、电话号码\t 3、退出\n");ret = scanf("%d",&choice);while(!ret || choice >3 || choice <1){printf("输入有误!请正确选择查找方式!\n");fflush(stdin);ret = scanf("%d",&choice);}switch(choice){case 1:SBName(head);break;case 2:SBTell(head);break;case 3:break;default:break;}}break;case 2:system("cls");ANParam(head);break;case 3:system("cls");PrintAll(head);break;case 4:system("cls");printf("请选择删除方式:\n");printf("1、清空通讯录\t2、删除指定联系人\n");ret = scanf("%d",&choice);while(!ret || choice <1 || choice >2){printf("输入错误!请正确选择删除方式!\n");ret = scanf("%d",&choice);}if(choice == 1){DeleteAll(head);}else if(choice == 2){Delete(head);}break;case 5:system("cls");Alter(head);break;case 6:choice = 0;while(choice < strlen(s)){putchar(s[choice]);Sleep(80);choice++;}exit(0);break;default:printf("输入错误!请重新输入!\n");break;}}Destroy(&head);//清除动态申请空间system("pause");return 0;}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
case '3':mainmenu();break;
}
}
void search()
{
printf("\n\t\t***************** 按姓名查找 *******************");
char name[20];
if (i+1<len) continue;//重名纪录再检索
}
printf(" -----------------------------------------------------------------------------\n");
printf("\n\t\t按任意键返回查询菜单");
strcpy(s->data.street,people[i].street);
strcpy(s->data.eip,people[i].eip);
strcpy(s->data.state,people[i].state);
s->prior=p->prior;
printf("\n\t\t请输入姓名:");
scanf("%s",name);
printf(" 查询到的信息:\n");
printf(" %-18s%-18s%-18s%-15s%s\n","姓名","街道","城市","邮编","国家");//格式控制输出
printf(" -----------------------------------------------------------------------------\n");
printf(" %-18s%-18s%-18s%-15s%s\n","姓名","街道","城市","邮编","国家");
printf(" -----------------------------------------------------------------------------\n");
printf("\n\t\t******************1-新添纪录 ***************");
printf("\n\t\t******************2-查找联系人 ***************");
printf("\n\t\t******************3-删除联系人 ***************");
getch();
searchmenu();
}
void del() //删除纪录
}
printf(" -----------------------------------------------------------------------------\n");
}
else
printf("\n\t\t通讯录中无任何纪录");
printf("\n\t\t按任意键返回查询菜单:");
getch();
searchmenu();
}
void display()//显示所有纪录
{
int i;
system("cls");
if(len!=0)
{
printf("\n\t\t*************** 以下为通讯录所有信息************\n\n");
char eip[20]; //邮编
char state[20];
}people[500];//500个记录,可修改
struct pnode
{
record data;
struct pnode *next, *prior;//双循环链表
};
typedef pnode * linklist;
getch();
return;
}
printf("\n\t\t文件导入成功");
printf("\n\t\t通讯录文件中无任何纪录");
printf("\n\t\t按任意键返回主菜单");
getch(); retu源自n; } void listinsert()//增加新结点
printf("\n\t\t**************** 1-显示所有记录 ****************");
printf("\n\t\t**************** 2-按姓名查询 ****************");
printf("\n\t\t**************** 3-返回主菜单 ****************");
void load();//载入文件内容
void save();//写入文件
void del();//删除记录
void listinsert();//插入结点函数
void initlist();//初始化链表函数
void main() //主函数
{
initlist();
load();
printf("\n\t\t邮编:");
scanf("%s",&people[len].eip);
printf("\n\t\t国家:");
scanf("%s",&people[len].state);
len++;
printf("\n\t\t是否继续添加?(Y/N):");
s->next=p;
p->prior->next=s;
p->prior=s;
p=p->next;
}
}
void mainmenu()//主菜单
{
char ch;
system("cls");
printf("\n\t\t***************欢迎进入通讯录系统***************");
exit(0);
}
else
{
printf("\n\t\t通讯录文件已建立");
printf("\n\t\t按任意键进入主菜单");
getch();
return;
}
exit(0);
}
//导入文件功能部分
fseek(fp,0,2);
if (getch()=='y')
enter();
return;
}
void searchmenu()//查询菜单
{
char ch;
system("cls");
printf("\n\t\t******************* 查询菜单 *******************");
for (i=0;i<len;i++)
{
printf(" %-18s%-18s%-18s%-15s%s\n",people[i].name,people[i].street,people[i].city,people[i].eip,people[i].state);
if (i+1<len) continue;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct record
{
char name[20];
char street[20];
char city[20];
printf("\n\t\t姓名:");
scanf("%s",&people[len].name);
printf("\n\t\t街道:");
scanf("%s",&people[len].street);
printf("\n\t\t城市:");
scanf("%s",&people[len].city);
printf("\n\t\t************************************************");
printf("\n\t\t请选择:");
printf("%c",ch=getch());
switch (ch)
{
case '1':display();break;
linklist l;
int len=0;//链表长度
FILE *fp; //文件指针
void mainmenu();//主菜单
void searchmenu();//查询菜单