UVa 748 Exponentiation

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>

using namespace std;

#ifndef BIGN_H_INCLUDED
#define BIGN_H_INCLUDED

#define MAX_N 3000 //最大位数

//大数
struct bign {
    char s[MAX_N];
    int len;
    bign() {
        memset(s, 0, sizeof(s));
        len = 1;
    }
    bign(int num) {
        *this = num;
    }
    bign(const char *num) {
        *this = num;
    }
    bign operator = (const char *num) {
        len = strlen(num);
        for(int i=0; i<len && num[len-1-i]>='0' && num[len-1-i]<='9'; i++) {
            s[i] = num[len-1-i] - '0';
        }
        while(s[len-1] == 0) len--;
        return *this;
    }
    bign operator = (int num) {
        char s[MAX_N];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }
    bign operator + (const bign &b) const
    {
        bign c;
        int m = len > b.len ? len : b.len;
        int i, g, sum;
        for(i=0, g=0; g || i< m; i++) {
            sum = g;
            if(i < len) sum += s[i];
            if(i < b.len) sum += b.s[i];
            c.s[i] = sum%10;
            g = sum / 10;
        }
        c.len = i;
        return c;
    }

    bign operator += (const bign &b)
    {
        *this = *this + b;
        return *this;
    }

    bign operator * (const bign &b) const
    {
        bign c;
        int i, j, g, sum;
        for(i=0; i<b.len; i++) {
            g = 0;
            for(j=0; g||j<len; j++) {
                sum = c.s[i+j] + g;
                if(j < len) sum += b.s[i]*s[j];
                c.s[j+i] = sum % 10;
                if(c.s[j+i] > 0) c.len = i+j+1;
                g = sum / 10;
            }
        }
        return c;
    }

    bign operator *= (const bign &b)
    {
        *this = *this * b;
        return *this;
    }

    bool operator < (const bign &b) const
    {
        if(b.len != len) return len < b.len;
        int i;
        for(i=len-1; i>=0; i--) {
            if(s[i] != b.s[i]) return s[i] < b.s[i];
        }
        return false;
    }


    bool operator > (const bign &b) const { return b < *this;}


    bool operator <= (const bign &b) const {return !(*this > b);}


    bool operator >= (const bign &b) const {return !(*this < b);}


    bool operator != (const bign &b) const {return (*this < b) || (*this > b);}


    bool operator == (const bign &b) const {return !(*this < b) && !(*this > b);}

};
ostream& operator << (ostream& outstream, const bign& b)
{
    for(int i=b.len-1; i>=0; i--) {
        outstream << (int)b.s[i] << "";
    }
    return outstream;
}


istream& operator >> (istream& instream, bign& b)
{
    string s;
    instream >> s;
    b = s.c_str();
    return instream;
}

//实数
struct real {
    bign _b; //保证末尾不为0
    unsigned int _dot;
    real(const bign b, const unsigned int dot) {
        _b = b;
        _dot = dot;
    }

};
ostream& operator << (ostream& outstream, const real& r)
{
    int i = r._dot - r._b.len;
    int j;
    if(i >= 0) {
        outstream << ".";
        for(j=0; j<i; j++) {
            outstream << "0";
        }
        outstream << r._b;
    } else {
        int l = r._b.len - 1;
        for(j=0; j>i; j--) {
            outstream << (int)r._b.s[l--];
        }
        if(l >= 0) outstream << ".";
        while(l >= 0) {
            outstream << (int)r._b.s[l--];
        }
    }
    return outstream;
}


#endif // BIGN_H_INCLUDED

char d[100];
int n;

#define MAX_INT ((1u<<(sizeof(int)*8-1))-1)
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif
    char *p, *q;
    int i, l, sum_dot, dot;
    bign a, sum;
    while(scanf("%s %d", d, &n) == 2) {
        //printf("%s %d\n", d, n);
        l = strlen(d);
        p = strchr(d, '.');
        dot = l - (p - d) - 1; //小数位数
        q = p;
        while(*(++p)) {  //去除末尾0
            if(*p != '0') q = p;
            *(p-1) = *p;
        }
        *q = 0;
        if(q-d < l-1) {
            dot -= l-1 - (q-d);
        }
        a = d;
        sum = 1;
        sum_dot = 0;
        for(i=0; i<n; i++) {
            sum *= a;
            sum_dot += dot;
        }
        real r(sum, sum_dot); //生成实数
        cout << r << endl;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值