用C++实现求3个数中最大数(分别考虑整数、实数、长整数的情况)。要求用函数重载来实现。函数原型为:int max(int,int,int);double max(double,double,double);long max(long,long,long);
时间: 2023-11-26 10:06:12 浏览: 75
这个问题可以回答。以下是对应的 C 代码实现,其中函数重载的实现使用了函数名相同但参数类型不同的方式:
```
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int max(int a, int b, int c) {
int max = (a > b) ? a : b;
max = (max > c) ? max : c;
return max;
}
double max(double a, double b, double c) {
double max = (a > b) ? a : b;
max = (max > c) ? max : c;
return max;
}
long max(long a, long b, long c) {
long max = (a > b) ? a : b;
max = (max > c) ? max : c;
return max;
}
int main() {
int a = 1, b = 2, c = 3;
double d = 1.5, e = 2.4, f = 3.2;
long g = LONG_MAX, h = 100, i = 0;
printf("max(int): %d\n", max(a, b, c));
printf("max(double): %.2f\n", max(d, e, f));
printf("max(long): %ld\n", max(g, h, i));
return 0;
}
```
输出结果为:
```
max(int): 3
max(double): 3.20
max(long): 9223372036854775807
```
阅读全文