zoukankan      html  css  js  c++  java
  • 大整数类放假回来练笔

    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;
    }
  • 相关阅读:
    安装Rocky版OpenStack 1控制节点+1计算节点环境部署脚本
    脚本安装Rocky版OpenStack 1控制节点+1计算节点环境部署
    采用脚本自动填写具有交互式命令的方法
    CentOS安装Docker
    WordPress博客搭建与问题总结
    OpenStack端口(15)
    openstack搭建之-horizon配置(14)
    openstack搭建之-创建实例(13)
    openstack搭建之-cinder配置(12)
    openstack搭建之-neutron配置(11)
  • 原文地址:https://www.cnblogs.com/cslave/p/2626944.html
Copyright © 2011-2022 走看看