还是把一个课程设计作为第一篇文章吧——学生学籍管理系统(C语言)
1 #include <stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 typedef struct student { 6 int number; 7 char name[20]; 8 char faculty[50]; 9 char classes[20]; 10 struct student * link; 11 }STU; 12 13 STU * CreateLink(); 14 void WriteLink(STU *); 15 16 void input(); 17 void del(); 18 void del_number(STU *); 19 void del_name(STU *); 20 void del_faculty(STU *); 21 void del_classes(STU *); 22 void search(); 23 void search_number(STU *); 24 void search_name(STU *); 25 void search_faculty(STU *); 26 void search_classes(STU *); 27 void sort(); 28 void show(); 29 void edit(); 30 31 32 void main() 33 { 34 char c; 35 FILE * fp; 36 37 if((fp=fopen("student.dat","r"))==NULL) 38 { 39 fp=fopen("student.dat","w"); 40 fclose(fp); 41 } 42 43 do 44 { 45 system("cls"); 46 printf("\n\n\n"); 47 printf("\t\t╔═════════════════════╗\n"); 48 printf("\t\t║ ║\n"); 49 printf("\t\t║ 1: 添加一个学生 ║\n"); 50 printf("\t\t║ 2: 删除一个学生 ║\n"); 51 printf("\t\t║ 3: 查找一个学生 ║\n"); 52 printf("\t\t║ 4: 学生信息排序 ║\n"); 53 printf("\t\t║ 5: 修改学生信息 ║\n"); 54 printf("\t\t║ 6: 查看学生信息 ║\n"); 55 printf("\t\t║ 9: 退出 ║\n"); 56 printf("\t\t║ ║\n"); 57 printf("\t\t╚═════════════════════╝\n"); 58 printf("\t\t \n"); 59 printf("\t\t请选择输入选项[1\\2\\3\\4\\5\\6\\9]:> "); 60 do 61 { 62 c=getchar(); 63 }while(c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='5'&&c!='6'&&c!='9'); 64 getchar(); 65 66 switch(c) 67 { 68 case '1': input(); break; 69 case '2': del(); break; 70 case '3': search(); break; 71 case '4': sort(); break; 72 case '5': edit(); break; 73 case '6': show(); break; 74 case '9': exit(0); 75 } 76 system("cls"); 77 }while(1); 78 } 79 80 STU * CreateLink() 81 { 82 FILE *fp; 83 STU * head,*p1, *p2; 84 85 if((fp=fopen("student.dat","r"))==NULL) 86 { 87 printf("打开文件错误,退出。"); 88 exit(1); 89 } 90 head=p1=p2=(STU *)malloc(sizeof(STU)); 91 while(!feof(fp)) 92 { 93 if(fscanf(fp,"%d %s %s %s\n",&p2->number,p2->name,p2->faculty,p2->classes)==EOF) 94 { 95 free(head); 96 return NULL; 97 } 98 p1->link=p2; 99 p1=p2; 100 p2=(STU *)malloc(sizeof(STU)); 101 } 102 p1->link=NULL; 103 free(p2); 104 fclose(fp); 105 106 return head; 107 } 108 109 void WriteLink(STU * p2) 110 { 111 FILE *fp; 112 113 if((fp=fopen("student.dat","w"))==NULL) 114 { 115 printf("打开文件错误,退出。"); 116 exit(1); 117 } 118 while(p2!=NULL) 119 { 120 fprintf(fp,"%d %s %s %s\n",p2->number,p2->name,p2->faculty,p2->classes); 121 p2=p2->link; 122 } 123 fclose(fp); 124 } 125 126 void input() 127 { 128 STU * head, *p1, *p2; 129 system("cls"); 130 131 head=CreateLink(); 132 p1=head; 133 134 if(p1!=NULL) 135 while(p1->link!=NULL) 136 p1=p1->link; 137 138 p2=p1; 139 printf("\n\n\n\t\t输入待添加学生(-1 结束):\n"); 140 while(1) 141 { 142 p2=(STU *)malloc(sizeof(STU)); 143 if(p1==NULL) head=p1=p2; 144 printf("输入学号:"); 145 scanf("%d",&p2->number); 146 if(p2->number==-1) break; 147 printf("输入 姓名、系别、班级:"); 148 scanf("%s%s%s",p2->name,p2->faculty,p2->classes); 149 p1->link=p2; 150 p1=p2; 151 } 152 p1->link=NULL; 153 free(p2); 154 155 WriteLink(head); 156 157 free(head); 158 } 159 160 void del() 161 { 162 STU * head=CreateLink(); 163 char c; 164 do 165 { 166 system("cls"); 167 printf("\n\n\n"); 168 printf("\t\t╔═════════════════════╗\n"); 169 printf("\t\t║ ║\n"); 170 printf("\t\t║ 请输入删除内容: ║\n"); 171 printf("\t\t║ ║\n"); 172 printf("\t\t║ 1: 按照学号删除 ║\n"); 173 printf("\t\t║ 2: 按照姓名删除 ║\n"); 174 printf("\t\t║ 3: 按照系别删除 ║\n"); 175 printf("\t\t║ 4: 按照班级删除 ║\n"); 176 printf("\t\t║ 9: 返回主菜单 ║\n"); 177 printf("\t\t║ ║\n"); 178 printf("\t\t╚═════════════════════╝\n"); 179 printf("\t\t \n"); 180 printf("\t\t请选择输入选项[0\\1\\2\\3\\4\\9]:> "); 181 do 182 { 183 c=getchar(); 184 }while(c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='9'); 185 getchar(); 186 187 switch(c) 188 { 189 case '1': del_number(head); break; 190 case '2': del_name( head); break; 191 case '3': del_faculty(head); break; 192 case '4': del_classes(head); break; 193 case '9': return; 194 } 195 printf("\t\t按任意键返回主菜单:\n"); 196 getchar(); 197 system("cls"); 198 }while(1); 199 200 free(head); 201 } 202 void del_number(STU * head) 203 { 204 STU * p1, *p2, temp; 205 system("cls"); 206 do 207 { 208 printf("\n\n\n\t\t输入要删除学生学号(-1 结束):"); 209 scanf("%d",&temp.number); 210 getchar(); 211 p1=p2=head; 212 while(p2!=NULL) 213 { 214 if(head->number==temp.number) 215 { 216 head=head->link; 217 p1->link=NULL; 218 free(p1); 219 p1=p2=head; 220 } 221 else 222 { 223 if(p2->number==temp.number) 224 { 225 p1->link=p2->link; 226 p2->link=NULL; 227 free(p2); 228 p2=p1; 229 } 230 p1=p2; 231 p2=p2->link; 232 } 233 } 234 }while(temp.number!=-1); 235 WriteLink(head); 236 } 237 void del_name(STU * head) 238 { 239 STU * p1, *p2, temp; 240 system("cls"); 241 do 242 { 243 printf("\n\n\n\t\t输入要删除学生姓名(EOF 结束):"); 244 if(scanf("%s",temp.name)==EOF) break; 245 getchar(); 246 p1=p2=head; 247 while(p2!=NULL) 248 { 249 if(strcmp(head->name,temp.name)==0) 250 { 251 head=head->link; 252 p1->link=NULL; 253 free(p1); 254 p1=p2=head; 255 } 256 else 257 { 258 if(strcmp(head->name,temp.name)==0) 259 { 260 p1->link=p2->link; 261 p2->link=NULL; 262 free(p2); 263 p2=p1; 264 } 265 p1=p2; 266 p2=p2->link; 267 } 268 } 269 }while(1); 270 WriteLink(head); 271 } 272 void del_faculty(STU * head) 273 { 274 STU * p1, *p2, temp; 275 system("cls"); 276 do 277 { 278 printf("\n\n\n\t\t输入要删除学生系别(EOF 结束):"); 279 if(scanf("%s",temp.faculty)==EOF) break; 280 getchar(); 281 p1=p2=head; 282 while(p2!=NULL) 283 { 284 if(strcmp(head->faculty,temp.faculty)==0) 285 { 286 head=head->link; 287 p1->link=NULL; 288 free(p1); 289 p1=p2=head; 290 } 291 else 292 { 293 if(strcmp(head->faculty,temp.faculty)==0) 294 { 295 p1->link=p2->link; 296 p2->link=NULL; 297 free(p2); 298 p2=p1; 299 } 300 p1=p2; 301 p2=p2->link; 302 } 303 } 304 }while(1); 305 WriteLink(head); 306 } 307 void del_classes(STU * head) 308 { 309 STU * p1, *p2, temp; 310 system("cls"); 311 do 312 { 313 printf("\n\n\n\t\t输入要删除学生班级(EOF 结束):"); 314 if(scanf("%s",temp.classes)==EOF) break; 315 getchar(); 316 p1=p2=head; 317 while(p2!=NULL) 318 { 319 if(strcmp(head->classes,temp.classes)==0) 320 { 321 head=head->link; 322 p1->link=NULL; 323 free(p1); 324 p1=p2=head; 325 } 326 else 327 { 328 if(strcmp(head->classes,temp.classes)==0) 329 { 330 p1->link=p2->link; 331 p2->link=NULL; 332 free(p2); 333 p2=p1; 334 } 335 p1=p2; 336 p2=p2->link; 337 } 338 } 339 }while(1); 340 WriteLink(head); 341 } 342 343 void search() 344 { 345 STU * head=CreateLink(); 346 347 char c; 348 do 349 { 350 system("cls"); 351 printf("\n\n\n"); 352 printf("\t\t╔═════════════════════╗\n"); 353 printf("\t\t║ ║\n"); 354 printf("\t\t║ 请输入查找内容: ║\n"); 355 printf("\t\t║ ║\n"); 356 printf("\t\t║ 1: 按照学号查找 ║\n"); 357 printf("\t\t║ 2: 按照姓名查找 ║\n"); 358 printf("\t\t║ 3: 按照系别查找 ║\n"); 359 printf("\t\t║ 4: 按照班级查找 ║\n"); 360 printf("\t\t║ 9: 返回主菜单 ║\n"); 361 printf("\t\t║ ║\n"); 362 printf("\t\t╚═════════════════════╝\n"); 363 printf("\t\t \n"); 364 printf("\t\t请选择输入选项[0\\1\\2\\3\\4\\9]:> "); 365 do 366 { 367 c=getchar(); 368 }while(c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='9'); 369 getchar(); 370 371 switch(c) 372 { 373 case '1': search_number(head); break; 374 case '2': search_name( head); break; 375 case '3': search_faculty(head); break; 376 case '4': search_classes(head); break; 377 case '9': return; 378 } 379 printf("\t\t按任意键返回主菜单:\n"); 380 getchar(); 381 system("cls"); 382 }while(1); 383 } 384 void search_number(STU * head) 385 { 386 STU temp,*p2=head; 387 system("cls"); 388 389 do 390 { 391 printf("\n\n\n\t\t输入要查找学生学号(-1 结束):\n\t\t"); 392 scanf("%d",&temp.number); 393 printf("\t\t查询结果:\n"); 394 while(p2!=NULL) 395 { 396 if(p2->number==temp.number) 397 printf("\t\t%d %s %s %s",p2->number,p2->name,p2->faculty,p2->classes); 398 p2=p2->link; 399 } 400 p2=head; 401 }while(temp.number!=-1); 402 403 } 404 void search_name(STU * head) 405 { 406 STU temp, *p2=head; 407 system("cls"); 408 409 printf("\n\n\n\t\t输入要查找学生姓名(EOF 结束):\n\t\t"); 410 do 411 { 412 if(scanf("%s",temp.name)==EOF) return; 413 printf("\t\t查询结果:\n"); 414 while(p2!=NULL) 415 { 416 if(strcmp(p2->name,temp.name)==0) 417 printf("\t\t%d %s %s %s\n\t\t",p2->number,p2->name,p2->faculty,p2->classes); 418 p2=p2->link; 419 } 420 p2=head; 421 getchar(); 422 printf("\n\n\n\t\t输入要查找学生姓名(EOF 结束):\n\t\t"); 423 }while(1); 424 } 425 void search_faculty(STU * head) 426 { 427 STU temp, *p2=head; 428 system("cls"); 429 430 printf("\n\n\n\t\t输入要查找学生系别(EOF 结束):\n\t\t"); 431 do 432 { 433 if(scanf("%s",temp.faculty)==EOF) return; 434 printf("\t\t查询结果:\n"); 435 while(p2!=NULL) 436 { 437 if(strcmp(p2->faculty,temp.faculty)==0) 438 printf("\t\t%d %s %s %s\n\t\t",p2->number,p2->name,p2->faculty,p2->classes); 439 p2=p2->link; 440 } 441 p2=head; 442 getchar(); 443 printf("\n\n\n\t\t输入要查找学生系别(EOF 结束):\n\t\t"); 444 }while(1); 445 } 446 void search_classes(STU * head) 447 { 448 STU temp, *p2=head; 449 system("cls"); 450 451 printf("\n\n\n\t\t输入要查找学生班级(EOF 结束):\n\t\t"); 452 do 453 { 454 if(scanf("%s",temp.classes)==EOF) return; 455 printf("\t\t查询结果:\n"); 456 while(p2!=NULL) 457 { 458 if(strcmp(p2->classes,temp.classes)==0) 459 printf("\t\t%d %s %s %s\n\t\t",p2->number,p2->name,p2->faculty,p2->classes); 460 p2=p2->link; 461 } 462 p2=head; 463 getchar(); 464 printf("\n\n\n\t\t输入要查找学生班级(EOF 结束):\n\t\t"); 465 }while(1); 466 } 467 468 void sort() 469 { 470 STU * head, *p1, *p2; 471 STU temp; 472 473 head=CreateLink(); 474 475 system("cls"); 476 printf("\n\n\n\t\t按照学号排序(正序):\n"); //常排序 477 for(p1=head;p1!=NULL;p1=p1->link) 478 for(p2=p1->link;p2!=NULL;p2=p2->link) 479 if(p1->number>p2->number) 480 { 481 temp.number=p1->number; 482 strcpy(temp.name,p1->name); 483 strcpy(temp.faculty,p1->faculty); 484 strcpy(temp.classes,p1->classes); 485 p1->number=p2->number; 486 strcpy(p1->name,p2->name); 487 strcpy(p1->faculty,p2->faculty); 488 strcpy(p1->classes,p2->classes); 489 p2->number=temp.number; 490 strcpy(p2->name,temp.name); 491 strcpy(p2->faculty,temp.faculty); 492 strcpy(p2->classes,temp.classes); 493 } 494 495 WriteLink(head); 496 497 printf("\t\t排序完毕,输出排序结果:"); 498 show(); 499 free(head); 500 } 501 502 void show() 503 { 504 STU * head, *p2; 505 506 head=CreateLink(); 507 508 printf("\n\n\n\t\t学号 姓名 系别 班级:\n"); 509 p2=head; 510 while(p2!=NULL) 511 { 512 printf("\t\t%d %s %s %s\n",p2->number,p2->name,p2->faculty,p2->classes); 513 p2=p2->link; 514 } 515 getchar(); 516 } 517 518 void edit() 519 { 520 STU * head, *p2; 521 STU temp; 522 head=CreateLink(); 523 p2=head; 524 525 system("cls"); 526 do 527 { 528 printf("\n\n\n\t\t输入要查找学生学号(-1 结束):\n"); 529 scanf("%d",&temp.number); 530 getchar(); 531 while(p2!=NULL) 532 { 533 if(p2->number==temp.number) 534 { 535 printf("请输入修改后的内容:姓名、系别、班级:\n"); 536 scanf("%s %s %s",p2->name,p2->faculty,p2->classes); 537 } 538 p2=p2->link; 539 } 540 }while(temp.number!=-1); 541 542 WriteLink(head); 543 free(head); 544 }