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

📄 mpbjq.c

📁 用双向链表
💻 C
📖 第 1 页 / 共 2 页
字号:
void data_showend()/*从最后一条显示记录函数*/
{
 int ch1;
 p3=end;
 View_Clear(&view_in);
 if(p3==NULL) 
 {
  View_PutString(&view_in,0,0,"There is no record.",NORMAL);
  View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
  getch();
 }
  while(p3!=NULL)
  {
   View_Clear(&view_out);
   View_PutString(&view_out,0,0,"No:",NORMAL);
   gotoxy(0,4);printf("%d",p3->number);
   View_PutString(&view_out,0,2,"Name:",NORMAL);
   View_PutString(&view_out,5,2,p3->name,NORMAL);
   View_PutString(&view_out,0,3,"Sex:",NORMAL);
   View_PutString(&view_out,4,3,p3->sex,NORMAL);
   View_PutString(&view_out,0,4,"Birthday(y/m/d):",NORMAL);
   View_PutString(&view_out,16,4,p3->birth.year,NORMAL);
   View_PutString(&view_out,21,4,"/",NORMAL);
   View_PutString(&view_out,22,4,p3->birth.month,NORMAL);
   View_PutString(&view_out,25,4,"/",NORMAL);
   View_PutString(&view_out,26,4,p3->birth.day,NORMAL);
   View_PutString(&view_out,0,5,"Postnumber:",NORMAL);
   View_PutString(&view_out,11,5,p3->addr.post_num,NORMAL);
   View_PutString(&view_out,0,6,"Address:",NORMAL);
   View_PutString(&view_out,8,6,p3->addr.addr,NORMAL);
   View_PutString(&view_out,0,7,"Tele:",NORMAL);
   View_PutString(&view_out,5,7,p3->telephone,NORMAL);
   View_PutString(&view_in,0,0,"Press (Page UP/Page Down) to continue...",NORMAL);
   View_PutString(&view_in,0,1,"Press (Esc) to return.",NORMAL);
   loop:
   ch1=bioskey(0);
     switch(ch1)
     {
      case 20736:/*Page Down*/
        p3=p3->next;
        if(p3==NULL)
        {
         View_Clear(&view_in);
         View_PutString(&view_in,0,0,"End of the note.",NORMAL);
         View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
         getch();
        }
        break;
     case 18688:/*Page Up*/
       p3=p3->pre;
       if(p3==NULL)
       {
        View_Clear(&view_in);
        View_PutString(&view_in,0,0,"Head of the note.",NORMAL);
        View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
        getch();
       }
     break;
     case 283:/*Esc*/
        View_Clear(&view_in);
        return;
     default:
       {
         View_Clear(&view_in);
         View_PutString(&view_in,0,0,"Press (Page UP/Page Down) to continue...",NORMAL);
         View_PutString(&view_in,0,1,"Press (Esc) to return to menu...",NORMAL);
         goto loop;
       }
     }
  }
}



void data_input() /*增加记录函数*/
{
 p4=(struct data*)malloc(sizeof(struct data));
 p4->number=(++n);
 View_PutString(&view_in,0,0,"Please input the data...",NORMAL);
 View_PutString(&view_out,0,0,"No:",NORMAL);
 gotoxy(4,0); printf("%d",p4->number);
 View_PutString(&view_out,0,1,"Name:",NORMAL);/*读入姓名*/
 scanf("%s",p4->name);
 View_PutString(&view_out,0,2,"Sex:",NORMAL); /*读入性别*/
 scanf("%s",p4->sex);
 View_PutString(&view_out,0,3,"Birthday:",NORMAL); /*读入出生年月*/
 View_PutString(&view_out,0,4,"year:",NORMAL); 
 scanf("%s",p4->birth.year);
 View_PutString(&view_out,10,4,"month:",NORMAL); 
 scanf("%s",p4->birth.month);
 View_PutString(&view_out,19,4,"day:",NORMAL); 
 scanf("%s",p4->birth.day);
 View_PutString(&view_out,0,5,"Post number:",NORMAL);/*读入邮编*/
 scanf("%s",p4->addr.post_num);
 View_PutString(&view_out,0,6,"Address:",NORMAL); /*读入家庭地址*/
 scanf("%s",p4->addr.addr);
 View_PutString(&view_out,0,7,"Telephone:",NORMAL);/*读入联系电话*/
 scanf("%s",p4->telephone);
 View_Clear(&view_in);
 View_PutString(&view_in,0,0,"Add successfull!",NORMAL);
 View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL); 
 getch();
   if(head==NULL)
   {
    head=p4;
    head->pre=NULL;
    head->next=NULL;
    end=head;
   }
   else
   {
    end->next=p4;
    p4->pre=end;
    end=p4;
    end->next=NULL;
   }

}




