⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inttype.cpp

📁 高精度加法和减法 我自己写的 主要是实现了IntType类
💻 CPP
字号:
#include "IntType.h"



string IntToString(int n)                               //整数转化为string字符串
{
    std::string s,l;
    if(n==0)
    {
        s.push_back('0');
        return s;
    }
    else
    {
        int m;
        char c;
        while(n>0)
        {
            m=n%10;
            c=m+48;
            l.push_back(c);
            n=n/10;
        }
        for(int i=l.size()-1;i>=0;i--)
        {
            s.push_back(l[i]);
        }
        return s;
    }
}

int ws(const vector<int> &a) //求有效位数
{
    int p = 0;
    bool k = false;
    for (int i = 0; i < a.size(); i++) {
        if (k || a[i] != 0) {
            p++;
            k = true;
        }
    }
    return p;
}

void fix( vector<int> & rhs)//格式化
{
    vector<int> a;
    int j=0;
    int n=ws(rhs);
    a.resize(n);
    for(int i=rhs.size()-n;i<rhs.size();i++)
    {
        a[j]=rhs[i];
        j++;
    }
    rhs.resize(n);
    for (int i = 0; i < a.size(); i++)
        rhs[i] = a[i];
}

int compare( vector<int> a, vector<int> b)     //比较两个无符号数的大小
{
    int result;
    fix(a);
    fix(b);
    if(a.size()>b.size())
        return 1;
    else if(a.size()<b.size())
        return -1;
    else
    {
        for(int i=0;i<a.size();i++)
        {
            if(a[i]>b[i])
            {
                return 1;
            }
            else if(a[i]<b[i])
            {
                return -1;
            }
        }
        return 0;
    }
}

vector<int> add( vector<int>  a, vector<int>  b)    //无符号数加法
{
    vector<int> c;
    int x,n,m,jw=0;
    bool ba;
    fix(a);
    fix(b);
    if(a.size()>b.size())
    {
        m=a.size();
        n=b.size();
        ba=true;
    }
    else
    {
        n=a.size();
        m=b.size();
        ba=false;
    }
    for(int i=0;i<m;i++)
    {
        if(n>0)
        {
            x=a[a.size()-i-1]+b[b.size()-i-1]+jw;
            c.push_back(x%10);
            jw=x/10;
            n--;
        }
        else
        {
            if(ba)
            {
                x=a[a.size()-i-1]+jw;
                c.push_back(x%10);
                jw=x/10;
            }
            else
            {
                x=b[b.size()-i-1]+jw;
                c.push_back(x%10);
                jw=x/10;
            }
        }
    }
    if(jw==1)
        c.push_back(1);
    a.resize(c.size());
    for(int i=0;i<a.size();i++)
        a[i]=c[c.size()-i-1];
    return a;
}

vector<int> sub( vector<int>  a, vector<int>  b)    //无符号数减法
{
    vector<int> c;
    fix(a);
    fix(b);
    int x,n=b.size(),m=a.size(),jw=0;
    for(int i=0;i<m;i++)
    {
        if(n>0)
        {
            x=a[a.size()-i-1]-b[b.size()-i-1]-jw;
            if(x<0)
            {
               x+=10;
               jw=1;
               c.push_back(x);
            }
            else
            {
                jw=0;
                c.push_back(x);
            }
            n--;
        }
        else
        {
            x=a[a.size()-i-1]-jw;
            if(x<0)
            {
               x+=10;
               jw=1;
               c.push_back(x);
            }
            else
            {
                jw=0;
                c.push_back(x);
            }
        }
    }
    a.resize(c.size());
    for(int i=0;i<a.size();i++)
        a[i]=c[c.size()-i-1];
    fix(a);
    if(a.size()==0)a.push_back(0);
    return a;
}

ostream & operator<<(ostream & out,const IntType &x)
{
    x.print(out);
    return out;
}

istream & operator>>(istream & in, IntType &x)
{
    x.input(in);
    return in;
}

void IntType::init(const string & s)
{
    if (s[0] == '-')
    {
        signalZ = false; //负数
        storeArray.resize(s.length() - 1);
        for (int i = 0; i < storeArray.size(); i++)
        {
            if (s[i + 1] > 57 || s[i + 1] < 48)
            {
                cout << "Input Error!\n";
                exit(0); //有非法字符
            } 
            else 
            {
                storeArray[i] = s[i + 1] - 48; //字符转化为整数
            }
        }
    }
    else 
    {
        if (s[0] == '+') 
        {
            signalZ=true;
            storeArray.resize(s.length() - 1);
            for (int i = 0; i < storeArray.size(); i++) 
            {
                if (s[i + 1] > 57 || s[i + 1] < 48) 
                {
                    cout << "Input Error!\n";
                    exit(0); //有非法字符
                }
                else 
                {
                    storeArray[i] = s[i + 1] - 48; //字符转化为整数
                }
            }
        } 
        else 
        {
            signalZ = true; //正数
            storeArray.resize(s.length());
            for (int i = 0; i < storeArray.size(); i++)
            {
                if (s[i] > 57 || s[i] < 48) 
                {
                    cout << "Input Error!\n";
                    exit(0);
                }
                else
                {
                    storeArray[i] = s[i] - 48;
                }
            }
        }
    }
    if(storeArray.size()==0)
        storeArray.push_back(0);
}

