C语言编程【问题描述】调用函数求两个数的和与差,要求自定义一个函数sum_diff(float op1,float op2,float *psum,float *pdiff),其中op1和op2是输入的两个实数,*psum和*pdiff是计算得出的和与差。自定义主函数,并在其中调用sum_diff函数。试编写相应程序。【样例输入】 4 6 【样例输出】 the sum is 10.000000 the diff is -2.000000
时间: 2023-12-03 17:45:28 浏览: 196
以下是C语言代码实现:
```
#include <stdio.h>
void sum_diff(float op1, float op2, float *psum, float *pdiff) {
*psum = op1 + op2;
*pdiff = op1 - op2;
}
int main() {
float op1, op2, sum, diff;
printf("Enter two numbers: ");
scanf("%f %f", &op1, &op2);
sum_diff(op1, op2, &sum, &diff);
printf("The sum is %.6f\n", sum);
printf("The diff is %.6f\n", diff);
return 0;
}
```
首先定义了一个自定义函数`sum_diff`,该函数的参数是两个实数`op1`和`op2`,以及指向计算结果的指针`psum`和`pdiff`。在函数中,对op1和op2求和并将结果存储在*psum中,同时对op1和op2求差并将结果存储在*pdiff中。
然后在主函数中,先读取输入的两个实数op1和op2,然后调用sum_diff函数进行计算,并将计算结果存储在sum和diff变量中。最后输出sum和diff的值即可。
阅读全文