首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

问条程序题目,该怎么解决

2012-03-17 
问条程序题目#include stdio.h#defineMAX#defineMAXIMUM(x,y)(x y)?x:y#defineMINIMUM(x,y)(x y)?y:xv

问条程序题目
#include <stdio.h>
#define   MAX
#define   MAXIMUM(x,y)   (x> y)?x:y
#define   MINIMUM(x,y)   (x> y)?y:x
void   main()
{
    int   a=10,b=20;
    #ifdef   MAX
        printf( "\40:the   larger   one   is   %d\n ",MAXIMUM(a,b));
    #else
        printf( "\40:the   lower   one   is   %d\n ",MINIMUM(a,b));
    #endif
    #ifndef   MIN
        printf( "\40:the   lower   one   is   %d\n ",MINIMUM(a,b));
    #else
        printf( "\40:the   larger   one   is   %d\n ",MAXIMUM(a,b));
    #endif
    #undef   MAX
    #ifdef   MAX
        printf( "\40:the   larger   one   is   %d\n ",MAXIMUM(a,b));
    #else
        printf( "\40:the   lower   one   is   %d\n ",MINIMUM(a,b));
    #endif
    #define   MIN
    #ifndef   MIN
        printf( "\40:the   lower   one   is   %d\n ",MINIMUM(a,b));
    #else
        printf( "\40:the   larger   one   is   %d\n ",MAXIMUM(a,b));
    #endif
}
里面的   #ifdef   MAX         #else         #endif
    #ifndef   MIN         #endif
    #undef   MAX
    #ifdef   MAX         #endif
    #define   MIN
    #ifndef   MIN
这些到底是什么啊起了些什么作用......完全没看懂

[解决办法]
#ifdef //如果定义了
#else //否则
#endif//结束
#ifndef //如果没定义

一般是为了防止重复编译。我也才刚学,见识少,不知道楼主你给的代码为什么这样折腾……

[解决办法]
防止重复编译呀!重复编译,编译器会提示出错了的!
[解决办法]
首先要知道#define MAX就是定义了MAX这个宏,#undefine MAX就是取消了MAX的定义,使MAX不存在了。

#ifdef MAX
~~~~~~~~~~~如果前面定义了MAX,就执行下面的东东
printf( "\40:the larger one is %d\n ",MAXIMUM(a,b));
#else
~~~~~~~~~~~否则,就执行这个东东
printf( "\40:the lower one is %d\n ",MINIMUM(a,b));
#endif
~~~~~~~~~到这里,由#ifdef和#endif构成的程序段就结束了。

后面的程序与之类似。
[解决办法]
这段代码是让人深刻理解define相关宏的
[解决办法]
ftp://book:book@222.214.218.61/book1/20070211/722b37b6-f3ca-4f0e-96ef-22171f2558ce.pdf

热点排行