C語言實現通訊錄管理系統

本文實例為大傢分享瞭C語言實現通訊錄管理系統的具體代碼,供大傢參考,具體內容如下

工具:Visual C++6.0

說明:

本系統基於C語言實現班級通訊錄管理系統,為大一時學習C語言剛入門所做的課程設計。功能包括增、刪、查、改等,非常適合初學者練手。通訊錄包括的個人信息有姓名、學號、性別、籍貫、政治面貌、手機號、QQ號、宿舍等。主要用到瞭指針、結構體、遍歷鏈表、文件讀取等知識點。

運行效果:

代碼:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
typedef struct student  //定義結構體
{
 char name[20]; //姓名
 char num[15];  //學號
 char sex[10];  //性別
 char from[20]; //籍貫
 char political[10]; //政治面貌
 char phone[15];  //手機號
 char QQ[15];   //QQ號
 char dorm[10];  //宿舍
 struct student *next; //結構體指針
}stu;
stu *head;

void print() //主菜單
{
 system("cls");
 printf("\n\n\n");
 printf("  ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆\n");
 printf("  ***********************************************************\n");
 printf("  \t\t\t班級通訊錄管理系統\n");
 printf("\n");
 printf("  \t\t1. 輸入數據");
 printf("\t\t2. 顯示數據\n");
 printf("  \t\t3. 插入數據");
 printf("\t\t4. 刪除數據\n");
 printf("  \t\t5. 查看數據");
 printf("\t\t6. 修改數據\n");
 printf("  \t\t7. 保存數據");
 printf("\t\t8. 返回主菜單\n");
 printf("  ***********************************************************\n");
 printf("  ~~~~~退~~~~~~~~~~出~~~~~~~~~~請~~~~~~~~~~按~~~~~~~~~~9~~~~~\n");
 printf("  -----------------------------------------------------------\n\n");
}


void input(stu *p1) //輸入相關數據
{
 printf("姓名:");
 scanf("%s",&p1->name);
 printf("學號:");
 scanf("%s",&p1->num);
 printf("性別:");
 scanf("%s",&p1->sex);
 printf("籍貫:");
 scanf("%s",&p1->from);
 printf("政治面貌:");
 scanf("%s",&p1->political);
 printf("手機號:");
 scanf("%s",&p1->phone);
 printf("QQ號:");
 scanf("%s",&p1->QQ);
 printf("宿舍:");
 scanf("%s",&p1->dorm);
}


stu * inputdata()  //數據輸入的函數
{
 stu *p1,*p2;
 int i=1;
 p1=(stu *)malloc(sizeof(stu));
 if(p1!=NULL)
 {
  head=p1;
  printf("\n\t\t\t☆☆☆輸入數據☆☆☆\n");
  printf("------------------------------------------------------------------\n");
  printf("在姓名處輸入“ok”代表輸入數據結束\n");
  while(i)
  {
   printf("姓名:");
   scanf("%s",&p1->name);
   if(strcmp(p1->name,"ok")==0)
   {
    printf("\n輸入完畢!\n");
    printf("========================================================================\n");
    i=0;
    p2->next=NULL;
    free(p1);
    p1=p2;
   }
   else
   {
    printf("學號:");
    scanf("%s",&p1->num);
    printf("性別:");
    scanf("%s",&p1->sex);
    printf("籍貫:");
     scanf("%s",&p1->from);
    printf("政治面貌:");
    scanf("%s",&p1->political);
    printf("手機號:");
    scanf("%s",&p1->phone);
    printf("QQ號:");
    scanf("%s",&p1->QQ);
    printf("宿舍:");
    scanf("%s",&p1->dorm);
    printf("=====================================\n");
    p2=p1;
    p1=(stu *)malloc(sizeof(stu));
    if(p1!=NULL)
    {
     p2->next=p1;
    }
   }
  }
  return(p1->next);
 }
}


stu * lookdata(stu *p1)  //查看數據的函數
{
 printf("\n\t\t\t☆☆☆顯示數據☆☆☆\n");
 printf("----------------------------------------------------------------------\n");
 while(p1!=NULL)
 {
  printf("姓名:%s\n",p1->name);
  printf("學號:%s\t",p1->num);
  printf("性別:%s\t",p1->sex);
  printf("籍貫:%s\t",p1->from);
  printf("政治面貌:%s\t",p1->political);
  printf("手機號:%s\t",p1->phone);
  printf("QQ號:%s\t",p1->QQ);
  printf("宿舍:%s\n",p1->dorm);
  printf("======================================================================\n");
  p1=p1->next;
 }
 return p1;
}


