作者kenboy99999 (Poppin~蛆~)
看板C_and_CPP
標題[問題] 有關函式指標
時間Sun Mar 21 22:35:28 2010
( *[1m *[m 為色碼,可以按 Ctrl+V 預覽會顯示的顏色 )
( 未必需要依照此格式,文章條理清楚即可 )
遇到的問題: (題意請描述清楚)
以下為程式碼
#include<stdio.h>
#include<stdlib.h>
#define STUDENT 3
#define EXAMS 4
void minimum(const int grade[][EXAMS], int pupils, int tests);
void maximum(const int grade[][EXAMS], int pupils, int tests);
void average(const int setOfGrades[], int tests);
void printArray(const int grade[][EXAMS], int pupils, int tests);
int main()
{
int student;
const int
studentGrades[STUDENT][EXAMS]={{77,68,86,73},{96,87,89,78},{70,90,86,81}};
void (*processGrades[4])(int)={minimum,maximum,average,printArray};<<這行有錯,不懂要怎麼改才可以
int choice;
printf(" Enter a choice :");
printf("\n 0 Print the array of grades\n");
printf(" 1 Find the minimum grade \n");
printf(" 2 Find the maximum grades\n");
printf(" 3 Print the average on all test for each student\n");
printf(" 4 End program\n");
scanf("%d",&choice);
system("pause");
printf("Enter:\n");
printArray(studentGrades, STUDENT, EXAMS);
printf("\n\nLowest grade: %d\nHighest grade: %d\n",
minimum(studentGrades, STUDENT, EXAMS),
maximum(studentGrades, STUDENT, EXAMS));
for(student = 0; student<STUDENT; student++){
printf("The average grade for student %d is %.2f\n",
student, average(studentGrades[student],EXAMS));
}
system("pause");
return 0;
}
void minimum(const int grade[][EXAMS], int pupils, int tests)
{
int i;
int j;
int lowGrade = 100;
for(i=0; i<pupils; i++){
for(j=0; j<tests; j++){
if(grade[i][j]<lowGrade){
lowGrade = grade[i][j];
}
}
}
}
void maximum(const int grade[][EXAMS], int pupils, int tests)
{
int i;
int j;
int highGrade = 0;
for(i=0; i<pupils; i++){
for(j=0; j<tests; j++){
if(grade[i][j]>highGrade){
highGrade = grade[i][j];
}
}
}
}
void average(const int setOfGrades[], int tests)
{
int i;
int total = 0;
for(i=0; i<tests; i++){
total += setOfGrades[i];
}
}
void printArray(const int grade[][EXAMS], int pupils, int tests)
{
int i;
int j;
printf(" [0] [1] [2] [3]");
for(i=0; i<pupils; i++){
printf("\nstudentGrades[%d]", i);
for(j=0; j<tests; j++){
printf("%-5d", grade[i][j]);
}
}
}
希望得到的正確結果:
程式跑出來的錯誤結果:
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
有問題的code: (請善用置底文標色功能)
補充說明:
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.42.163.225
推 QQ29:你function 傳入型態都不一樣 怎麼可用同一個type的fun ptr呢 03/21 22:54
→ kenboy99999:那有辦法一次傳入那麼多的東西嗎? 還是只能傳一個? 03/21 23:03
→ alongalone:你似乎有點誤會函式指標;你能理解錯誤的那行是要做啥嗎 03/21 23:07
→ kenboy99999:不太理解....似乎是要可以讓使用者輸入數字後,呼叫 03/21 23:13
→ kenboy99999:所選擇的函式 03/21 23:13
→ james732:先用google查詢函式指標 寫幾個簡單小範例弄懂它吧 03/21 23:16
推 QQ29:你都不知道函式指標了 你怎麼會想用它? 而且錯的點是你沒查 03/21 23:44
→ QQ29:怎麼用 就寫了.. 03/21 23:44
→ tomnelson:你要不要先試試把average功能先拿掉? 你的average函式原 03/22 05:02
→ tomnelson:型... 喔看錯了,你的問題在你4個函式的參數宣告不一致. 03/22 05:04
→ tomnelson:建議要學function pointer先用較簡單的範例入門,當然你 03/22 05:05
→ tomnelson:這程式算是概念上很簡單,但是很多東西你沒搞懂前,真的會 03/22 05:06
→ tomnelson:讓你卡在某一個小問題小地方上! 03/22 05:07