1

我编写了一个程序,要求用户使用字符串输入一个数字,然后程序将该数字转换为十进制,但是当我编译(使用 -lm)并运行 a.out 时,我遇到了问题,我得到一个分段错误(核心转储),不确定在哪里查看或如何修复它,还有一个问题我需要什么才能打印转换结果(printf(“something..”))?

#include <stdio.h>
#include <string.h>
#include <math.h>

int main()
{

char string[100];
int s;
char a;
char j;
int sum;

printf("B = B to D\n");
printf("D = D to B\n");
printf("choose which one to convert to:");
scanf("%c%c", &a, &j);

if (a == 'B')
{
    printf("enter binary number to convert to decimal: ");
    scanf("%s", string);

    for(s = strlen-1; s >= 0; s--)
    {

            if(string[s] == '1')
            {
            sum = sum + pow(2,s);

            }
    }

}

return 0;
4

5 回答 5

1

你可能打算拥有strlen(string) - 1,不是strlen - 1。我最好的猜测是你的程序被解释strlen为一个函数指针,而且在那之后会发生疯狂的事情。

实际上,您可能对strtol函数感兴趣,它似乎完全符合您的要求。

于 2012-08-15T14:09:18.267 回答
0

strlen 使用不正确。我认为您想做类似 strlen(string)-1 的事情。顺便说一句,您的逻辑无法转换小数部分。检查此代码:

#include <stdio.h>
#define MAX 1000

int main()
{
    double fraDecimal=0.0,dFractional=0.0 ,fraFactor=0.5;
    long dIntegral = 0,bIntegral=0,bFractional[MAX];
    long intFactor=1,remainder,i=0,k=0,flag=0;
    char fraBinary[MAX];

    printf("Enter any fractional binary number: ");
    scanf("%s",&fraBinary); 
    while(fraBinary[i]) //Separating the integral and fractional parts
    {

      if(fraBinary[i] == '.')
          flag = 1; //If dot is found start taking the fractional part.
      else if(flag==0)
          bIntegral = bIntegral * 10 + (fraBinary[i] -48); 
          /* char - 48 to get the numerical value.*/
      else
          bFractional[k++] = fraBinary[i] -48;
      i++;
    }

    while(bIntegral!=0){
     remainder=bIntegral%10;
     dIntegral= dIntegral+remainder*intFactor;
     intFactor=intFactor*2;
     bIntegral=bIntegral/10;
    }

    for(i=0;i<k;i++){
     dFractional  = dFractional  + bFractional[i] * fraFactor;
     fraFactor = fraFactor / 2;
    }

    fraDecimal = dIntegral + dFractional ;

    printf("Equivalent decimal value: %Lf",fraDecimal);   
    return 0;
}

来源: 将二进制转换为十进制数的 C 程序

于 2013-09-01T11:45:03.980 回答
0

您使用 strlen 作为整数。我想你的意思是 strlen(string)

于 2012-08-15T14:12:36.837 回答
0
for(sum=0, j=0, s=strlen(string)-1; s >= 0; s--, ++j){
    if(string[s] == '1'){
        sum = sum + pow(2,j);
    }
}
printf("%d\n",sum);
于 2012-08-15T19:34:20.347 回答
0
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>

void reverse_string(char *string)
{
   int string_length = strlen(string);
   char temp;
   int i;
   for (i = 0; i < string_length/2; i++)
   {
      temp = string[i];
      string[i] = string[string_length - (i + 1)];
      string[string_length - (i + 1)] = temp;
   }
}

int main()
{

   char string[100];
   int s;
   char a;
   char j;
   int sum = 0;
   int string_length = 0;
   int number, original_number;
   int remainder;
   char binary_string[200];
   int i = 0;

   printf("B = B to D\n");
   printf("D = D to B\n");
   printf("choose which one to convert to:");
   scanf("%c%c", &a, &j);
   a = toupper(a);
   if (a == 'B')
   {
      printf("enter binary number to convert to decimal: ");
      scanf("%s", string);

      string_length = strlen(string);
      for(s = strlen(string)-1; s >= 0; s--)
      {
         if(string[s] == '1')
         {
            sum = sum + pow(2,string_length - (s + 1));
         }
      }
      printf("%s in binary is %d\n",string,sum);
   }
   else if (a == 'D')
   {
      printf("enter positive decimal number to convert to binary: ");
      scanf("%s",string);
      number = atoi(string);
      original_number = number;
      if ( number < 0 )
      {
         printf("ERROR: only positive numbers please\n");
         return 1;
      }
      do
      {
         remainder = number % 2;
         if ( remainder == 0 )
            binary_string[i] = '0';
         else
            binary_string[i] = '1';
         number = number / 2;
         i += 1;
      }
      while (number > 0);
      binary_string[i] = '\0';
      reverse_string(binary_string);
      printf("decimal %d is %s in binary\n",original_number,binary_string);
   }
   return 0;
}
于 2012-08-15T16:48:30.183 回答