赞
踩
本文只是对于结构体类型的练习。
只是简单的静态通讯录实现,没有具体的UI界面,只有一些简单的功能,同时也比较粗糙,有很多地方没有经过足够的打磨。
本项目包含三个文件:
- #include "contacts_list.h"
-
- int main()
- {
- int status = 0;
-
- Contacts_list contacts_list;//通讯录结构体变量
-
- InitContact(&contacts_list);
- int res = 0;
- int num = 0;
- do
- {
- menu();//打印菜单
-
- scanf("%d", &status);
-
- system("cls");
-
- switch (status)
- {
- case 1:
- AddContact(&contacts_list);
- break;
- case 2:
- DeleteContact(&contacts_list);
- break;
- case 3:
- char name1[21];
- printf("please type in the name>:");
- scanf("%s", name1);
- res = SearchContact(&contacts_list, name1);
- if (res != -1)
- {
- printf("%d\n", res);
- }
- else
- {
- printf("cannot find\n");
- }
- break;
- case 4:
- char name2[21];
- printf("please type in the name>:");
- scanf("%s", name2);
- ModifyContact(&contacts_list,name2);
- break;
- case 5:
- DisplayContact(&contacts_list);
- break;
- case 6:
- printf("1.sort by name\n");
- printf("2.sort by age\n");
- scanf("%d", &num);
- SortContact(&contacts_list,num);
- break;
- case 0:
- printf("exit\n");
- break;
- default :
- printf("error\n");
- break;
- }
- } while (status);
-
- return 0;
- }
- #include "contacts_list.h"
-
- void menu()
- {
- printf("*************************************\n");
- printf("***** 1.add 2.del ****\n");
- printf("***** 3.search 4.modify ****\n");
- printf("***** 5.display 6.sort ****\n");
- printf("***** 0.exit ****\n");
- printf("*************************************\n");
- printf("please choose a option>:");
- }
-
- void InitContact(Contacts_list* list)
- {
- int i = 0;
- for (i = 0; i < 100; i++)
- {
- list->num = 0;//将联系人个数初始化为零
- memset(list->data, 0, sizeof(list->data));//将数据部分初始化为0
- }
- }
-
- void AddContact(Contacts_list* list)
- {
- if (list->num == MAXCONTACTS)
- {
- printf("there is no more space\n");//没有空间
- return;
- }
- printf("please type in name(maximum 20)>:");
- scanf("%s", list->data[list->num].name);
- printf("please type in gender(maximum 8)>:");
- scanf("%s", list->data[list->num].gender);
- printf("please type in age>:");
- scanf("%d", &(list->data[list->num].age));
- list->num++;
- printf("operating successfully\n");
- }
-
- void DisplayContact(const Contacts_list* list)
- {
- int i = 0;
- for (i = 0; i < list->num; i++)
- {
- printf("name: %s\n", list->data[i].name);
- printf("gender: %s\n", list->data[i].gender);
- printf("age: %d\n", list->data[i].age);
- printf("-------------------------\n");
- }
- printf("display successfully\n");
- }
-
- void DeleteContact(Contacts_list* list)
- {
- char name[21];
- if (list->num == 0)
- {
- printf("there is no contact can be deleted\n");//联系人为零,无需删除
- return;
- }
-
- printf("please type in the name who you want to delete>:");
- scanf("%s", name);
- int res = SearchContact(list, name);
-
- if (res != -1)
- {
- int i = 0;
- for (i = res; i < list->num - 1; i++)
- {
- list->data[i] = list->data[i + 1];
- }
- list->num--;
- printf("delete successfully\n");
- }
- else
- {
- printf("cannot find\n");//没找到要删除的联系人
- return;
- }
- }
-
- int SearchContact(Contacts_list* list, const char* string)
- {
- int i = 0;
- for (i = 0; i < list->num; i++)
- {
- if (strcmp(list->data[i].name, string) == 0)
- {
- return i;//查找到返回下标
- }
- }
- return -1;//未查找到返回-1
- }
-
- void ModifyContact(Contacts_list* list, const char* string)
- {
- int res = SearchContact(list, string);
- if (res == -1)
- {
- printf("cannot find\n");
- return;
- }
- printf("please type in name(maximum 20)>:");
- scanf("%s", list->data[res].name);
- printf("please type in gender(maximum 8)>:");
- scanf("%s", list->data[res].gender);
- printf("please type in age>:");
- scanf("%d", &(list->data[res].age));
-
- printf("modification successfully\n");
- }
-
- int cmp_by_name(const void* element1, const void* element2)
- {
- PeopleInfo* ele1 = (PeopleInfo*)element1;//强制转换为联系人结构体类型,方便访问name变量
- PeopleInfo* ele2 = (PeopleInfo*)element2;
- return strcmp(ele1->name, ele2->name);
- }
-
- int cmp_by_age(const void* element1, const void* element2)
- {
- int res = ((PeopleInfo*)element1)->age - ((PeopleInfo*)element2)->age;
- return ((-res < 0) - (res < 0));
- }
-
- void SortContact(Contacts_list* list, int num)
- {
- switch (num)
- {
- case 1:
- qsort(list->data, list->num, sizeof(list->data[0]), cmp_by_name);
- printf("sort successfully\n");
- break;
- case 2:
- qsort(list->data, list->num, sizeof(list->data[0]), cmp_by_age);
- printf("sort successfully\n");
- break;
- default :
- printf("error\n");
- break;
- }
- }
- #pragma once
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
-
- #define MAXCONTACTS 100
-
- //打印菜单函数
- void menu();
-
- //声明联系人信息结构体变量
- typedef struct PeopleInfo
- {
- char name[21];
- char gender[9];
- int age;
- }PeopleInfo;
-
- //声明通讯录结构体变量
- typedef struct Contacts_list
- {
- PeopleInfo data[MAXCONTACTS];//存放联系人数据
- int num;//记录通讯录联系人个数
- }Contacts_list;
-
- //初始化函数
- void InitContact(Contacts_list* list);
-
- //添加联系人
- void AddContact(Contacts_list* list);
-
- //显示通讯录
- void DisplayContact(const Contacts_list* list);
-
- //删除联系人
- void DeleteContact(Contacts_list* list);
-
- //查找联系人
- int SearchContact(Contacts_list* list, const char* string);
-
- //修改联系人
- void ModifyContact(Contacts_list* list,const char* string);
-
- //排序联系人
- void SortContact(Contacts_list* list, int num);
-
- //按名字排序
- int cmp_by_name(const void* element1, const void* element2);
-
- //按年龄排序
- int cmp_by_age(const void* element1, const void* element2);
如有错误,欢迎指正。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。