鱼C论坛

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

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

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

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

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

x
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>

  4. #define STACK_INIT_SIZE 20
  5. #define STACKINCREAMENT 10

  6. typedef char ElemType;
  7. typedef struct
  8. {
  9.         ElemType *base;
  10.         ElemType *top;
  11.         int stacksize;
  12. }sqStack;

  13. void InitStack(sqStack *s)
  14. {
  15.         s->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
  16.         if (!s->base)
  17.         {
  18.                 exit(0);
  19.         }
  20.         s->top = s->base;
  21.         s->stacksize = STACK_INIT_SIZE;
  22. }

  23. void Push(sqStack *s, ElemType e)
  24. {
  25.         if (s->top - s->base >= s->stacksize)
  26.         {
  27.                 s->base = (ElemType *)realloc(s->base, (s->stacksize + STACKINCREAMENT) * sizeof(ElemType));
  28.                 if ( !s->base )
  29.                 {
  30.                         exit(0);
  31.                 }
  32.         }
  33.         *(s->top) = e;
  34.         ++s->top;
  35. }

  36. void Pop(sqStack *s, ElemType *e)
  37. {
  38.         if (s->top == s->base)
  39.         {
  40.                 return;
  41.         }
  42.         *e = *--(s->top);
  43. }

  44. int StackLen(sqStack s)
  45. {
  46.         return (s.top - s.base);
  47. }

  48. int main()
  49. {
  50.         ElemType c;
  51.         sqStack s;
  52.         int len, i, sum = 0;

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

  60.         getchar();

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

  63.         for (i = 0; i < len; i++)
  64.         {
  65.                 Pop(&s, &c);
  66.                 sum = sum + (c - 48) * 2^i;
  67.         }

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

  69.         return 0;
  70. }
复制代码

请问为什么输入完数字后运行错误???
想知道小甲鱼最近在做啥?请访问 -> 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 的时候就应该扩容了

能不能具体说一下是哪条语句错误了,找了一遍还是没有找到
  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-5-22 19:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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