C语言中条件表达式求最大值和最小值
1、最大值
#include <stdio.h>
int main(void)
{
int a,b,c,d,m;
puts("please input four integers.");
printf("a = "); scanf("%d", &a);
printf("b = "); scanf("%d", &b);
printf("c = "); scanf("%d", &c);
printf("d = "); scanf("%d", &d);
m = a;
if (m == b && m ==c && m ==d)
puts("the integers are equal!\a");
else
{
m = (m > b) ? m:b;
m = (m > c) ? m:c;
m = (m > d) ? m:d;
printf("the larger integer is %d.\n", m);
}
return 0;
}
2、最小值
#include <stdio.h>
int main(void)
{
int a,b,c,d,m;
puts("please input four integers.");
printf("a = "); scanf("%d", &a);
printf("b = "); scanf("%d", &b);
printf("c = "); scanf("%d", &c);
printf("d = "); scanf("%d", &d);
m = a;
if (m == b && m == c && m == d)
puts("the integers are equal!\a");
else
{
m = (m < b) ? m:b;
m = (m < c) ? m:c;
m = (m < d) ? m:d;
printf("the smaller integer is %d.\n", m);
}
return 0;
}