📄 noname.c
字号:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define LEN sizeof(NODE)
typedef struct node
{char name[16];
char phone[18];
char sex[6];
char age[6];
char birthday[12];
char dormitory[20];
struct node *next;
}NODE;
extern int n;
NODE *creat(NODE *);
NODE *add(NODE *);
NODE *del_by_name(NODE *);
NODE *del_by_phone(NODE *);
void search_by_name(NODE *);
void search_by_phone(NODE *);
void search_by_dormitory(NODE *);
void show(NODE *);
void save(NODE *);
NODE *readfile();
void link_files();
void free_nodes(NODE *);
void quit(NODE *);
void display_menu();
void choice_item();
void display_del();
void display_search();
NODE *add(NODE *head)
{
NODE *p0,*p1;
p1=head;
p0=(NODE *)malloc(LEN);
clrscr();
printf("\n\n\n\tplease input the name:\n\t");
gets(p0->name);
printf("\tplease input the phone number:\n\t");
gets(p0->phone);
printf("\tplease input the sex:\n\t");
gets(p0->sex);
printf("\tplease input the age:\n\t");
gets(p0->age);
printf("\tplease input the birthday:\n\t");
gets(p0->birthday);
printf("\tplease input the dormitory:\n\t");
gets(p0->dormitory);
if(head==NULL)
{head=p0;p0->next=NULL;n=1;}
else
{while(p1->next!=NULL)
p1=p1->next;
}
p1->next=p0;
p0->next=NULL;
n++;
return head;
}
void choice_item()
{
NODE*head ;
head=NULL ;
while(1)
{
char c ;
char s[2];
int cn=0 ;
while(1)
{
display_menu();
printf("\t");
gets(s);
cn=atoi(s);
if(cn<1||cn>9){printf("\n\t input error!please input again:\n");getch();}
else break ;
}
switch(cn)
{
case 1 :
head=creat(head);
printf("\n\n\n\tpress any key to continute...\n\t");
getch();
break ;
case 2 :
head=add(head);
printf("\n\n\n\tpress any key to continute...\n\t");
getch();
break ;
case 3 :
{
display_del();
while(1)
{
printf("\t");
c=getch();
if(c=='a'||c=='b')break ;
else
printf("\n\t input error!please choose again\n");
}
switch(c)
{
case 'a' :
head=del_by_name(head);break;
case 'b' :
head=del_by_phone(head);
}
}
printf("\n\n\n\tpress any key to continute...\n\t");
getch();
break ;
case 4 :
{
display_search();
while(1)
{
printf("\t");
c=getch();
if(c=='a'||c=='b'||c=='c')break ;
else
printf("\n\t input error!please choose again\n");
}
switch(c)
{
case 'a' :
search_by_name(head); break;
case 'b' :
search_by_phone(head);break;
case 'c' :
search_by_dormitory(head);
}
}
printf("\n\n\n\tpress any key to continute...\n\t");
getch();
break ;
case 5 :
show(head);
printf("\n\n\n\tpress any key to continute...\n\t");
getch();
break ;
case 6 :
save(head);
printf("\n\n\n\tpress any key to continute...\n\t");
getch();
break ;
case 7 :
head=readfile();
printf("\n\n\n\tpress any key to continute...\n\t");
getch();
break ;
case 8 :
link_files();
printf("\n\n\n\tpress any key to continute...\n\t");
getch();
break ;
case 9 :
quit(head);
getch();
}
}
}
int n;
NODE *creat(NODE *head)
{
NODE *p1,*p2;
char c;
clrscr();
printf("\n\n\n\tWarning!Creat a new adressbook will loose the old adress book.\n");
printf("\n\t continute?(y/n)\n\t");
c=getch();
if(c=='n'||c=='N')return head;
free_nodes(head);
printf("\n\tpress any key to continute...\n\t");
getch();
clrscr();
n=0;
p2=(NODE *)malloc(LEN);
p1=p2;
printf("\n\n\n\tPlease input the name:\n\t");
gets(p1->name);
printf("\tPlease input the phone number:\n\t");
gets(p1->phone);
printf("\tPlease input the sex:\n\t");
gets(p1->sex);
printf("\tPlease input the age:\n\t");
gets(p1->age);
printf("\tPlease input the birthday:\n\t");
gets(p1->birthday);
printf("\tPlease input the dormitory:\n\t");
gets(p1->dormitory);
head=NULL;
while(1)
{
n+=1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
printf("\n\t continute?(y/n)\n\t");
c=getch();
if(c=='n'||c=='N')break;
p1=(NODE *)malloc(LEN);
printf("\n\t now,please input the %dth adress.\n",n+1);
printf("\tplease input the name:\n\t");
gets(p1->name);
printf("\tplease input the phone number:\n\t");
gets(p1->phone);
printf("\tplease input the sex:\n\t");
gets(p1->sex);
printf("\tplease input the age:\n\t");
gets(p1->age);
printf("\tplease input the birthday:\n\t");
gets(p1->birthday);
printf("\tplease input the dormitory:\n\t");
gets(p1->dormitory);
}
p2->next=NULL;
return head;
}
NODE *del_by_name(NODE *head)
{
NODE *p0,*p1,*p2;
char name[16];
if(head==NULL){printf("\n\tlist null!\n");
goto end;}
p0=p1=head;
printf("please input the name and the adress will be del!!!\n\t");
gets(name);
while(strcmp(name,p1->name)!=0&&p1->next!=NULL)
{p2=p1;
p1=p1->next;
}
if(strcmp(name,p1->name)==0)
{if(p1==head)head=p1->next;
else{p0=p1;p2->next=p1->next;}
printf("\nThe del address is:\n");
printf("Name Phone Sex Age Birthday Dormitory\n");
printf("%-18s%-19s%-9c%-9d%-14s%s\n",p0->name,p0->phone,p0->sex,
p0->age,p0->birthday,p0->dormitory);
free(p0);
n-=1;
}
else printf("%s not been found!\n",name);
end:
return head;
}
NODE *del_by_phone(NODE *head)
{
NODE *p0,*p1,*p2;
char phone[18];
if(head==NULL){printf("\n\tlist null!\n");
goto end2;}
p0=p1=head;
printf("please input the phone and the adress will be del!!!\n\t");
gets(phone);
while(strcmp(phone,p1->phone)!=0&&p1->next!=NULL)
{p2=p1;
p1=p1->next;
}
if(strcmp(phone,p1->phone)==0)
{if(p1==head)head=p1->next;
else{p0=p1;p2->next=p1->next;}
printf("\nThe del address is:\n");
printf("Name Phone Sex Age Birthday Dormitory\n");
printf("%-18s%-19s%-9c%-9d%-14s%s\n",p0->name,p0->phone,p0->sex,
p0->age,p0->birthday,p0->dormitory);
free(p0);
n-=1;
}
else printf("%s not been found!\n",phone);
end2:
return head;
}
void display_menu(void)
{
clrscr();
printf("\n\n\n");
printf("\t\t\t\tn=%d\n",n);
printf("\t********************************************\n");
printf("\t1......creat a new address book...\n\n");
printf("\t2......add new records...\n\n");
printf("\t3......del records...\n\n");
printf("\t4......search records...\n\n");
printf("\t5......show the address book's imformation...\n\n");
printf("\t6......save to file...\n\n");
printf("\t7......read file...\n\n");
printf("\t8......line files...\n\n");
printf("\t9......quit\n\n");
printf("\t********************************************\n");
printf("\n\n\tplease choice item. (you may choice from 1 to 9)\n");
}
void display_del()
{
clrscr();
printf("\n\n\n\n\n\n");
printf("\t***********************\n");
printf("\ta......del by name...\n\n\n");
printf("\tb......del by phone...\n\n\n");
printf("\t***********************\n");
}
void display_search()
{
clrscr();
printf("\n\n\n\n\n\n");
printf("\t******************************\n");
printf("\ta......search by name...\n\n\n");
printf("\tb......search by phone...\n\n\n");
printf("\tc......search by dormitory...\n\n\n");
printf("\t******************************\n");
}
void free_nodes(NODE *head)
{
NODE *p,*u;
p=head;
if(head!=NULL)
{ do{u=p->next;
free(p);
p=u;
n--;}
while(p);
}
printf("\n\tnow there are %d records in the memory.\n\n\n\t",n);
getch();
}
void link_files(void)
{
FILE *fp1,*fp2;
NODE *p;
char filename1[20],filename2[20];
int num=0;
clrscr();
printf("\n\n\nPlease input the file name that you want to load:\n");
printf("(press enter and use 'file1')\n");
gets(filename1);
if(strlen(filename1)==0)strcpy(filename1,"file1");
if((fp1=fopen(filename1,"ab"))==NULL)
{printf("can not open the file!\nplease choose again\n");
return;
}
printf("Please input the other file that you want to link:\n");
gets(filename2);
if((fp2=fopen(filename2,"rb"))==NULL)
{printf("can not open the file!\nplease choose again\n");
return;
}
p=(NODE *)malloc(LEN);
while(1)
{
if(1!=fread(p,LEN,1,fp2))break;
if(feof(fp2)!=0)break;
fwrite(p,LEN,1,fp1);
num++;
}
fclose(fp1);
fclose(fp2);
printf("\t add %d records\n",num);
}
void quit(NODE *head)
{
char c;
printf("\tWarning: all records in the memory will loss\n");
printf("\tquit?(y/n)\n\t");
c=getch();
if(c=='N'||c=='n')
{printf("\n\n\n\tpress any key to continute...\n\t");
getch();
return;
}
printf("\n\tquit now,byebye!\n\t");
getch();
free_nodes(head);
exit(0);
}
NODE *readfile(void)
{
FILE *fp;
NODE *p,*q,*head;
char filename[20];
char c;
printf("\tWarning!Read file will loose the old adress book.\n");
printf("\tcontinute?(y/n)\n\t");
c=getch();
if(c=='n'||c=='N')return head;
clrscr();
printf("\n\n\n\tPlease input the file name that you want to read:\n");
printf("\t(press enter and use 'file1')\n\t");
gets(filename);
if(strlen(filename)==0)strcpy(filename,"file1");
if((fp=fopen(filename,"rb"))==NULL)
{printf("\tcan not open the file!\nplease choose again\n");
return head;
}
free_nodes(head);
printf("\n\t read file...\n");
p=(NODE *)malloc(LEN);
q=head=p;
while(!feof(fp))
{
if(1!=fread(p,LEN,1,fp))break;
n++;
p->next=(NODE *)malloc(LEN);
q=p;
p=p->next;
}
q->next=NULL;
fclose(fp);
printf("\tThere are %d records which have been read.\n\t",n);
return head;
}
void save(NODE *head)
{
NODE *p;
FILE *fp;
int m=0;
char filename[20];
clrscr();
if(head==NULL)
{ printf("\n\n\n\tno records to save!\n");
return;
}
printf("\n\n\nPlease input the file name that you want to save:\n");
printf("(press enter and use 'file1')\n");
gets(filename);
if(strlen(filename)==0)strcpy(filename,"file1");
if((fp=fopen(filename,"wb"))==NULL)
{printf("can not save file!\nplease choose again\n");
return;
}
printf("\n save file...\n");
p=head;
while(p)
{
fwrite(p,LEN,1,fp);
p=p->next;
m++;
}
fclose(fp);
printf("There are %d records which have been saved\n",m);
printf("Press any key to continute...\n");
getch();
}
void search_by_name(NODE *head)
{
NODE *p;
int flag=1;
char name[16];
if(head==NULL){printf("\n list null!\n");goto end;}
p=head;
printf("please input the name that you want to search\n\t");
gets(name);
clrscr();
while(p!=NULL)
{ if(strcmp(name,p->name)==0)
{
printf("\n\nThe address is:\n");
printf("Name Phone Sex Age Birthday Dormitory\n");
printf("%-18s%-19s%-9s%-9s%-14s%s\n",p->name,p->phone,p->sex,
p->age,p->birthday,p->dormitory);
flag=0;
}
p=p->next;
}
if(flag)printf("\n %s not been found!\n",name);
end:;
}
void search_by_phone(NODE *head)
{
NODE *p;
int flag=1;
char phone[18];
if(head==NULL){printf("\n list null!\n");goto end;}
p=head;
printf("please input the phone that you want to search\n\t");
gets(phone);
clrscr();
while(p!=NULL)
{ if(strcmp(phone,p->phone)==0)
{
printf("\n\nThe address is:\n");
printf("Name Phone Sex Age Birthday Dormitory\n");
printf("%-18s%-19s%-9s%-9s%-14s%s\n",p->name,p->phone,p->sex,
p->age,p->birthday,p->dormitory);
flag=0;
}
p=p->next;
}
if(flag)printf("\n %s not been found!\n",phone);
end: ;
}
void search_by_dormitory(NODE *head)
{
NODE *p;
int flag=1;
char dormitory[20];
if(head==NULL){printf("\n list null!\n");goto end;}
p=head;
printf("please input the dormitory that you want to search\n\t");
gets(dormitory);
clrscr();
while(p!=NULL)
{ if(strcmp(dormitory,p->dormitory)==0)
{
printf("\n\nThe address is:\n");
printf("Name Phone Sex Age Birthday Dormitory\n");
printf("%-18s%-19s%-9s%-9s%-14s%s\n",p->name,p->phone,p->sex,
p->age,p->birthday,p->dormitory);
flag=0;
}
p=p->next;
}
if(flag)printf("\n %s not been found!\n",dormitory);
end: ;
}
void show(NODE *head)
{
NODE *p0;
p0=head;
clrscr();
if(head==NULL){printf("\n list null!\n");return;}
printf("\n\n\nThe address is:\n");
printf("Name Phone Sex Age Birthday Dormitory\n");
for(;p0!=NULL;p0=p0->next)
{
printf("%-18s%-19s%-9s%-9s%-14s%s\n",p0->name,p0->phone,p0->sex,
p0->age,p0->birthday,p0->dormitory);
}
}
void main()
{
clrscr();
choice_item();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -