⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 c语言课程设计之通讯录管理
💻 C
字号:
/* C语言程序设计课程设计实验报告 */
/*题目:学生通讯录管理系统 */
/*黄真超制作*/

#include"head.h"
#include"int.h"



/*追加新记录*/
void add(ADDR *head)
{
 char str[10];
 clrscr();
 if(n==0)                            /*若内存中无记录*/
 {printf("\nPlease open a file first!\n");     /*输出提示信息*/
 return;                            /*返回主菜单*/
 }
 enter(head);                      /*调用enter函数输入记录*/
 if(flag==1)                       /*标志新记录没保存*/
 {printf("Are you want to save the add records?(y/n)\n");  /*提示是否保存*/
 scanf("%s",str);
 if(strcmp(str,"y")==0||strcmp(str,"Y")==0)save(head);  /*调用save函数保存信息*/
 }
}
void save(ADDR *head)      /*形参为头指针*/
{ADDR *p;                /*定义指针变量指向*/
 FILE *fp;
 char file_name[20];
 clrscr();                          /*清屏*/
 if(head==NULL)              /*表头为空*/
 {printf("\nNo records to be saved!\n");
  return;                      /*返回主菜单*/        
  }
   clrscr();
  printf("Please input the file_name(or enter #)!\n");
   scanf("%s",file_name);
  if((fp=fopen(file_name,"wb"))==NULL) /*打开一个二进制文件,如没有就新建一个文件*/               
  {printf("Can't open the file\n");                   
   return;
   }
   printf("\n------saving file-----\n");                      /*提示在保存*/
   p=head;                                   /*移动指针从头指针开始*/

   while(p!=NULL)
   {fwrite(p,sizeof(ADDR),1,fp);               /*写入一条记录*/
    p=p->next;                               /*指针后移*/ 
    }
    fclose(fp);                           /*关闭文件*/
    printf("\n-----save success!-----\n");    /*提示保存成功*/
}
ADDR *load(void)
{
 FILE *fp;                              /*指向文件的指针*/
 ADDR *p1,*p2,*head;                   /*定一个变量*/
 char fname[20];clrscr();
 n=0;                                 /*n是外部变量,清空内存中的记录*/
 printf("Please input a filename(or enter #).\n");
 scanf("%s",fname);                     /*输入文件的名字*/
 if((fp=fopen(fname,"rb"))==NULL)        /*若文件不存在*/
 {printf("Can't open file\n");
  return NULL;                        /*返回主菜单*/        
  }
  printf("\n------loading file!------\n");       /*提示信息*/
  p1=(ADDR *)malloc(sizeof(ADDR));     /*开辟一段内存*/
  if(!p1)
  {printf("Out of memory!\n");           /*出错处理*/
   return (head);
   }
   head=p1;p2=head;                 /*p1,p2指向表头*/
   while(!feof(fp))
   {n++;                           /*每读入一个记录,n加1*/
if(1!=fread(p1,sizeof(ADDR),1,fp))break;
    if(n%5==0)                     /*换页输出*/
    {printf("\Press any to continue...");
     getch();clrscr();
    }
    printf("\nThe No.%d record are:\n",n);   /*输出信息*/
    printf("name\t\ttel\tsex\tage\tbir_day\t\troom\n");  printf("%s\t\t%s\t%s\t%d\t%s\t\t%s\n",p1->name,p1->tel,p1->sex,p1->age,p1->bir_day,p1->room);
    p1->next=(ADDR *)malloc(sizeof(ADDR));  /*另开辟一段内存写下一个记录*/
    if(!p1->next)
    {printf("out of memory!\n");         /*出错处理*/
    return head;
    }
    p2=p1;                         
    p1=p1->next;                  /*指针后移*/
    }
    p2->next=NULL;             /*到文件末尾*/
    fclose(fp);                   /*关闭文件*/
    n--;
    printf("\n-----You have success read the file!-----\n");
   printf("\nPress any eny to continue...\n");
   getch();
   clrscr();
   return (head);
}
/*查询功能的模块*/

