计算三个数的最大值与最小值

// HOngT.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#define MAX(a,b,c) ((((a>b)?(a):(b))>c)?((a>b)?(a):(b)):(c))
#define MIN(a,b,c) ((((a<b)?(a):(b))<c)?((a<b)?(a):(b)):(c))

int _tmain(int argc, _TCHAR* argv[])
{
int x,y,z;
printf("pelease putin your three numbers: \n");
scanf("%d %d %d",&x,&y,&z);
printf("MAX = %d\n",MAX(x,y,z));
printf("MIN = %d\n",MIN(x,y,z));
return 0;
}

以上是用宏设计进行计算的;

// MAX.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
int x,y,z;
printf("pelease putin your three numbers: \n");
scanf("%d %d %d",&x,&y,&z);
if((x == y) && (x == z)){
printf("MAX = %d,%d,%d\n",x,y,z);
printf("MIN = %d,%d,%d\n",x,y,z);}else if((x == y) && (x > z)){
printf("MAX = %d,%d\n",x,y);
printf("MIN = %d\n",z);}else if((x == y) && (x < z)){
printf("MAX = %d\n",z);
printf("MIN = %d,%d\n",x,y);}else if((x == z) && (x > y)){
printf("MAX = %d,%d\n",x,z);
printf("MIN = %d\n",y);}else if((y == z) && (y > x)){
printf("MAX = %d,%d\n",y,z);
printf("MIN = %d\n",x);}else if(x>y){
if(x>z){
printf("MAX = %d\n",x);if(y>z){
printf("MIN = %d\n",z);}else{
printf("MIN = %d\n",y);}}else{
printf("MAX = %d\n",z);
printf("MIN = %d\n",y);}}else if(x<y){
if(y>z){
printf("MAX = %d\n",y);
if(x>z){
printf("MIN = %d\n",z);}else{
printf("MIN = %d\n",x);}}else{
printf("MAX = %d\n",z);
printf("MIN = %d\n",x);}}
return 0;
}

以上是选择语句实现。

posted on 2011-10-27 10:55  呓语若梦半浮生  阅读(671)  评论(0编辑  收藏  举报

导航