鱼C论坛

 找回密码
 立即注册
查看: 3456|回复: 5

栈的运用:二进制转十进制

[复制链接]
发表于 2014-12-21 17:29:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define STACK_INIT_SIZE 20
#define STACKINCREAMENT 10

typedef char ElemType;
typedef struct
{
        ElemType *base;
        ElemType *top;
        int stacksize;
}sqStack;

void InitStack(sqStack *s)
{
        s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
        if (!s->base)
        {
                exit(0);
        }
        s->top = s->base;
        s->stacksize = STACK_INIT_SIZE;
}

void Push(sqStack *s, ElemType e)
{
        if (s->top - s->base >= s->stacksize)
        {
                s->base = (ElemType *)realloc(s->base, (s->stacksize + STACKINCREAMENT) * sizeof(ElemType));
                if ( !s->base )
                {
                        exit(0);
                }
        }
        *(s->top) = e;
        ++s->top;
}

void Pop(sqStack *s, ElemType *e)
{
        if (s->top == s->base)
        {
                return;
        }
        *e = *--(s->top);
}

int StackLen(sqStack s)
{
        return (s.top - s.base);
}

int main()
{
        ElemType c;
        sqStack s;
        int len, i, sum = 0;

        printf("请输入二进制数,输入#表示输入结束! \n");
        scanf_s("%c", &c);
        while (c != '#')
        {
                Push(&s, c);
                scanf_s("%c", &c);
        }

        getchar();

        len = StackLen(s);
        printf("栈的当前容量为: %d\n", len);

        for (i = 0; i < len; i++)
        {
                Pop(&s, &c);
                sum = sum + (c - 48) * 2^i;
        }

        printf("转换为十进制数是: %d\n", sum);

        return 0;
}
请问为什么输入完数字后运行错误???
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-12-22 14:02:46 | 显示全部楼层
由于s->top指向的是数据的上一个位置,所以当容量为s->stacksize - 1 的时候就应该扩容了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-22 15:15:14 | 显示全部楼层
BeatificDevin 发表于 2014-12-22 14:02
由于s->top指向的是数据的上一个位置,所以当容量为s->stacksize - 1 的时候就应该扩容了

能不能具体说一下是哪条语句错误了,找了一遍还是没有找到
        if (s->top - s->base >= s->stacksize)
难道是这句错了????小甲鱼老师在视频里就是这样的呀???
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-12-22 16:59:10 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

#define STACK_INIT_SIZE 20
#define STACKINCREAMENT 10

typedef char ElemType;
typedef struct
{
        ElemType *base;
        ElemType *top;
        int stacksize;
}sqStack;

void InitStack(sqStack *s)
{
        s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
        if (!s->base)
        {
                exit(0);
        }
        s->top = s->base;
        s->stacksize = STACK_INIT_SIZE;
}

void Push(sqStack *s, ElemType e)
{
        if (s->top - s->base >= s->stacksize - 1)
        {
                s->base = (ElemType *)realloc(s->base, (s->stacksize + STACKINCREAMENT) * sizeof(ElemType));
                if ( !s->base )
                {
                        exit(0);
                }
        }
        *(s->top) = e;
        s->top++;
}

void Pop(sqStack *s, ElemType *e)
{
        if (s->top == s->base)
        {
                return;
        }
        *e = *--(s->top);
}

int StackLen(sqStack s)
{
        return (s.top - s.base);
}

int main()
{
        ElemType c;
        sqStack s;
        int len,i;
        int sum = 0;
        InitStack(&s);
        printf("请输入二进制数,输入#表示输入结束! \n");
        fflush(stdin);
        scanf("%c",&c);
        while (1)
        {
            if(c == '\n')
                break;
                Push(&s, c);
                scanf("%c",&c);
        }
        len = StackLen(s);
        printf("栈的当前容量为: %d\n", len);

        for (i = 0; i < len; i++)
        {
                Pop(&s, &c);
                sum = sum + (c - 48) * pow(2,i);
        }

        printf("转换为十进制数是: %d\n", sum);

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-12-22 17:01:26 | 显示全部楼层
帮你修改好了{:1_1:}
你的程序主要有以下问题
1、你在主函数中没有初始化
2.次方要用pow函数。2^3这种格式编译器不认得
其他都挺好的{:1_1:}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-12-22 17:04:29 | 显示全部楼层
输入回车表示结束,提示忘记改了:ton:
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-2 22:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表