教学材料《C语言》-强大的通讯录源代码_第1页
教学材料《C语言》-强大的通讯录源代码_第2页
教学材料《C语言》-强大的通讯录源代码_第3页
教学材料《C语言》-强大的通讯录源代码_第4页
教学材料《C语言》-强大的通讯录源代码_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include"stdio.h"/*I/O函数*/#include"stdlib.h" /*标准库函数*/#include"string.h" /*字符串函数*/#include"ctype.h" /*字符操作函数*/#include"conio.h"#defineMAX50 /*定义常数表示记录数*/structADDRESS /*定义数据结构*/{charname[15]; /*姓名*/charunits[20];/*单位*/charphone[15];/*电话*/};intInputRecord(structADDRESSr[]);/*输入记录*/voidListRecord(structADDRESSr[],intn);/*显示记录*/intDeleteRecord(structADDRESSr[],intn);/*删除记录*/intInsertRecord(structADDRESSr[],intn);/*插入记录*/voidSaveRecord(structADDRESSr[],intn);/*记录保存为文件*/intLoadRecord(structADDRESSr[]);/*从文件中读记录*/intFindRecord(structADDRESSr[],intn,char*s);/*查找函数*/voidShowRecord(structADDRESStemp); /*显示单条记录*/voidmain(){inti;chars[128];structADDRESSaddress[MAX];/*定义结构体数组*/intnum;/*保存记录数*/system("cls");while(1){ system("cls"); printf("********************MENU*******************\n\n"); printf("|0:Inputarecord|\n"); printf("|1:Listrecordsinthefile|\n"); printf("|2:Deletearecord|\n"); printf("|3:Insertarecordtothelist|\n"); printf("|4:Saverecordstothefile|\n"); printf("|5:Loadrecordsfromthefile|\n"); printf("|6:Quit|\n\n"); printf("*******************************************\n"); do{ printf("\n Inputyourchoice(0~6):");/*提示输入选项*/ scanf("%s",s);/*输入选择项*/ i=atoi(s);/*将输入的字符串转化为整型数*/ }while(i<0||i>6);/*选择项不在0~11之间重输*/switch(i)/*调用主菜单函数,返回值整数作开关语句的条件*/{ case0:num=InputRecord(address);break;/*输入记录*/ case1:ListRecord(address,num);break;/*显示全部记录*/ case2:num=DeleteRecord(address,num);break;/*删除记录*/ case3:num=InsertRecord(address,num);break;/*插入记录*/ case4:SaveRecord(address,num);break;/*保存文件*/ case5:num=LoadRecord(address);break;/*读文件*/ case6:exit(0);/*如返回值为11则程序结束*/}}}/***输入记录,形参为结构体数组,函数值返回类型为整型表示记录长度*/intInputRecord(structADDRESSt[]){inti,n;char*s;system("cls");/*清屏*/printf("\npleaseinputrecordnumyouwanttoinput.\n");/*提示信息*/scanf("%d",&n);/*输入记录数*/printf("pleaseinputthe%drecords\n",n);/*提示输入记录*/printf("nameunitstelephone\n");printf("------------------------------------------------\n");for(i=0;i<n;i++){scanf("%s%s%s",t[i].name,t[i].units,t[i].phone);/*输入记录*/printf("----------------------------------------------\n");}getch();returnn;/*返回记录条数*/}/*显示记录,参数为记录数组和记录条数*/voidListRecord(structADDRESSt[],intn){inti;system("cls");printf("\n\n*******************ADDRESSLIST*****************\n");printf("nameunitstelephone\n");printf("------------------------------------------------\n");for(i=0;i<n;i++)printf("%-13s%-20s%-15s\n",t[i].name,t[i].units,t[i].phone);printf("************************end*********************\n");getch();}/*显示指定的一条记录*/voidShowRecord(structADDRESStemp){printf("\n\n************************************************\n");printf("nameunitstelephone\n");printf("------------------------------------------------\n");printf("%-13s%-20s%-15s\n",,temp.units,temp.phone);printf("**********************end***********************\n");getch();}/*删除函数,参数为记录数组和记录条数*/intDeleteRecord(structADDRESSt[],intn){chars[20];/*要删除记录的姓名*/inti,j;system("cls");printf("pleaserecordname:\n");/*提示信息*/scanf("%s",s);/*输入姓名*/i=FindRecord(t,n,s);/*调用FindRecord函数*/if(i>n-1)/*如果i>n-1超过了数组的长度*/printf("Can'tfoundtherecord\n");/*显示没找到要删除的记录*/else{ShowRecord(t[i]);/*调用输出函数显示该条记录信息*/ 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].phone,t[j].phone);/*将后一条记录的电话拷贝到前一条*/ } n--;/*记录数减1*/ printf("Deletearecordsuccessfully!\n");}getch();returnn;/*返回记录数*/}/*插入记录函数,参数为结构体数组和记录数*/intInsertRecord(structADDRESSt[],intn)/*插入函数,参数为结构体数组和记录数*/{structADDRESStemp;/*新插入记录信息*/inti,j;chars[20];/*确定插入在哪个记录之前*/system("cls");printf("pleaseinputrecord\n");printf("************************************************\n");printf("nameunitstelephone\n");printf("------------------------------------------------\n");scanf("%s%s%s",,temp.units,temp.phone);/*输入插入信息*/printf("------------------------------------------------\n");printf("pleaseinputlocatename\n");scanf("%s",s);/*输入插入位置的姓名*/i=FindRecord(t,n,s);/*调用FindRecord,确定插入位置*/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].phone,t[j].phone);/*当前记录的电话拷贝到后一条*/}strcpy(t[i].name,);/*将新插入记录的姓名拷贝到第i个位置*/strcpy(t[i].units,temp.units);/*将新插入记录的单位拷贝到第i个位置*/strcpy(t[i].phone,temp.phone);/*将新插入记录的电话拷贝到第i个位置*/n++;/*记录数加1*/getch();returnn;/*返回记录数*/}/*保存函数,参数为结构体数组和记录数*/voidSaveRecord(structADDRESSt[],intn){inti;FILE*fp;/*指向文件的指针*/system("cls");printf("Savingtherecordstothefileaddress.txt\n.............\n");if((fp=fopen("address.txt","wb"))==NULL)/*打开文件,并判断打开是否正常*/{printf("cannotopenfile\n");/*没打开*/exit(1);/*退出*/}printf("\nSavingthefile\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].phone);/*格式写入记录*/fprintf(fp,"\r\n");/*将换行符号写入文件*/}fclose(fp);/*关闭文件*/printf("Savetherecordssuccessfully!\n");/*显示保存成功*/getch();}/*读入函数,参数为结构体数组*/intLoadRecord(structADDRESSt[]){inti,n;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论