实现一个通讯录

来源:互联网 发布:au cs6 mac 汉化 编辑:程序博客网 时间:2024/05/16 05:55
实现一个通讯录; 
通讯录可以用来存储1000个人的信息,每个人的信息包括: 
姓名、性别、年龄、电话、住址 

提供方法: 
1. 添加联系人信息 
2. 删除指定联系人信息 
3. 查找指定联系人信息 
4. 修改指定联系人信息 
5. 显示所有联系人信息 
6. 清空所有联系人 

7. 以名字排序所有联系人

#define    LEN 3  struct people  {      char name[10];      char sex[3];      char number[12];      int age;  };   struct contact  {       struct people *human;      int capacity;      size_t size;  };   void Init(struct contact *con)   {       con->capacity = LEN;       con->human = (struct people*)malloc(LEN*sizeof(struct people));       if (con->human == NULL)       {           printf("申请失败\n");           return;       }       con->size = 0;   }   void Check_cal(struct contact* con)   {             if (con->size >= con->size)       {           struct people *p = { 0 };           p = (struct people*)realloc(con->human, (con->capacity + 3)*sizeof(struct people));           if (p == NULL)           {               printf("error\n");               return;           }           con->human = p;           con->capacity += 3;       }   }   void Insert(struct contact* con,char*name,char* sex,char* number,int age)   {       Check_cal(con);       con->human[con->size].age = age;               strcpy((con->human[con-> size].sex), sex);       strcpy((con->human[con->size].number), number);         strcpy((con->human[con->size].name), name);       con->size++;   }   void display(struct contact con)   {       for (int i = 0; i < con.size; i++)       {           printf("%s ", con.human[i].name);           printf("%d ", con.human[i].age);           printf("%s", con.human[i].sex);           printf("%s ", con.human[i].number);           printf("\n");       }   }   int find(struct contact *con,char*name)   {       for (int i = 0; i < con->size; i++)       {           if (strcmp(name, con->human[i].name)==0)               return i;       }       return -1;   }   void erase(struct contact *con,char*name)   {       int i=find(con,name);       for (int j = i; j < con->size - 1; j++)       {           con->human[j] = con->human[j + 1];       }       con->size--;   }   void alter(struct contact *con,char*name)   {       int i = find(con,name);     }   void bubbsort(struct contact *con)   {       for (int i = 0; i < con->size; i++)       {           int k = i;           for (int j = i+1; j < con->size; j++)           {               if (strcmp(con->human[k].name, con->human[j ].name))               {                   k = j;               }           }           swap(con->human[i],con->human[k]);                 }  }   int main()   {       struct contact l;       Init(&l);       Insert(&l, "雷宇飞", "男", "18309233241", 22);       Insert(&l, "尚石资", "男", "18309233242", 22);       Insert(&l, "李乐", "男", "18309233243", 22);       Insert(&l, "找小路", "男", "18309233244", 22);       Insert(&l, "李玲", "男", "18309233245", 22);       Insert(&l, "张蓉", "男", "18309233246", 22);       Insert(&l, "刘蕾", "男", "18309233247", 22);       Insert(&l, "马浩", "男", "18309233248", 22);       Insert(&l, "马晓晨", "男", "18309233249", 22);       Insert(&l, "崔浩本", "男", "1830923324q", 22);       Insert(&l, "马鹏质", "男", "1830923324r", 22);       display(l);       printf("\n");       bubbsort(&l);                erase(&l,"刘蕾");                 display(l);       system("pause");   }  


原创粉丝点击