void insert()  //插入數據
{
 int i;
 char named[20];
 stu *p1,*p2,*p3;
 p1=head;
 p3=(stu *)malloc(sizeof(stu));
 p3->next=NULL;
 printf("\n\t\t\t☆☆☆插入數據☆☆☆\n");
 printf("----------------------------------------------------------------------\n");
 printf("請輸入插入者的資料:\n");
 input(p3);
 printf("\n插入選項\n");
 printf("1.首位置插入\t2.尾位置插入\t3.前插\n");
 printf("請輸入你的選擇:");
 scanf("%d",&i);
 switch(i)
 {
 case 1:p3->next=p1;
  head=p3;
  break;
 case 2:while(p1->next!=NULL)
   {
    p2=p1;
    p1=p1->next;
   }
  p1->next=p3;
  break;
 case 3:printf("請輸入姓名(前插):");
  scanf("%s",named);
  while(strcmp(named,p1->name)!=0)
  {
   p2=p1;
   p1=p1->next;
  }
  p2->next=p3;
  p3->next=p1;
  break;
 }
 printf("插入成功!\n");
 printf("======================================================================\n");
 return;
}

void deleted()   //刪除數據
{
 stu *p1,*p2;
 char Name[20]; //想要刪除的人的姓名
 printf("\n\t\t\t☆☆☆刪除數據☆☆☆\n");
 printf("----------------------------------------------------------------------\n");
 printf("請輸入要刪除的姓名:");
 scanf("%s",Name);
 p1=head;
 if(head==NULL)
 {
 printf("內存空空神馬都沒有!\n");
 printf("======================================================================\n");
 return;
 }
 if(strcmp(Name,p1->name)==0)
 {
 head=p1->next;
 printf("刪除成功!\n");
 printf("======================================================================\n");
 return;
 }
 while(p1!=NULL&&(strcmp(Name,p1->name)!=0))
 {
 p2=p1;
 p1=p1->next;
 }
 if(p1==NULL)
 {
 printf("此人不存在!\n");
 printf("======================================================================\n");
 return;
 }
 if(p1->next!=NULL)
 {
 p1=p1->next;
 p2->next=p1;
 printf("刪除成功!\n");
 printf("======================================================================\n");
 return;
 }
 else
 {
 p2->next=NULL;
 printf("刪除成功!\n");
 printf("======================================================================\n");
 return;
 }
}


find(stu *p2)  //通過姓名查找查看數據的函數
{
 char name[20];
 int b=0;
 printf("\n\t\t\t☆☆☆查看數據☆☆☆\n");
 printf("----------------------------------------------------------------------\n");
 printf("請輸入您想查找人的姓名:");
 scanf("%s",name);
 while(p2!=NULL)
 {
  if(strcmp(name,p2->name)==0)
  {
   printf("你要找到的數據\n");
   printf("姓名:%s\n",p2->name);
   printf("學號:%s\t",p2->num);
   printf("性別:%s\t",p2->sex);
   printf("籍貫:%s\t",p2->from);
   printf("政治面貌:%s\t",p2->political);
   printf("手機號:%s\t",p2->phone);
   printf("QQ號:%s\t",p2->QQ);
   printf("宿舍:%s\n",p2->dorm);
   printf("======================================================================\n");
   b=1;
  }
  p2=p2->next;
 }
 if(b==0)
 {
  printf("\n您要查找的人不存在!\n");
 }
}