void data_dele()/*删除记录函数*/
{
 if(head==NULL)
 {
  View_PutString(&view_in,0,0,"There is no record!",NORMAL);
  View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
  getch();
 }
 else
 {
  View_PutString(&view_in,0,0,"Please enter the name...",NORMAL);
  p5=head;
  View_Clear(&view_out);
  View_PutString(&view_out,4,4,"Name:",NORMAL);
  scanf("%s",name1);
    if(strcmp(p5->name,name1)==0)/*要删除的是第一个记录*/
    {
      if(p5->next==NULL)/*要删除的是头指针指向的记录*/
      {
       head=NULL;
       end=NULL;
      }
 
      else/*要删除的不是头指针指向的记录*/
      {
      p5=p5->next;
      head=p5;
      head->pre=NULL;
        while(p5!=NULL)
        {
         p5->number=p5->number-1;
         p5=p5->next;
        }
      }
      View_Clear(&view_in); 
      View_Clear(&view_out);
      View_PutString(&view_in,0,0,"Delete successfull!",NORMAL);
      View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
      getch();
      n=n-1;
    }
/*----------------------*/
    else/*要删除的是中间的某个记录*/
    { 
      while(strcmp(p5->next->name,name1)!=0)
      {
         if(p5->next==NULL)/*没有找到要删除的记录*/ 
         {
          View_Clear(&view_in);
          View_Clear(&view_out);
          View_PutString(&view_in,0,0,"Can't find the record you want to delete!",NORMAL);
          View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
          getch();
          break;
         }
       p5=p5->next;
      }
     p6=p5->next;
      if(p6==NULL)/*没有找到要删除的记录*/
      {
       free(p6);
       View_Clear(&view_in); 
       View_Clear(&view_out);
       View_PutString(&view_in,0,0,"Can't find the record you want to delete!",NORMAL); 
       View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
       getch();
      }
      else/*删除要删除的记录*/
      {
       p5->next=p6->next;
       p6->next->pre=p5;
       free(p6);
         while(p5->next!=NULL)
         { 
          p5->next->number=p5->next->number-1;
          p5=p5->next; 
         }
       end=p5;
       p5->pre->next=end;
       end->next=NULL;
       n=n-1;
       View_Clear(&view_in);
       View_Clear(&view_out); 
       View_PutString(&view_in,0,0,"Delete successfull!",NORMAL);
       View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
       getch();
      }
   }
  }
}


void data_search()/*查找记录函数*/
{ 
 p6=head;
   if(head==NULL)
   {
    View_PutString(&view_in,0,0,"There is no record!",NORMAL);
    View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
    getch();
   }
   else
   {
    View_PutString(&view_in,0,0,"Please input the name...",NORMAL);
    View_PutString(&view_out,4,4,"Name:",NORMAL);
    scanf("%s",name2);
      while(strcmp(name2,p6->name)!=0&&p6!=NULL) /*代码查找节点*/
      p6=p6->next;
        if(p6==NULL)
        {
         View_Clear(&view_in); 
         View_Clear(&view_out);
         View_PutString(&view_in,0,0,"There is no record you want to find!",NORMAL);
         View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
         getch();
        }
        else
        {  
         View_Clear(&view_out);
         View_Clear(&view_in);
         View_PutString(&view_in,0,0,"It's the record you want to find:",NORMAL);
        View_PutString(&view_out,0,0,"No:",NORMAL);
        gotoxy(0,4); printf("%d",p6->number);
       View_PutString(&view_out,0,2,"Name:",NORMAL);
       View_PutString(&view_out,5,2,p6->name,NORMAL);
       View_PutString(&view_out,0,3,"Sex:",NORMAL);
       View_PutString(&view_out,4,3,p6->sex,NORMAL);
       View_PutString(&view_out,0,4,"Birthday(y/m/d):",NORMAL);
       View_PutString(&view_out,16,4,p6->birth.year,NORMAL);
       View_PutString(&view_out,21,4,"/",NORMAL);
       View_PutString(&view_out,22,4,p6->birth.month,NORMAL);
       View_PutString(&view_out,25,4,"/",NORMAL);
       View_PutString(&view_out,26,4,p6->birth.day,NORMAL);
       View_PutString(&view_out,0,5,"Postnumber:",NORMAL);
       View_PutString(&view_out,11,5,p6->addr.post_num,NORMAL);
       View_PutString(&view_out,0,6,"Address:",NORMAL);
       View_PutString(&view_out,8,6,p6->addr.addr,NORMAL);
       View_PutString(&view_out,0,7,"Tele:",NORMAL);
       View_PutString(&view_out,5,7,p6->telephone,NORMAL);
         View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
         getch();
        }
   }
}



