Linux下的通訊錄項目

//Massage.h文件

#ifndef __MASSAGELIST_H__

#define __MASSAGELIST_H__


#define unit unsigned int
#define PASS           0
#define ERROR         -1
#define MALLOC_ERROR  -2
#define N             40


typedef int LinkData; 
typedef struct node
{
LinkData            ID;
char Name           [N];
char Mobile_Phone   [N];
char Home_Address   [N];
char Company_Phone  [N];


struct node *next;      //節點指針
}Node;


typedef Node *PNode;  //重命名節點指針類型


int Time_Display();    //定義時間函數


int Menu();    //顯示界面


int Insert_Last(PNode head, LinkData data); //尾插法添加好友


int Get_Information(PNode head) ;             //顯示好友信息


int Find_Element(PNode head, char *Name);     //查找好友


void Delete_Friend(PNode head, char *Name);   //刪除好友

#endif



//Massage.c文件

#include "Massage.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>


int Menu()  
  {      
 system ("clear");  
 printf ("\t************************************************\n");
 printf ("\t*                                              *\n");  
 printf ("\t*        wellcome to use Massage list          *\n");  
 printf ("\t*                                              *\n");  
 printf ("\t*        1 -------->  add friend info          *\n");  
 printf ("\t*        2 -------->  list friend info         *\n");  
 printf ("\t*        3 -------->  search friend info       *\n");  
 printf ("\t*        4 -------->  delete friend            *\n");  
 printf ("\t*        5 -------->  exit                     *\n");  
 printf ("\t*                                              *\n");  
 printf ("\t*                              auther:yuhuofei *\n");
      printf ("\t************************************************\n");    
 printf ("                                                  \n");  
      printf("\t");
      Time_Display();
      printf ("                                                  \n");  
 printf ("\tPlease enter the num to choose function:");  
  }  




// 時間顯示函數
int Time_Display()
{
    time_t timep;


    struct tm *p;
    time(&timep); /*獲得time_t結構的時間,UTC時間*/


    p = localtime(&timep); /*轉換爲struct tm結構的當地時間*/


    printf("Time: %4d/%02d/%02d ", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday);
    printf("%02d:%02d:%02d\n", p->tm_hour, p->tm_min, p->tm_sec);


return 0;
}




//添加好友信息
int Insert_Last(PNode head, LinkData data)
{  
        // 創建新的結點  
        if(head == NULL)  
        {  
            return ERROR;  
        }  
          
        PNode p = (PNode)malloc(sizeof(Node)/sizeof(char));  
        if (p == NULL)  
        {  
            return MALLOC_ERROR;  
        }  
          
        // 將新數據賦給新結點      
        system ("clear");  
          
        printf ("\t********************Add friend********************\n");  
          
        p->ID = data;  
        printf ("\tFriend ID is:%d\n", p->ID);  
        printf ("\n");  
          
        printf ("\tPlease input your friend name:");  
        scanf ("%s", p->Name);  
        printf ("\n");  
          
        printf ("\tPlease input your friend phone number:");  
        scanf ("%s", p->Mobile_Phone);  
        printf ("\n");  
          
        printf ("\tPlease input your friend home address:");  
        scanf ("%s", p->Home_Address);  
        printf ("\n");  
          
        printf ("\tPlease input your friend company phone:");  
        scanf ("%s", p->Company_Phone);  
        printf ("\n");  
          
        p->next = NULL;  
          
        //找到最後一個結點  
        PNode Ptemp;    //將頭結點地址給臨時指針Ptemp  
        Ptemp = head;  
        while (Ptemp->next)          
        {  
            Ptemp = Ptemp->next;  
        }  
        Ptemp->next = p;  
          
        return PASS;  
}  
  


//顯示好友的信息
int Get_Information(PNode head)
{  
        if(head == NULL)  
        {  
            return ERROR;  
        }  
          
        PNode p;  
          
        printf ("\tID\tName\t\tPhone number\t\tAddress\t\tCcmpany phone\n");  
        for (p = head->next; p != NULL; p = p->next)  
        {  
            printf ("\t%d\t%s\t\t%s\t\t\t%s\t\t%s\n", p->ID, p->Name, p->Mobile_Phone, 
            p->Home_Address, p->Company_Phone);  
        }  
        printf("\n");  
          
        return PASS;  
}  






//查找信息
int Find_Element(PNode head, char *Name)
{  
   if(head == NULL)  
   {  
       return ERROR;  
   }          
   PNode p;  
   int flag = 1;   
   for (p = head->next; p != NULL; p = p->next)  
   {  
      if (strcmp(p->Name,Name) == 0)  
      {     
        flag = 0;  
        printf ("\tFriend information:\n\tID: %d\n\tName: %s\n\tPhone number: %s\n\tAddress: %s\n\tCompany phone:%s\n", p->ID, p->Name, p->Mobile_Phone, p->Home_Address, p->Company_Phone);  
                  
       }  
    }  
    
    if (flag)   
     {     
         printf ("\tSorry,This name doesn't in it!\n");        
     }  
          
    printf("\n");  
          
    return PASS;  
}




