0

选择号 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("\a\nInvalid input.\n");
            continue;
        }
    }
}

void printmenu(void)
{
    printf("\n3 - Decimal -> Binary");
    printf("\n19 - Exit Program\n");
}

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");
}
4

3 回答 3

1
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 位足以存储所有二进制数字。

于 2012-07-06T05:28:32.617 回答
0

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

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] != '\0')
    {
        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: %ld\n",str,base,number);
    system("pause");
 }

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

于 2012-07-06T05:37:34.953 回答
0

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

#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);
}
于 2012-07-06T08:28:19.580 回答