简单的成绩录入系统程序及分析以及思考

这是一年前写的笔记,那个时候还没有用博客记录学习的点滴,然后就用word写了这个文档。为了防止丢失,以及自己回忆下,就贴出来吧。


本程序是在教材的基础之上找到的疑惑部分,程序是基础教材的思想自己编写的,关于使用函数模块来使程序更加的简洁且便于维护都是自己的对知识的运用实践,本程序更能体现对指针的理解,对于二维数组指针的使用是一个很好的练习。

本程序的功能是对以一个班,3个学生,4门课为例进行 实现一系列的基本操作,例如录入所有学生各科成绩、求所有学生总成绩的平均值、输出指定学生的所有科目成绩、输出成绩不及格学生的各科成绩等,分别运用函数模块实现,使得程序清晰,利于理解。

第一种是成绩录入的程序在main主函数中直接完成,第二种是将成绩录入部分用一个函数inputscore来完成,之后在主函数main当中调用此函数。

第一种程序:

#include<stdio.h>

int main()

{

      /* function declaration  */

         void average(float *p,int n);             //n represents the sum of the numble of all subjects

      void search(float (*p)[4],int n);         //n represents No.n student

      void search_fail(float (*p)[4],int n);    //n represents the numble of students

      float score[3][4];

      /* input the scores */

      float (*point)[4]=score;

      int i,j;

       printf("Please input each student's scores\n");

      for(i=0;i<3;i++)

      {

           printf("Please input No.%d student's scores:",i+1);

           for(j=0;j<4;j++)

           {

                 scanf("%f",*(point+i)+j);

           }

           printf("\n");

      }

      /* function call part */

      average(*score,12);                       //average(score[0],12);

      search(score,2);                          //output the scores of No.2 student

      search_fail(score,3);                     //output the scores of students who fail to pass the examination

      return 0;

 

}

   /* function defenition */

void average(float *p,int n)

{

      float *(p_end)=p+n,sum=0,aver;

      for(;p<p_end;p++)

           sum+=*p;

      aver=sum/n;

      printf("Average=%5.2f\n",aver);

}

void search(float (*p)[4],int n)

{

      int i;

      printf("The scores of No.%d student:\n",n);

      for(i=0;i<4;i++)

           printf("%5.2f ",*(*(p+n-1)+i));

      printf("\n");

}

void search_fail(float (*p)[4],int n)

{

      int i,j,flag;

      for(i=0;i<3;i++)

      {

           flag=0;

           for(j=0;j<4;j++)

           {

                 if(*(*(p+i)+j)<60) flag=1;

           }

           if(flag==1)

           {

                 printf("No.%d students fails, her/his scores are \n",i+1);

                 for(j=0;j<4;j++)

                      printf("%5.2f ",*(*(p+i)+j));

           }

           printf("\n");

      }

}

 

第二种程序:

#include<stdio.h>

int main()

{

      /* function declaration  */

    void inputscore(float (*p)[4],int n);     //n represents the numble of students

      void average(float *p,int n);             //n represents the sum of the numble of all subjects

      void search(float (*p)[4],int n);         //n represents No.n student

      void search_fail(float (*p)[4],int n);    //n represents the numble of students

      float score[3][4];

      /* input the scores */

      /* function call part */

      inputscore(score,3);                      //input the scores of all students

      average(*score,12);                       //average(score[0],12);

      search(score,2);                          //output the scores of No.2 student

      search_fail(score,3);                     //output the scores of students who fail to pass the examination

      return 0;

 

}

   /* function defenition */

void inputscore(float (*point)[4],int n)

{

      int i,j;

       printf("Please input each student's scores\n");

 

      for(i=0;i<n;i++)

      {

           printf("Please input No.%d student's scores:",i+1);

           for(j=0;j<4;j++)

           {

                 scanf("%f",*(point+i)+j);

           }

           printf("\n");

      }

     

          

}

void average(float *p,int n)

{

      float *(p_end)=p+n,sum=0,aver;

      for(;p<p_end;p++)

           sum+=*p;

      aver=sum/n;

      printf("Average=%5.2f\n",aver);

}

void search(float (*p)[4],int n)

{

      int i;

      printf("The scores of No.%d student:\n",n);

      for(i=0;i<4;i++)

           printf("%5.2f ",*(*(p+n-1)+i));

      printf("\n");

}

void search_fail(float (*p)[4],int n)

{

      int i,j,flag;

      for(i=0;i<3;i++)

      {

           flag=0;

           for(j=0;j<4;j++)

           {

                 if(*(*(p+i)+j)<60) flag=1;

           }

           if(flag==1)

           {

                 printf("No.%d students fails, her/his scores are \n",i+1);

                 for(j=0;j<4;j++)

                      printf("%5.2f ",*(*(p+i)+j));

           }

           printf("\n");

      }

}

红色字体部分为有点疑问的地方,为什么红色部分上下两条语句不能调换,这个不经意的问题花费了我不少时间,原来的程序是printf在前,int i,j;在后,但程序总是编译出错,费了好大功能才改成现在的程序那样,正常编译,链接,执行。

