void fun(int a, int b, int *sum, int *diff, int *product, float *divide) 在主函数中,输入两个整数a和b,然后调用此函数计算数学上的a + b、a - b、a * b和a / b四种运算的值,四种运算的结果通过函数的后四个参数返回主函数;最后在主函数中,输出这四种运算的值。除法运算要求小数点后必须保留2位有效数字(四舍五入),不足补零。
时间: 2024-01-22 19:18:03 浏览: 26
下面是实现该功能的代码:
```c
#include <stdio.h>
#include <math.h>
void fun(int a, int b, int *sum, int *diff, int *product, float *divide) {
*sum = a + b;
*diff = a - b;
*product = a * b;
*divide = (float)a / b;
}
int main() {
int a, b, sum, diff, product;
float divide;
printf("请输入两个整数a和b:");
scanf("%d %d", &a, &b);
fun(a, b, &sum, &diff, &product, ÷);
printf("%d + %d = %d\n", a, b, sum);
printf("%d - %d = %d\n", a, b, diff);
printf("%d * %d = %d\n", a, b, product);
printf("%d / %d = %.2f\n", a, b, roundf(divide * 100) / 100); // 保留两位小数,四舍五入
return 0;
}
```
在该代码中,`fun`函数通过指针参数返回计算结果,主函数中调用`fun`函数并输出结果。其中,在除法运算中,使用了`roundf`函数进行四舍五入,并将结果乘以100后再除以100,来保留两位小数。
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)