全国计算机等级考试二级教程——C语言程序设计(2021年版)
编程题答案(第8章):
[8.17]
请编写函数,其功能是对传送过来的两个浮点数求出和值与差值,并通过形参传送回调用函数。
#include<stdio.h>
void fun(double x,double y,double *a,double *b)
{*a=x+y;
*b=x-y;
}
main()
{double x,y,a,b;
scanf("%lf%lf",&x,&y);
fun(x,y,&a,&b);
printf("%f,%f",a,b);
}
[8.18]
请编写函数,对传送过来的三个数选出最大数和最小数,并通过形参传回调用函数。
#include<stdio.h>
void fun(int a,int b,int c,int *x,int*y)
{if(a>b) *x=a,*y=b;
else *x=b,*y=a;
if(*x<c) *x=c;/*选出最大*/
if(*y>c) *y=c;/*选出最小*/
}
main()
{int a,b,c,x,y;
scanf("%d%d%d",&a,&b,&c);
fun(a,b,c,&x,&y);
printf("%d,%d",x,y);
}