#include"stdio.h"
#include"string.h"
#include"math.h"
long any_convert(char b[],char a[],int i)
{
int m,j,c;
long sum=0,n;
char k[100];
m=strlen(b);
printf("m=%d",m);
j=0;
for(c=m-1;c>=0;c--)
{
n=(int)pow(i,c);
sum=sum+(b[c]-48)*n;
}
return sum;
}
void convert_any(long a,char p[],int m)
{
int i=0,k,j=0,r;
char t[10];
while(a>0)
{
r=a%m;
if(r<10)t[i]=r+'0';
else t[i]=r-10+'A';
a=a/m;
i++;
}
for(k=i-1;k>=0;k--,j++)
p[j]=t[k];
p[j]='\0';
}
main()
{
long a,res;
int i,j,m;
char b[100],n[100],c[100];
printf("1: any itoal convert itoal 10\n2:10 itoal convert any atoal\n");
scanf("%d",&j);
switch(j)
{
case 1:
printf("please output c:");
scanf("%s",&c);
printf("1: itoal 2 convert\n2: itoal 8 convert\n3: itoal 16 convert\n");
printf("input 1 to 3:");
scanf("%d",&m);
switch(m)
{
case 1:res=any_convert(c,n,2);break;
case 2:res=any_convert(c,n,8);break;
case 3:res=any_convert(c,n,16);break;
}
printf("the result is:");
printf("%ld",res);break;
case 2:
printf("please output a :");
scanf("%ld",&a);
if(a<=0)
{
printf("Error!!");
exit(0);
}
printf("please choose which you want:\n");
printf("1:to itoal 16\n2: to itoal 8\n3:to itoal 2\n");
printf("input between 1 to 4:");
scanf("%d",&i);
switch(i)
{
case 1:convert_any(a,b,16);break;
case 2:convert_any(a,b,8);break;
case 3:convert_any(a,b,2);break;
}
printf("the result is:");
puts(b);break;
}
}
为什么运行res=any_convert(c,n,2);时有时候是对的,有时候就是错的`
例如用数10111000进行测试的时候就有错误了~
大家帮忙帮我看一下~小女感激不尽~~麻烦各位大虾了~~