1 #include "students.h" 2 3 void search(stu *top) 4 { 5 char choose[2],input[30]; 6 int cn =0; 7 int flag; 8 char *c[5]={"学号","姓名","性别","学院","专业班级"}; 9 char *d[4]={"外语","计算机","数学","总分"}; 10 11 stu *old, *star; 12 for( ; ; ) 13 { 14 flag = 1; 15 if(count == 0) 16 { 17 printf("记录为空表,退出查找操作。\n"); 18 return; 19 } 20 printf("1. 通过学号查找\n"); 21 printf("2. 通过姓名查找\n"); 22 printf("3. 退出查找操作\n"); 23 printf("选择1-3:"); 24 gets(choose); 25 while(1) 26 { 27 cn = atoi(choose); 28 if(cn<1 || cn>3) 29 { 30 printf("输入错误,重选1-3:"); 31 gets(choose); 32 } 33 else 34 break; 35 } 36 switch(cn) 37 { 38 case 1: 39 printf("请输入学号:"); 40 break; 41 case 2: 42 printf("请输入姓名:"); 43 break; 44 case 3: 45 printf("退出查找操作\n"); 46 return; 47 } 48 gets(input); 49 50 old = star = top; 51 while(star != NULL) 52 { 53 if((cn == 1 && strcmp(input, star->num) == 0) || (cn == 2 && strcmp(input, star->name)) == 0) 54 { 55 printf("已找到所查找的学生信息!\n"); 56 printf("\n%10s\t%8s\t%4s\t%8s\t%12s\n",c[0],c[1],c[2],c[3],c[4]); 57 printf("\n%10s\t%8s\t%4s\t%8s\t%12s\n", star->num, star->name, star->gender, star->dept, star->Class); 58 printf("\n%10s\t%8s\t%4s\t%8s\n",d[0],d[1],d[2],d[3]); 59 printf("\n%10.2f\t%8.2f\t%4.2f\t%8.2f\n", star->gEngl, star->gComp, star->gMath, star->gSum); 60 flag = 0; 61 break; 62 } 63 else 64 { 65 old = star; 66 star = star->next; 67 } 68 } 69 if(flag != 0) 70 { 71 printf("没有找到该学生\n"); 72 flag = 1; 73 } 74 continue; 75 } 76 }
我随便写了2组数据进去:
接着神奇的事情发生了:
我使用学号查找的话,无论输入什么(即使没有这个信息)都会输出学号2的信息,但是用姓名查找却完全没有问题。
if((cn == 1 && strcmp(input, star->num) == 0) || (cn == 2 && strcmp(input, star->name)) == 0)
这句话这里打个断点,看看究竟怎么回事,就知道答案了。