如何根据 一定条件 设定 计算的 行数(如何根据手机号码查到个人信息)
720
2022-05-30
这是一年前写的笔记,那个时候还没有用博客记录学习的点滴,然后就用word写了这个文档。为了防止丢失,以及自己回忆下,就贴出来吧。
本程序是在教材的基础之上找到的疑惑部分,程序是基础教材的思想自己编写的,关于使用函数模块来使程序更加的简洁且便于维护都是自己的对知识的运用实践,本程序更能体现对指针的理解,对于二维数组指针的使用是一个很好的练习。
本程序的功能是对以一个班,3个学生,4门课为例进行 实现一系列的基本操作,例如录入所有学生各科成绩、求所有学生总成绩的平均值、输出指定学生的所有科目成绩、输出成绩不及格学生的各科成绩等,分别运用函数模块实现,使得程序清晰,利于理解。
第一种是成绩录入的程序在main主函数中直接完成,第二种是将成绩录入部分用一个函数inputscore来完成,之后在主函数main当中调用此函数。
第一种程序:
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
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
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
{
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
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
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
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
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
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");
}
}
}
除此之外,我们可以很容易的发现,第二个程序是第一个的优化,使用函数来完成主函数需要的每一个功能,很整洁,便于阅读与维护。
总结:使用文档以及笔记记录自己对于程序的理解以及疑惑,并且不断实践演练,这是一个不错的习惯,个人一定会坚持下去。为了下一个目标积蓄力量。目标:编写一个应用程序以及各行业的管理系统软件。优化是使其更加的人性化,实用性强。
数据结构
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。