当前位置: 代码迷 >> C语言 >> 超难想!!!!想来试试就快来看看吧!!!
  详细解决方案

超难想!!!!想来试试就快来看看吧!!!

热度:221   发布时间:2007-12-13 16:07:38.0
复杂....中国人恐怕都在钻这个东西了.
----------------解决方案--------------------------------------------------------
#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]]
----------------解决方案--------------------------------------------------------
  相关解决方案