C primer plus (第六版)第九章 编程练习第6题
题目:
6.编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小值放入第1个函数,中间值放入第2个变量,最大值放入第3个变量。
思路:用2个浮点数比较大小的函数比较三次完成排序,直接用本章指针函数的示例来做;
#include <stdio.h>
void arrange_two(double * x, double * y); //两个浮点数比较大小的函数
int main()
{double a, b, c;printf("Please entry three float numbers:\n");while (scanf("%lf, %lf, %lf", &a, &b, &c) == 3){printf("Entered float numbers are %.3lf; %.3lf; %.3lf;\n",a,b,c); //未排序前输入的浮点数arrange_two(&a, &b);arrange_two(&b, &c);arrange_two(&a, &b);printf("After arrange from low to large: %.3lf; %.3lf; %.3lf;\n",a,b,c); //排序后打印printf("Please entry another three float numbers:\n"); //循环提示输入,非浮点数退出}printf("Done!.\n");return 0;
}void arrange_two(double * x, double * y)
{double temp;if (*x > *y){temp = *x;*x = *y;*y = temp;}
}