读书人

C语言“全能比较函数”新鲜出炉(其次版

发布时间: 2012-09-14 11:53:44 作者: rapoo

C语言“全能比较函数”新鲜出炉(第二版),任意数据类型
第一版:

#include <stdio.h>// 下面就是“全能比较函数”,a>b返回整数,a<b返回负数,a==b返回0 #define COMPARE(TYPE,a,b)(((TYPE)a-(TYPE)b)==0?0:(((TYPE)a-(TYPE)b)>0?1:-1))int main(int argc, char *argv[]){int result;double a=1, b=1.5;int x=10, y=1;char m='m', n='n';float f1=6.02, f2=6.0200002;result = COMPARE(double, a, b);printf("%d \n", result);result = COMPARE(int, x, y);printf("%d \n", result);result = COMPARE(char, m, n);printf("%d \n", result);result = COMPARE(float, f1, f2);printf("%d \n", result);return 0;}

读书人网 >C语言

热点推荐