查找好友信息
void SearchContact(const struct Contact* ps) { printf("请输入要查找人的名字\n"); char name[MAX_NAME]; scanf("%s", &name); int i = FindByName(ps, name); if (i == -1) { printf("查无此人\n"); } else { printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址"); printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", ps->data[i].name, ps->data[i].age, ps->data[i].sex, ps->data[i].tele, ps->data[i].addr); } }
修改好友信息
void ModifyContact(struct Contact* ps) { int pos = 0; char name[MAX_NAME]; printf("请输入要修改人的名字:"); scanf("%s", name); pos = FindByName(ps, name); if (pos == -1) printf("要修改人的信息不存在\n"); else { printf("请输入名字:"); scanf("%s", ps->data[pos].name); printf("请输入年龄:"); scanf("%d", &(ps->data[pos].age)); printf("请输入性别:"); scanf("%s", ps->data[pos].sex); printf("请输入电话:"); scanf("%s", ps->data[pos].tele); printf("请输入地址:"); scanf("%s", ps->data[pos].addr); ps->size++; printf("用户修改成功\n"); } }
打印好友信息
void ShowContact(const struct Contact* ps) { if (ps->size == 0) printf("通讯录为空\n"); else { int i = 0; //标题 printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址"); for (i = 0; i < ps->size; i++) { printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", ps->data[i].name, ps->data[i].age, ps->data[i].sex, ps->data[i].tele, ps->data[i].addr); } } }
按名字排序好友信息
int compar(const void* e1,const void* e2) { return strcmp(((struct PeoiInfor*)e1)->name, ((struct PeoiInfor*)e2)->name); } void SortContact(struct Contact *ps) { qsort(ps->data, ps->size, sizeof(ps->data[0]), compar); if (ps->size == 0) printf("通讯录为空\n"); else { int i = 0; //标题 printf("%-20s\t%-4s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址"); for (i = 0; i < ps->size; i++) { printf("%-20s\t%-4d\t%-5s\t%-12s\t%-20s\n", ps->data[i].name, ps->data[i].age, ps->data[i].sex, ps->data[i].tele, ps->data[i].addr); } } }
退出销毁内存
//退出销毁内存 void DestroyContact(struct Contact* ps) { free(ps->data); ps->data; }