📄 dhbgl.c
字号:
void Modify(TELEBOOK temp[],int n)
{
char findmess[20];
int p=0;
if(n<=0)
{ system("cls");
printf("\n=====>No telephone number record!\n");
getchar();
return ;
}
system("cls");
printf("modify telephone 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].phonenum,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(TELEBOOK temp[],int n)
{
char ch,num[10],s[10]; /*s[]保存插入点位置之前的记录编号,num[]保存输入的新记录的编号*/
TELEBOOK newinfo;
int flag=0,i=0,kkk=0;
system("cls");
Disp(temp,n);
while(1)
{ stringinput(s,10,"please input insert location after the Number:");
flag=0;i=0;
while(i<n) /*查询该编号是否存在,flag=1表示该编号存在*/
{
if(strcmp(temp[i].num,s)==0) {kkk=i;flag=1;break;}
i++;
}
if(flag==1)
break; /*若编号存在,则进行插入之前的新记录输入操作*/
else
{ getchar();
printf("\n=====>The number %s 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:");
i=0;flag=0;
while(i<n) /*查询该编号是否存在,flag=1表示该编号存在*/
{
if(strcmp(temp[i].num,num)==0) {flag=1;break;}
i++;
}
if(flag==1)
{
getchar();
printf("\n=====>Sorry,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,"Adress:");
saveflag=1; /*在main()有对该全局变量的判断,若为1,则进行存盘操作*/
for(i=n-1;i>kkk;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[kkk+1].num,newinfo.num); /*在kkk的元素位置后插入新记录*/
strcpy(temp[kkk+1].name,newinfo.name);
strcpy(temp[kkk+1].phonenum,newinfo.phonenum);
strcpy(temp[kkk+1].address,newinfo.address);
n++;
Disp(temp,n);
printf("\n\n");
getchar();
return n;
}
/*利用选择排序法实现数组的按记录编号或姓名的升序排序*/
void SelectSort(TELEBOOK temp[],int n)
{
int i=0,j=0,flag=0,indexmin,select;
char charflag[10];
TELEBOOK newinfo;
if(n<=0)
{ system("cls");
printf("\n=====>Not telephone record!\n");
getchar();
return ;
}
system("cls");
Disp(temp,n); /*显示排序前的所有记录*/
printf(" ==>1 SORT BY NUMBER ==>2 SORT BY NAME\n");
printf(" please choice[1,2]:");
scanf("%d",&select);
if(select==1) /*按记录编号排序*/
{
for(i=0;i<n-1;i++)
{
flag=32767;indexmin=0;
for(j=i;j<n;j++)
{ if(atoi(temp[j].num)<flag)
{ flag=atoi(temp[j].num);
indexmin=j;
}
}
strcpy(newinfo.num,temp[i].num); /*利用结构变量newinfo实现数组元素的交换*/
strcpy(newinfo.name,temp[i].name);
strcpy(newinfo.phonenum,temp[i].phonenum);
strcpy(newinfo.address,temp[i].address);
strcpy(temp[i].num,temp[indexmin].num);
strcpy(temp[i].name,temp[indexmin].name);
strcpy(temp[i].phonenum,temp[indexmin].phonenum);
strcpy(temp[i].address,temp[indexmin].address);
strcpy(temp[indexmin].num,newinfo.num);
strcpy(temp[indexmin].name,newinfo.name);
strcpy(temp[indexmin].phonenum,newinfo.phonenum);
strcpy(temp[indexmin].address,newinfo.address);
}
Disp(temp,n); /*显示排序后的所有记录*/
saveflag=1;
printf("\n =====>sort complete!\n");
getchar();
return;
}
else if(select==2)
{
for(i=0;i<n-1;i++)
{
charflag[0]=255;indexmin=0;
for(j=i;j<n;j++)
{ if(strcmp(temp[j].name,charflag)>0)
{ charflag[0]=temp[j].name;
indexmin=j;
}
}
strcpy(newinfo.num,temp[i].num); /*利用结构变量newinfo实现数组元素的交换*/
strcpy(newinfo.name,temp[i].name);
strcpy(newinfo.phonenum,temp[i].phonenum);
strcpy(newinfo.address,temp[i].address);
strcpy(temp[i].num,temp[indexmin].num);
strcpy(temp[i].name,temp[indexmin].name);
strcpy(temp[i].phonenum,temp[indexmin].phonenum);
strcpy(temp[i].address,temp[indexmin].address);
strcpy(temp[indexmin].num,newinfo.num);
strcpy(temp[indexmin].name,newinfo.name);
strcpy(temp[indexmin].phonenum,newinfo.phonenum);
strcpy(temp[indexmin].address,newinfo.address);
}
Disp(temp,n); /*显示排序后的所有记录*/
saveflag=1;
printf("\n =====>sort complete!\n");
getchar();
return;
}
else
{Wrong();
getchar();
getchar();
return;}
}
/*数据存盘,若用户没有专门进行此操作且对数据有修改,在退出系统时, 会提示用户存盘*/
void Save(TELEBOOK temp[],int n)
{
FILE* fp;
int i=0;
fp=fopen("c:\\telephon","w");/*以只写方式打开文本文件*/
if(fp==NULL) /*打开文件失败*/
{
printf("\n=====>open file error!\n");
getchar();
return ;
}
for(i=0;i<n;i++)
{
if(fwrite(&temp[i],sizeof(TELEBOOK),1,fp)==1)/*每次写一条记录或一个结构数组元素至文件*/
{
continue;
}
else
{
break;
}
}
if(i>0)
{
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 telephone record is saved!\n");
getchar();
}
fclose(fp); /*关闭此文件*/
}
void main()
{
TELEBOOK tele[N]; /*定义TELEBOOK结构体*/
FILE *fp; /*文件指针*/
int select; /*保存选择结果变量*/
char ch; /*保存(y,Y,n,N)*/
int count=0; /*保存文件中的记录条数(或元素个数)*/
fp=fopen("C:\\telephon","a+");
/*以追加方式打开文本文件c:\telephon,可读可写,若此文件不存在,会创建此文件*/
if(fp==NULL)
{
printf("\n=====>can not open file!\n");
exit(0);
}
while(!feof(fp))
{
if(fread(&tele[count],sizeof(TELEBOOK),1,fp)==1) /*一次从文件中读取一条电话簿记录*/
count++;
}
fclose(fp); /*关闭文件*/
printf("\n==>open file sucess,the total records number is : %d.\n",count);
getchar();
menu();
while(1)
{
system("cls");
menu();
printf("\n Please Enter your choice(0~8):"); /*显示提示信息*/
scanf("%d",&select);
if(select==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(select)
{
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-9*/
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -