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
| void printcontact(const contact* pc) { int i = 0; printf("%-20s %-5s %-5s %-12s %-20s\n", "名字","年龄","性别","电话","地址"); for (i = 0; i < pc->sz; i++) { printf("%-20s %-5d %-5s %-12s %-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("要删除的人不存在"); return; }
int i; for (i = pos; i < pc->sz-1; i++) { pc->data[i] = pc->data[i + 1]; } pc->sz--; printf("删除成功");
}
void searchcontact(contact*pc) { char name[max_name]; printf("请输入要查找人的名字"); scanf("%s", name); int pos = findbyname(pc, name);
if (pos == -1) { printf("要查找的人不存在"); return; } else { printf("%-20s %-5d %-5s %-12s %-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("要修改的人不存在"); 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("修改成功"); }
}
|