c++编写function函数计算ab最大值,在住函数中输入3个数(a,b,c),其中2个int ,1个double类型,主函数输出计算ab中最大值与c相乘的结果
时间: 2024-05-25 17:19:21 浏览: 15
#include <stdio.h>
double function(int a, int b, double c) {
if (a > b) {
return a * c;
} else {
return b * c;
}
}
int main() {
int a, b;
double c;
printf("请输入3个数(其中2个int类型,1个double类型):\n");
scanf("%d%d%lf", &a, &b, &c);
double result = function(a, b, c);
printf("ab中最大值与c相乘的结果为:%.2lf\n", result);
return 0;
}
相关问题
c++编写function函数计算ab中最大值在主函数中输入3个数(a,b,c)其中2个int,1个double类型主函数输出计算ab中最大值与c相乘的结果
```
#include <stdio.h>
double function(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int main() {
int a, b;
double c;
printf("请输入两个整数和一个小数:");
scanf("%d %d %lf", &a, &b, &c);
double max = function(a, b);
double result = max * c;
printf("%.2lf", result);
return 0;
}
```
c++用重载函数编写程序计算2个或3个整数的最大数
以下是使用重载函数来计算两个或三个整数的最大值的C++程序:
```c++
#include <iostream>
using namespace std;
int max(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int max(int a, int b, int c) {
if (a > b) {
if (a > c) {
return a;
} else {
return c;
}
} else {
if (b > c) {
return b;
} else {
return c;
}
}
}
int main() {
int a, b, c;
cout << "请输入两个或三个整数:" << endl;
cin >> a >> b >> c;
if (c) {
cout << "最大值是:" << max(a, b, c) << endl;
} else {
cout << "最大值是:" << max(a, b) << endl;
}
return 0;
}
```
这个程序定义了两个重载函数 max,一个用于计算两个整数的最大值,另一个用于计算三个整数的最大值。在 main 函数中,程序通过检查是否输入了第三个整数来决定使用哪个函数来计算最大值。