鱼C论坛

 找回密码
 立即注册
查看: 583|回复: 2

[已解决]大一小白,十进制转二进制,到底哪错了

[复制链接]
发表于 2020-4-28 22:26:08 | 显示全部楼层 |阅读模式

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

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

x
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
#define M
typedef struct
{
        ElemType elem[M];
        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进制
  1. #include<stdio.h>

  2. int main()
  3. {
  4.     int n;
  5.     scanf("%d",&n);
  6.     printf("%o",n);
  7.     return 0;
  8. }
复制代码
就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-4-28 22:33:48 | 显示全部楼层    本楼为最佳答案   
你这程序是十进制转8进制,十进制转8进制
  1. #include<stdio.h>

  2. int main()
  3. {
  4.     int n;
  5.     scanf("%d",&n);
  6.     printf("%o",n);
  7.     return 0;
  8. }
复制代码
就可以了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-28 23:14:41 | 显示全部楼层
你的程序问题太多了。就不一一列举了
挑着说一些吧
#define写的不对
Init内申请空间是无效的。帮你修改了一下Init
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef int ElemType;
  4. #define M 20
  5. typedef struct
  6. {
  7.     ElemType elem[M];
  8.     int top;
  9. }Seqstack;
  10. void Init(Seqstack *s)//初始化;
  11. {
  12.     //s=(Seqstack *)malloc(sizeof(Seqstack));//定义栈空间;
  13.     s->top=-1;
  14. }
  15. void Push (Seqstack *s,ElemType e)
  16. {
  17.    
  18.     if (s->top==M-1) exit(0);
  19.     s->elem[++s->top]=e;
  20. }
  21. int Pop(Seqstack *s,ElemType *e)
  22. {
  23.     if (s->top==-1) return 0;
  24.     *e=(s->elem[s->top--]);
  25. }
  26. int main()
  27. {
  28.         
  29.     int n,i,e;
  30.     Seqstack *s;
  31.     s=(Seqstack*)malloc(sizeof(Seqstack));
  32.     Init (s);
  33.     //Empty(s);
  34.     printf ("请输入十进制数 :");
  35.     scanf ("%d",&n);
  36.     while (n)//int型;
  37.     {
  38.         Push(s,n%8);
  39.         n = n/8;
  40.     }
  41.    
  42.     printf ("输出八进制数 :");
  43.     while(s->top!=-1)
  44.    
  45.     {
  46.         Pop(s,&e);
  47.         printf ("%d",e);
  48.     }
  49.    
  50.     return 0;                        
  51. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-28 07:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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