有三个实数a,b和c,由用户输入1,2或3。如果输入1,程序就给出a,b和c之和,输入2,程序就给出a,b和c之平均数,输入3,程序就给出a,b和c之乘积。使用指向函数的指针变量完成。
代码如下,望采纳
#include <stdio.h>
float (*process)(float a, float b, float c);
float add(float a, float b, float c) {
return a + b + c;
}
float mean(float a, float b, float c) {
return (a + b + c) / 3;
}
float prod(float a, float b, float c) {
return a * b * c;
}
int main()
{
float a=1, b=2, c=3;
int d;
float (*process)(float, float, float) = NULL;
scanf_s("%d", &d);
if (d ==1)
{
process = &add;
}
else if (d == 2) {
process = &mean;
}
else if(d ==3)
{
process = ∏
}
printf("%f", (*process)(a, b, c));
return 0;
}
不用指针可以吗?
这个题没什么实用价值,代码拿走不谢。
#include <iostream>
double choose(int);
using namespace std;
int main()
{
int i;
double dou;
double (*cho)(int)=choose;
cout << "Please Input 1 or 2 or 3" << endl;
cin >> i;
dou=cho(i);
cout << dou <<endl;
return 0;
}
double choose(int i)
{
double a=7,b=8,c=9;
if(i==1)
return a+b+c;
else if(i==2)
return (a+b+c)/3;
else if(i==3)
return a*b*c;
else
{
cout << "Input Error!And Return:" << endl;
return 0;
}
}