标题:求助,未完善的程序,谁知道为什么调用fun 1,fun2,fun3时会停止运行?望指点 ...
只看楼主
duansheng00
Rank: 1
等 级:新手上路
帖 子:5
专家分:2
注 册:2012-1-24
结帖率:0
已结贴  问题点数:5 回复次数:2 
求助,未完善的程序,谁知道为什么调用fun 1,fun2,fun3时会停止运行?望指点
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define LEN sizeof(struct student)
int n=0;
struct student
{ long int num;
  char name[20];
  float chinese,math,english,average;
  int sum;
  struct student *next;  
};
main()
{  
   struct student *head,*stud;
   struct student *fun1(void);
   struct student *fun2(struct student *head,long num);
   struct student *fun3(struct student *head,long num);
   struct student *fun4(struct student *head,struct student *stu);
   int fun5(void);
   
  
   char a='1' ;
   printf("\n         **********************  学生成绩管理系统  **********************   \n\n\n");
   printf("\t\t请选择功能: 1.初始化\t 2.学生删除\n");
   printf("\t\t            3.学生查找\t 4.学生添加\n");
   printf("\t\t            5.学生总数\t 0.退出系统\n\n\n");
   printf("********************************************************************************\n\n");
   while(a!='0')
  {
   a=getchar();
    switch(a)
    { case '1':fun1();break;
      case '2':{long m;printf("请输入要删除的学号:");scanf("%ld",&m);head=fun2(head,m);};break;
      case '3':{long h;printf("请输入要查找学生的学号:");scanf("%ld",&h);fun3(head,h);};break;
      case '4':{printf("请依次输入要插入学生的学号,姓名,语文成绩,数学成绩,英语成绩:\n");scanf("%ld",&stud->num);scanf("%s",&stud->name);scanf("%d%d%d",&stud->chinese,&stud->math,&stud->english);head=fun4(head,stud);};break;
      case '5':fun5();break;
      
    }
   }
return 0;
}





/*调用的函数1*/
struct student *fun1(void)
{ struct student *head;
  struct student *p1,*p2;
  p1=p2=(struct student*)malloc(LEN);
  printf("请依次输入学生学号,姓名,语文成绩,数学成绩,英语成绩\n");
  scanf("%ld",&p1->num);
  scanf("%s",p1->name);
  scanf("%d%d%d",&p1->chinese,&p1->math,&p1->english);
  head=NULL;
  p1->sum=p1->chinese+p1->math+p1->english;
  p1->average=p1->sum/3;
  while(1)
  {n=n+1;
  if(n==1)
   head=p1;
  else
   p2->next=p1;
  p2=p1;
  p1=(struct student*)malloc(LEN);
  printf("请依次输入学生学号,姓名,语文成绩,数学成绩,英语成绩\n");
  scanf("%d",&p1->num);
  if(p1->num==0)
  {printf("           此操作已完成,请选择其他操作:\n\n\n");

   printf("\t\t            1.初始化\t 2.学生删除\n");
   printf("\t\t            3.学生查找\t 4.学生添加\n");
   printf("\t\t            5.学生总数\t 0.退出系统\n\n\n");
printf("********************************************************************************\n\n");
      break;}
  scanf("%s",p1->name);
  scanf("%d%d%d",&p1->chinese,&p1->math,&p1->english);
  }
  p2->next=NULL;
  return(head);
}





/*调用的函数2*/
struct student *fun2(struct student *head,long num)
{struct student *p1,*p2;
 if(head==NULL)
 {printf("\nlist null!\n");
 goto end;
 }
 p1=head;
 while(num!=p1->num&&p1->next!=NULL)
 {p2=p1;
  p1=p1->next;
 }
 if(num==p1->num)
 {if(p1==head)
 {
     head=p1->next;
     printf("%ld号已从系统删除\n",num);}

 else
 p2->next=p1->next;
 printf("%ld号已从系统删除\n",num);
 n=n-1;
 }
 else
 printf("%ld 号不在系统当中\n",num);
 end:
 return (head);
}





/*调用的函数5*/
int fun5()
{
printf("系统中共有%d个学生",n);
printf("           此操作已完成,请选择其他操作:\n\n\n");

   printf("\t\t            1.初始化\t 2.学生删除\n");
   printf("\t\t            3.学生查找\t 4.学生添加\n");
   printf("\t\t            5.学生总数\t 0.退出系统\n\n\n");
printf("********************************************************************************\n\n");
return 0;
}




/*调用的函数4*/
 struct student *fun4(struct student *head,struct student *stu)
 {
     struct student *p0,*p1,*p2;
     p1=head;
     p0=stu;
     if(head==NULL)
     {head=p0;p0->next=NULL;}
     else
     {
         while((p0->num>p1->num)&&(p1->num!=NULL))
         {p2=p1;
         p1=p1->next;}
         if(p0->num<=p1->num)
         {if(head==p1)head=p0;
         else p2->next=p0;
         p0->next=p1;}
         else
         {p1->next=p0;p0->next=NULL;}
     }
     n=n+1;
   printf("           此操作已完成,请选择其他操作:\n\n\n");

   printf("\t\t            1.初始化\t 2.学生删除\n");
   printf("\t\t            3.学生查找\t 4.学生添加\n");
   printf("\t\t            5.学生总数\t 0.退出系统\n\n\n");
printf("********************************************************************************\n\n");
     return(head);
 }




 /*调用的函数3*/
 struct student *fun3(struct student *head,long num)
 {
     struct student *p1,*p2;
     p1=head;
     if(p1==NULL)
         printf("系统中没有要查找的学生\n");
     else
     while(num!=p1->num&&p1->next==NULL)
     {p2=p1;p1=p1->next;}
     if(p1->next==NULL)
         printf("系统中没有要查找的学生\n");
     else
         printf("要查找的学生学号, 姓名,语文成绩,数学成绩,英语成绩依次是:%ld  %s  %d  %d  %d",p1->num,p1->name,p1->chinese,p1->math,p1->english);
         return 0;
 }


搜索更多相关主题的帖子: include average long head 
2012-06-13 00:47
如此悠然
Rank: 2
等 级:论坛游民
帖 子:62
专家分:86
注 册:2012-5-18
得分:3 
小手一抖,十分到手,手提酱油,低头猛走。。。。虽然我还没学到这里。。但是等我学会了会来帮你的,呵呵、楼猪、再等等吧。

——————
严谨
2012-06-13 07:06
_大浪淘沙_
Rank: 2
等 级:论坛游民
帖 子:4
专家分:16
注 册:2012-5-2
得分:3 
呵呵,跟着2楼走
2012-06-13 08:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-371427-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.033876 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved