C primer plus 第六版 第九章 第六题 编程练习答案

版权声明:转载请注明来源~ https://blog.csdn.net/Lth_1571138383/article/details/80536235

Github 地址:这里这里φ(>ω<*)

/*
    本程序应 习题 - 6 建立。
  题目要求: 编写并测试一个函数,该函数以三个double类型变量的地址为参数;
              把最小值放入第一个变量,中间值放入第二个,最大值放入第三个变量。
*/


#include<stdio.h>


void test(double *, double *, double *);


int main(void)
{
// 三个 double 变量。
double one = 0;
double two = 0;
double three = 0; 


int i = 0;  // 循环用。


for (i = 1; i < 4; i++)
{
if (i == 1)
{
printf("Please input first numbers :");
scanf_s("%lf", &one);
putchar(getchar());
}
else if (i == 2)
{
printf("Please input second numbers :");
scanf_s("%lf", &two);
putchar(getchar());
}
else if(i == 3)
{
printf("Please input third numbers :");
scanf_s("%lf", &three);
putchar(getchar());
}
else
{
printf("Wrring ! The program is close now !\n");
}
}


test(&one, &two, &three);


printf("现在三个值的大小关系为 %lf > %lf > %lf .\n", one, two, three);
printf("Bye !\n");

getchar();


return 0;
}


void test(double * one, double * two, double * three)
{
double change = 0;


if (*one > *two)
{
// 在 one > two 的情况下。拿 one 与 three 比较。


if (*two > *three)
{
// 如果 two > three 。 那么大小关系是 one > two > three 不做任何修改。
;
}

else if (*one > *three)
{
// 在 two 不大于 three 的情况下。
// 如果 one > three ,则大小关系为 one > three > two 。 修改相应 指针 的值。
change = *two;


*two = *three;
*three = change;
}

else 
{
// 现在的大小关系为 three > one > two 。 进行修改对应 指针 值。
change = *two;

*two = *one;
*one = *three;
*three = change;
}
}

else if (*two > *three)
{
// 在 one 小于 two的情况下。 

if (*three > *one)
{
// 在 three > one 的情况下, 则大小关系为  two > three > one 。修改相应 指针 的值。
change = *one;


*one = *two;
*two = *three;
*three = change;
}


else if (*three < *one)
{
// 在 one > three 的情况下, 则大小关系为 two > one > three 。 修改相应 指针 的值。
change = *one;

*one = *two;
*two = change;
}
}


else
{
// 现在为 one < two , 但 two < three 的情况下。
// 大小关系为 three > two > one 。 修改对应 指针 的值。
change = *one;

*one = *three;
*three = change;
}


// 不写 return 0; 是因为编译器会报错函数的void返回值默认为 int 。
return;
}

猜你喜欢

转载自blog.csdn.net/Lth_1571138383/article/details/80536235