ZBLOG

#include #include #define MAX_SIZE 100 typedef struct Stack { int data[MAX_SIZE]; int top; } Stack; void initStack(Stack* stack) { stack->top = -1; } int isEmpty(Stack* stack) { return stack->top == -1; } ...

您的代码中存在几处问题:

  1. isFull(SeqStack* stack) 函数中,应将 SeqStack 改为 Stack,因为定义的结构体名字是 Stack

  2. push(SeqStack* stack, int value) 函数中,同样需要将 SeqStack 改为 Stack

  3. pop(SeqStack* stack) 函数中,同样需要将 SeqStack 改为 Stack

请修正以上问题后的代码如下所示:

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

#define MAX_SIZE 100

typedef struct Stack {
    int data[MAX_SIZE];
    int top;
} Stack;

void initStack(Stack* stack) {
    stack->top = -1;
}

int isEmpty(Stack* stack) {
    return stack->top == -1;
}

int isFull(Stack* stack) {
    return stack->top == MAX_SIZE - 1;
}

void push(Stack* stack, int value) {
     if (isFull(stack)) {
        printf("Stack overflow\n");
        return;
    }
    
    stack->data[++(stack->top)] = value;
}

int pop(Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack underflow\n");
        return -1; // 表示栈为空值
    }
    
    return stack->data[(stack->top)--];
}

void convertDecimalToBinary(int decimal) {
   Stack stack;
   initStack(&stack);
   
   while (decimal > 0) {
       int remainder = decimal % 2;
       push(&stack, remainder);
       decimal /= 2;
   }
   
   while (!isEmpty(&stack)) {
       printf("%d", pop(&stack));
   }
}

void convertDecimalToOctal(int decimal) {
    Stack stack;
    initStack(&stack);
    
    while (decimal > 0) {
        int remainder = decimal % 8;
        push(&stack, remainder);
        decimal /= 8;
    }
    
    while (!isEmpty(&stack)) {
        printf("%d", pop(&stack));
    }
}

void convertDecimalToHexadecimal(int decimal) {
    Stack stack;
    initStack(&stack);
    
    while (decimal > 0) {
        int remainder = decimal % 16;

		if (remainder < 10)
			push(&stack, remainder + '0');
		else
			push(&stack, remainder - 10 + 'A');
        
        decimal /= 16;
     }
     
     while (!isEmpty(&stack)) {
         printf("%c", pop(&stack));
     }
}

这样修改后的代码应该可以正确运行了。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=2508

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?