| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
 本帖最后由 体弱多病程序权 于 2019-3-22 00:06 编辑  
 
- #include <stdio.h>
 
 - #include <stdlib.h>
 
 - #include <string.h>
 
  
- struct Student *head = NULL;
 
  
- int count = 1;
 
  
- struct Student
 
 - {
 
 -         int number;
 
 -         char *name[4];
 
 -         float score;
 
 -         struct Student *next;
 
 - };
 
  
- void create(struct Student **head);
 
 - void print(struct Student *head);
 
 - void change(struct Student *head);
 
 - void releaseStorager(struct Student **head);
 
  
- int main(void){
 
 -         char menu[][40] = 
 
 -         {
 
 -         "\t****************************\n",        
 
 -         "\t\t学生成绩管理系\n",
 
 -         "\t\t1.录入学生成绩\n", 
 
 -         "\t\t2.打印学生成绩\n", 
 
 -         "\t\t3.修改学生成绩\n", 
 
 -         "\t****************************\n",
 
 -         };
 
 -         while(1)
 
 -         {
 
 -                 system("cls");
 
 -                 for(int i = 0;i<sizeof(menu)/sizeof(menu[0]);i++)
 
 -                 {                
 
 -                           printf("%s",menu[i]);
 
 -                 }
 
 -                 char input;
 
 -                 fflush(stdin); 
 
 -                 input = getchar();
 
 -                 switch(input)
 
 -                 {
 
 -                         case '1':create(&head);break;
 
 -                         case '2':print(head);break;
 
 -                         case '3':change(head);break;        
 
 -                         case '4':exit(1);
 
 -                 }
 
 -         }
 
 - }
 
  
- void create(struct Student **head){
 
 -         system("cls");
 
 -         struct Student *p1,*p2;
 
 -         p1 = p2 = (struct Student *)malloc(sizeof(struct Student));
 
 -         if(p1 == NULL)
 
 -         {
 
 -                 printf("分配内存失败了.....\n");
 
 -                 exit(1); 
 
 -         }
 
 -         do{
 
 -                 printf("第%d个学生\n",count); 
 
 -                 printf("请输入学生的学号:");
 
 -                 scanf("%d",&p1->number);
 
 -                 printf("请输入学生的姓名:");
 
 -                 scanf("%s",&p1->name);
 
 -                 printf("请输入学生的成绩:");
 
 -                 scanf("%f",&p1->score);
 
 -                 system("cls");
 
 -                 if(*head != NULL)
 
 -                 {
 
 -                         p2->next = p1;
 
 -                         p1->next = NULL;
 
 -                 }
 
 -                 else 
 
 -                 {
 
 -                         *head = p1;
 
 -                         p1->next = NULL;
 
 -                 }
 
 -                 count++;
 
 -                 p2 = p1;
 
 -                 p1 = (struct Student *)malloc(sizeof(struct Student));
 
 -         }while(count != 3);
 
 -         p2->next = NULL;
 
 - }
 
  
- void print(struct Student *head)
 
 - {
 
 -                 system("cls");
 
 -                 printf("学生成绩如下:\n");
 
 -                 while(head != NULL)
 
 -                 {
 
 -                         printf("%d %s %f\n",head->number,head->name,head->score); 
 
 -                         head = head->next;
 
 -                 }
 
 -                 system("pause");
 
 - }
 
  