//刪除好友
void Delete_Friend(PNode head, char *Name)  
{  
       PNode p = head;  
       PNode q = NULL;  
      
       while (p != NULL && (p->next) != NULL)  
       {  
           q = p->next;  
           if (q != NULL && strcmp(q->Name,Name) == 0)  
           {  
               p->next = q->next;  
               free(q);  
                 
               int j;  
              
               printf ("\tDeleting\n");  
               printf ("\tPlease hold on");  
               fflush(stdout);        //強制刷新緩存,輸出顯示  
               for( j=0;j<3;j++ )  
               {  
                   sleep(1);          //使用sleep,參數:s  
                   printf(".");  
                   fflush(stdout);    //強制刷新緩存,輸出顯示  
               }  
               printf ("\n");  
               printf ("\tSucceed!\n");  
           }  
           else if (q->next == NULL && strcmp(q->Name,Name) != 0)  
           {  
                printf ("\tThis name doesn't in it!\n");  
           }  
           p = p->next;  
       }  
}  




//main.c文件

#include <stdio.h>
#include "Massage.h"
#include "stdlib.h"


int main ()  
    {     
        int Function;  
        int i = 0;  
        char Name[N];  
        int cho;  
          
        PNode head_node = (PNode)malloc(sizeof(Node)/sizeof(char));  
        if(head_node == NULL)  
        {  
            return MALLOC_ERROR;  
        }  
        head_node->next = NULL;  
          
        while(1)  
        {  
            Menu();  
            scanf ("%d", &Function);  
              
            switch (Function)                //功能選擇  
            {  
                case 1://添加好友  
                {  
                    Function = 0;  
                    Insert_Last (head_node,i++);  
                    int j;  
              
                    printf ("\tAddinng\n");  
                    printf ("\tPlease hold on");  
                    fflush(stdout);        //強制刷新緩存,輸出顯示  
                    for( j=0;j<3;j++ )  
                    {  
                        sleep(1);          //linux 使用sleep,參數爲秒  
                        printf(".");  
                        fflush(stdout);    //強制刷新緩存,輸出顯示  
                    }  
                    printf ("\n");  
                    printf ("\tSucceed!\n");  
                    printf ("\tBack the menu,please entre 1:");  
                    scanf ("%d",&cho);  
                    if (cho == 1)  
                    {  
                        break;  
                    }  
                    else  
                    {  
                        printf ("\tSorry!your input is error,please try again:");  
                        scanf ("%d", &cho);  
                        break;  
                    }  
      
                    break;  
                }  
                  
                case 2://顯示好友信息  
                {  
                    system ("clear");  
                    printf ("\t********************Friend Information********************\n");  
                    printf ("\n");  
                      
                    Get_Information (head_node);  
                    Function = 0;  
                    printf ("\tBack the menu,please entre 1:");  
                    scanf ("%d",&cho);  
                    if (cho == 1)  
                    {  
                        break;  
                    }  
                    else  
                    {  
                        printf ("\tSorry!your input is error,please try again:");  
                        scanf ("%d", &cho);  
                        break;  
                    }  
      
                    break;  
                }  
                  
                case 3://查找好友  
                {  
                    system ("clear");  
                    printf ("\t********************Search Friend********************\n");  
                    printf ("\tPlease input your friend name: ");  
                    scanf ("%s", Name);  
                    printf ("\n");  
                      
                    int j;  
              
                    printf ("\tSearching\n");  
                    printf ("\tPlease hold on");  
                    fflush(stdout);        //強制刷新緩存,輸出顯示  
                    for( j=0;j<3;j++ )  
                    {  
                        sleep(1);          //linux 使用sleep,參數爲秒  
                        printf(".");  
                        fflush(stdout);    //強制刷新緩存,輸出顯示  
                    }  
                    printf ("\n");  
                    Find_Element(head_node,Name);  
                    printf ("\tBack the menu,please entre 1:");  
                    scanf ("%d",&cho);  
                    if (cho == 1)  
                    {  
                        break;  
                    }  
                    else  
                    {  
                        printf ("\tSorry!your input is error,please try again:");  
                        scanf ("%d", &cho);  
                        break;  
                    }  
      
                    break;  
                }  
                  
                case 4://刪除好友  
                {  
                    system ("clear");  
                    printf ("\t********************Delete Friend********************\n");  
                    printf ("\tPlease input name taht you want delete:");  
                    scanf ("%s", Name);  
                    printf ("\n");  
                    Delete_Friend(head_node,Name);  
                    printf ("\tBack the menu,please entre 1:");  
                    scanf ("%d",&cho);  
                    if (cho == 1)  
                    {  
                        break;  
                    }  
                    else  
                    {  
                        printf ("\tSorry!your input is error,please try again:");  
                        scanf ("%d", &cho);  
                        break;  
                    }  
      
                    break;  
                }  
                  
                case 5://退出通訊錄  
                {  
                    Function = 0;  
                    printf ("\n");  
                    system ("clear");  
                    exit(0);  
                }  
                  
                default://輸入錯誤  
                {  
                    Function = 0;  
                    printf ("\tSorry!your input is error,please try again:");  
                    scanf ("%d", &Function);  
                    printf ("%d", Function);  
                    break;  
                }  
            }  
        }  
          
        return 0;  




運行效果如下:
    }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章