//////////////////////////////////////////////////////
//求三个数的最大值和最小值
/////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
float compTwo(float x, float y, int flag);
main()
{
float a,b,c;
float minNum,maxNum;
printf("Please input three float/int numbers:\n");
scanf("%f %f %f",&a,&b,&c);
printf("The three numbers you input are: %f %f %f\n",a,b,c);
minNum=compTwo(compTwo(a,b,0),c,0);
maxNum=compTwo(compTwo(a,b,1),c,1);
printf("The minmun number you input is: %f\n",minNum);
printf("The maxmum number you input is: %f\n",maxNum);
system("pause");
}
float compTwo(float x, float y, int flag)
{
if(flag==0)
{
return x<y?x:y; //return the small number
}
else if(flag==1)
{
return x<y?y:x; //return the large number
}
else
{
printf("Error: \"flag\" in compTwo() has a problem!\n");
}
}
//求三个数的最大值和最小值
/////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
float compTwo(float x, float y, int flag);
main()
{
float a,b,c;
float minNum,maxNum;
printf("Please input three float/int numbers:\n");
scanf("%f %f %f",&a,&b,&c);
printf("The three numbers you input are: %f %f %f\n",a,b,c);
minNum=compTwo(compTwo(a,b,0),c,0);
maxNum=compTwo(compTwo(a,b,1),c,1);
printf("The minmun number you input is: %f\n",minNum);
printf("The maxmum number you input is: %f\n",maxNum);
system("pause");
}
float compTwo(float x, float y, int flag)
{
if(flag==0)
{
return x<y?x:y; //return the small number
}
else if(flag==1)
{
return x<y?y:x; //return the large number
}
else
{
printf("Error: \"flag\" in compTwo() has a problem!\n");
}
}
本博客介绍了一个简单的C程序,用于找到输入的三个浮点数或整数中的最大值和最小值。通过使用自定义函数`compTwo()`,程序能够高效地比较两个数并返回较大的或较小的数,最终输出所有输入数以及它们的最大和最小值。
657

被折叠的 条评论
为什么被折叠?



