展开全部
发我帮你注释下
追问
怎么发给你呢QWQ
追答
私信联系。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2018-07-21
展开全部
#include <stdio.h>//通讯录
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define N 100
int M=5;
struct student
{ char num[10];char name[10];char tel[10];};
void myprint()
{
system("cls");
printf(" |-----------------------------------------------|\n");
printf(" | :-)请输入选项编号(0-7): |\n");
printf(" |-----------------------------------------------|\n");
printf(" |--------------1--创建通讯录--------------------|\n");
printf(" |--------------2--显示通讯录--------------------|\n");
printf(" |--------------3--查询通讯录--------------------|\n");
printf(" |--------------4--修改通讯录--------------------|\n");
printf(" |--------------5--添加通讯录--------------------|\n");
printf(" |--------------6--删除通讯录--------------------|\n");
printf(" |--------------7--排序通讯录--------------------|\n");
printf(" |--------------0--退出--------------------------|\n");
printf(" |-----------------------------------------------|\n");
}
void mycreat(struct student *p,int n)
{
struct student *q=p;
FILE *fp=NULL;
fp=fopen("d:\\jilu.dat","w");
if(fp==NULL){printf("error"); return;}
for(p=q;p-q<n;p++)
{
printf("\n");
printf("第%d条记录:\n",p-q+1);
printf("学号:");
do
{gets(p->num);}
while(strcmp(p->num,"")==0);
printf("姓名:");
gets(p->name);
printf("电话:");
gets(p->tel);
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
}
}
void mydisplay(struct student *p,int n)
{
FILE *fp=NULL;
fp=fopen("d:\\jilu.dat","r");
if(fp==NULL){printf("error"); return;}
system("cls");
printf(" 学号 姓名 电话\n");
while(n>0)
{
fscanf(fp,"%23s%15s%15s",p->num,p->name,p->tel);
printf("%23s%15s%15s\n",p->num,p->name,p->tel);
p++;n--;
}
}
void mysearch(struct student *p,int n)
{
struct student *q=p;
int flag=0;
char c='\0',nm[10]="",nam[10]="";
printf("\n按照学号查询(h)或姓名查询(m):\n");
c=getche();
if(c=='h'||c=='H')
{
printf("请输入要查询记录的学号:");gets(nm);
for(p=q;p-q<n;p++)
if(strcmp(nm,p->num)==0)
{
printf(" 学号 姓名 电话\n");
printf("%23s%15s%15s\n",p->num,p->name,p->tel);
flag=1;
}
if(flag==0) printf("wrong...\n");
}
else if(c=='m'||c=='M')
{
printf("请输入要查询记录的姓名:");gets(nam);
for(p=q;p-q<n;p++)
if(strcmp(nam,p->name)==0)
{
printf(" 学号 姓名 电话\n");
printf("%23s%15s%15s\n",p->num,p->name,p->tel);
flag=1;
}
if(flag==0) printf("wrong...\n");
}
else printf("wrong...\n");
}
void mymodify(struct student *p,int n)
{
struct student *q=p;
FILE *fp=NULL;
char nam[10]="";
mydisplay(p,n);
printf("请输入您想要修改记录的姓名:\n");gets(nam);
for(p=q;p-q<n;p++)
if(strcmp(nam,p->name)==0) break;
if(p-q==n)
printf("wrong...");
else
{
printf("请输入正确的学号:");
do
gets(p->num);
while(strcmp(p->num,"")==0);
printf("请输入正确的姓名:");
gets(p->name);
printf("请输入正确的电话:");
gets(p->tel);
}
fp=fopen("d:\\jilu.dat","w");
if(fp==NULL){printf("error"); exit(0);}
for(p=q;p-q<n;p++)
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
fclose(fp);
}
void myadd(struct student *p,int n)
{
FILE *fp=NULL;
struct student *q=p;
char c='\n',tempnum[10]="",tempname[10]="",temptel[10]="";
printf("\n");
while(strcmp(p->num,"")!=0)
{
printf("%23s%15s%15s\n",p->num,p->name,p->tel);
p++;
}
n=p-q;
do
{
printf("请输入新纪录的学号:");
do
gets(tempnum);
while(strcmp(tempnum,"")==0);
printf("请输入新纪录的姓名:");
gets(tempname);
printf("请输入新纪录的电话:");
gets(temptel);
for(p=q;p-q<n;p++)
if(strcmp(tempnum,p->num)==0||strcmp(temptel,p->tel)==0)
{
printf("学号或电话号码需要重新输入吗(Y/N)?");
c=getche();
putchar('\n');
break;
}
if(p-q==n)
{
strcpy(p->num,tempnum);
strcpy(p->name,tempname);
strcpy(p->tel,temptel);
break;
}
}while(c=='y'||c=='Y');
fp=fopen("d:\\jilu.dat","w");
if(fp==NULL){printf("error"); return;}
for(p=q;p-q<=n;p++)
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
fclose(fp);
M++;
}
void mydelete(struct student *p,int n)
{
FILE *fp=NULL;
struct student *q=p;
char c[10]="",tempnum[10]="",tempname[10]="",temptel[10]="",choose='\0';
mydisplay(p,n);
printf("选择删除记录的方式:学号(h)姓名(m)");
choose=getche();
printf("\n");
if(choose=='m'||choose=='M')
{
printf("删除的记录:");
gets(c);
for(;p-q<n;p++)
if(strcmp(c,p->name)==0) break;
if(p-q<n)
for(;p-q<n-1;p++)
{
strcpy(p->num,(p+1)->num);
strcpy(p->name,(p+1)->name);
strcpy(p->tel,(p+1)->tel);
}
else printf("no\n");
}
else if(choose=='h'||choose=='H')
{
printf("删除的记录:");
gets(c);
for(;p-q<n;p++)
if(strcmp(c,p->num)==0) break;
if(p-q<n)
for(;p-q<n-1;p++)
{
strcpy(p->num,(p+1)->num);
strcpy(p->name,(p+1)->name);
strcpy(p->tel,(p+1)->tel);
}
else printf("no");
}
else printf("wrong\n");
fp=fopen("d:\\jilu.dat","w");
if(fp==NULL){printf("error"); return;}
for(p=q;p-q<n-1;p++)
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
fclose(fp);
M--;
}
void mysort(struct student *p,int n)
{
FILE *fp=NULL;
struct student *q=p,*w=p,*a=p;
char temp[10]="",c1='\0',c2='\0';
mydisplay(p,n);
printf("选择按照学号(h)或姓名(m)排序:\n");
c1=getche();
if(c1=='h'||c1=='H')
{
printf("升序(s)或者降序(j)排列:");
c2=getche();
if(c2=='s'||c2=='S')
for(;p-a<n;p++)
{
w=p;
for(q=p+1;q-a<n;q++)
if(strcmp(w->num,q->num)>0) w=q;
strcpy(temp,w->num);
strcpy(w->num,p->num);
strcpy(p->num,temp);
strcpy(temp,w->name);
strcpy(w->name,p->name);
strcpy(p->name,temp);
strcpy(temp,w->tel);
strcpy(w->tel,p->tel);
strcpy(p->tel,temp);
}
else if(c2=='j'||c2=='J')
for(;p-a<n;p++)
{
w=p;
for(q=p+1;q-a<n;q++)
if(strcmp(w->num,q->num)<0) w=q;
strcpy(temp,w->num);
strcpy(w->num,p->num);
strcpy(p->num,temp);
strcpy(temp,w->name);
strcpy(w->name,p->name);
strcpy(p->name,temp);
strcpy(temp,w->tel);
strcpy(w->tel,p->tel);
strcpy(p->tel,temp);
}
else printf("555wrong...\n");
}
else if(c1=='m'||c1=='M')
{
printf("升序(s)或者降序(j)排列:");
c2=getche();
if(c2=='s'||c2=='S')
for(;p-a<n;p++)
{
w=p;
for(q=p+1;q-a<n;q++)
if(strcmp(w->name,q->name)>0) w=q;
strcpy(temp,w->num);
strcpy(w->num,p->num);
strcpy(p->num,temp);
strcpy(temp,w->name);
strcpy(w->name,p->name);
strcpy(p->name,temp);
strcpy(temp,w->tel);
strcpy(w->tel,p->tel);
strcpy(p->tel,temp);
}
else if(c2=='j'||c2=='J')
for(;p-a<n;p++)
{
w=p;
for(q=p+1;q-a<n;q++)
if(strcmp(w->name,q->name)<0) w=q;
strcpy(temp,w->num);
strcpy(w->num,p->num);
strcpy(p->num,temp);
strcpy(temp,w->name);
strcpy(w->name,p->name);
strcpy(p->name,temp);
strcpy(temp,w->tel);
strcpy(w->tel,p->tel);
strcpy(p->tel,temp);
}
else printf("555wrong...\n");
}
else printf("555wrong...\n");
fp=fopen("d:\\jilu.dat","w");
if(fp==NULL){printf("error"); return;}
for(p=a;p-a<=n;p++)
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
fclose(fp);
}
main()
{
char choose='\0',yes_no='\0';
struct student record[N]={0};
do
{
myprint();
printf(" :-)请选择(0-7):");
choose=getche();
switch(choose)
{
case '1':mycreat(record,M);break;
case '2':mydisplay(record,M);break;
case '3':mysearch(record,M);break;
case '4':mymodify(record,M);break;
case '5':myadd(record,M);break;
case '6':mydelete(record,M);break;
case '7':mysort(record,M);break;
case '0':exit(0);
default :printf("\nerror...\n");
}
printf("\n :-)继续选择吗(y/n)?\n");
do
{yes_no=getch();}
while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');
}
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define N 100
int M=5;
struct student
{ char num[10];char name[10];char tel[10];};
void myprint()
{
system("cls");
printf(" |-----------------------------------------------|\n");
printf(" | :-)请输入选项编号(0-7): |\n");
printf(" |-----------------------------------------------|\n");
printf(" |--------------1--创建通讯录--------------------|\n");
printf(" |--------------2--显示通讯录--------------------|\n");
printf(" |--------------3--查询通讯录--------------------|\n");
printf(" |--------------4--修改通讯录--------------------|\n");
printf(" |--------------5--添加通讯录--------------------|\n");
printf(" |--------------6--删除通讯录--------------------|\n");
printf(" |--------------7--排序通讯录--------------------|\n");
printf(" |--------------0--退出--------------------------|\n");
printf(" |-----------------------------------------------|\n");
}
void mycreat(struct student *p,int n)
{
struct student *q=p;
FILE *fp=NULL;
fp=fopen("d:\\jilu.dat","w");
if(fp==NULL){printf("error"); return;}
for(p=q;p-q<n;p++)
{
printf("\n");
printf("第%d条记录:\n",p-q+1);
printf("学号:");
do
{gets(p->num);}
while(strcmp(p->num,"")==0);
printf("姓名:");
gets(p->name);
printf("电话:");
gets(p->tel);
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
}
}
void mydisplay(struct student *p,int n)
{
FILE *fp=NULL;
fp=fopen("d:\\jilu.dat","r");
if(fp==NULL){printf("error"); return;}
system("cls");
printf(" 学号 姓名 电话\n");
while(n>0)
{
fscanf(fp,"%23s%15s%15s",p->num,p->name,p->tel);
printf("%23s%15s%15s\n",p->num,p->name,p->tel);
p++;n--;
}
}
void mysearch(struct student *p,int n)
{
struct student *q=p;
int flag=0;
char c='\0',nm[10]="",nam[10]="";
printf("\n按照学号查询(h)或姓名查询(m):\n");
c=getche();
if(c=='h'||c=='H')
{
printf("请输入要查询记录的学号:");gets(nm);
for(p=q;p-q<n;p++)
if(strcmp(nm,p->num)==0)
{
printf(" 学号 姓名 电话\n");
printf("%23s%15s%15s\n",p->num,p->name,p->tel);
flag=1;
}
if(flag==0) printf("wrong...\n");
}
else if(c=='m'||c=='M')
{
printf("请输入要查询记录的姓名:");gets(nam);
for(p=q;p-q<n;p++)
if(strcmp(nam,p->name)==0)
{
printf(" 学号 姓名 电话\n");
printf("%23s%15s%15s\n",p->num,p->name,p->tel);
flag=1;
}
if(flag==0) printf("wrong...\n");
}
else printf("wrong...\n");
}
void mymodify(struct student *p,int n)
{
struct student *q=p;
FILE *fp=NULL;
char nam[10]="";
mydisplay(p,n);
printf("请输入您想要修改记录的姓名:\n");gets(nam);
for(p=q;p-q<n;p++)
if(strcmp(nam,p->name)==0) break;
if(p-q==n)
printf("wrong...");
else
{
printf("请输入正确的学号:");
do
gets(p->num);
while(strcmp(p->num,"")==0);
printf("请输入正确的姓名:");
gets(p->name);
printf("请输入正确的电话:");
gets(p->tel);
}
fp=fopen("d:\\jilu.dat","w");
if(fp==NULL){printf("error"); exit(0);}
for(p=q;p-q<n;p++)
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
fclose(fp);
}
void myadd(struct student *p,int n)
{
FILE *fp=NULL;
struct student *q=p;
char c='\n',tempnum[10]="",tempname[10]="",temptel[10]="";
printf("\n");
while(strcmp(p->num,"")!=0)
{
printf("%23s%15s%15s\n",p->num,p->name,p->tel);
p++;
}
n=p-q;
do
{
printf("请输入新纪录的学号:");
do
gets(tempnum);
while(strcmp(tempnum,"")==0);
printf("请输入新纪录的姓名:");
gets(tempname);
printf("请输入新纪录的电话:");
gets(temptel);
for(p=q;p-q<n;p++)
if(strcmp(tempnum,p->num)==0||strcmp(temptel,p->tel)==0)
{
printf("学号或电话号码需要重新输入吗(Y/N)?");
c=getche();
putchar('\n');
break;
}
if(p-q==n)
{
strcpy(p->num,tempnum);
strcpy(p->name,tempname);
strcpy(p->tel,temptel);
break;
}
}while(c=='y'||c=='Y');
fp=fopen("d:\\jilu.dat","w");
if(fp==NULL){printf("error"); return;}
for(p=q;p-q<=n;p++)
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
fclose(fp);
M++;
}
void mydelete(struct student *p,int n)
{
FILE *fp=NULL;
struct student *q=p;
char c[10]="",tempnum[10]="",tempname[10]="",temptel[10]="",choose='\0';
mydisplay(p,n);
printf("选择删除记录的方式:学号(h)姓名(m)");
choose=getche();
printf("\n");
if(choose=='m'||choose=='M')
{
printf("删除的记录:");
gets(c);
for(;p-q<n;p++)
if(strcmp(c,p->name)==0) break;
if(p-q<n)
for(;p-q<n-1;p++)
{
strcpy(p->num,(p+1)->num);
strcpy(p->name,(p+1)->name);
strcpy(p->tel,(p+1)->tel);
}
else printf("no\n");
}
else if(choose=='h'||choose=='H')
{
printf("删除的记录:");
gets(c);
for(;p-q<n;p++)
if(strcmp(c,p->num)==0) break;
if(p-q<n)
for(;p-q<n-1;p++)
{
strcpy(p->num,(p+1)->num);
strcpy(p->name,(p+1)->name);
strcpy(p->tel,(p+1)->tel);
}
else printf("no");
}
else printf("wrong\n");
fp=fopen("d:\\jilu.dat","w");
if(fp==NULL){printf("error"); return;}
for(p=q;p-q<n-1;p++)
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
fclose(fp);
M--;
}
void mysort(struct student *p,int n)
{
FILE *fp=NULL;
struct student *q=p,*w=p,*a=p;
char temp[10]="",c1='\0',c2='\0';
mydisplay(p,n);
printf("选择按照学号(h)或姓名(m)排序:\n");
c1=getche();
if(c1=='h'||c1=='H')
{
printf("升序(s)或者降序(j)排列:");
c2=getche();
if(c2=='s'||c2=='S')
for(;p-a<n;p++)
{
w=p;
for(q=p+1;q-a<n;q++)
if(strcmp(w->num,q->num)>0) w=q;
strcpy(temp,w->num);
strcpy(w->num,p->num);
strcpy(p->num,temp);
strcpy(temp,w->name);
strcpy(w->name,p->name);
strcpy(p->name,temp);
strcpy(temp,w->tel);
strcpy(w->tel,p->tel);
strcpy(p->tel,temp);
}
else if(c2=='j'||c2=='J')
for(;p-a<n;p++)
{
w=p;
for(q=p+1;q-a<n;q++)
if(strcmp(w->num,q->num)<0) w=q;
strcpy(temp,w->num);
strcpy(w->num,p->num);
strcpy(p->num,temp);
strcpy(temp,w->name);
strcpy(w->name,p->name);
strcpy(p->name,temp);
strcpy(temp,w->tel);
strcpy(w->tel,p->tel);
strcpy(p->tel,temp);
}
else printf("555wrong...\n");
}
else if(c1=='m'||c1=='M')
{
printf("升序(s)或者降序(j)排列:");
c2=getche();
if(c2=='s'||c2=='S')
for(;p-a<n;p++)
{
w=p;
for(q=p+1;q-a<n;q++)
if(strcmp(w->name,q->name)>0) w=q;
strcpy(temp,w->num);
strcpy(w->num,p->num);
strcpy(p->num,temp);
strcpy(temp,w->name);
strcpy(w->name,p->name);
strcpy(p->name,temp);
strcpy(temp,w->tel);
strcpy(w->tel,p->tel);
strcpy(p->tel,temp);
}
else if(c2=='j'||c2=='J')
for(;p-a<n;p++)
{
w=p;
for(q=p+1;q-a<n;q++)
if(strcmp(w->name,q->name)<0) w=q;
strcpy(temp,w->num);
strcpy(w->num,p->num);
strcpy(p->num,temp);
strcpy(temp,w->name);
strcpy(w->name,p->name);
strcpy(p->name,temp);
strcpy(temp,w->tel);
strcpy(w->tel,p->tel);
strcpy(p->tel,temp);
}
else printf("555wrong...\n");
}
else printf("555wrong...\n");
fp=fopen("d:\\jilu.dat","w");
if(fp==NULL){printf("error"); return;}
for(p=a;p-a<=n;p++)
fprintf(fp,"%23s%15s%15s\n",p->num,p->name,p->tel);
fclose(fp);
}
main()
{
char choose='\0',yes_no='\0';
struct student record[N]={0};
do
{
myprint();
printf(" :-)请选择(0-7):");
choose=getche();
switch(choose)
{
case '1':mycreat(record,M);break;
case '2':mydisplay(record,M);break;
case '3':mysearch(record,M);break;
case '4':mymodify(record,M);break;
case '5':myadd(record,M);break;
case '6':mydelete(record,M);break;
case '7':mysort(record,M);break;
case '0':exit(0);
default :printf("\nerror...\n");
}
printf("\n :-)继续选择吗(y/n)?\n");
do
{yes_no=getch();}
while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询