复杂....中国人恐怕都在钻这个东西了.
#include <conio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*可以把2~36进制的数转换为十进数*/
int main()
{
int i,soc,len,*num,yn;
unsigned long int res;
char s[100];
printf("Please input the system of computation:\n"); //原数制2~36
scanf("%d",&soc);
printf("Please input the source data:\n"); //输入原数
scanf("%s",s);
len=strlen(s);
/*printf("STRint LENght=%d\n",len);
strlwr(s);
puts(s);*/
num=calloc(len,sizeof(int));
if(soc>=2 && soc <=10)
for(i=0;i<len;i++)
{
if(s[i]>='0'&&s[i]<='0'+soc-1)
num[i]=s[i]-'0';
else
{
printf("Input Error!\n");
goto end;
}
}
else if(soc>10 && soc<=36)
for(i=0;i<len;i++)
{
if(s[i]>='0'&&s[i]<='9')
num[i]=s[i]-'0';
else if(s[i]>='a'&&s[i]<='a'+soc-11)
num[i]=s[i]-'a'+10;
else
{
printf("Input Error!\n");
goto end;
}
}
else
{
printf("The system of computation ERROR!");
goto end;
}
res = num[0];
for(i=1;i<len;i++)
{
res*=soc;
res+=num[i];
}
printf("The result=%d\n",res); //十进制结果
end: ;
return 0;
}
[[italic] 本帖最后由 许一民 于 2007-12-18 14:38 编辑 [/italic]]