Exponentiation

#include <stdio.h>

#include <stdlib.h>

#include <memory.h>

char srcR[7], srcn[3];//string length, +1

int  dstR[5], dstn[2], point, n;//point记录小数点位置,n记录dstn的int形式;

int  ans[200], result[200];

int main()

{

    while (1)

    {

        if (scanf("%s", srcR) == EOF)

            break;

        getchar();

        scanf("%s", srcn);

        getchar();



        point = -1;

        int i, j;

        for (i = 0; i < 5; i++)

            dstR[i] = -1;

        for (i = 0; i < 2; i++)

            dstn[i] = -1;

        for (i = 0; i < 200; i++)

        {

            ans[i] = 0;

            result[i] = 0;

        }



        for (i = 0; srcR[i] != '.' && i < strlen(srcR); i++);

            if (srcR[i] == '.')

                point = i;



        j = 0;

        int Rtail;

        int hasPoint = 0;

        for (i = 0; i < strlen(srcR); i++)

            if (srcR[i] != '.')

                dstR[j++] = srcR[i] - '0';

            else

                hasPoint = 1;

        if (hasPoint && srcR[i-1] != '.')

            Rtail = i - 2;//记录R的结束,小数点占一位,循环多加了1,所以要减2

        else if (hasPoint && srcR[i-1] == '.')

            Rtail = i - 3;//结尾是小数点,要在多减1;

        else//无小数点

            Rtail = i - 1;

        j = 199;

        for (i = Rtail; i >= 0; i--)

            ans[j--] = dstR[i];

        for (i = 0; i < strlen(srcn); i++)

            dstn[i] = srcn[i] - '0';

        if (i == 2)

            n = dstn[0] * 10 + dstn[1];

        if (i == 1)

            n = dstn[0];



        int isZero = 1;

        for (i = 0; i < 6; i++)

            if (dstR[i] != 0 && dstR[i] != -1)

                isZero = 0;

        if (n == 0)

        {

            if (!isZero)

                printf("1\n");

            continue;

        }

        if (isZero)

        {

            if (n != 0)

                printf("0\n");

            continue;

        }

        //ans * dstR,dstR的每一位放在循环,ans放内循环

        int k;

        for (k = 0; k < n - 1; k++)

        {

            for (j = Rtail; j >= 0 ; j--)

            {

                for (i = 199; i >= 1; i--) // i>=1;

                {

                    result[i - (Rtail - j)] += ans[i] * dstR[j];

                    if (result[i - (Rtail - j)] >= 10)

                    {

                        result[i - (Rtail - j) - 1] += result[i - (Rtail - j)] / 10;

                        result[i - (Rtail - j)] = result[i - (Rtail - j)] % 10;

                    }

                }

            }

            for (i = 0; i < 200; i++)

            {

                 ans[i] = result[i];

                 result[i] = 0;//不要忘了清零;

            }

        }

        if (hasPoint)

        {

            point = (strlen(srcR) - point - 1) * n;

            point = 200 - point;

        }

        int start = 0, end = 0;

        //循环,直到遇到非零或小数点

        for (i = 0; i < 200; i++)

            if (ans[i] != 0)

                break;

        start = i;

        if (point < start && hasPoint)

            start = point - 1;

        if (hasPoint)

        {

            for (i = 199; i >= point; i--)

                if (ans[i] != 0)

                    break;

            end = i;

        }

        else

        {

            end = 199;

        }

        if (ans[start] == 0 && point == start + 1)

            start++;

        for (i = start ; i <= end; i++)

        {

            if (i == point && hasPoint)

                printf(".");

            printf("%d", ans[i]);

        }

        printf("\n");

    }

    return 0;

}

  

你可能感兴趣的:(exp)