zonghui 发表于 2020-4-28 22:26:08

大一小白,十进制转二进制,到底哪错了

#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
#define M
typedef struct
{
        ElemType elem;
        int top;
}Seqstack;
void Init(Seqstack *s)//初始化;
{
        s=(Seqstack *)malloc(sizeof(Seqstack));//定义栈空间;
        s->top=-1;
}
void Push (Seqstack *s,ElemType e)
{
       
        if (s->top==M-1) exit(0);
        s->top++;
        s->top=e;
}
int Pop(Seqstack *s,ElemType e)
{
        if (s->top==-1) return 0;
        e=--(s->top);
       
        printf ("%d ",e);
}
int main()
{
       
        int n,i,e;
        Seqstack s;
        Init (&s);
        //Empty(s);
        printf ("请输入十进制数 :");
        scanf ("%d",&n);
        while (n)//int型;
        {
                Push(&s,n%8);
                n =        n/8;
        }
       
        printf ("输出八进制数 :");
        while(s.top!=-1)
       
        {
                Pop(&s,e);
               
                printf ("%d ",e);
        }
       
        return 0;                       
}

永恒的蓝色梦想 发表于 2020-4-28 22:33:48

你这程序是十进制转8进制,十进制转8进制#include<stdio.h>

int main()
{
    int n;
    scanf("%d",&n);
    printf("%o",n);
    return 0;
}就可以了

sunrise085 发表于 2020-4-28 23:14:41

你的程序问题太多了。就不一一列举了
挑着说一些吧
#define写的不对
Init内申请空间是无效的。帮你修改了一下Init
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
#define M 20
typedef struct
{
    ElemType elem;
    int top;
}Seqstack;
void Init(Seqstack *s)//初始化;
{
    //s=(Seqstack *)malloc(sizeof(Seqstack));//定义栈空间;
    s->top=-1;
}
void Push (Seqstack *s,ElemType e)
{
   
    if (s->top==M-1) exit(0);
    s->elem[++s->top]=e;
}
int Pop(Seqstack *s,ElemType *e)
{
    if (s->top==-1) return 0;
    *e=(s->elem);
}
int main()
{
      
    int n,i,e;
    Seqstack *s;
    s=(Seqstack*)malloc(sizeof(Seqstack));
    Init (s);
    //Empty(s);
    printf ("请输入十进制数 :");
    scanf ("%d",&n);
    while (n)//int型;
    {
      Push(s,n%8);
      n = n/8;
    }
   
    printf ("输出八进制数 :");
    while(s->top!=-1)
   
    {
      Pop(s,&e);
      printf ("%d",e);
    }
   
    return 0;                        
}
页: [1]
查看完整版本: 大一小白,十进制转二进制,到底哪错了