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

long int input;
long int base, exponent;

void convert(long int x,long int y)
{
    input = x;
    base = y;

    while (input > 0){
        exponent = floor(log(input)/log(base));
        input = input - pow(base, exponent);
    }
}

int main(void){
    scanf("%d", &input);
    scanf("%d", &base);
    convert(input, base);
}

我试图编译上面的代码以查看是否可以将十进制转换为不同的基数,但出现以下错误:

assignment2.c(14): error C2143: syntax error : missing ';' before 'type'

我已经挣扎了几个小时,但我不知道这是指什么。我怎样才能解决这个问题?

编辑:这是整个信息

1>------ Build started: Project: assignment2, Configuration: Debug Win32 ------
1>Build started 2012-04-27 오후 5:45:10.
1>InitializeBuildStatus:
1>  Touching "Debug\assignment2.unsuccessfulbuild".
1>ClCompile:
1>  assignment2.c
1>c:\users\조화수\documents\visual studio 2010\projects\assignment2\assignment2\assignment2.c(14): error C2143: syntax error : missing ';' before 'type'
1>  assign2.c
1>c:\users\조화수\documents\visual studio 2010\projects\assignment2\assignment2\assign2.c(13): warning C4244: '=' : conversion from 'double' to 'long', possible loss of data
1>c:\users\조화수\documents\visual studio 2010\projects\assignment2\assignment2\assign2.c(14): warning C4244: '=' : conversion from 'double' to 'long', possible loss of data
1>c:\users\조화수\documents\visual studio 2010\projects\assignment2\assignment2\assign2.c(19): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>c:\users\조화수\documents\visual studio 2010\projects\assignment2\assignment2\assign2.c(20): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files\microsoft visual studio 10.0\vc\include\stdio.h(304) : see declaration of 'scanf'
1>  Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.98
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
4

4 回答 4

2

powlog期望双倍或浮动。有了演员表,它对我有用:

exponent = floor(log((double)input)/log((double)base));
input = input - pow((double)base, (double)exponent);

最初我收到一个错误,指出编译器无法解决各种超载之间的歧义(在 VS 2008 上)

于 2012-04-27T09:44:31.610 回答
0

包括前面代码中的小数部分

#include<stdio.h>
main()
{
      int rem,m=0,i,n;
      int a[100],base;
      float num,t,j;
      scanf("%f%d",&num,&base);
      n=(int)num;
      t=num-n;
      j=1.0/base;
      while(n){
               rem=n%base;
               if(rem<10)
               a[m++]=rem+'0';
               else
               a[m++]='A'+rem-10;
               n/=base;
      }
      while(m--)
      printf("%c",a[m]);
      printf(".");
      m=0;
      while(t>0.00001){
                       rem=0;
                        if(t>j)
                        {for(i=1;(j*i)<t;i++);
                        rem=i-1;}
                        if(rem==0)
                        a[m++]=0;
                        else if(rem<10)
                        a[m++]=rem+'0';
                        else
                        a[m++]='A'+rem-10;                        
                        t-=(j*rem);
                        j/=base;
      }
      for(i=0;i<m;i++)
      printf("%c",a[i]);
}
于 2014-05-09T01:57:39.093 回答
0

我的朋友怎么样 :)?

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

int CB, DB;
 void base(void)
 {
   int adad2[100], i=-1,j;
   char adad1[100], ch;
   long int num1=0, num2=0;

   printf("Enter your num: ");
   scanf("%c", &ch);

   do
     {
       i++;
       scanf("%c", &adad1[i]);
     } while(adad1[i]!='\n');

   j=i-1;
   for(i=j;i>=0;i--)
     { //converts the base to 10.                                                                                                                                 
       if(adad1[i]<='9'&& adad1[i]>='0')
     {
       num1+=((long int)pow((float)CB,(j-i)))*(((int)adad1[i])-48);  //converting ascii code to num                                         
     }
       else if(adad1[i]<='Z'&&adad1[i]>='A')
     {
       num1+=((long int)pow((float)CB,(j-i)))*(((int)adad1[i])-55);
     }
       else if(adad1[i]<='z'&&adad1[i]>='a')
     {
       num1+=((long int)pow((float)CB,(j-i)))*(((int)adad1[i])-87);
     }
     }

   i=0;
   while(num1>=DB)
     { //converts the base to b. (START)                                                                                                                             
       adad2[i]=num1%DB;
       i++;
       num1/=DB;
     }

   adad2[i]=num1; //converts the base to b. (END)                                                                                                                                
   printf("\nResult: \n");

   for(;i>=0;i--)
     { //prints the result.                                                                                                                                          
       if(adad2[i]<=9&&adad2[i]>=0){
     printf("%d",adad2[i]);
       }

       else if(adad2[i]>=10&&adad2[i]<=35){
     printf("%c",(char)(adad2[i]+55));
       }
     }
 }

 void main(void)
 {
   printf("Guide: You must enter \"Current Base & Desired Base\" between 2 adn 32...");
   printf("\nEnter current base: ");
   scanf("%d", &CB);

   printf("\nEnter desired base: ");
   scanf("%d", &DB);

   base();
   getchar();
   printf("\n");

 }
于 2013-12-02T19:58:49.933 回答
-1

如果你想从十进制转换为任何高达 36 的基数,试试这个

#include<stdio.h>
main()
{
      int n,rem,m=0,i;
      int a[100],base;
      scanf("%d%d",&n,&base);
      while(n){
               rem=n%base;
               if(rem<10)
               a[m++]=rem+'0';
               else
               a[m++]='A'+rem-10;
               n/=base;
      }
      while(m--)
      printf("%c",a[m]);
}
于 2014-05-08T08:54:41.303 回答