void search(ADDR *head)                    /*head为当前链表表头,无返回值*/               
{int c;                                    /*c是选择查询方式的变量*/ 
 char input[20];                            /*input是查询信息的变量*/        
ADDR *p1,*p2;                         /*p1、p2是用于查找信息的指向结点的指针*/
 if(n==0)
 {clrscr();
printf("The list is empty,exit to the menu!");            /*若无记录*/
  return;
 }
 clrscr();
 printf("You can search by these ways:\n");               /*输出查询的选择方法*/
 printf("(1).By name\n");
 printf("(2).By tel\n");
 printf("(3).By room\n");
 scanf("%d",&c);                                 /*输入选择*/
 while(1)
 {
  if(c!=1&&c!=2&&c!=3)
  {printf("Input error,select(1--3)again:");             /*出错处理*/
  scanf("%d",&c);}
   else break;
  }
  switch(c)
  {case 1:printf("Please input a name:");break;          /*输出提示信息*/
   case 2:printf("Please input a tel_num:");break;        
   case 3:printf("Please input a room_num:");break;   
  }scanf("%s",input);
  p1=p2=head;                            /*p1、p2都指向当前头指针*/
  clrscr();
  while(p1!=NULL) {if((c==1&&strcmp(input,p1->name)==0)||(c==2&&strcmp(input,p1->tel)==0)||(c==3&&strcmp(input,p1->room)==0))  /*表头不为空时对照输入的内容与内存中的内容是否匹配*/
   {printf("The information you search is:\n");   /*输出查询的信息*/
    printf("name\t\ttel\tsex\tage\tbir_day\t\troom\n");   printf("%s\t\t%s\t%s\t%d\t%s\t\t%s\n",p1->name,p1->tel,p1->sex,p1->age,p1->bir_day,p1->room);
    printf("\nPress any eny to continue...\n");      /*任意键继续*/
    getch();
    return;
    }
  else{p2=p1;p1=p1->next;}          /*若找不到,后移指针*/
  }clrscr();
  printf("Can not find the record.\n");  /*直到表尾,输出找不到的信息*/
  return;
}

/*连接文件*/
void link_files(void)
{FILE *fp1,*fp2;
 ADDR *p;int n=0;
 char filename1[20],filename2[20];
 printf("please input a filename to read:\n");
 scanf("%s",filename1);
 if((fp1=fopen(filename1,"ab"))==NULL)
 {printf("Can not open the file!Please input again.\n");
  return;
 }
 printf("Please input the other filename to link:\n");
 scanf("%s",filename2);
 if((fp2=fopen(filename2,"rb"))==NULL)
  {printf("Can not open the file!\n");
   return;
  }
  p=(ADDR *)malloc(sizeof(ADDR));
  while(1)
  {if(1!=fread(p,sizeof(ADDR),1,fp2))break;
   if(feof(fp2)!=0)break;
   fwrite(p,sizeof(ADDR),1,fp1);
   n++;
  }
  fclose(fp1);
  fclose(fp2);
  printf("\nAdd %d records\n",n);
}

/*输入记录*/
ADDR *enter(ADDR *head)
{
 ADDR *old=NULL,*new;
 new=(ADDR *)malloc(sizeof(ADDR));      /*申请一段内存*/

 if(head!=NULL)                      /*表头不为空*/
 {old=head;
  while(old->next!=NULL)
  {old=old->next;
  }
 }
 else old=head=new;
 printf("Input the record,end with \"0\"\n");  
 while(1)
 {
  printf("name:");scanf("%s",&new->name);
  if(strcmp(new->name,"0")==0)break;
  printf("tel:");scanf("%s",&new->tel);
  printf("sex:");scanf("%s",&new->sex);
  printf("age:");scanf("%d",&new->age);
  printf("bir_day:");scanf("%s",&new->bir_day);
  printf("room:");scanf("%s",&new->room);
  n++;
  if(head!=new)
  {old->next=new;old=new;}
  new=(ADDR *)malloc(sizeof(ADDR));
 }
 flag=1;
 if(head==new)
 {head=NULL;flag=0;}
 else old->next=NULL;
 return(head);
}

 /*显示内存中的记录*/
void display(ADDR *head)
{int k;
 ADDR *p;                      /*借助p输出记录*/
 clrscr();                           /*清屏*/
 if(head==NULL)                 /*若无记录*/
 {printf("\nNow no records!\n");       /*输出提示信息*/
 return;
 }
 else
 {
  p=head;                      /*p指向头指针,开始输出信息*/
   printf("\nNow these %d records are:\n",n);
   printf("\tname\t\ttel\tsex\tage\tbir_day\t\troom\n");
   for(k=1;k<=n,p!=NULL;k++)    {{printf("No.%d:\t%s\t\t%s\t%s\t%d\t%s\t\t%s\n",k,p->name,p->tel,p->sex,p->age,p->bir_day,p->room);          /*借for循环输出记录*/
    p=p->next;}          /*p后移*/
    if(k%10==0){printf("Press any key to  continue...\n");getch();clrscr();}
    }                /*换页输出*/
 }
 printf("Press any key to continue...\n");getch();
}

 /*删除记录*/