- void change(struct Student *head){
 
 -         system("cls");
 
 -         int temp = 0;
 
 -         printf("请输入要修改的学生学号:");
 
 -         scanf("%d",&temp);
 
 -         struct Student *p1,*p2;
 
 -         p1 = p2 =  (struct Student *)malloc(sizeof(struct Student));
 
 -         p1 = head;
 
 -         while(1){
 
 -                 if( p1 == NULL )
 
 -                 {
 
 -                         printf("This list is null!\n");
 
 -                         system("pause");
 
 -             break;
 
 -                 }
 
 -                 else if(p1->number == temp ) 
 
 -                 {
 
 -                         printf("查询该学生信息如下:\n"); 
 
 -                         printf("%d %s %f",p1->number,p1->name,p1->score);
 
 -                         printf("\n请重新输入该学生的信息:");
 
 -                         scanf("%d %s %f",&p2->number,&p2->name,&p2->score);
 
 -                         p1 = p2; 
 
 -                         printf("Change ID:%d succeed!",temp);
 
 -                         system("pause");
 
 -                         break;
 
 -                 }
 
 -                 else if(p1->number != temp && p1->next == NULL)
 
 -                 {
 
 -                         printf("\n您输入的学号有误,请核对后重新输入.");
 
 -                         system("pause");                        
 
 -                         break;
 
 -                 }
 
 -                 p1 = p1->next;
 
 -         }
 
 -         printf("修改后的成绩如下:\n");
 
 -         print(head); 
 
 - }
 
  
- void releaseStorager(struct Student **head){
 
  
 
- }
 
 -  
 
  复制代码 
 
录入和打印功能正常,就是修改功能出问题了,如图,程序直接报错了...... 
希望能人相助,帮助迷途的羊羔....
 本帖最后由 jackz007 于 2019-3-22 09:58 编辑 
    楼主的代码逻辑太混乱了,实在看不下去,帮你重写了代码,但基本维持原来的框架:
 - #include <stdio.h>
 
 - #include <stdlib.h>
 
 - #include <string.h>
 
  
- typedef struct Student {
 
 -         int number            ;
 
 -         char name[20]         ;
 
 -         float score           ;
 
 -         struct Student * next ;
 
 - } node                        ;
 
  
- node * head = NULL            ;
 
 - int count = 0                 ;
 
  
- bool find(node ** px , node * head , const int number)   // 寻找学号为 number 节点的前一个节点 
 
 - {
 
 -         node * p1 , * p2                        ;
 
 -         bool ret                                ;
 
 -         
 
 -         ret = false                             ;
 
 -         if(count > 0) {
 
 -                 p1 = p2 = head                  ;
 
 -                 while(p2 != NULL) {
 
 -                         if (p2 -> number == number) {
 
 -                                 * px = p1       ;        // 返回指向目标节点前一个节点的指针
 
 -                                 ret = true      ;
 
 -                                 break           ;
 
 -                         } else {
 
 -                                 p1 = p2         ;
 
 -                                 p2 = p1 -> next ;                        
 
 -                         } 
 
 -                 }
 
 -         }
 
 -         return ret                              ;        // 返回布尔变量指示是否找到目标节点
 
 - }
 
  
- void print(node * head)
 
 - {
 
 -         node * p                                                                   ;
 
 -         system("cls")                                                              ;
 
 -         if(count > 0) {
 
 -                 printf("学生成绩如下:\n")                                          ;
 
 -                 p = head                                                           ;
 
 -                 while(p != NULL) {
 
 -                         printf("%d %s %f\n", p -> number , p -> name , p -> score) ; 
 
 -                         p = p -> next                                              ;
 
 -                 }
 
 -         } else {
 
 -                 printf("链表为空!\n")                                             ;
 
 -         }
 
 -         system("pause")                                                            ;
 
 - }
 
  
