数字系统转换器不适用于特定编译器 (Dev-C++)

Number systems converter doesn't work on a specific compiler (Dev-C++)

本文关键字:编译器 Dev-C++ 系统 转换器 不适用 适用于 数字      更新时间:2023-10-16

>选择数字 3(十进制到二进制) 具体来说,它适用于 Code:Blocks,还没有尝试过 Visual C++,但我们学校使用 Dev-C++,我无法弄清楚是什么导致了这个问题。我的意思是它在逻辑和语法上都是正确的。

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define MAXDIGITS 100
void printmenu(void);
void getchoice(void);
void decitobinary(int str);

int main(void)
{
    char choice;
    printmenu();
    getchoice();
    system("cls");
    while(1)
    {
        printf("nAgain? y/n: ");
        scanf("n%c",&choice);
        if(choice == 'y' || choice == 'Y')
        {
            system("cls");
            main();
        }
        else if(choice == 'n' || choice == 'N')
            exit(EXIT_SUCCESS);
        else
        {
            printf("anInvalid input.n");
            continue;
        }
    }
}
void printmenu(void)
{
    printf("n3 - Decimal -> Binary");
    printf("n19 - Exit Programn");
}
void getchoice(void)
{
    int choice;
    char number[MAXDIGITS];
    int digits;
    printf("nEnter choice: ");
    scanf("n%d",&choice);
    switch(choice)
    {
        case 3:
        {
            system("cls");
            printf("Enter number: ");
            scanf("n%d",&digits);
            decitobinary(digits);
            break;
        }
        case 19:
            exit(EXIT_SUCCESS);
    }
}

void decitobinary(int str)
{
    int arraycntr = 0;
    int number[arraycntr];
    printf("n%d in binary is: ",str);
    while(str > 0)
    {
        number[arraycntr] = str % 2;
        str /= 2;
        ++arraycntr;
    }
    for(arraycntr -= 1;arraycntr >= 0;arraycntr--)
    {
        printf("%d",number[arraycntr]);
    }
    printf("n");
    system("pause");
}
void decitobinary(int str)
{
    int arraycntr = 0;
    int number[arraycntr];
    printf("n%d in binary is: ",str);
    while(str > 0)
    {
        number[arraycntr] = str % 2;
        str /= 2;
        ++arraycntr;
    }
    for(arraycntr -= 1;arraycntr >= 0;arraycntr--)
    {
        printf("%d",number[arraycntr]);
    }
    printf("n");
    system("pause");
}

问题出在数字数组的初始化中。它应该是这样的:

int number[33];

33 位足以存储所有二进制数字。

这不是

答案。 只是需要考虑的事情。 对于相同的功能,您有大约 10 个相同的实现! 您是否考虑过将十进制和十进制的所有变体合并为一对函数:

void decitobase(int str, int base)
{
    int arraycntr = 33;
    int number[arraycntr] = {};
    printf("n%d in base %d is: ",str, base);
    while(str > 0)
    {
        number[arraycntr] = str % base;
        str /= base;
        ++arraycntr;
    }
    for(arraycntr -= 1;arraycntr >= 0;arraycntr--)
    {
        printf("%d",number[arraycntr]);
    }
    printf("n");
    system("pause");
}
void basetodeci(char str[], int base)
{
    int arraycntr,digitcntr,power;
    int value[MAXDIGITS];
    int number;
    arraycntr = 0;
    digitcntr = 0;
    number = 0;
    power = 1;
    while(str[arraycntr] != '')
    {
        value[arraycntr] = str[arraycntr] - '0'
        ++arraycntr;
    }
    digitcntr = arraycntr - 1;
    for(digitcntr; digitcntr >= 0; digitcntr--)
    {
        number += value[digitcntr] * power;
        power *= base;
    }
    printf("n%s base %d in decimal is: %ldn",str,base,number);
    system("pause");
 }

我不知道上面的代码是否比您提交的代码更正确,但我希望您了解我的意思。 另外不要忘记有一个 C 函数可以将字符串转换为您想要的任何基数中的整数:strtol 和 friends。

您使用的算法非常慢。相反,由于您正在处理二进制数,请使用按位运算符。

#include <stdio.h>
#include <stdlib.h>
#define BITS_N  (sizeof(unsigned int) * 8)
#define MSB     (1 << (BITS_N - 1))

void print_binary (unsigned int val)
{
  for(size_t i=0; i<BITS_N; i++)
  {
    printf("%c", (val & MSB) ? '1' : '0' );
    val <<= 1;
    if( ((i+1)%8) == 0) // print a space every 8th digit
    {
      printf(" ");
    }
  }
}

int main()
{
  unsigned int some_val = 0xBAADBEEF;
  print_binary (some_val);
}