void data_insert()/*插入记录函数*/
{
  if(n==0) data_input();
  else
  {
  p7=(struct data*)malloc(sizeof(struct data));
  loop:
  View_PutString(&view_in,0,0," ",NORMAL);
  gotoxy(0,0); printf("Please enter 1-%d number..",n+1);
  View_PutString(&view_out,0,0,"No:",NORMAL);
  scanf("%d",&p7->number);
    if(p7->number<=0||p7->number>n+1)/*输入数字不在范围之内*/
    {
     View_Clear(&view_out);
     goto loop;
    }
    else/*输入数字在范围之内插入记录*/
    {  
     View_PutString(&view_out,0,1,"Name:",NORMAL);/*读入姓名*/
     scanf("%s",p7->name);
     View_PutString(&view_out,0,2,"Sex:",NORMAL); /*读入性别*/
     scanf("%s",p7->sex);
     View_PutString(&view_out,0,3,"Birthday:",NORMAL); /*读入出生年月*/
     View_PutString(&view_out,0,4,"year:",NORMAL); 
     scanf("%s",p7->birth.year);
     View_PutString(&view_out,10,4,"month:",NORMAL); 
     scanf("%s",p7->birth.month);
     View_PutString(&view_out,19,4,"day:",NORMAL); 
     scanf("%s",p7->birth.day);
     View_PutString(&view_out,0,5,"Post number:",NORMAL);/*读入邮编*/
     scanf("%s",p7->addr.post_num);
     View_PutString(&view_out,0,6,"Address:",NORMAL); /*读入家庭地址*/
     scanf("%s",p7->addr.addr);
     View_PutString(&view_out,0,7,"Telephone:",NORMAL);/*读入联系电话*/
     scanf("%s",p7->telephone);
      if(p7->number==1)/*插入记录为第一条记录*/
      {
       head->pre=p7;
       p7->next=head;
       head=p7;
       head->pre=NULL;
       p8=head->next;    
         while(p8!=NULL)
         {
          p8->number=p8->number+1;
          p8=p8->next;  
         }
       n=n+1;                         
      }
      else if(p7->number<=n)/*向中间插入记录*/
      {
      p8=head;
        while(p7->number!=p8->number)    
        p8=p8->next;
        p8->pre->next=p7;
        p7->pre=p8->pre;
        p7->next=p8;
        p8->pre=p7;
        while(p8!=NULL)
         {
          p8->number=p8->number+1;
          p8=p8->next;  
         }
       n=n+1;
      }
      else/*向最后插入记录*/
      {
       end->next=p7;
       p7->pre=end;
       end=p7;
       end->next=NULL;
       n=n+1;
      }
      View_Clear(&view_in); 
      View_Clear(&view_out);
      View_PutString(&view_in,0,0,"Insert successfull!",NORMAL);
      View_PutString(&view_in,0,1,"Press any key to return to menu...",NORMAL);
      getch();       
    }
  }
}


void data_save()/*保存信息函数*/
{
fseek(fp,0*sizeof(struct data),0);
p9=head;
  while(p9!=NULL)
  {
  fwrite(p9,sizeof(struct data),1,fp);
  p9=p9->next;
  }
}

/*-----------------------------------------------主函数---------------------------------------------------*/

char *items[]={"FIRST RECORD","LAST RECORD","ADD RECORD","DELETE RECORD","SEARCH RECORD","INSERT RECORD","SAVE and EXIT","Exit"};

main()
{

 int index;
 char ch2,ch3;


 textbackground(11);
 clrscr();

 data_load();
 Menu_Init(&menu,18,9,1,8,items,BLACK,WHITE,SINGLELINE);
 Menu_Open(&menu);

 View_Init(&view_out,35,9,30,10,BLACK,WHITE,SINGLELINE);
 View_Init(&view_in,18,5,47,4,BLACK,WHITE,SINGLELINE);
 View_Open(&view_out);
 View_Open(&view_in);
  
 View_Clear(&view_out);

   while(1)
   {
     View_Clear(&view_in);
     View_Clear(&view_out);
     View_PutString(&view_in,0,0,"Welcome to use XiongYuwei's Address Book!",NORMAL);
     gotoxy(1,2); 
     if(n==0) printf("There is no record.");
     else if(n==1) printf("There is only 1 record.");
     else printf("There are %d records.",n); 

     index=Menu_Drive(&menu);
      if(index==0)
      {
       View_Clear(&view_in);
       View_Clear(&view_out);
       data_showhead();
      }
      else if(index==1)
      {
       View_Clear(&view_in);
       View_Clear(&view_out);
       data_showend(); 
      }
      else if(index==2)
      {
       View_Clear(&view_in);
       View_Clear(&view_out);
       data_input();
      }
      else if(index==3)
      {   
       View_Clear(&view_in);
       View_Clear(&view_out);
       data_dele();
      }
      else if(index==4)
      {
       View_Clear(&view_in);
       View_Clear(&view_out);
       data_search();
      }
      else if(index==5)
      {
       View_Clear(&view_in);
       View_Clear(&view_out);
       data_insert();
      }
      else if(index==6)
      {
       View_Clear(&view_in);
       View_Clear(&view_out);
       loop:
       View_PutString(&view_in,0,0,"Do you want to save and exit?:(Y/N)",NORMAL);
       ch2=getch();
         if(ch2=='y'||ch2=='Y')
         {
          data_save();
          fclose(fp);
          break;
         }
          else if(ch2=='n'||ch2=='N') View_Clear(&view_in);
          else 
          goto loop;
      }
      else if(index==7)
      {
       fclose(fp);
       break;
      }
   }

    textattr((BLACK<<4)+WHITE);
    window(1,1,80,25);
    clrscr();
}



⌨️ 快捷键说明

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