IntType::IntType(const string & s)
{
    init(s);
}

IntType::IntType()
{
    signalZ=true;
    storeArray.push_back(0);
}

IntType::IntType(const IntType & rhs)
{
    signalZ=rhs.signalZ;
    storeArray.resize(rhs.storeArray.size());
    for(int i=0;i<storeArray.size();i++)
        storeArray[i]=rhs.storeArray[i];
}

IntType::IntType(int n)
{
    string s,l;
    if(n<0)
    {
        s.push_back('-');
        n=-n;
        l=IntToString(n);
        s+=l;
    }
    else
    {
        l=IntToString(n);
        s+=l;
    }
    init(s);
}

void IntType::print(ostream & out=cout)const
{
    if(!signalZ)
        out<<'-';
    for(int i=0;i<storeArray.size();i++)
        out<<storeArray[i];
}

void IntType::input(istream & in=cin)
{
    string c;
    in>>c;
    init(c);
}

void IntType::fix()
{
    vector<int> a;
    int j=0;
    int n=ws(storeArray);
    a.resize(n);
    for(int i=storeArray.size()-n;i<storeArray.size();i++)
    {
        a[j]=storeArray[i];
        j++;
    }
    storeArray.resize(n);
    for(int i=0;i<n;i++)
        storeArray[i]=a[i];
    if(storeArray.size()==0)
        storeArray.push_back(0);
}

const IntType & IntType:: operator= (const IntType & rhs)
{
    if(this!=&rhs)
    {
        signalZ=rhs.signalZ;
        storeArray.resize(rhs.storeArray.size());
        for(int i=0;i<storeArray.size();i++)
            storeArray[i]=rhs.storeArray[i];
    }
    return *this;
}

const IntType & IntType::operator+=(const IntType & rhs)
{
    int k=compare(storeArray,rhs.storeArray);
    vector<int> c;
    if(signalZ&&rhs.signalZ)//同为正
    {
        signalZ=true;
        storeArray=add(storeArray,rhs.storeArray);
    }
    else if(!signalZ&&!rhs.signalZ)//同为负
    {
        signalZ=false;
        storeArray=add(storeArray,rhs.storeArray);
    }
    else if(k==0)//相反数
    {
        signalZ=true;
        storeArray.resize(0);
        storeArray.push_back(0);
    }
    else if(signalZ&&!rhs.signalZ)//第一个为正,第二个为负
    {
        if(k==1)//第一个的绝对值大于第二个
        {
            signalZ=true;
            storeArray=sub(storeArray,rhs.storeArray);
        }
        else if(k==-1)
        {
            signalZ=false;
            storeArray=sub(rhs.storeArray,storeArray);
        }
    }
    else
    {
        if(k==1)
        {
            signalZ=false;
            storeArray=sub(storeArray,rhs.storeArray);
        }
        else if(k==-1)
        {
            signalZ=true;
            storeArray=sub(rhs.storeArray,storeArray);
        }
    }
    fix();
    return *this;
}

const IntType & IntType::operator-=(const IntType & rhs)
{
    IntType a(-rhs);
    *this+=a;
    return *this;
}

IntType IntType::operator+(const IntType & rhs)const
{
    IntType answer(*this);
    answer+=rhs;
    return answer;
}

IntType IntType::operator-(const IntType & rhs)const
{
    IntType answer(*this);
    answer-=rhs;
    return answer;
}

IntType  IntType::operator+( ) const
{
    IntType a(*this);
    a.fix();
    return a;
}

IntType IntType::operator -()const
{
    IntType a(*this);
    if(storeArray.size()!=0)
        a.signalZ=!signalZ;
    a.fix();
    return a;
}

bool IntType::operator <(const IntType & rhs)const
{
    int k=compare(storeArray,rhs.storeArray);
    if(signalZ)
        if(rhs.signalZ)
        {
            if(k==-1)
                return true;
            else
                return false;
        }
        else
            return false;
    else
        if(rhs.signalZ)
            return true;
        else
        {
        if(k==1)
            return true;
        else
            return false;
        }
}

bool IntType::operator ==(const IntType & rhs)const
{
    int k=compare(storeArray,rhs.storeArray);
    if(signalZ&&rhs.signalZ&&k==0||!signalZ&&!rhs.signalZ&&k==0)
        return true;
    else
        return false;
}

bool IntType::operator >(const IntType & rhs)const
{
    if(!(*this<rhs)&&!(*this==rhs))
        return true;
    else
        return false;
}

bool IntType::operator <=(const IntType & rhs)const
{
    return(!(*this>rhs));
}

bool IntType::operator >=(const IntType & rhs)const
{
    return(!(*this<rhs));
}

bool IntType::operator !=(const IntType & rhs)const
{
    return(!(*this==rhs));
}

bool IntType::operator !()const
{
    return(*this==0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -