PAT1024科学计数法

#include
int charToInt(char a[10000])
{
    int i,sum = 0;
    for(i = 0;a[i] != '\0';i++)
    {
        sum = sum * 10 + (a[i] - '0');
    }
    return sum;
}
int main()
{
    char str[80000],a[80000],b[80000],outp[80000];
    bool flagA,flagB,endA = false;
    int point;
    int lenA = 0,lenB = 0,i;
    scanf("%s",&str);
    if(str[0] == '+') flagA = true;
    else flagA = false;
    for(i = 1;str[i] != '\0';i++)
    {
        if(str[i] == '.') point = i - 1;
        else if(str[i] == 'E') a[lenA] = '\0',endA = true;
        else if(str[i] == '+') flagB = true;
        else if(str[i] == '-') flagB = false;
        else
        {
            if(endA == false)
            {
                a[lenA++] = str[i];
            }
            else
            {
                b[lenB++] = str[i];
            }
        }
    }
    b[lenB] = '\0';
    int zhi = charToInt(b);
    int outIndex;
    //if zhishu is 0
    if(zhi == 0)
    {
        outIndex = 0;
        for(i = 0;a[i] != '\0';i++)
        {
            if(point != i)
            {
                outp[outIndex++] = a[i];
            }
            else
            {
                outp[outIndex++] = '.';
                outp[outIndex++] = a[i];
            }
        }
        outp[outIndex] = '\0';
    }
    else
    {
        //if flagB is false ,point goes to the left direction


        if(!flagB)
        {
            outp[0] = '0';
            outp[1] = '.';
            outIndex = 2;
            zhi--;
            while(zhi--)
            {
                outp[outIndex++] = '0' ;
            }
            for(i = 0;a[i] != '\0';i++)
            {
                outp[outIndex++] = a[i];
            }
            outp[outIndex] = '\0';
        }
        //if flagB is true,point goes to the right direction
        else
        {
            outIndex = 0;
            if((point + zhi) <= lenA)
            {
                for(i = 0;a[i] != '\0';i++)
                {
                    if(i != (point+zhi))
                       {
                            outp[outIndex++] = a[i];
                       }
                    else
                        {
                            outp[outIndex++] = '.';
                            outp[outIndex++] = a[i];
                        }
                }
                outp[outIndex] = '\0';
            }
            else
            {
                outIndex = 0;
                for(i = 0;a[i] != '\0';i++)
                {
                    outp[outIndex++] = a[i];
                }
                int count = point+zhi-lenA;
                while(count--)
                {
                    outp[outIndex++] = '0';
                }
                outp[outIndex] = '\0';
            }
        }
    }


    if(flagA) puts(outp);
    else
    {
        printf("-");
        puts(outp);
    }
   // puts(a);
    //puts(b);
    return 0;
}

你可能感兴趣的:(PAT,pat1024科学计数法,c++,c语言)