源代码:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"
#include "stdlib.h"
#define null 0
struct 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输入e-mail:");
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 { 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\te-mail:%s",student[i].e_mail); printf("\t\t"); if (i+1 { printf("\n\t\t__________________________"); system("pause"); } } printf("\n\t\t************************************************"); } else printf("\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 { 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\te-mail:%s",student[i].e_mail); printf("\n\t\t************************************************"); mark++; if ((i+1) { printf("\n\t\t是否继续查找相同名字的用户信息:(y/n)"); if (getch()=='y') { a=i; continue; } else return(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 { 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\te-mail:%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 { 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输入e-mail:"); 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 { 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\te-mail:%s",student[i].e_mail); printf("\n\t\t是否删除?(y/n)"); if (getch()=='y') { for (j=i;j student[j]=student[j+1]; num--; deletemark++; printf("\n\t\t删除成功"); printf("\n\t\t是否继续删除?(y/n)"); if (getch()=='y') deletebyphone(); return; } else return; } 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 { 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\te-mail:%s",student[i].e_mail); printf("\n\t\t________________________________"); printf("\n\t\t是否删除?(y/n)"); if (getch()=='y') { for (j=i;j student[j]=student[j+1]; num--; deletemark++; printf("\n\t\t删除成功"); if((i+1) { 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) { printf("\n\t\t是否继续删除相同姓名的用户信息?(y/n)"); if (getch()=='y') { a=i; continue; } } } else continue; } 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 { 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(https://www.doczj.com/doc/294726164.html,,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 { fwrite(student,sizeof(student),1,fp); } } printf("保存成功!"); fclose(fp); return(0); }