如何根據 一定條件 設定 計算的 行數(如何根據手機號碼查到個人信息)
756
2025-04-02
這是一年前寫的筆記,那個時候還沒有用博客記錄學習的點滴,然后就用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"); } } 第二種程序: 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小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。