update(stu *p2) //通過姓名查找修改數據
{
 char name[20];
 int b=0,i;
 printf("\n\t\t\t☆☆☆修改數據☆☆☆\n");
 printf("----------------------------------------------------------------------\n");
 printf("請輸入將要修改人的姓名:");
 scanf("%s",name);
 while(p2!=NULL)
 {
  if(strcmp(name,p2->name)==0)
  {
   printf("該同學的基本信息\n");
   printf("姓名:%s\n",p2->name);
   printf("學號:%s\t",p2->num);
    printf("性別:%s\t",p2->sex);
   printf("籍貫:%s\t",p2->from);
   printf("政治面貌:%s\t",p2->political);
   printf("手機號:%s\t",p2->phone);
   printf("QQ號:%s\t",p2->QQ);
   printf("宿舍:%s\n",p2->dorm);
   printf("\n請選擇要修改的信息\n");
   printf("\t1.姓名\t2.學號\t3.性別\t4.籍貫\n\t5.政治面貌\t6.手機號\t7.QQ\t8.宿舍\n");
   printf("\n您的選擇是(1~8):");
   scanf("%d",&i);
   printf("請輸入修改之後的內容\n");
   switch(i)
   {
   case 1:printf("姓名:");
    scanf("%s",&p2->name);
    break;
   case 2:printf("學號:");
    scanf("%s",&p2->num);
    break;
   case 3:printf("性別:");
    scanf("%s",&p2->sex);
    break;
   case 4:printf("籍貫:");
    scanf("%s",&p2->from);
    break;
   case 5:printf("政治面貌:");
    scanf("%s",&p2->political);
    break;
   case 6:printf("手機號:");
    scanf("%s",&p2->phone);
    break;
   case 7:printf("QQ:");
    scanf("%s",&p2->QQ);
    break;
   case 8:printf("宿舍:");
    scanf("%d",&p2->dorm);
    break;
   }
   printf("\n修改成功!\n");
   printf("=========================================================================\n");
   b=1;
  }
  p2=p2->next;
 }
 if(b==0)
 {
  printf("沒有找到該人的資料!\n");
 }
}


save(stu *p2) //保存數據
{
 FILE *fp;
 char file[15];
 printf("\n\t\t\t☆☆☆保存數據☆☆☆\n");
 printf("----------------------------------------------------------------------\n");
 printf("輸入文件名:");
 scanf("%s",file);
 if((fp=fopen(file,"w"))==NULL)
 {
  printf("cannot open this file\n");
  exit(0);
 }
 fprintf(fp,"姓名\t學號\t性別\t籍貫\t政治面貌\t手機號\tQQ號\t宿舍\n");
 while(p2!=NULL)
 {
  fprintf(fp,"%s\t",p2->name);
  fprintf(fp,"%s\t",p2->num);
  fprintf(fp,"%s\t",p2->sex);
  fprintf(fp,"%s\t",p2->from);
  fprintf(fp,"%s\t",p2->political);
  fprintf(fp,"%s\t",p2->phone);
  fprintf(fp,"%s\t",p2->QQ);
  fprintf(fp,"%s\n",p2->dorm);
  p2=p2->next;
 }
 printf("\n保存成功!\n");
 printf("======================================================================\n");
 fclose(fp);
}


void screen()
{
 int i;
 char s[251]={"歡迎使用由ZM制作班級通訊錄管理系統,\n\n\t\t\t本系統用於通訊錄管理----排序,打印\n\n\n\t\tWelcome to use produced by ZM class address book\n\n\t\t management system,sorting,printing"};

 printf("\n================================================================================\n");
 printf("\n\n\n\t\t\t");
 for(i=0;s[i]!=NULL;i++)
 {
  Sleep(30);
  printf("%c",s[i]);
 }
 printf("\n\n\n\n\n\n\n\n\t\t ~ Hi~ o(* ̄▽ ̄*)ブ~ ~ ~祝您旅途愉快~ ~\n");
 printf("================================================================================\n");

}


void main()
{
 int i;
 system("color 4e");
 screen();
 Sleep(3000);
 print();
 while(1)
 {
  printf("請輸入你的選擇(1~9):");
  loop:scanf("%d",&i);
  if(i<1||i>9)
  {
   printf("輸入有誤,請在1~9中進行選擇:");
   goto loop;
  }
  switch(i)
  {
  case 1:
   inputdata();
   break;
  case 2:
   lookdata(head);
   break;
  case 3:
   insert();
   break;
  case 4:
    deleted();
   break;
  case 5:
   find(head);
   break;
  case 6:
   update(head);
   break;
  case 7:
   save(head);
   break;
  case 8:
   print();
   break;
  case 9:
   exit(1);
   break;
  }
 }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: