#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 ==========
答案 0 :(得分:2)
pow
和log
期望加倍或浮动。有一个演员,它适合我:
exponent = floor(log((double)input)/log((double)base));
input = input - pow((double)base, (double)exponent);
最初我收到一条错误,指出编译器无法解决各种过载之间的歧义(在VS 2008上)
答案 1 :(得分: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");
}
答案 2 :(得分: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]);
}
答案 3 :(得分:-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]);
}