【文件属性】:
文件名称:C语言,通讯录管理程序设计
文件大小:60KB
文件格式:RAR
更新时间:2011-11-18 02:52:24
通讯录,管理,程序设计,C
C语言编写的通讯录管理程序设计
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"conio.h"
#define HEADER1"------------------------------------------MESSAGE MENU--------------------------------------------------------------\n"
#define HEADER2" | num | name | phonenumber | address |\n"
#define HEADER3" |-------------------------------------------------------------------|\n"
#define FORMAT " | %-10s %-10s %-15s %-20s |\n"
#define DATA p->num,p->name,p->phonenum,p->address
#define END " ------------------------------------------------------------------------------------------------------------------\n"
#define N 100
int saveflag=0; /*是否需要存盘的标志变量*/
typedef struct record /*标记为record*/
{
char num[4]; /*编号*/
char name[10]; /*姓名*/
char phonenum[15]; /*电话*/
char address[20]; /*地址*/
}
RECORD;
RECORD tele[N];
void menu();
void Disp();
void Qur();
void Modify();
void SelectSort();
void Wrong();
void Save();
void main()
{
RECORD tele[N]; /*定义RECOED结构体*/
FILE *fp; /*文件指针*/
int select; /*保持内选择结果变量*/
char ch; /*保存y,Y,n,N*/
int count=0; /*保存文件中的记录条数(或元素个数)*/
fp=fopen("C:\\record.txt","a+"); /*以追加方式打开文本文件c;\record,可读可写,若此文件不存在,会创建此文件*/
if(fp==NULL)
{
printf("\n=>Can not open file!\n");
exit(0);
}
while(!feof(fp))
{
if(fread(&tele[count],sizeof(RECORD),1,fp)==1) /*一次从文件中读取一条通讯记录*/
count++;
}
fclose(fp); /*关闭文件*/
printf("\n=>Open file success,the total records number is:%d.\n",count);
getchar();
menu();
while(1)
{
system("cls");
menu();
cprintf("\n Please input your choice(0-8):"); /*显示提示信息*/
ch=getch();
if(ch=='0')
{
if(saveflag==1) /*若对数组的数据有修且未进行存盘操作,则此标志为1*/
{
getchar();
printf("\n==>Whether save the modified record to file?(y/n):");
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
Save(tele,count);
}
printf("\n=>Thank you for useness!");
getchar();
break ;
}
switch(ch)
{
case'0':break;
case'1':count=Add(tele,count);break; /*增加通讯记录*/
case'2':system("cls");Disp(tele,count);break; /*显示通讯记录*/
case'3':count=Del(tele,count) ;break; /*删除通讯记录*/
case'4':Qur(tele,count);break; /*查询通讯记录*/
case'5':Modify(tele,count);break; /*修改通讯记录*/
case'6':count=Insert(tele,count);break; /*插入通讯记录*/
case'7':SelectSort(tele,count);break;/*排序通讯记录*/
case'8':Save(tele,count);break; /*保存通讯记录*/
default: Wrong();getchar();break; /*按键有误,必需为0~8*/
}
}
}
void menu()
{
system("cls"); /*调用DOS命令清屏,与clrscr()功能相同*/
textcolor(63); /*在文本模式中选择新的字符颜色*/
gotoxy(10,5); /*在文本窗口中设置光标*/
printf(" The communication management system\n");
gotoxy(10,8);
printf(" ****Menu**** \n");
gotoxy(10,9);
printf("\n");
gotoxy(10,10);
printf("\* 1. Add record\n"); /*增加*/
gotoxy(10,11);
printf("\* 2. Display record\n"); /*展览*/
gotoxy(10,12);
printf("\* 3. Delete record\n"); /*删除*/
gotoxy(10,13);
printf("\* 4. Search record\n"); /*查找*/
gotoxy(10,14);
printf("\* 5. Modify record\n"); /*更改*/
gotoxy(10,15);
printf("\* 6. Insert record\n"); /*插入*/
gotoxy(10,16);
printf("\* 7. Sort record\n"); /*整理*/
gotoxy(10,17);
printf("\* 8. Save record\n"); /*保存*/
gotoxy(10,18);
printf("\* 0 Quit system\n"); /*退出*/
gotoxy(10,19);
printf(" ************ \n");
/*cprintf()送格式话输出至文本窗口屏幕中*/
}
/*格式化输出表头*/
void printheader()
{
printf(HEADER1);
printf(HEADER2);
printf(HEADER3);
}
/*格式化输出表中数据*/
void printdata(RECORD q)
{
RECORD *p;
p=&q;
printf(FORMAT,DATA);
}
void Disp( RECORD temp[],int n) /*显示数组temp[]中存储的通讯记录,内容为record结构中定义的内容*/
{
int i;
if(n==0) /*表示没有通讯记录*/
{
printf("\n=>No menu record!\n");
getchar();
return ;
}
printf("\n\n");
printheader(); /*输出表格头部*/
i=0;
while(iNot find this menu record!\n");
}
int Locate(RECORD temp[],int n,char findmess[],char nameorphonenum[])
{
int i=0;
if(strcmp(nameorphonenum,"phonenum")==0) /*按电话号码查询*/
{
while(ilens) printf("\n Exceed the required length! \n"); /*进行长度检测,超过lens值重新输入*/
}
while(strlen(n)>lens);
strcpy(t,n); /*将输入发字符串拷贝到字符串t中*/
}
int Add(RECORD temp[],int n) /*增加通讯记录*/
{
char ch,num[10];
int i,flag=0;
system("cls");
Disp(temp,n); /*先打印出已有的通讯录信息*/
while(1) /*一次可输入多条记录,直至输入编号为0的记录才结束添加操作*/
{
while(1) /*输入记录编号,保证该编号没有被使用,若输入编号为0,则退出添加记录操作*/
{
stringinput(num,10,"Input number(press '0' return menu):"); /*格式化输入编号并检验*/
flag=0;
if(strcmp(num,"0")==0) /*输入为0,则退出添加操作,返回主界面*/
{
return n;
}
i=0;
while(iThe number %s is existing,try again?(y/n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y') continue;
else
return n;
}
else {break;}
}
strcpy(temp[n].num,num); /*将字符串num拷贝到temp[n].num中*/
stringinput(temp[n].name,15,"Name:\n");
stringinput(temp[n].phonenum,15,"Telephone:\n");
stringinput(temp[n].address,15,"Address:\n");
saveflag=1;
n++;
}
return n;
}
void Qur(RECORD temp[],int n) /*按编号或姓名查询通讯记录*/
{
int select; /*按1:按姓名查,2:按电话号码查,其他:返回住界面(菜单)*/
char searchinput[20]; /*包寻用户输入的查询内容*/
int p=0;
if(n<=0) /*若数组为空*/
{
system("cls");
printf("\n=>No menu record!\n");
getchar();
return ;
}
system("cls");
printf("\n =>1 Search by name =>2 Search by telephonenumber\n");
printf(" Please choice[1,2]:");
scanf("%d",&select);
if(select==1) /*按姓名查询*/
{
stringinput(searchinput,10,"Input the existing name:");
p=Locate(temp,n,searchinput,"name");
/*在数组temp中查找编号为searchinput值的元素,并返回该数组元素的下标值*/
if(p!=-1) /*若找到该记录*/
{
printheader();
printdata(temp[p]);
printf(END);
printf("Press any key to return");
getchar();
}
else Nofind();
getchar();
}
else if(select==2) /*按电话号码查询*/
{
stringinput(searchinput,15,"Input the existing telephone number:");
p=Locate(temp,n,searchinput,"phonenum");
if(p!=-1)
{
printheader();
printdata(temp[p]);
printf(END);
printf("Press any key to return");
getchar();
}
else Nofind();
getchar();
}
else Wrong();
getchar();
}
int Del(RECORD temp[],int n)
{
int sel;
char findmess[20];
int p=0,i=0;
if(n<=0)
{
system("cls");
printf("\n=>No menu record!\n");
getchar();
return n;
}
system("cls");
Disp(temp,n);
printf("\n =>1 Delete by name =>2 Delete by telephonenumber\n");
printf(" Please choice[1,2]:");
scanf("%d",&sel);
if(sel==1)
{
stringinput(findmess,10,"Input the existing name:");
p=Locate(temp,n,findmess,"name");
getchar();
if(p!=-1)
{
for(i=p+1;iDelete success!\n");
n--;
getchar();
saveflag=1;
}
else Nofind();
getchar();
}
else if(sel==2) /*先按电话号码查询到该记录所在的数组元素的下标值*/
{
stringinput(findmess,15,"Input the existing telephone number:");
p=Locate(temp,n,findmess,"phonenum");
getchar();
if(p!=-1)
{
for(i=p+1;iDelete success!\n");
n--;
getchar();
saveflag=1;
}
else Nofind();
getchar();
}
return n;
}
void Modify(RECORD temp[],int n)
{
char findmess[20];
int p=0;
if(n<=0)
{
system("cls");
printf("\n=>No menu record!\n");
getchar();
return ;
}
system("cls");
printf("modify menu book recorder");
Disp(temp,n);
stringinput(findmess,10,"Input the existing name:"); /*输入并检验该姓名*/
p=Locate(temp,n,findmess,"name"); /*查询到该数组元素,并返回下标值*/
if(p!=-1) /*若p!=-1,表明已经找到该数组元素*/
{
printf("Number:%s,\n",temp[p].num);
printf("Name:%s,",temp[p].name);
stringinput(temp[p].name,15,"Input new name:");
printf("Name:%s,",temp[p].phonenum);
stringinput(temp[p].name,15,"Input new telephone:");
printf("Name:%s,",temp[p].address);
stringinput(temp[p].address,30,"Input new address:");
printf("\n=>modify success!\n");getchar();
Disp(temp,n); getchar();saveflag=1;
}
else
{
Nofind();
getchar();
}
return ;
}
int Insert(RECORD temp[],int n)
{
char ch,num[10],s[10];
/*s[]保存插入点位置之前的记录编号,num[]保存输入的新记录的编号*/
RECORD newinfo;
int flag=0,i=0,m=0;
system("cls");
Disp(temp,n);
while(1)
{
stringinput(s,10,"Please input insert location after the Number:");
flag=0;i=0;
while(iThe number is not existing,try again?(y\n):",s);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{
continue;
}
else
{
return n;
}
}
}
/*以下新记录的输入操作与Add()相同*/
while(1)
{
stringinput(num,10,"Input new Number:");
flag=0;i=0;
while(iSorry,The number %s is existing,try again?(y\n):",num);
scanf("%c",&ch);
if(ch=='y'||ch=='Y')
{
continue;
}
else
{
return n;
}
}
else break;
}
strcpy(newinfo.num,num); /*将字符串num拷贝到newinfo.num中*/
stringinput(newinfo.name,15,"Name:");
stringinput(newinfo.phonenum,15,"Telephone:");
stringinput(newinfo.address,15,"Address:");
saveflag=1; /*在main()中有对该全局变量的判断,若为1,则进行存盘操作*/
for(i=n-1;i>m;i--) /*从最后一个组织元素开始往前移一个元素位置*/
{
strcpy(temp[i+1].num,temp[i].num);
strcpy(temp[i+1].name,temp[i].name);
strcpy(temp[i+1].phonenum,temp[i].phonenum);
strcpy(temp[i+1].address,temp[i].address);
}
strcpy(temp[m+1].num,newinfo.num);
strcpy(temp[m+1].name,newinfo.name);
strcpy(temp[m+1].phonenum,newinfo.phonenum);
strcpy(temp[m+1].address,newinfo.address);
n++;
Disp(temp,n);
printf("\n\n");getchar();
return n;
}
void SelectSort(RECORD temp[],int n)
{
int i=0,j=0,flag=0,indexmin,select;
char charflag[10];
RECORD newinfo;
if(n<=0)
{
system("cls");
printf("\n=>Not record!\n");
getchar();
return ;
}
system("cls");
Disp(temp,n); /*显示排序前的所有记录*/
printf("\n =>1 SORT by num =>2 SORT by another way \n");
printf(" Please choice[1,2]:");
scanf("%d",&select);
if(select==1) /*按记录编号排序*/
{
for(i=0;iSort complete!\n");getchar();
return;
}
else if(select==2)
{
for(i=0;iSort complete!\n");getchar();
return;
}
else
{
Wrong();
getchar();
getchar();
return;
}
}
void Save(RECORD temp[],int n)
{
int i=0;
FILE *fp;
fp=fopen("c:\\record.txt","w"); /*以只写方式打开文本文件*/
if(fp==NULL) /*打开文件失败*/
{
printf("\n=>Open file error!\n");
getchar();
return;
}
for(i=0;i0)
{
getchar();
printf("\n\n=>Save file complete,total saved's record number is:%d\n",i);
getchar();
saveflag=0;
}
else
{
system("cls");
printf("The current link is empty,no menu record is saved!\n");
getchar();
}
fclose(fp); /*关闭此文件*/
}
【文件预览】:
通讯录管理Part1.doc
通讯录管理Part2.doc