注册 登录
编程论坛 数据结构与算法

数组堆栈的宏?????

h1023417614 发布于 2013-01-30 20:26, 521 次点击
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include <assert.h>// 断言
//下面是数组堆栈的宏 函数依次是 判空 判满 压入 置顶 输出
#define generic_stack(stack_type,suffix,stack_size)        \
        static stack_type stack##suffix[stack_size];    \
        static int  top_element##suffix=-1;                 \
        int is_empty##suffix()                             \
        {                                                 \
           return top_element##suffix==-1;                 \
        }                                                  \
        void is_full##suffix()                              \
        {                                                   \
           return top_element##suffix==stack_size-1;         \
        }                                                    \
        int push##suffix(stack_type value)                     \
        {                                                     \
                assert(!is_full##suffix());                     \
               top_element##suffix+=1;                         \
         stack##suffix[top_element##suffix]=value;             \
        }                                                    \                           
        void pop##suffix()                                    \
        {                                                     \
                assert(!is_empty##suffix());                 \
               top_element##suffix-=1;                         \
        }                                                    \
        stack_type top##suffix()                            \
        {                                                    \   
               assert(!is_empty##suffix());                    \
              return stack##suffix[top_element##suffix];    \
        }                                                   
   



void main()
{
generic_stack(int,_int,10);
push_int(5);//输入5
push_int(10);
push_int(20);
    while(!is_empty_int())
    {
        printf("%d\n",top_int());//输出
        pop_int();
    }


}


通不过编译器啊!!!!!!!
7 回复
#2
yuccn2013-01-31 09:54
1 编码问题,使用宏,编码如果不小心常常都会出现这个问题
2 你的代码等价于 主函数中嵌套函数了,这样不通过的。例如下面:
int _tmain(int argc, _TCHAR* argv[])
{   
    void test() // 你的宏在main内,展开后就是主函数嵌套函数了,一定失败的
    {
        printf("sss");
    }


    return 1;

}

如果要写就写成模板吧,

这样写没有什么好处的,给自己调试 和查看代码都不带来什么优点

[ 本帖最后由 yuccn 于 2013-1-31 09:59 编辑 ]
#3
h10234176142013-01-31 10:54
学习了额
#4
h10234176142013-01-31 18:26
顶起
#5
yuccn2013-02-01 12:26
回复 4楼 h1023417614
为什么定起?还不知道错误的原因吗?
#6
h10234176142013-02-01 22:25
1.有两个函数类型搞错了,
2.嵌套了函数。

改了还是没用,,,,,,不过感觉真的没啥好处,,暂时还是觉得模版行
#7
h10234176142013-02-01 22:37
发现了宏定义后面的\,这个符号后面必须马上回车,不能加任何一个字符,空格 tab都不行,,,,这里没做到啊


通过了
#8
不玩虚的2013-02-02 22:57
学习下
1