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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
| #define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<string.h> #include<stdlib.h> struct STUDENT { int number; char name[50]; int sex; union stu { struct birthday { unsigned int day : 5; unsigned int month : 4; unsigned int year : 11; }stus; int date; }STU; int score[4]; struct STUDENT* next; }; struct STUDENT* head = NULL; struct STUDENT* student = NULL, * current = NULL;
enum types { 男 = 0, 女 = 1 }; void Input() { int i; char temp[50]; while (1) { current = (struct STUDENT*)malloc(sizeof(struct STUDENT)); if (current != NULL) { if (head == NULL) head = current; else student->next = current; current->next = NULL; puts("请输入学生姓名: "); gets(temp); strcpy(current->name, temp); puts("请输入该学生的学号: "); scanf("%d", ¤t->number); puts("请输入该学生的性别(男:0女:1): "); scanf("%d", ¤t->sex); puts("请输入学生的出生日期年 月 日: "); int year, month, day; scanf("%d %d %d", &year, &month, &day); current->STU.stus.year = year; current->STU.stus.month = month; current->STU.stus.day = day; puts("请依次输入学生的语文 数学 英语 理综成绩: "); scanf("%d %d %d %d", ¤t->score[0], ¤t->score[1], ¤t->score[2], ¤t->score[3]); student = current; rewind(stdin); printf("继续输入请按1,输入完成请按2:"); scanf("%d", &i); getchar(); if (i == 1) continue; else break; } } } void Find() {
puts("所有学生的信息如下:\n"); current = head; while (current != NULL) { printf("学生姓名:%s\n", current->name); printf("学号:%d\n", current->number); if (current->sex == 0) printf("性别: 男\n"); else if (current->sex == 1) printf("性别: 女\n"); printf("出生日期:%d%02d%02d\n", current->STU.stus.year, current->STU.stus.month, current->STU.stus.day); printf("该学生的各科成绩为\n语文:%d\n", current->score[0]); printf("数学:%d\n", current->score[1]); printf("英语:%d\n", current->score[2]); printf("理综:%d\n", current->score[3]); current = current->next; } } void Search() { char names[50]; printf("请输入学生的姓名: "); scanf("%s", names); current = head; while (current != NULL) { if (match(names, current) == 1) { printf("您要查找的学生信息如下:\n"); printf("姓名: %s\n", current->name); printf("学号: %d\n", current->number); if (current->sex == 0) { printf("性别: 男\n"); } else if (current->sex == 1) { printf("性别: 女\n"); } printf("出生日期:%4d%02d%02d\n", current->STU.stus.year, current->STU.stus.month, current->STU.stus.day); printf("该学生的各科成绩为\n语文:%d\n", current->score[0]); printf("数学:%d\n", current->score[1]); printf("英语:%d\n", current->score[2]); printf("理综:%d\n", current->score[3]); break; } current = current->next; } if (current == NULL) printf("没有该学生信息\n"); }
void Average() { int subject, n = 0; double avg = 0, sum = 0; current = head; printf("请输入你想统计的科目:\n1.语文\n2.数学\n3.英语\n4.理综\n"); scanf("%d", &subject); if (subject == 1) { while (current != NULL) { sum += current->score[0]; current = current->next; n++; } avg = sum / n; printf("语文的平均成绩为:%.2lf", avg); } else if (subject == 2) { while (current != NULL) { sum += current->score[2]; current = current->next; n++; } avg = sum / n; printf("数学的平均成绩为:%.2lf", avg); } else if (subject == 3) { while (current != NULL) { sum += current->score[2]; current = current->next; n++; } avg = sum / n; printf("英语的平均成绩为:%.2lf\n", avg); } else if (subject == 4) { while (current != NULL) { sum += current->score[3]; current = current->next; n++; } avg = sum / n; printf("理综的平均成绩为:%.2lf", avg); } }
int match(char names[], struct STUDENT* current) { int flag, i; for (i = 0; i < strlen(names); i++) { if (current->name[i] == names[i]) { flag = 1; } else if (current->name[i] != names[i]) { flag = 0; break; } } if (flag == 1) return 1; else if (flag == 0) return 0; } void preserve() { current = head; FILE* p1; p1 = fopen("student", "w"); if (p1 == NULL) { printf("文件打开失败"); exit(1); } while (current != NULL) { fprintf(p1, "%s\n", current->name); fprintf(p1, "%d\n", current->number); fprintf(p1, "%d\n", current->sex); fprintf(p1, "%4d%02d%02d\n", current->STU.stus.year, current->STU.stus.month, current->STU.stus.day); fprintf(p1, "%d\n", current->score[0]); fprintf(p1, "%d\n", current->score[1]); fprintf(p1, "%d\n", current->score[2]); fprintf(p1, "%d\n", current->score[3]); current = current->next; }
if (fclose(p1) != 0) printf("文件关闭失败"); else printf("保存数据成功,文件关闭成功\n"); } void read() { FILE* p2; p2 = fopen("student", "r"); if (p2 == NULL) { printf("打开文件失败"); exit(1); } while (!feof(p2)) { current = (struct STUDENT*)malloc(sizeof(struct STUDENT)); if (head == NULL) head = current; else student->next = current; current->next = NULL; fscanf(p2, "%s\n", current->name); fscanf(p2, "%d\n", ¤t->number); fscanf(p2, "%d\n", ¤t->sex); int year, month, day; fscanf(p2, "%4d%02d%02d\n", &year, &month, &day);。 current->STU.stus.year = year; current->STU.stus.month = month; current->STU.stus.day = day; fscanf(p2, "%d\n", ¤t->score[0]); fscanf(p2, "%d\n", ¤t->score[1]); fscanf(p2, "%d\n", ¤t->score[2]); fscanf(p2, "%d\n", ¤t->score[3]); student = current;
} if (fclose(p2) != 0) printf("文件关闭失败"); else printf("学生数据加载成功\n"); } void SearchID() { int a, b = 0; printf("请输入学生的学号: "); scanf("%d", &a); current = head; while (current != NULL) { if (a == current->number) { break; } current = current->next; b++; } if (current == NULL) printf("无该学生信息"); else { printf("该学生信息如下:\n"); printf("姓名: %s\n", current->name); printf("学号: %d\n", current->number); if (current->sex == 0) { printf("性别: 男\n"); } else if (current->sex == 1) { printf("性别: 女\n"); } printf("出生日期:%4d%02d%02d\n", current->STU.stus.year, current->STU.stus.month, current->STU.stus.day); printf("该学生的各科成绩为\n语文:%d\n", current->score[0]); printf("数学:%d\n", current->score[1]); printf("英语:%d\n", current->score[2]); printf("理综:%d\n", current->score[3]); } } void DeleteID() { int a, flag; printf("请输入删除学生的学号: "); scanf("%d", &a); current = head; while (current->next != NULL) { if (current->next->next != NULL) { if (current->next->number == a) { current->next = current->next->next; flag = 1; break; } } else { if (current->next->number == a) { current->next = NULL; flag = 1; break; } } current = current->next; flag = 0; } if (flag == 1) printf("删除成功"); else printf("删除失败,找不到该学生");
} void Deletename() { char names[50]; int flag; printf("请输入删除学生的姓名: "); scanf("%s", names); current = head; while (current->next != NULL) { if (current->next->next != NULL) { if (match(names, current) == 1) { current->next = current->next->next; flag = 0; break; } } else { if (match(names, current->next) == 1) { current->next = NULL; flag = 0; break; } } flag = 1; current = current->next; } if (flag == 0) printf("删除成功"); else { printf("删除失败,没有该学生信息"); } } void menu() { int func; printf("请选择你要使用的功能:\n"); printf("1.查看所有学生\n"); printf("2.姓名查找学生信息\n"); printf("3.学号查找学生信息\n"); printf("4.输入学号删除学生信息\n"); printf("5.输入姓名删除学生信息\n"); printf("6.统计某科目的平均成绩\n"); printf("7.保存学生数据\n"); scanf("%d", &func); switch (func) { case 1: Find(); break; case 2: rewind(stdin); Search(); break; case 3: SearchID(); break; case 4: DeleteID(); break; case 5: Deletename(); break; case 6: Average(); break; case 7: preserve(); break; } }
|