• 大整数类放假回来练笔


    View Code
    // bigMultiply.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include <string>
    #include<cstdio>
    #include <iostream>
    using namespace std;
    const int MaxN=1000;
    int MAX(int a,int b)
    {
        return a>b?a:b;
    }
    
    
    class BigN
    {
    private:
        int len,BigArr[MaxN];
    public:
        BigN() {memset(BigArr,0,sizeof(BigArr));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;i++)
                BigArr[i]=Num[len-i-1]-'0';
            return *this;
        }
        BigN operator=(int num)
        {
            char s[MaxN];
            sprintf(s,"%d",num);
            *this=s;
            return *this;
        }
        BigN& operator =(const BigN& Num)
        {
            if(this==&Num)
                return *this;
            len=Num.len;
            for(int i=0;i<len;i++)
                BigArr[i]=Num.BigArr[i];
            return *this;
        }
        string str() const
        {
            string res="";
            for(int i=0;i<len;i++)
                res=(char)(BigArr[i]+'0')+res;
            if(res=="") res="0";
            return res;
        }
        BigN operator+(const BigN& b) const
        {
            BigN c;
            c.len=0;
            int Carry=0;
            for(int i=0;i< MAX(len,b.len);i++)
            {
                int s=Carry;
                if(i<len) s+=BigArr[i];
                if(i<b.len) s+=b.BigArr[i];
                c.BigArr[c.len++]=s%10;
                Carry=s/10;
            }
            return c;
        }
        BigN operator+=(const BigN& b)
        {
            *this=*this+b;
            return *this;
        }
       void clean() 
        {
            while(len > 1 && !BigArr[len-1]) len--;
        }
        BigN operator-(const BigN b) const
        {
            BigN c;
            c.len=0;int i=0;
            int Carry=0;
            for(;i<b.len;i++)
            {
                int s=BigArr[i]+Carry;
                if(BigArr[i]==0&&Carry==-1) s=9;
                else  Carry=0;
                if(s<b.BigArr[i])
                {
                    Carry--;
                    s+=10;
                }
                s=s-b.BigArr[i];
                c.BigArr[c.len++]=s;
            }
            for(;i<len;i++) 
            {
                if(Carry==-1&&BigArr[i]==0) 
                    c.BigArr[c.len++]=9;
                else
                    break;
            }
            c.BigArr[c.len++]=BigArr[i]-1;
            for(;i<len;i++) 
                c.BigArr[c.len++]=BigArr[i];
                c.clean();
                return c;
        }
        BigN operator-=(const BigN b)
        {
            *this=*this-b;
            return *this;
        }
    
        BigN operator* (const BigN& b)
        {
            BigN c;
            c.len=len+b.len;
            for(int i=0;i<len;i++)
            {
                for(int j=0;j<b.len;j++)
                {
                    c.BigArr[i+j]+=BigArr[i]*b.BigArr[j];
                }
            }
            for(int i=0;i<c.len-1;i++)
            {
                c.BigArr[i+1]+=c.BigArr[i]/10;
                c.BigArr[i]%=10;
            }
            c.clean();
            return c;
        }
    
    
        bool operator<(const BigN& b) const
        {
            if(len!=b.len) return len<b.len;
            for(int i=len-1;i>=0;i--)
                if(BigArr[i]!=b.BigArr[i])
                    return BigArr[i]<b.BigArr[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);
        }
    friend istream& operator>>(istream &in,BigN& x)
    {
        string s;
        in>>s;
        x=s.c_str(); 
        return in;
    }
    
    friend ostream& operator<<(ostream& out,BigN& x)
    {
        out<<x.str();
        return out;
    }
    ~BigN() {len=0;memset(BigArr,0,sizeof(BigArr));}
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        BigN tempA="555553";
        BigN tempB="32134";
        BigN temp=tempA*tempB;
        cout<<temp;
    
        return 0;
    }
  • 相关阅读:
    Android -- 桌面悬浮,仿360
    android 定时短信app之时间选择器(一)
    如何在Android中为TextView动态设置drawableLeft等
    装饰器3(装饰函数带参数)
    装饰器2(被装饰函数自带参数)
    装饰器1(被装饰函数不带参数)
    工资管理系统(初级实现)
    购物车程序
    python中read、readline、readlines之间的区别
    深复制与浅复制的区别
  • 原文地址:https://www.cnblogs.com/cslave/p/2626944.html
Copyright © 2020-2023  润新知