C语言通讯录小功能 C语言实现通讯录小功能
噫吁嚱。 人气:0想了解C语言实现通讯录小功能的相关内容吗,噫吁嚱。在本文为您仔细讲解C语言通讯录小功能的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C语言,通讯录,下面大家一起来学习吧。
头文件contact.h
#pragma once #define _CRT_SECURE_NO_WARNINGS 1 //#define max 1000 #define fault 3 #define max_name 20 #define max_telephone 12 #define max_sex 5 #define max_address 30 #include<stdio.h> #include<string.h> #include<assert.h> #include<stdlib.h> #include<errno.h> enum option { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, SORT, SAVE }; struct people_information { char name[max_name]; char telephone[max_telephone]; char sex[max_sex]; char address[max_address]; int age; }; struct contact { //struct people_information data[max]; struct people_information *data;//动态空间 int size;//记录当前有多少信息 int capacity;//当前最大容量 }; void initcontact(struct contact* ps);//初始化函数 void add(struct contact* ps); void my_delete(struct contact* ps); void search(const struct contact* ps); void modify(struct contact* ps);//修改指定联系人 void show(const struct contact* ps);//展示全部联系人 void sort(struct contact* ps); void destroy(struct contact* ps); void save(struct contact* ps);
分装函数contact.c
#define _CRT_SECURE_NO_WARNINGS 1 #include"contact.h" void initcontact(struct contact* ps) { //memset(ps->data, 0, sizeof(ps->data)); //ps->size = 0;//初始化通讯录中只有0个信息 ps->data = (struct people_information*)malloc(fault * sizeof(struct people_information)); if (ps->data == NULL) { return; } ps->size = 0; ps->capacity = fault; } void checkcapacity(struct contact* ps) { if (ps->size == ps->capacity) { struct people_information * ptr=realloc(ps->data, (ps->capacity + 2)*sizeof(struct people_information)); if (ptr != NULL) { ps->data = ptr; ps->capacity += 2; printf("增容成功\n"); } else { printf("增容失败\n"); } } } void add(struct contact* ps) { /*if (ps->size == max) { printf("通讯录已满,无法增加\n"); } else { printf("请输入姓名:>"); scanf("%s", ps->data[ps->size].name); printf("请输入电话:>"); scanf("%s", ps->data[ps->size].telephone); printf("请输入性别:>"); scanf("%s", ps->data[ps->size].sex); printf("请输入住址:>"); scanf("%s", ps->data[ps->size].address); printf("请输入年龄:>"); scanf("%d", &(ps->data[ps->size].age)); ps->size++; printf("添加成功!\n"); }*/ //检测当前通讯录的容量 checkcapacity(ps); printf("请输入姓名:>"); scanf("%s", ps->data[ps->size].name); printf("请输入电话:>"); scanf("%s", ps->data[ps->size].telephone); printf("请输入性别:>"); scanf("%s", ps->data[ps->size].sex); printf("请输入住址:>"); scanf("%s", ps->data[ps->size].address); printf("请输入年龄:>"); scanf("%d", &(ps->data[ps->size].age)); ps->size++; printf("添加成功!\n"); } void show(const struct contact* ps) { if (ps->size == 0) { printf("通讯录为空\n"); } else { int i = 0; for (i = 0; i < ps->size; i++) { printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4s\n", "姓名", "电话", "性别", "住址", "年龄"); printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4d\n", ps->data[i].name, ps->data[i].telephone, ps->data[i].sex, ps->data[i].address, ps->data[i].age); } } } static int FindByName(const struct contact* ps, char name[max_name])//冗余函数、查找函数 { //找到了返回名字所在元素的下标,找不到返回-1 int i = 0; for (i = 0; i < ps->size; i++) { if (0 == strcmp(ps->data[i].name, name)) { return i; } } return -1; } void my_delete(struct contact* ps) { printf("请输入您要删除联系人的姓名:"); char name[max_name] ; scanf("%s", name); int pos = FindByName(ps, name); if (pos==-1) { printf("您要删除的人不存在\n"); } else { //删除数据 int j = 0; for (j = pos; j < ps->size - 1; j++) { ps->data[j] = ps->data[j + 1]; } ps->size--; printf("删除成功!\n"); } } void search( const struct contact* ps) { printf("请输入您要查找联系人的姓名:"); char name[max_name]; scanf("%s", name); int pos = FindByName(ps, name); if (pos == -1) { printf("您要查找的联系人不存在\n"); } else { printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4s\n", "姓名", "电话", "性别", "住址", "年龄"); printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4d\n", ps->data[pos].name, ps->data[pos].telephone, ps->data[pos].sex, ps->data[pos].address, ps->data[pos].age); } }//完成 void modify(struct contact* ps) { printf("请输入您要修改联系人的姓名:"); char name[max_name]; scanf("%s", name); int pos = FindByName(ps, name); if (pos == -1) { printf("您要修改的联系人不存在\n"); } else { printf("新的姓名:"); scanf("%s", ps->data[pos].name); printf("新的电话:"); scanf("%s", ps->data[pos].telephone); printf("新的性别:"); scanf("%s", ps->data[pos].sex); printf("新的住址:"); scanf("%s", ps->data[pos].address); printf("新的年龄:"); scanf("%d", &(ps->data[pos].age)); printf("修改完成!\n"); } } void sort(struct contact* ps) { assert(ps); if (ps->size == 0) { printf("通讯录为空,无需排序\n"); } else if (ps->size == 1) { printf("您的通讯录中只有一个人,无需排序\n"); printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4s\n", "姓名", "电话", "性别", "住址", "年龄"); printf("%-20s\t%-12s\t%-5s\t%-30s\t%-4d\n", ps->data[0].name, ps->data[0].telephone, ps->data[0].sex, ps->data[0].address, ps->data[0].age); } else { int i = 0; for (i = 0; i < ps->size; i++) { int j = 0; for (j = 0; j < ps->size - i - 1; j++) { int ret = strcmp(ps->data[j].name, ps->data[j + 1].name); if (ret > 0) { char tmp1[max_name] = { 0 }; char tmp2[max_telephone] = { 0 }; char tmp3[max_sex] = { 0 }; char tmp4[max_address] = { 0 }; int tmp5 = 0; strcpy(tmp1, ps->data[j].name); strcpy(tmp2, ps->data[j].telephone); strcpy(tmp3, ps->data[j].sex); strcpy(tmp4, ps->data[j].address); tmp5 = ps->data[j].age;//交给临时变量 strcpy(ps->data[j].name, ps->data[j+1].name); strcpy(ps->data[j].telephone, ps->data[j+1].telephone); strcpy(ps->data[j].sex, ps->data[j+1].sex); strcpy(ps->data[j].address, ps->data[j+1].address); ps->data[j].age= ps->data[j+1].age; strcpy( ps->data[j + 1].name,tmp1); strcpy( ps->data[j + 1].telephone,tmp2); strcpy( ps->data[j + 1].sex,tmp3); strcpy( ps->data[j + 1].address,tmp4); ps->data[j + 1].age=tmp5; } } } } printf("排序完成!\n"); } void destroy(struct contact* ps) { free(ps->data); ps->data = NULL; } void save(struct contact* ps) { FILE* pfwrite = fopen("contact.dat", "wb"); if (pfwrite == NULL) { printf("%s\n", strerror(errno)); return ; } //写通讯录中的数据到文件中 int i = 0; for (i = 0; i < ps->size; i++) { fwrite(&(ps->data[i]), sizeof(struct people_information), 1, pfwrite); } fclose(pfwrite); pfwrite = NULL; }
源文件test.c
#define _CRT_SECURE_NO_WARNINGS 1 //功能: //一、存放1000个系人信息,信息包括:姓名、电话、性别、住址、年龄 //二、增加联系人信息 //三、删除指定联系人信息 //四、查找联系人信息 //五、修改联系人信息 //六、显示全部联系人信息 //七、排序 #include<stdio.h> #include"contact.h" void menu() { printf("******************************\n"); printf("****1.add 2.delete****\n"); printf("****3.search 4.modify****\n"); printf("****5.show 6.sort ****\n"); printf("****7.save 0.exit ****\n"); printf("******************************\n"); } int main() { int input = 0; //创建通讯录 struct contact con;//con就是通讯录,存放1000个信息 //初始化通讯录 initcontact(&con); do { menu(); printf("请选择:>"); scanf("%d", &input); switch (input) { case ADD: add(&con);//增加联系人信息 break; case DEL: my_delete(&con);//删除指定联系人信息 break; case SEARCH: search(&con);//查找联系人信息 break; case MODIFY: modify(&con);//修改联系人信息 break; case SHOW: show(&con);//显示全部联系人信息 break; case SORT: sort(&con);//排序 break; case EXIT: printf("退出通讯录\n"); //销毁通讯录 destroy(&con); break; case SAVE: save(&con); default: printf("选择错误\n"); break; } } while(input); return 0; }
加载全部内容