使用数组和堆栈进行十进制到二进制转换

问题描述 投票:0回答:6

这是我编写的 C 程序,用于将十进制数转换为等效的二进制数。我使用了 Stack(使用数组实现)和以下算法:

数字被除,余数被压入堆栈。 一次弹出一个余数并转换为二进制

问题是该程序对于 3 以内的数字运行良好,从 4 开始,每个二进制数都比实际数字少 1。

// Decimal to Binary conversion using Stack
#include<stdio.h> 
#include<math.h>

#define max 20

int top=-1, stk[max];
void push(int);
int pop(void);

int main() 
{
     int i,num,x,flag=0,s, bin=0, factor;
     printf("Enter any decimal number: ");
     scanf("%d",&num);
     while(num>0)
     {
         if(num==1)
             push(num);
         else
         {
             x = num%2;
             push(x);
          }
         num/=2;
         flag++;
     }

for(i=0;i<flag;i++)
{
    s = pop();
    bin = bin + s*pow(10,(flag-1-i));
}

printf("\nEquivalent Binary number is --> %d",bin);
return 0;
}

void push(int n)
{
     if(top == max-1)
     {
          printf("Error! Overflow");
          return;
     }
     stk[++top] = n;
 }

 int pop(void)
 {
     int y;
     if(top == -1)
     {
          printf("Error! Underflow");
          return;
     }
     y = stk[top];
     top = top-1;
     return y;
  }

有人能帮我找到逻辑缺陷吗?

谢谢你

c arrays stack
6个回答
0
投票

我的答案是你的程序不必要地复杂。

#include<stdio.h> 

int main() 
{
    unsigned num, i, zeros = 0;
    printf("Enter a decimal number: ");
    scanf("%u", &num);
    printf ("Decimal %u in binary is ", num);
    for (i=sizeof(unsigned)*8; i>0; i--)
    {
        if ((int)num < 0)           // get MSB
            zeros = printf ("1");   // cancel 0-suppresion
        else if (zeros)
            printf ("0");
        num <<= 1;
    }
    printf ("\n");
    return 0;
}

0
投票

函数 pow 返回一个 double,小数点后可以有 9999999...,当它转换为 int 时,它会四舍五入到下限,您可以使用 ceil() 函数来解决您的问题,该函数返回最小的整数值大于或等于参数,就像这样。

bin = bin + ceil(s*pow(10,(flag-1-i)));

0
投票
 //C Program to convert Decimal to binary using Stack

 #include<stdio.h>

 #define max 100

 int stack[max],top=-1,i,x;

         /*------ Function Prototype------------*/
  void push (int x)
  {
    ++top;
    stack [top] = x;

   }
  int pop ()
   {
    return stack[top];

   }

          /*-------------------------------------*/
  void  main()
  {
    int num, total = 0,item;
    printf( "Please enter a decimal: ");
    scanf("%d",&num);

    while(num > 0)
     {
       total = num % 2;
       push(total);
       num /= 2;
     }

    for(i=top;top>-1;top--)
    {
     item = pop ();
     printf("%d",item);
    }
  }

0
投票

这是上述程序的简单版本

    int main(){
    int n,remainder;
    printf("Enter a decimal number:");
    scanf("%d",&n);
    while(n!=0){
        remainder = n%2;
        n = n/2;
        push(remainder); // inserting in stack
    }
    display(); // displaying the stack elements
}

参考上述代码 C 程序使用堆栈将十进制数转换为二进制数


0
投票

所以我对几个数字进行了数学计算,这似乎是正确的。我同意其他人的观点,即这不必要地复杂,但这本身并不会导致您的问题,它只是让它们更难找到。

因此,从逻辑的角度来看,该程序的输出看起来是正确的。让我们看看其他潜在的问题:

  1. 您正在使用初始化为 -1 的 int 来索引数组
  • 这是不好的做法,而且没有必要。 C 中的数组索引永远不能为负数,因此编译器会假设这是一个无符号数,因此如果您有一个 32 位处理器,它会假设您正在尝试获取 array[2^32 - 1],这不是你想要什么。始终对数组索引使用无符号值

  • 可能发生的情况,我不确定,是你的编译器正在幕后做一些事情,这会搞砸你的程序,这真的很难说。但在您进行加法之前,它可能会尝试将您的负数转换为无符号整数。通过将 top 声明更改为:

    来解决此问题

    无符号整型顶部 = 0;

并更改您访问顶部的位置:

 stk[++top] = n;

 stk[top++] = n;

你也必须改变

 y = stk[top];
 top = top-1;

 top = top-1;
 y = stk[top];

我想说从那里开始。我还建议删除 pow 行,并单独打印数组的每一部分,因为它将以相同的方式输出,并且您已经拥有了所有信息。

PRINTF("%d%d%d",stk[2],stk[1],stk[0]);

0
投票

表示十进制到二进制转换的堆栈:(56)10 到 (---)2

© www.soinside.com 2019 - 2024. All rights reserved.