C語言實現手機通訊錄系統

#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#define Node struct node
Node             //結構體類型定義,包括:姓名、電話號碼和聯繫地址
{
 char szName[20];
 char szPhoneNum[12];
 char szAddress[30];
 Node *next;
};
Node *strpHead;  //全局變量,鏈表頭指針
Node *strpCurrent;  //用於指向當前正在操作的結點
 //函數原型聲明
void HandleChoice_f(int);
void AddRecord_f();
void InsertNode_f(Node * );
Node *InsertPoint_f(char * );
void MakeNewHead_f(Node * );
void AddToEnd_f(Node * );
void MoveToEnd_f();
void DisplayList_f();
void DeleteRecord_f();
void DelHead_f();
void DelEnd_f(Node * );
void DelMid_f(Node * );
int VerifyDel_f();
void DelNode_f(Node * );
void DelList_f();
void SearchByName_f();
void WriteFile_f();
void LoadFile_f();
void Help_f();

//主程序
int main()
{
 int nChoice;
 system("color 1a");
 printf("             歡迎來到通訊錄管理系統                \n");
 system("pause");
 system("cls");
 strpHead=NULL;
 LoadFile_f();
 do
 {printf("   ☆☆☆☆☆☆☆☆☆菜單☆☆☆☆☆☆☆☆☆☆\n");
 printf("   ┌─────────────────────┐  \n");
 printf("   │     1.增加記錄                           │  \n");
 printf("   │     2.顯示所有記錄                       │ \n");
 printf("   │     3.按名字查找朋友的信息               │ \n");
 printf("   │     4.刪除記錄                           │ \n");
 printf("   │   ★5.請求幫助                           │  \n");
 printf("   │     6.退出程序                           │  \n");
 printf("   └─────────────────────┘  \n");
 printf("      ☆程珊☆  手機:13684601282             \n");
 printf("    請選擇代碼(1,2,3,4,5,6):");
 scanf("%d",&nChoice);
 HandleChoice_f(nChoice);  /*接受用戶的選擇*/
 }while(nChoice!=6);
 return 0;
 
}
void HandleChoice_f(int nChoice)   /*根據用戶選擇nChoice調用相應的函數*/
{
 switch(nChoice)
 {
 case 1:
  AddRecord_f();
  break;
 case 2:
  DisplayList_f();
  break;
 case 3:
  SearchByName_f();
  break;
 case 4:
  DeleteRecord_f();
  break;
 case 5:
  Help_f();
  break;
 case 6:
  WriteFile_f();       /*將鏈表中的數據寫回文件*/
  if(strpHead!=NULL)
  {
   DelList_f();
  }
  break;
 default:
  printf("沒有您要的選項!\n");
  break;
 }
}

