怎么用一个宏来求三个数值的中间值呢?请看:
#include <stdio.h> #define MIDDLE(x,y,z) ((x)<(y)?((y)<(z)?(y):(x)<(z)?(z):(x)):((y)>(z)?(y):(x)>(z)?(z):(x))) int main(void) { printf("MIDDLE(%d,%d,%d) = %d\r\n", 1,2,3,MIDDLE(1,2,3)); printf("MIDDLE(%d,%d,%d) = %d\r\n", 2,1,3,MIDDLE(2,1,3)); printf("MIDDLE(%d,%d,%d) = %d\r\n", 1,3,2,MIDDLE(1,3,2)); printf("MIDDLE(%d,%d,%d) = %d\r\n", -1,-2,-3,MIDDLE(-1,-2,-3)); printf("MIDDLE(%d,%d,%d) = %d\r\n", -2,-1,-3,MIDDLE(-2,-1,-3)); printf("MIDDLE(%d,%d,%d) = %d\r\n", -1,-3,-2,MIDDLE(-1,-3,-2)); printf("MIDDLE(%f,%f,%f) = %f\r\n", 1.1,2.4,3.5,MIDDLE(1.1,2.4,3.5)); printf("MIDDLE(%f,%f,%f) = %f\r\n", 1.1,3.3,2.4,MIDDLE(1.1,3.3,2.4)); printf("MIDDLE(%f,%f,%f) = %f\r\n", 2.4,1.1,3.3,MIDDLE(2.4,1.1,3.3)); return 0; }
可见结果均正确!
原文地址:http://blog.csdn.net/dcx1205/article/details/45750305