首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

不能按照目的执行,错在那儿?该如何处理

2012-02-16 
不能按照目的执行,错在那儿?3、建立一个链表,每个结点包括学号、姓名及英语、计算机、数学三门课程的成绩、平均

不能按照目的执行,错在那儿?
3、建立一个链表,每个结点包括学号、姓名及英语、计算机、数学三门课程的成绩、平均成绩。除平均成绩外,各项均由键盘输入。

要求:

        (A)计算平均成绩。

        (B)要实现插入功能。

(C)要有排序功能。

(D)要有存取功能。


#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
FILE*fp;
struct   student
{   char   num[10];
    char   name[20];
    int   score[3];
    float   ave;
    struct   student   *next;
};
main()
{   int   i,sum=0,a;
    char   m;
    struct   student   *head,*p,*pr,*temp;
    head=NULL;
    do
    {   printf( "Do   you   want   to   input   new   student 's   information?(Y/N)\n ");
        scanf( "%c ",&m);
        if(m== 'N '||m== 'n ')   break;
        p=(struct   student   *)malloc(sizeof(struct   student));
        if(p==NULL)
            {   printf( "No   enough   memory   to   alloc!\n ");
              exit(0);
            }
        if(head==NULL)
  {   printf( "It 's   the   first!\n ");
      head=p;
                      p-> next=NULL;
                  }
        else{   pr=head;
    for(i=1;pr-> next!=NULL;i++)
                            pr=pr-> next;
                    printf( "Now   has   %d   nodes!\n ",i);
    do{   printf( "Please   input   insert   node 's   number(> 0)!\n ");
            scanf( "%d ",&a);
        }while(a <=0||a> i+1);
    if(a==1)
                        {   p-> next=head;
                            head=p;
                        }
                    else{   pr=head;
                for(i=1;a-1!=i;i++)
                pr=pr-> next;
                                p-> next=pr-> next;
                                pr-> next=p;
                            }
                  }
          printf( "Please   enter   the   new   student 's   information!\n ");
                  scanf( "%s ",p-> num);


                  scanf( "%s ",p-> name);
  for(i=0;i <3;i++)
      {   scanf( "%d ",p-> score[i]);
  sum=sum+p-> score[i];
                      }
                  p-> ave=sum/3.0;

    }while(m== 'Y '||m== 'y ');

        printf( "Do   you   need   make   order?(Y/N)\n ");
  scanf( "%c ",&m);
          if(m== 'Y '||m== 'y '){
          if(head==NULL)
                {   printf( "No   information!\n ");

                }
          for(pr=head;pr-> next!=NULL;pr=pr-> next)
for(p=pr-> next;p-> next!=NULL;p=p-> next)
    {     if(pr-> ave <p-> ave)
                          {     *temp=*pr;
                *pr=*p;
  *p=*temp;
          }
                    }
          if(pr-> next==NULL)
          printf( "Make   order   finish!\n ");

      }

        printf( "Do   you   want   to   write   to   a   file!\n ");
              scanf( "%c ",&m);
          if(m== 'Y '||m== 'y '){
  fp=fopen( "zhan.txt ", "w+ ");
  if(fp==NULL)
      {   printf( "   File   open   error! ");
          exit(0);
      }
          for(p=head;p-> next!=NULL;p=p-> next)
  {     fwrite(p,sizeof(struct   student),1,fp);
        printf( "%s,%s,%d,%d,%d,%f\n ",p-> num,p-> name,p-> score[0],p-> score[1],p-> score[2],p-> ave);
  }
          if(!fclose(fp))
                printf( "File   close   OK!\n ");

      }
}


[解决办法]
还是用map吧!多简单
[解决办法]
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
FILE*fp;
struct student
{ char num[10];
char name[20];
int score[3];
float ave;
struct student *next;
};
main()
{ int i,sum=0,a;
char m;
struct student *head,*p,*pr,*temp;
head=NULL;
do
{ printf( "Do you want to input new student 's information?(Y/N)\n ");
scanf( "%c ",&m);
if(m== 'N '||m== 'n ') break;
p=(struct student *)malloc(sizeof(struct student));
if(p==NULL)
{ printf( "No enough memory to alloc!\n ");


exit(0);
}
if(head==NULL)
{ printf( "It 's the first!\n ");
head=p;
p-> next=NULL;
}
else{ pr=head;
for(i=1;pr-> next!=NULL;i++)
pr=pr-> next;
printf( "Now has %d nodes!\n ",i);
do{ printf( "Please input insert node 's number(> 0)!\n ");
scanf( "%d ",&a);
}while(a <=0||a> i+1);
if(a==1)
{ p-> next=head;
head=p;
}
else{ pr=head;
for(i=1;a-1!=i;i++)
pr=pr-> next;
p-> next=pr-> next;
pr-> next=p;
}
}
printf( "Please enter the new student 's information!\n ");
scanf( "%s ",p-> num);
scanf( "%s ",p-> name);
for(i=0;i <3;i++)
{ scanf( "%d ",p-> score[i]); //这里改为scanf( "%d ",&p-> score[i]);
sum=sum+p-> score[i];
}
p-> ave=sum/3.0;

}while(m== 'Y '||m== 'y ');

printf( "Do you need make order?(Y/N)\n ");
scanf( "%c ",&m);
if(m== 'Y '||m== 'y '){
if(head==NULL)
{ printf( "No information!\n ");

}
for(pr=head;pr-> next!=NULL;pr=pr-> next)
for(p=pr-> next;p-> next!=NULL;p=p-> next)
{ if(pr-> ave <p-> ave)
{ *temp=*pr;
*pr=*p;
*p=*temp;
}
}
if(pr-> next==NULL)
printf( "Make order finish!\n ");

}

printf( "Do you want to write to a file!\n ");
scanf( "%c ",&m);
if(m== 'Y '||m== 'y '){
fp=fopen( "zhan.txt ", "w+ ");
if(fp==NULL)
{ printf( " File open error! ");
exit(0);
}
for(p=head;p-> next!=NULL;p=p-> next)
{ fwrite(p,sizeof(struct student),1,fp);
printf( "%s,%s,%d,%d,%d,%f\n ",p-> num,p-> name,p-> score[0],p-> score[1],p-> score[2],p-> ave);
}
if(!fclose(fp))
printf( "File close OK!\n ");

}
}

热点排行