1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| #define _CRT_SECURE_NO_WARNINGS 1 #include "contact.h"
void InitContact(Contact* pc) { pc->data = (PeoInfo*)malloc(sizeof(PeoInfo)*DEFAULT_SZ); if (pc->data == NULL) { perror("InitContact"); return; } pc->sz = 0; pc->capacity = DEFAULT_SZ; }
void AddContact(Contact* pc) { if (pc->sz == pc->capacity) { PeoInfo* ptr = (PeoInfo*)realloc(pc->data, (pc->capacity + INC_SZ) * sizeof(PeoInfo)); if (ptr != NULL) { pc->data = ptr; pc->capacity = pc->capacity + INC_SZ; printf("开辟空间成功!\n"); } else { perror("AddContact"); printf("增加失败!\n"); } } if (pc->sz == MAX) { printf("满了\n"); return; } printf("请输入名字:>"); scanf("%s", pc->data[pc->sz].name); printf("请输入年龄:>"); scanf("%d", &(pc->data[pc->sz].age)); printf("请输入性别:>"); scanf("%s", pc->data[pc->sz].sex); printf("请输入电话:>"); scanf("%s", pc->data[pc->sz].tele); printf("请输入地址:>"); scanf("%s", pc->data[pc->sz].addr); pc->sz++; printf("添加成功!\n"); }
void PrinContact(const Contact* pc) { int i = 0; printf("%-20s\t%-5s\t%-5s\t%-12s\t%-20s\n","名字","年龄","性别","电话","地址"); for (i = 0; i < pc->sz; i++) { printf("%-20s\t%-5d\t%-5s\t%-12s\t%-20s\n", pc->data[i].name, pc->data[i].age, pc->data[i].sex, pc->data[i].tele, pc->data[i].addr); } }
static int FindByName(Contact* pc, char name[]) { int i = 0; for (i = 0; i < pc->sz; i++) { if (strcmp(pc->data[i].name, name) == 0) { return i; } } return -1; }
void DelContact(Contact* pc) { char name[MAX_NAME]; if (pc->sz == 0) { printf("无人可删\n"); return; } printf("请输入要删除人的名字:>"); scanf("%s", &name); int pos = FindByName(pc,name); if (pos == -1) { printf("查无此人,无法删除\n"); } int i = 0; for (i = pos; i < pc->sz - 1; i++) { pc->data[i] = pc->data[i + 1]; } pc->sz--; printf("删除成功!\n"); }
void SearchContact(Contact* pc) { char name[MAX_NAME]; printf("请输入要查找人的名字:>"); scanf("%s", name);
int pos = FindByName(pc, name); if (pos == -1) { printf("查无此人\n"); return; } else { printf("%-20s\t%-5s\t%-5s\t%-12s\t%-20s\n", "名字", "年龄", "性别", "电话", "地址"); printf("%-20s\t%-5d\t%-5s\t%-12s\t%-20s\n", pc->data[pos].name, pc->data[pos].age, pc->data[pos].sex, pc->data[pos].tele, pc->data[pos].addr); } }
void ModifyContact(Contact* pc) { char name[MAX_NAME]; printf("请输入要修改人的名字:>"); scanf("%s", name);
int pos = FindByName(pc, name); if (pos == -1) { printf("查无此人\n"); return; } else { printf("请输入名字:>"); scanf("%s", pc->data[pos].name); printf("请输入年龄:>"); scanf("%d", &(pc->data[pos].age)); printf("请输入性别:>"); scanf("%s", pc->data[pos].sex); printf("请输入电话:>"); scanf("%s", pc->data[pos].tele); printf("请输入地址:>"); scanf("%s", pc->data[pos].addr); printf("修改成功!\n"); } }
void DestoryContact(Contact* pc) { free(pc->data); pc->data = NULL; pc->sz = 0; pc->capacity = 0; }
void SortContactByName(Contact* pc) { if (pc->sz == 0) { printf("没有联系人,无法排序!\n"); return; }
int i = 0; int j = 0; for (i = 0; i < pc->sz; i++) { for (j = 0; j < pc->sz - i - 1; j++) { if (strcmp(pc->data[j].name, pc->data[j + 1].name) > 0) { PeoInfo tmp = pc->data[j]; pc->data[j] = pc->data[j + 1]; pc->data[j + 1] = tmp; } } } printf("排序成功!\n"); }
|