其次,定义行指针时,最好是在定义时候就赋值,例如:float  (*p)[4]=score;就是将二维数组的序号为0的行赋值给行指针变量p;当然也可以先定义,等所有定义完成后,在赋值。注意是所有定义部分完成后在赋值,不能定义一条,接着下个语句赋值,其次在定义其他部分,这样会出现错误。例如,下面程序是会在编译时出现错误的:

#include<stdio.h>

int main()

{

/********************************************************************************************************************************

      The aim of the program is to get the average of all stuents'grades, and to output the grades of all subjects of a student, and

      finally to output the grades of the students whoes some grades below 60,that is he or she fails to pass the examination.

********************************************************************************************************************************/

      void average(float *p,int n);                  //function declaration, and n represents the numble of all subjects of all students

      void search(float (*p)[4],int n);              //function declaration; output the No.n student's grades, and n represents NO.n

      void search_fail(float (*p)[4],int n);         //function declaration; output the student's grades who fails to pass the exam

      float score[3][4];                             //3 students and 4 subjects

      float (*point)[4];

       point=score;

       int i,j;

     

      printf("please input the student's scores:\n");

      for(i=0;i<3;i++)

      {

           printf("the No.%d student's scores: ",i+1);

           for(j=0;j<4;j++)

           scanf("%f",*(point+i)+j);

           printf("\n");

 

      }

      average(*score,12);                            //function call

      search(score,3);                               //function call; output the third student's scores of each subject

      search_fail(score,4);                          //function call

           return 0;

}

void average(float *p,int n)                       //function definitation

{

      float sum=0,aver,*p_end=p+n;

      for(;p<p_end;p++)

           sum+=*p;

      aver=sum/n;

      printf("average=%5.2f\n",aver);

}

void search(float (*p)[4],int n)

{

      int i;

      printf("the scores of No.%d are\n",n);

      for(i=0;i<4;i++)

           printf("%5.2f ",*(*(p+n-1)+i));

      printf("\n");

}

void search_fail(float (*p)[4],int n)

{

      int i,j,flag;

      for(i=0;i<3;i++)

      {

           flag=0;

           for(j=0;j<4;j++)

                 if(*(*(p+i)+j)<60)

                      flag=1;

                 if(flag==1)

                 {

                      printf("No.%d fails, his or her scores are\n",i+1);

                      for(j=0;j<4;j++)

                            printf("%5.2f ",*(*(p+i)+j));

                      printf("\n");

                 }

 

      }

}

编译:

E:\Visual c++\exercise_1\eg-10_input system\workspace_majorization9\project_majorization9\majorization9.c(14) : error C2143: syntax error : missing ';' before 'type'

错误之处就在红色部分。下面给出正确程序:

#include<stdio.h>

int main()

{

/********************************************************************************************************************************

      The aim of the program is to get the average of all stuents'grades, and to output the grades of all subjects of a student, and

      finally to output the grades of the students whoes some grades below 60,that is he or she fails to pass the examination.

********************************************************************************************************************************/

      void average(float *p,int n);                  //function declaration, and n represents the numble of all subjects of all students

      void search(float (*p)[4],int n);              //function declaration; output the No.n student's grades, and n represents NO.n

      void search_fail(float (*p)[4],int n);         //function declaration; output the student's grades who fails to pass the exam

      float score[3][4];                             //3 students and 4 subjects

      float (*point)[4];

      int i,j;

      point=score;

      printf("please input the student's scores:\n");

      for(i=0;i<3;i++)

      {

           printf("the No.%d student's scores: ",i+1);

           for(j=0;j<4;j++)

           scanf("%f",*(point+i)+j);

           printf("\n");

 

      }

      average(*score,12);                            //function call

      search(score,3);                               //function call; output the third student's scores of each subject

      search_fail(score,4);                          //function call

           return 0;

}

void average(float *p,int n)                       //function definitation

{

      float sum=0,aver,*p_end=p+n;

      for(;p<p_end;p++)

           sum+=*p;

      aver=sum/n;

      printf("average=%5.2f\n",aver);

}

void search(float (*p)[4],int n)

{

      int i;

      printf("the scores of No.%d are\n",n);

      for(i=0;i<4;i++)

           printf("%5.2f ",*(*(p+n-1)+i));

      printf("\n");

}

void search_fail(float (*p)[4],int n)

{

      int i,j,flag;

      for(i=0;i<3;i++)

      {

           flag=0;

           for(j=0;j<4;j++)

                 if(*(*(p+i)+j)<60)

                      flag=1;

                 if(flag==1)

                 {

                      printf("No.%d fails, his or her scores are\n",i+1);

                      for(j=0;j<4;j++)

                            printf("%5.2f ",*(*(p+i)+j));

                      printf("\n");

                 }

 

      }

}

 

除此之外,我们可以很容易的发现,第二个程序是第一个的优化,使用函数来完成主函数需要的每一个功能,很整洁,便于阅读与维护。

总结:使用文档以及笔记记录自己对于程序的理解以及疑惑,并且不断实践演练,这是一个不错的习惯,个人一定会坚持下去。为了下一个目标积蓄力量。目标:编写一个应用程序以及各行业的管理系统软件。优化是使其更加的人性化,实用性强。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李锐博恩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值