gpt4 book ai didi

c - 使用函数指针创建菜单驱动系统

转载 作者:行者123 更新时间:2023-11-30 16:00:23 25 4
gpt4 key购买 nike

我正在尝试使用函数指针来创建菜单驱动的系统,但是当我编译它时,我收到了警告

[Warning] initialization from incompatible pointer type 

void (*gradeProcess[4])(int)={printArray,minimum,maximum,average};

我无法解决这个问题,有人可以帮助我吗?

这是代码:

#include <stdio.h>
#define STUDENTS 3
#define EXAMS 3

void printArray(const int grades[][EXAMS],int pupils,int tests);
void minimum(const int grades[][EXAMS],int pupils,int tests);
void maximum(const int grades[][EXAMS],int pupils,int tests);
void average(const int grades[][EXAMS],int pupils,int tests);

int main(void)
{
int examGrades[STUDENTS][EXAMS];
int choice;
int i,j;
void (*gradeProcess[4])(int)={printArray,minimum,maximum,average};

for(i=0;i<STUDENTS;i++){
printf("Enter the grades of student %d:\n",i);
for(j=0;j<EXAMS;j++){
scanf("%d",&examGrades[i][j]);
}
}

printf("\n\n0 to print array\n1 to find the lowest grade\n2 to find the highest grade\n3 to find average for each student\n4 to end program\n\n");

printf("Enter the number of operation you want to process:");
scanf("%d",&choice);

while(choice>=0 && choice<4){
(*gradeProcess[choice])(choice);

printf("Enter the number of operation you want to process:");
scanf("%d",&choice);
}

printf("\n\nProgram execution completed\n");

system("PAUSE");
return (0);
}

void printArray(const int grades[][EXAMS],int pupils,int tests)
{
printf("You chose to print array\n\n");
printf(" [0] [1] [2]");

for(pupils=0;pupils<STUDENTS;pupils++){
printf("\nstudent[%d]= ",pupils);
for(tests=0;tests<EXAMS;tests++){
printf("%4d",grades[pupils][tests]);
}
}
}

void minimum(const int grades[][EXAMS],int pupils,int tests)
{
int lowest=0;

printf("The lowest grade will be displayed");

for(pupils=0;pupils<STUDENTS;pupils++){
for(tests=0;tests<EXAMS;tests++){
if(lowest>grades[pupils][tests]){
lowest=grades[pupils][tests];
}
}
}

printf("\nThe lowest grade is %d",lowest);
}

void maximum(const int grades[][EXAMS],int pupils,int tests)
{
int highest=0;

printf("The highest grade will be displayed");

for(pupils=0;pupils<STUDENTS;pupils++){
for(tests=0;tests<EXAMS;tests++){
if(highest>grades[pupils][tests]){
highest=grades[pupils][tests];
}
}
}

printf("\nThe highest grade is %d",highest);
}

void average(const int grades[][EXAMS],int pupils,int tests)
{
int total;
double average;

printf("You chose to display average of each student\n\n");

for(pupils=0;pupils<STUDENTS;pupils++){
printf("The average for student[%d]:",pupils);
for(tests=0;tests<EXAMS;tests++){
total+=grades[pupils][tests];
}
average=(double)total/tests;
printf("%.2lf",average);
}
}

最佳答案

帮自己一个忙,使用 typedef:

typedef void (*fptr)(const int [][EXAMS], int, int);

//...

fptr gradeProcess[4] = { printArray, minimum, maximum, average };

关于c - 使用函数指针创建菜单驱动系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7805805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com