LucienMa 2022-04-10 04:16 采纳率: 60%
浏览 44

用栈 实现二进制到八进制的转换,0报错但无法实现,警告传参方式错误,如何解决

//利用栈制作一个将二进制数转化为八进制数的程序
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>

#define INIT_STACK_SIZE 20
#define STACK_INCREMENT 10

typedef char ElemType;
typedef struct { //创建栈的顺序结构;
    ElemType *base;
    ElemType *top;
    int stacksize;
} sqStack;

void InitStack(sqStack *s) { //栈的创建
    s->base = (ElemType *)malloc(INIT_STACK_SIZE * sizeof(ElemType));
    if (!s->base) {
        printf("栈创建失败!\n");
        exit(0);
    }
    s->top = s->base;
    s->stacksize = INIT_STACK_SIZE;
}

void Push(sqStack *s, ElemType e) { //入栈;
    if (s->top - s->base >= s->stacksize) {
        s->base = realloc(s->base, (INIT_STACK_SIZE + STACK_INCREMENT) * sizeof(ElemType));
        if (!s->base) {
            printf("增长栈失败!\n");
            exit(0);
        }
        s->top = s->base + s->stacksize;
    }
    *(s->top) = e;
    s->top++;
    s->stacksize = INIT_STACK_SIZE + STACK_INCREMENT;

}

void Pop(sqStack *s, ElemType *e) { //出栈;
    if (s->base = s->top) {
        printf("你的链表为空!\n");
        exit(0);
    }
    (*e) = *(s->top - 1);
    s->top--;
}

int Len(sqStack *s) { //取栈长;
    return s->top - s->base;
}

void main() { //主函数;
    ElemType c;
    int i, j, len, sum = 0;
    sqStack *s, *o;

    InitStack(&s);
    InitStack(&o);

    printf("输入您的二进制数,当输入#时结束!\n");//将二进制数存入栈中;
    scanf("%c", &c);
    while (c != '#') {
        Push( & s, c);
        scanf("%c", &c);
    }
    getchar();

    len = Len(s);
    for (i = 0; i < len; i += 3) {
        if (Len(s) > 3) {
            for (j = 0; j < 3; j++) {
                Pop(&s, &c);
                sum = sum + (c - 48) * pow(2, j);
                Push(&o, sum + 48);
            }
        }
        if (Len(s) < 3) {
            for (j = 0; s->top - s->base != 0; j++) {
                Pop(&s, &c);
                sum = sum + (c - 48) * pow(2, j);
                Push(&o, sum + 48);
            }
        }
    }

    printf("该二进制数的八进制形式为:\n");
    while (Len(o)) {
        Pop(&o, &c);
        printf("%c", c);
    }

}

这里显示传参有问题,但用相同传参方式这段代码无警告,为什么呢?


#define STACK_INIT_SIZE 900
#define STACKINCREMENT 10
typedef int SElemType;
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct {
    SElemType* base;//base是栈底指针
    SElemType* top;//栈顶指针
    int stacksize;  //栈的当前可使用最大容量
} SqStack;

void InitStack(SqStack* S) { //修改&为*
    S->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if (!S->base)
        exit(0);//为防止退出异常,我们返回0(加上(0))
    S->top = S->base;
    S->stacksize = STACK_INIT_SIZE;
}

SElemType GetTop(SqStack* S) { //修改&为*
    if (*S->top == *S->base)
        exit(0);//修改
    return *(S->top - 1); //修改
}

void Push(SqStack* S, SElemType e) { //修改&为*
    if (S->top - S->base >= S->stacksize) { //栈满,追加存储空间
        S->base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType));
        if (!S->base)
            exit(0);//存储分配失败;修改;
        S->top = S->base + S->stacksize;
        S->stacksize += STACKINCREMENT;
    }
    *S->top = e;
    S->top++;
}

void Pop(SqStack* S, SElemType* x) { //修改 引用函数POP时,实参传址,则应用指针类型对应
    if (S->top == S->base) {
        printf("空");
        exit(0);//修改
    }
    (*x) = *(S->top - 1); //修改
    S->top--;//修改
}

int main() {
    SqStack* S;//修改:加* 因为s的实质是一个sqstack型指针变量,需要提前声明;
    SElemType e = 0;
    printf("做一个栈……\n");
    InitStack(&S);
    printf("向栈顶插入元素吧\n");
    while (e >= 0) {
        printf("请输入要插入的元素(输入负数时停止输入):\n");//增加一个循环语句以实现多个元素的插入;
        scanf_s("%d", &e);
        if (e >= 0)
            Push(&S, e);//修改:加& 进行传址;
    }
    SElemType y;
    y = GetTop(&S);//修改
    printf("栈顶的元素为:\n");
    printf("%d\n", y);
    SElemType x;
    printf("删除栈顶元素\n");
    Pop(&S, &x);//修改
    printf("被删除的元素是:\n");
    printf("%d", x);//修改:将scanf_s()修改为peintf()因为此时指令为打印被删除的栈顶元素;
    return 0;
}
  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-04-11 11:28
    关注
    
    void InitStack(SqStack** pS) { //修改&为*
        SqStack* S=*pS;
        S->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));
        if (!S->base)
            exit(0);//为防止退出异常,我们返回0(加上(0))
        S->top = S->base;
        S->stacksize = STACK_INIT_SIZE;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 4月10日

悬赏问题

  • ¥15 asp写PC网站开通了微信支付,扫码付款不能跳转
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 关于#c语言#的问题:我在vscode和codeblocks中编写c语言时出现打不开源文件该怎么办
  • ¥15 非科班怎么跑代码?如何导数据和调参
  • ¥15 福州市的全人群死因监测点死亡原因报表
  • ¥15 Altair EDEM中生成一个颗粒,并且各个方向没有初始速度
  • ¥15 系统2008r2 装机配置推荐一下
  • ¥500 服务器搭建cisco AnyConnect vpn
  • ¥15 悬赏Python-playwright部署在centos7上
  • ¥15 psoc creator软件有没有人能远程安装啊