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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
| #define _CRT_SECURE_NO_WARNINGS 1 #include"contact.h" struct Contact* BuyNewNode(char* name, int age, char* sex, char* tel, char* adr) { struct Contact* newnode = (struct Contact*)malloc(sizeof(struct Contact)); assert(newnode); newnode->next = newnode; newnode->front = newnode; strcpy(newnode->name , name); newnode->age = age; strcpy(newnode->sex , sex); strcpy(newnode->tel , tel); strcpy(newnode->adr , adr); return newnode; } void SaveFileData(PC*phead) { FILE* fp = fopen("data.txt", "r"); assert(phead); if (fp == NULL) { printf("%s", strerror(errno)); return; } getc(fp); while (!feof(fp)) { rewind(fp); PC* newnode = BuyNewNode("000000", 0, "000000", "00000", "000000"); fscanf(fp, "%s ", newnode->name); fscanf(fp, "%d ", &(newnode->age)); fscanf(fp, "%s ", newnode->sex); fscanf(fp, "%s ", newnode->tel); fscanf(fp, "%s ", newnode->adr); PC* head = phead->front; head->next = newnode; newnode->front = head; newnode->next = phead; phead->front = newnode; } fclose(fp); fp = NULL; } void Print(PC* phead) { PC* cur = phead->next; assert(phead); if (cur == phead) { printf("还没有添加信息\n"); } printf("姓名 年龄 性别 电话 地址\n"); while (cur != phead) { printf("%-15s\t%-5d\t%-5s\t%-15s\t%-10s\n", cur->name, cur->age, cur->sex, cur->tel, cur->adr); cur = cur->next; } } void ADDPc(PC* phead) { PC* newnode = BuyNewNode("000000", 0, "000000", "00000", "000000"); assert(phead); assert(newnode); printf("请输入姓名\n"); scanf("%s", newnode->name); printf("请输入年龄\n"); scanf("%d", &(newnode->age)); printf("请输入性别\n"); scanf("%s", newnode->sex); printf("请输入电话\n"); scanf("%s", newnode->tel); printf("请输入地址\n"); scanf("%s", newnode->adr); PC* head = phead->front; head->next = newnode; newnode->front = head; newnode->next = phead; phead->front = newnode; printf("添加成功\n"); } PC* Find_by_name( PC* phead, char* name) { assert(phead); PC* pos = phead->next; while (pos !=phead) { if (0 == strcmp(name, pos->name)) { return pos; } pos = pos->next; } return NULL; } void DelPc(PC* pos) { assert(pos); PC* Front = pos->front; PC* Next = pos->next; Front->next = Next; Next->front = Front; free(pos); pos = NULL; } void DesTroy(PC* phead) { assert(phead); PC* cur=phead->next; while (cur != phead) { PC* Next = cur->next; free(cur); cur = Next; } free(phead); phead = NULL; } void MoDify(PC* pos) { char str_name2[NAM_MAX] = "0"; char str_sex2[SEX_MAX] = "0"; char str_tel2[TEL_MAX] = "0"; char str_adr2[ADR_MAX] = "0"; assert(pos);
printf("请输入新的姓名\n"); scanf("%s", str_name2); strcpy(pos->name, str_name2);
printf("请输入新的年龄\n"); scanf("%d", &(pos->age));
printf("请输入新的性别\n"); scanf("%s", str_sex2); strcpy(pos->sex, str_sex2);
printf("请输入新的电话\n"); scanf("%s",str_tel2); strcpy(pos->tel, str_tel2);
printf("请输入新的地址\n"); scanf("%s", str_adr2); strcpy(pos->adr, str_adr2); } int Size(PC* phead) { int sz = 0; PC* cur = phead->next; assert(phead); while (cur != phead) { sz++; cur = cur->next; } return sz; }
void Compare_ByName(PC*phead) { int i =0; int j = 0; PC* cur = phead->next; PC* Next = cur->next; char Name_s[NAM_MAX] = "0"; int age_s=0; char sex_s[SEX_MAX] = "0"; char tel_s[TEL_MAX] = "0"; char adr_s[ADR_MAX] = "0"; assert(phead);
for (i = 0; i < Size(phead)+1; i++) { for (j = 0; j < Size(phead) - i ;j++) { if ((strcmp(cur->name, Next->name) > 0) && (cur != phead) && (Next != phead)) { strcpy(Name_s, cur->name); strcpy(cur->name, Next->name); strcpy(Next->name, Name_s);
age_s = cur->age; cur->age = Next->age; Next->age = age_s;
strcpy(sex_s, cur->sex); strcpy(cur->sex, Next->sex); strcpy(Next->sex, sex_s);
strcpy(tel_s, cur->tel); strcpy(cur->tel, Next->tel); strcpy(Next->tel, tel_s);
strcpy(adr_s, cur->adr); strcpy(cur->adr, Next->adr); strcpy(Next->adr, adr_s); } cur = cur->next; Next = Next->next; } } }
void SaveData(PC* phead) { FILE* fp = NULL; PC* cur = phead->next; assert(phead); fp = fopen("data.txt", "w"); if (fp == NULL) { printf("%s", strerror(errno)); return; } while (cur != phead) { fprintf(fp, "%s ", cur->name); fprintf(fp, "%d ", cur->age); fprintf(fp, "%s ", cur->sex); fprintf(fp, "%s ", cur->tel); fprintf(fp, "%s ", cur->adr); fprintf(fp, "\n"); cur = cur->next; } fclose(fp); fp = NULL; }
|