void AddRecord_f()
{
 Node *strpNew;/*爲新記錄定義臨時指針變量*/
 strpNew=(Node *)malloc(sizeof(Node));/*開闢空間存放新記錄數據*/
 getchar();
 printf("姓名:");
 gets(strpNew->szName);
 printf("電話號碼:");
 gets(strpNew->szPhoneNum);
 printf("聯繫地址:");
 gets(strpNew->szAddress);
 InsertNode_f(strpNew);
 system("cls");
}
void InsertNode_f(Node *strpNew)
{
 Node *strpFront;
 Node *strpBack;
 system("cls");
 if(strpHead==NULL)
 {
  strpNew->next=NULL;
  strpHead=strpNew;
 }
 else
 {
  if(strcmp(strpNew->szName,strpHead->szName)>0)
  {
   MakeNewHead_f(strpNew);
  }
  else //查找新結點的位置
  {
   strpCurrent=InsertPoint_f(strpNew->szName);
   strpFront=strpCurrent;
   strpBack=strpCurrent->next;
   if(strpBack==NULL)
   {
    AddToEnd_f(strpNew);
   }
   else
   {
    strpFront->next=strpNew;
    strpNew->next=strpBack;
   }
  }
 }
}
Node *InsertPoint_f(char *szName) /*根據新增記錄的姓氏,返回其將插入的正確位置*/
{
 char szTempName[20];
 Node *strpTemp;
 int nTemp;
 if(strpHead->next!=NULL)
 {
  strpCurrent=strpHead;
  strpTemp=strpCurrent->next;
  strcpy(szTempName,strpTemp->szName);
  nTemp=strcmp(szName,szTempName);
  while((nTemp>0)&&(strpCurrent->next!=NULL))
  {
   strpCurrent=strpTemp;
   if(strpCurrent->next!=NULL)
   {
    strpTemp=strpCurrent->next;
    strcpy(szTempName,strpTemp->szName);
    nTemp=strcmp(szName,szTempName);
   }
  }
 }
 else
 {
  strpCurrent=strpHead;
 }
 return(strpCurrent);
}
void MakeNewHead_f(Node *strpNew)/*新結點成爲鏈表的頭結點*/
{
 Node *strpTemp;
 strpTemp=strpHead;
 strpNew->next=strpTemp;
 strpHead=strpNew;
}
void AddToEnd_f(Node *strpNew)/*新結點成爲鏈表的尾結點*/
{
 strpNew->next=NULL;
 MoveToEnd_f();
 strpCurrent->next=strpNew;
}
void MoveToEnd_f()/*當前指針移到鏈表尾*/
{
 strpCurrent=strpHead;
 while(strpCurrent->next!=NULL)
 {
  strpCurrent=strpCurrent->next;
 }
}
void DisplayList_f()
{
 
 strpCurrent=strpHead;
 if(strpCurrent!=NULL)
 {
  printf("\n");
  printf("      姓名             電話號碼           聯繫地址         \n");
  printf("------------------------------------------------------------\n");
  do
  {
   printf("%10s",strpCurrent->szName);
   printf("%20s",strpCurrent->szPhoneNum);
   printf("%20s\n",strpCurrent->szAddress);
   strpCurrent=strpCurrent->next;
   printf("\n");
  }while(strpCurrent!=NULL);
  system("pause");
  system("cls");
 }
 else
 {
  printf("沒有記錄可以顯示!\n");
 }
}
void SearchByName_f()
{
 char szSearch[20];
 strpCurrent=strpHead;
 system("cls");
 getchar();
 printf("\n輸入您要查找的姓名:");
 gets(szSearch);
 while((strpCurrent!=NULL)&&(strcmp(strpCurrent->szName,szSearch)!=0))
 {
  strpCurrent=strpCurrent->next;
 }
 if(strpCurrent!=NULL)
 {
  printf("\n記錄找到了!\n");
  printf("%s\n",strpCurrent->szName);
  printf("%s\n",strpCurrent->szPhoneNum);
  printf("%s\n",strpCurrent->szAddress);
 }
 else
 {
  printf("沒有相應的記錄!\n");
  printf("按ENTER鍵繼續\n");
  system("pause");
  system("cls");
 }
}
void DeleteRecord_f()
{
 char szSearch[20];
 Node *strpFront;
 system("cls");
 strpFront=NULL;
 strpCurrent=strpHead;
 getchar();
 printf("\n輸入朋友的姓名以刪除該記錄:");
 gets(szSearch);
 while((strpCurrent!=NULL)&&(strcmp(strpCurrent->szName,szSearch)!=0))
 {
  strpFront=strpCurrent;
  strpCurrent=strpCurrent->next;
 }
 if(strpCurrent!=NULL)
 {
  printf("\n記錄找到了\n");
  printf("%s\n",strpCurrent->szName);
  printf("%s\n",strpCurrent->szPhoneNum);
  printf("%s\n",strpCurrent->szAddress);
  if(VerifyDel_f())
  {
   DelNode_f(strpFront);
   printf("\n該記錄已經刪除!\n");
  }
  else
  {
   printf("\n該記錄沒有刪除!\n");
  }
 }
 else
 {
  printf("\n沒有匹配的記錄被刪除!\n");
 }
 system("cls");
}
void Help_f()
{
 int nChoice;
 do
 {
  system("cls");
  printf("歡迎來到幫助欄,請選擇代號\n");
  printf("1:通訊錄的功能\n");
  printf("2:怎麼清除所有記錄\n");
  printf("3:在加入新的信息時,原來的信息還在嗎\n");
  printf("4:你操作時需要注意的事項!\n");
  printf("5:退出\n");
  scanf("%d",&nChoice);
  switch(nChoice)
  {
  case 1:
   printf("這是一個簡單的通訊錄,剛開始是一個空的電話本。你可以根據提示加入或刪除紀錄,還可以根據需要顯示和查詢電話號碼\n");
   printf("按ENTER鍵繼續\n");
   getch();
   system("cls");
   break;
  case 2:
   printf("你只要刪除文件Friend.dat或者退出程序釋放存儲空間,就可以刪除所有紀錄了\n");
   printf("按ENTER鍵繼續\n");
   getch();
   system("cls");
   break;
  case 3:
   printf("當然拉,加入信息不會改變原來的信息\n");
   printf("按ENTER鍵繼續\n");
   getch();
   system("cls");
   break;
  case 4:
   printf("在查找和刪除紀錄時,應該把要找的朋友的全名寫好,否則將無法顯示和操作\n");
   printf("按ENTER鍵繼續\n");
   getch();
   system("cls");
   break;
  default:
  case 5:
   printf("按ENTER鍵繼續\n");
   break;
  }
 }while(nChoice!=5);
}
int VerifyDel_f() /*刪除信息時要求予以確認*/
{
 char chYesNo;
 printf("你確定要刪除嗎?(Y/N)");
 scanf("%c",&chYesNo);
 if((chYesNo=='Y')||(chYesNo=='y'))
 {
  return(1);
 }
 else
 {
  return(0);
 }
}
void DelNode_f(Node *strpFront)/*刪除結點*/
{
 if(strpCurrent==strpHead)
  DelHead_f();
 else
 {
  if(strpCurrent->next==NULL)
   DelEnd_f(strpFront);
  else
   DelMid_f(strpFront);
 } 
}
void DelHead_f()  /*刪除頭結點*/
{
 strpCurrent=strpHead;
 if(strpHead->next!=NULL)
  strpHead=strpCurrent->next;
 else
  strpHead=NULL;
 free(strpCurrent);
}
void DelEnd_f(Node *strpFront) /*刪除尾結點*/
{
 free(strpCurrent);
 strpFront->next=NULL;
 strpCurrent=strpHead;
}
void DelMid_f(Node *strpFront) /*刪除鏈表中的結點*/
{
 strpFront->next=strpCurrent->next;
 free(strpCurrent);
 strpCurrent=strpHead;
}
void DelList_f()/*刪除鏈表,釋放空間*/
{
 Node *strpTemp;
 strpCurrent=strpHead;
 do
 {
  strpTemp=strpCurrent->next;
  free(strpCurrent);
  strpCurrent=strpTemp;
 }while(strpTemp!=NULL);
}
void Deln(char *szString)/*刪除字符串後的回車符*/
{
 int nLength;
 nLength=strlen(szString);
 if(nLength>=2)
 {
  if(szString[nLength-1]=='\n')
   szString[nLength-1]='\0';
 }
}
void WriteFile_f()/*程序退出,將鏈表數據寫回文件*/
{
 FILE *fpoutfile;
 if((fpoutfile=fopen("FRIENDS.DAT","w"))==NULL)
 {
  printf("File Error!\n");
  exit(0);
 }
 strpCurrent=strpHead;
 if(strpHead!=NULL)
 {
  do
  {
   fprintf(fpoutfile,"%s\n",strpCurrent->szName);
   fprintf(fpoutfile,"%s\n",strpCurrent->szPhoneNum);
   fprintf(fpoutfile,"%s\n",strpCurrent->szAddress);
   strpCurrent=strpCurrent->next;
  }while(strpCurrent!=NULL);
 }
 fprintf(fpoutfile,"END OF FILE");
 fclose(fpoutfile);//關閉文件
}
void LoadFile_f()/*從數據文件FRIEND.DAT中讀取數據重建鏈表*/
{
 Node *strpNew;
 FILE *fpinfile;/*輸入文件指針*/
 int nEndLoop=0;
 if((fpinfile=fopen("FRIENDS.DAT","r"))==NULL)
 {
  printf("現在沒有可用數據載入,鏈表爲空\n");
 }
 else{
  do{
  strpNew=(Node *)malloc(sizeof(Node));
  if(strpNew!=NULL)
  {
   fgets(strpNew->szName,20,fpinfile);
   if((strcmp(strpNew->szName,"")!=0)&&(strcmp(strpNew->szName,"END OF FILE")!=0))
   {
    fgets(strpNew->szPhoneNum,15,fpinfile);
    fgets(strpNew->szAddress,15,fpinfile);
    Deln(strpNew->szName);
    Deln(strpNew->szPhoneNum);
    Deln(strpNew->szAddress);
    InsertNode_f(strpNew);
   }
   else
   {
    free(strpNew);
    nEndLoop=1;
   }
  }
  else
  {
   printf("WARNING:Memory error!\n");
   nEndLoop=1;
  }   
  }while(nEndLoop==0);
  fclose(fpinfile);
 }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章