- node * create(node * hd)
 
 - {
 
 -         node * p1 , * p2 , * px , * head                                      ;
 
 -         int number                                                            ;
 
 -         float score                                                           ;
 
 -         char name[20]                                                         ;
 
  
-         system("cls")                                                         ;
 
 -         p1 = head = hd                                                        ;
 
 -         if(p1 != NULL) {
 
 -                 while(p1 -> next != NULL) p1 = p1 -> next                     ; // p1 指向当前链表的最后一个节点
 
 -         } else {
 
 -                 p1 = p2 = head = NULL                                         ;
 
 -         }
 
 -         for(;;) {
 
 -                 printf("第 %d 个学生\n" , count + 1)                          ;  
 
 -                 printf("请输入学生的学号:")                                   ;
 
 -                 scanf("%d" , & number)                                        ;
 
 -                 if (! number) break                                           ;
 
 -                 if (! find(& px , head , number)) {
 
 -                         printf("请输入学生的姓名:")                           ;
 
 -                         scanf("%s" , name)                                    ;
 
 -                         printf("请输入学生的成绩:")                           ;
 
 -                         scanf("%f" , & score)                                 ;
 
 -                         system("cls")                                         ;
 
 -                         if ((p2 = (node *)malloc(sizeof(node))) != NULL) {
 
 -                                 p2 -> number = number                         ;
 
 -                                 strcpy(p2 -> name , name)                     ;
 
 -                                 p2 -> score = score                           ;
 
 -                                 p2 -> next = NULL                             ;
 
 -                                 if(! count) head = p2                         ;
 
 -                                 else p1 -> next = p2                          ;
 
 -                                 p1 = p2                                       ;
 
 -                                 count ++                                      ;  
 
 -                         } else {
 
 -                                 fprintf(stderr , "malloc() 分配内存失败")     ;
 
 -                                 break                                         ;
 
 -                         }
 
 -                 } else {
 
 -                         printf("抱歉,学号为 %d 的学生已经存在!\n" , number) ;
 
 -                 }
 
 -         }
 
 -         return head                                                           ;
 
 - }
 
  
- void change(node * head)
 
 - {
 
 -         node * p1 , * p2 , * px                                                      ;
 
 -         int temp                                                                     ;
 
  
-         system("cls")                                                                ;
 
 -         if(count > 0) { 
 
 -                 printf("请输入要修改的学生学号 : ")                                  ;
 
 -                 scanf("%d", & temp)                                                  ;
 
 -                 if (find(& p1 , head , temp)) {
 
 -                         if(p1 != head) p2 = p1 -> next                               ; // 如果找到的节点不是首节点 head
 
 -                         else p2 = p1                                                 ; // 否则,p1、p2 都指向 head
 
 -                         printf("查询该学生信息如下:\n")                              ; 
 
 -                         printf("%d %s %f" , p2 -> number , p2 -> name , p2 -> score) ;
 
 -                         printf("\n请重新输入该学生的信息:")                          ; // 学号就不可以修改了吧。
 
 -                         scanf("%s %f",  p2 -> name , & p2 -> score)                  ;
 
 -                         printf("Change ID:%d succeed!" , temp)                       ;
 
 -                         system("pause")                                              ;
 
 -                 } else {
 
 -                         printf("抱歉:没有查询到学号为 %d 的学生\n" , temp)          ;
 
 -                 }
 
 -                 printf("修改后的成绩如下:\n")                                        ;
 
 -                 print(head)                                                          ;
 
 -         } else {
 
 -                 printf("链表为空!\n")                                               ;
 
 -         } 
 
 - }
 
  
- int main(void)
 
 - {
 
 -         char menu[][40] = {
 
 -         "\t****************************\n",        
 
 -         "\t\t学生成绩管理系\n",
 
 -         "\t\t1.录入学生成绩\n", 
 
 -         "\t\t2.打印学生成绩\n", 
 
 -         "\t\t3.修改学生成绩\n", 
 
 -         "\t****************************\n",
 
 -         }                                                                                     ;
 
 -         node * head                                                                           ;
 
 -         char input                                                                            ;
 
 -         int i                                                                                 ;
 
 -         bool f                                                                                ;
 
 -         f = true                                                                              ;
 
 -         count = 0                                                                             ;
 
 -         head = NULL                                                                           ;
 
 -         while(f) {
 
 -                 system("cls")                                                                 ;
 
 -                 for(i = 0 ; i < sizeof(menu) / sizeof(menu[0]) ; i ++) printf("%s" , menu[i]) ;
 
 -                 fflush(stdin)                                                                 ; 
 
 -                 input = getchar()                                                             ;
 
 -                 switch(input) {
 
 -                         case '1': head = create(head) ; break                                 ;
 
 -                         case '2': print(head)         ; break                                 ;
 
 -                         case '3': change(head)        ; break                                 ;        
 
 -                         case '4': f = false           ;                                       ;
 
 -                 }
 
 -         }
 
 - }
 
 
  复制代码 
 
 
 |   
- 
 
 
 
 
 
 
 
 |