ADDR *del(ADDR *head)   
{char input[20];                                  /*定义各个变量*/
 int c;
 ADDR *old,*new; 
 clrscr();                                       /*清屏*/
 if(n==0)                                       /*若内存无记录*/
 {printf("\n List is empty!\n");return head;           /*提示信息,返回主菜单*/
 }
 printf("You can delete a record by these ways:\n");        /*提示信息*/

 printf("(1).By name\n");
 printf("(2).By tel\n");
 scanf("%d",&c);                                 /*输入选择*/
 while(1)
 {
  if(c!=1&&c!=2)
   {printf("Input error,choose again(1-2):");           /*出错处理*/
    scanf("%d",&c);
   }
  else break;
 }
 switch(c)
 {case 1:printf("Please input a name:");break;
  case 2:printf("Please input a tel:");
 }
 scanf("%s",input);                               /*输入要删除的记录的名字或电话*/
 old=new=head;                         /*指针变量指向表头,开始查找记录*/
 while(new!=NULL)
 {if((c==1&&strcmp(input,new->name)==0)||(c==2&&strcmp(input,new->tel)==0))
 { if(new==head)head=new->next;
   else
  { printf("The record will be delete is below:\n");     /*输出被删除的信息*/
    printf("name\t\ttel\tsex\tage\tbir_day\t\troom\n");
    printf("%s\t\t%s\t%s\t%d\t%s\t\t%s\n",new->name,new->tel,new->sex,new->age,new->bir_day,new->room);
    old->next=new->next;
    free(new);         
    printf("\nDelete success.\n");                      /*提示信息*/
    --n;                                         /*记录个数减少1个*/
    return head;                                  /*返回头指针*/
   }
  }
  else{old=new;new=new->next;}                      /*找不到,指针后移*/
  }printf("\nCan't find the record.\n");                  /*提示信息*/
   return(head);                                  /*返回头指针*/
}

void quit(ADDR *head)                 
{char str;
 getchar();
 if(flag==1)                          /*没保存*/
 {printf("The addrbook has changed,save it?(Y/N)\n");
  scanf("%c",&str);
  if(str=='Y'||str=='y')save(head);
  }
  printf("Exit!BYe-bye!!\n");
  printf("Press any key to continue.....");
  free_nodes(head);
  exit(0);
}
ADDR *new_addrbook(ADDR *head)
{
 char str[10];
  if(flag==1)
 {printf("The addrbook is to be saved??(Y/N)\n");
  scanf("%s",str);
  if(strcmp(str,"y")==0||strcmp(str,"Y")==0)
  save(head);
  }
 free_nodes(head);
 head=NULL;
  head=enter(head);

  if(flag==1)
 {printf("The addrbook is to be saved??(Y/N)\n");
  scanf("%s",str);
  if(strcmp(str,"y")==0||strcmp(str,"Y")==0)
  save(head);
  }
  return head;
}
void main()                       /*主函数*/
{
 ADDR *head=0;
 textbackground(4);              /*背景颜色*/
 clrscr();
 printf("\n");
 printf("******************************************\n");
 printf("******************WELCOME*****************\n");
 printf("******************************************\n");
 printf("\n");
 n=0;                                    /*做空表*/
 handle_menu(head);
}

void handle_menu(ADDR *head)           /*菜单处理函数,对通讯录的操作类型*/
{
 for(;;)                                /*无限循环*/
 {switch(menu_select())                 /*调用菜单选择函数*/
  {
   case 1:search(head);break;
   case 2:display(head);break;
   case 3:add(head);break;
   case 4:head=del(head);break;
   case 5:save(head);break;
   case 6:head=load();break;
   case 7:head=new_addrbook(head);break;
   case 8:link_files();break;
   case 9:quit(head);break;
  }
 }
}



int menu_select()                         /*菜单选择函数*/
{
 int c;
 printf("------------------------------------------\n");
 printf("|(1).Search a student.                   |\n");
 printf("|(2).Display the list of the students.   |\n");
 printf("|(3).Add the records.                    |\n");
 printf("|(4).Delete a student.                   |\n");
 printf("|(5).Save the record of the student.     |\n");
 printf("|(6).Open the record of the student.     |\n");
 printf("|(7).Make a new addrbook.                |\n");
 printf("|(8).Link the files.                     |\n");
 printf("|(9).Exit.                               |\n");
 printf("------------------------------------------");
 printf("\n Please select(1--9)");
 for(;;)
 {scanf("%d",&c);
  if(c<1||c>9)printf("\nInput error!Please select(1--9)again:");
  else break;
 }return(c);
}

void free_nodes(ADDR *head)                /*释放内存函数*/
{ADDR *p=head;
 ADDR *old=p;
 while(p)
 {old=p;
  p=p->next;
  free(old);
  }
  n=0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -