用C语言函数的方式写三个数中最大数与最小数的差
时间: 2024-03-15 12:45:56 浏览: 38
好的,下面是用 C 语言函数的方式写三个数中最大数与最小数的差的代码:
```c
#include <stdio.h>
int max(int a, int b, int c) { // 定义比较三个数中最大数的函数
int max_num = a;
if (b > max_num) {
max_num = b;
}
if (c > max_num) {
max_num = c;
}
return max_num;
}
int min(int a, int b, int c) { // 定义比较三个数中最小数的函数
int min_num = a;
if (b < min_num) {
min_num = b;
}
if (c < min_num) {
min_num = c;
}
return min_num;
}
int main() {
int a, b, c;
printf("请输入三个数,以空格分隔:");
scanf("%d %d %d", &a, &b, &c);
int max_num = max(a, b, c); // 调用比较三个数中最大数的函数
int min_num = min(a, b, c); // 调用比较三个数中最小数的函数
int diff = max_num - min_num;
printf("三个数中最大数为%d,最小数为%d,它们的差为%d\n", max_num, min_num, diff);
return 0;
}
```
在上面的代码中,我们分别定义了两个函数 `max()` 和 `min()`,用于比较三个数中的最大数和最小数。在 `main()` 函数中,我们调用这两个函数,求出最大数和最小数,然后计算它们的差,并将结果输出。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)