鱼C论坛

 找回密码
 立即注册
查看: 1395|回复: 5

[技术交流] 大数计算加减法

[复制链接]
发表于 2019-12-28 20:01:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 bin554385863 于 2019-12-28 20:30 编辑

lnum.h
目前只支持同号加法
#ifndef LNUM_H
#define LNUM_H
#include <iostream>
#include <string>
#include <vector>
using std::istream;
using std::ostream;
using std::string;
using std::vector;
class lnum
{
    struct attr
    {
        int sign = 1;          //符号;
        size_t dpsize = 0;     //小数位;
        size_t prec = 0;       //精度
        size_t nsize = 1;      //有效位数
        string str = "0";      //过渡字符串
        vector<int> vec = {0}; //数组;
    };
    struct align
    {
        vector<int> first;

        vector<int> second;
    };

private:
    //小数精度;
    static size_t precision;
    //数据;
    attr num;
    //str to attr
    const attr StrToAttr(const string &str) const;
    //attr to str
    const string AttrToStr() const;
    //进1
    lnum &Carry();
    //借1
    vector<int> Borrow() const;
    //对齐
    const align AlignVec(const vector<int> &m, const vector<int> &n) const;
    //反转正负
    vector<int> Negative() const;
    //clear zero vec
    lnum &ClearZeroVec();
    //vec to str
    lnum &VecToStr();

public:
    lnum(const char *cstr = "0") : num(StrToAttr(cstr))
    {
    }
    lnum(const string &str) : num(StrToAttr(str)) {}
    lnum(const lnum &l) : num(l.num) {}
    //------------------------------------------------------------------
    //设置精度
    static void SetPrecision(const size_t i);
    //展示信息
    void Showinfo() const;
    //>
    const bool operator>(const lnum &l) const;
    //<
    const bool operator<(const lnum &l) const;
    //==
    const bool operator==(const lnum &l) const;
    //>=
    const bool operator>=(const lnum &l) const;
    //<=
    const bool operator<=(const lnum &l) const;
    //+
    const lnum operator+(const lnum &l) const;
    //-
    const lnum operator-(const lnum &l) const;
    //>>
    friend istream &operator>>(istream &is, const lnum &l);
    //<<
    friend ostream &operator<<(ostream &os, const lnum &l);
};
size_t lnum::precision = 0;
#endif

lnum.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
//str to attr
const lnum::attr lnum::StrToAttr(const string &str) const
{
    attr n;
    n.prec = precision;
    n.str = str;
    string &nstr = n.str;
    if (nstr[0] == '-')
    {
        nstr.erase(0, 1);
    }
    if (int(nstr.find('.')) != -1)
    {
        nstr.erase(nstr.find('.'), 1);
    }
    if (nstr == string(nstr.size(), '0'))
    {
        nstr.clear();
        nstr.push_back('0');
    }
    else if (nstr != string(nstr.size(), '0'))
    {
        size_t s = nstr.size(), i = 0;
        if (str[0] == '-')
        {
            n.sign = -1;
        }
        if (int(str.find('.')) != -1)
        {
            n.dpsize = str.size() - str.find('.') - 1;
            if (nstr[0] == '0')
            {

                for (; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                if (i >= s - n.dpsize)
                {
                    nstr.erase(0, s - n.dpsize - 1);
                }
                else if (i < s - n.dpsize)
                {
                    nstr.erase(0, i);
                }
            }
        }
        else
        {
            if (nstr[0] == '0')
            {
                for (i = 0; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                nstr.erase(0, i);
            }
        }
        if (n.prec > n.dpsize)
        {
            nstr.insert(nstr.end(), n.prec - n.dpsize, '0');
        }
        else if (n.prec < n.dpsize)
        {
            nstr.erase(nstr.size() - (n.dpsize - n.prec));
            n.dpsize = n.prec;
        }
        n.vec.clear();
        for (char c : nstr)
        {
            n.vec.push_back(c - 48);
        }
        n.nsize = nstr.size();
    }
    return n;
}
//attr to str
const string lnum::AttrToStr() const
{
    string str = num.str;
    const size_t &prec = num.prec, &dpsize = num.dpsize;
    if (str.size() != 1 && prec != 0)
    {
        if (dpsize == prec)
        {
            str.insert(str.size() - prec, 1, '.');
        }
        else if (dpsize < prec && dpsize != 0)
        {
            str.erase(str.size() - (prec - dpsize));
            str.insert(str.size() - dpsize, 1, '.');
        }
        else if (dpsize == 0)
        {
            str.erase(str.size() - prec);
        }
    }
    if (num.sign == -1)
    {
        str.insert(0, 1, '-');
    }
    return str;
}
//进1
lnum &lnum::Carry()
{
    vector<int> &v = num.vec;
    v.insert(v.begin(), 1, 0);
    size_t s = v.size();
    for (size_t i = s - 1; i > 0; i--)
    {
        int t = v[i];
        v[i] = t % 10;
        v[i - 1] += t / 10;
    }
    return *this;
}
//借1
vector<int> lnum::Borrow() const
{
    vector<int> t = num.vec;
    size_t s = t.size();
    for (size_t i = 1; i < s; i++)
    {
        t[i - 1] -= 1;
        t[i] += 10;
    }
    return t;
}
//对齐
const lnum::align lnum::AlignVec(const vector<int> &m, const vector<int> &n) const
{
    align agn;
    vector<int> &fst = agn.first, &sec = agn.second;
    fst = m, sec = n;
    size_t fsize = fst.size(), ssize = sec.size();
    if (fsize < ssize)
    {
        fst.insert(fst.begin(), ssize - fsize, 0);
    }
    else if (fsize > ssize)
    {
        sec.insert(sec.begin(), fsize - ssize, 0);
    }
    return agn;
}
//反转正负
vector<int> lnum::Negative() const
{
    auto r = num.vec;
    for (int &i : r)
    {
        i = -1;
    }
    return r;
}
//clear zero vec
lnum &lnum::ClearZeroVec()
{
    const size_t &prec = num.prec;
    vector<int> &v = num.vec;
    if (v == vector<int>(v.size(), 0))
    {
        v.clear();
        v.push_back(0);
    }
    else if (v[0] == 0)
    {
        size_t s = v.size(), i = 0;
        if (prec != 0)
        {
            
            for (; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            if (i >= s - prec)
            {
                v.erase(v.begin(), v.begin() + (s - prec - 1));
            }
            else if (i < s - prec)
            {
                v.erase(v.begin(), v.begin() + i);
            }
        }
        else
        {
            for ( i = 0; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            v.erase(v.begin(), v.begin() + i);
        }
        
    }
    return *this;
}
//vec to str
lnum &lnum::VecToStr()
{
    num.str.clear();
    for (int i : num.vec)
    {
        num.str.push_back(i + 48);
    }
    return *this;
}
//------------------------------------------------------------------------
//设置精度
void lnum::SetPrecision(const size_t i)
{
    lnum::precision = i;
}
//展示信息
void lnum::Showinfo() const
{
    std::cout << "有效数位: ";
    for (int i : num.vec)
    {
        std::cout << i << " ";
    }
    std::cout << std::endl;
    std::cout << "正负符号: " << num.sign << "\n"
              << "小数位数: " << num.dpsize << "\n"
              << "小数精度: " << num.prec << "\n"
              << "有效位数: " << num.nsize << std::endl;
}
//>
const bool lnum::operator>(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign < l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst > sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//<
const bool lnum::operator<(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign > l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst < sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//==
const bool lnum::operator==(const lnum &l) const
{
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    return fst == sec && num.sign == l.num.sign;
}
//>=
const bool lnum::operator>=(const lnum &l) const
{
    return !(*this < l);
}
//<=
const bool lnum::operator<=(const lnum &l) const
{
    return !(*this > l);
}
//+
const lnum lnum::operator+(const lnum &l) const
{

    lnum r;
    attr &lr = r.num;
    const size_t &prec = num.prec, &dpsize = num.dpsize, &ldps = l.num.dpsize;
    if ((num.vec == vector<int>(1, 0)) && (l.num.vec == vector<int>(1, 0)))
    {
        lr.prec = num.prec;
    }
    else if ((num.vec != vector<int>(1, 0)) || (l.num.vec != vector<int>(1, 0)))
    {
        const align &agn = AlignVec(num.vec, l.num.vec);
        const vector<int> &fst = agn.first,
                          &sec = agn.second,
                          &tvec = this->num.vec,
                          &lvec = l.num.vec;
        lr.prec = prec;
        lr.dpsize = dpsize >= ldps ? dpsize : ldps;
        if (num.sign == l.num.sign)
        {
            lr.sign = num.sign;
            if (((tvec == vector<int>(1, 0)) && (lvec != vector<int>(1, 0))) ||((tvec != vector<int>(1, 0)) && (lvec == vector<int>(1, 0))))
            {
                lr.vec = num.nsize > l.num.nsize ? tvec : lvec;
            }
            else if ((tvec != vector<int>(1, 0)) && (lvec != vector<int>(1, 0)))
            {
                size_t s = fst.size();
                lr.vec = fst;
                for (size_t i = 0; i < s; i++)
                {
                    lr.vec[i] += sec[i];
                }
            }
            r.Carry().ClearZeroVec().VecToStr();
            lr.nsize = lr.vec.size();
        }
        else
        {
            r = *this - l;
        }
    }
    return r;
}
//-
const lnum lnum::operator-(const lnum &l) const
{
}
//>>
istream &operator>>(istream &is, lnum &l)
{
    string str;
    (is >> str).get();
    l = str;
    return is;
}
//<<
ostream &operator<<(ostream &os, const lnum &l)
{
    os << l.AttrToStr();
    return os;
}

lnum_test.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.cpp"
int main(int argc, char const *argv[])
{
    lnum::SetPrecision(3);
    lnum a, b, c;
    int i = 0;
    while (i != 5)
    {
        std::cout << "input data\n";
        std::cin >> a >> b;
        std::cout << "a = " << a << "  "
                  << "b = " << b << "\n"
                  << "a + b = " << (a + b) << std::endl;
                  std::cout << std::endl;
        i++;
    }
    
    return 0;
}
======================================================
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-hjg4v2n2.0lr --stdout=Microsoft-MIEngine-Out-ggdaz51y.hrw --stderr=Microsoft-MIEngine-Error-1w25bxep.ub3 --pid=Microsoft-MIEngine-Pid-pa4lnrzg.3fq --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi

E:\Users\admin\Documents\VScode\Code>cmd /C "c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-rdahdasn.3vx --stdout=Microsoft-MIEngine-Out-2nxw4ara.tc2 --stderr=Microsoft-MIEngine-Error-b5yp3gag.nte --pid=Microsoft-MIEngine-Pid-twogf14w.0fy --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi "
input data
-999.321 -111.123
a = -999.321  b = -111.123
a + b = -1110.444

input data
123456789
987654321
a = 123456789  b = 987654321
a + b = 1111111110

input data
1111111111111111111111111
3333333333333333333333333
a = 1111111111111111111111111  b = 3333333333333333333333333
a + b = 4444444444444444444444444

input data
999999999999
999999999999
a = 999999999999  b = 999999999999
a + b = 1999999999998

input data
987654321
12345679
a = 987654321  b = 12345679
a + b = 1000000000



E:\Users\admin\Documents\VScode\Code>cmd /C "c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-dlfxekp3.bym --stdout=Microsoft-MIEngine-Out-jtwn51h5.ubn --stderr=Microsoft-MIEngine-Error-rc5va4if.ni3 --pid=Microsoft-MIEngine-Pid-xrrvgcs4.jrr --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi "
input data
-9999999999999999999999
-9999999999999999999999
a = -9999999999999999999999  b = -9999999999999999999999
a + b = -19999999999999999999998

input data
-99999999999999999999999999999999999999999
-99999999999999999999999999999999999999999
a = -99999999999999999999999999999999999999999  b = -99999999999999999999999999999999999999999
a + b = -199999999999999999999999999999999999999998

input data
9999999999999999999999999999999999999999999999999999
9999999999999999999999999999999999999999999999999999
a = 9999999999999999999999999999999999999999999999999999  b = 9999999999999999999999999999999999999999999999999999
a + b = 19999999999999999999999999999999999999999999999999998

input data
111111111111111111111111111111111111111.1111111111111111111111
99999999999999999999999999999999999.9999999999999999999999
a = 111111111111111111111111111111111111111.111  b = 99999999999999999999999999999999999.999
a + b = 111211111111111111111111111111111111111.110

input data
-1.2366666
-9.3654
a = -1.236  b = -9.365
a + b = -10.601


E:\Users\admin\Documents\VScode\Code>


-------------------------------------------------------------------------------------------------
test.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.cpp"
int main(int argc, char const *argv[])
{
    lnum::SetPrecision(3);
    lnum a, b, c;
    int i = 0;
    while (i != 5)
    {
        std::cout << "input data\n";
        std::cin >> a >> b;
        std::cout << std::boolalpha << "a = " << a << "  "
                  << "b = " << b << "\n"
                  << "a >= b: " << (a >= b) << "\n"
                  << "a >  b: " << (a > b) << "\n"
                  << "a <= b: " << (a <= b) << "\n"
                  << "a <  b: " << (a < b) << "\n"
                  << "a == b: " << (a == b);
        std::cout << std::endl;
        i++;
    }
    
    return 0;
}
================================================
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-zcwtj5dz.rhq --stdout=Microsoft-MIEngine-Out-rmj34qps.1zn --stderr=Microsoft-MIEngine-Error-opbzjopy.skk --pid=Microsoft-MIEngine-Pid-t23shlyh.4d2 --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
input data
0
0.01
a = 0  b = 0.01
a >= b: false  
a >  b: false  
a <= b: true   
a <  b: true   
a == b: false  
input data     
0
0
a = 0  b = 0
a >= b: true
a >  b: false
a <= b: true
a <  b: false
a == b: true
input data
99999999999999999999999999
66666666666666666666666666
a = 99999999999999999999999999  b = 66666666666666666666666666
a >= b: true
a >  b: true
a <= b: false
a <  b: false
a == b: false
input data
11111111111111111111111111111111111111111111111111
11111111111111111111111111111111111111111111111112
a = 11111111111111111111111111111111111111111111111111  b = 11111111111111111111111111111111111111111111111112
a >= b: false
a >  b: false
a <= b: true
a <  b: true
a == b: false
input data
0.02
0.03
a = 0.02  b = 0.03
a >= b: false
a >  b: false
a <= b: true
a <  b: true
a == b: false


E:\Users\admin\Documents\VScode\Code>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-12-29 03:16:31 | 显示全部楼层
lnum.h
#ifndef LNUM_H
#define LNUM_H
#include <iostream>
#include <string>
#include <vector>
using std::istream;
using std::ostream;
using std::string;
using std::vector;
class lnum
{
    struct attr
    {
        int sign = 1;          //符号;
        size_t dpsize = 0;     //小数位;
        size_t prec = 0;       //精度
        size_t nsize = 1;      //有效位数
        string str = "0";      //过渡字符串
        vector<int> vec = {0}; //数组;
    };
    struct align
    {
        vector<int> first;

        vector<int> second;
    };

private:
    //小数精度;
    static size_t precision;
    //数据;
    attr num;
    //str to attr
    const attr StrToAttr(const string &str) const;
    //attr to str
    const string AttrToStr() const;
    //进1
    lnum &Carry();
    //借1
    void Borrow(vector<int> &v) const;
    //对齐
    const align AlignVec(const vector<int> &m, const vector<int> &n) const;
    //反转正负
    const lnum Negative() const;
    //clear zero vec
    lnum &ClearZeroVec();
    //vec to str
    lnum &VecToStr();

public:
    lnum(const char *cstr = "0") : num(StrToAttr(cstr)){}
    lnum(const string &str) : num(StrToAttr(str)) {}
    lnum(const char c) : num(StrToAttr(string(1, c))) {}
    lnum(const lnum &l) : num(l.num) {}
    //------------------------------------------------------------------
    //设置精度
    static void SetPrecision(const size_t i);
    //展示信息
    void Showinfo() const;
    //>
    const bool operator>(const lnum &l) const;
    //<
    const bool operator<(const lnum &l) const;
    //==
    const bool operator==(const lnum &l) const;
    //>=
    const bool operator>=(const lnum &l) const;
    //<=
    const bool operator<=(const lnum &l) const;
    //+
    const lnum operator+(const lnum &l) const;
    //-
    const lnum operator-(const lnum &l) const;
    //>>
    friend istream &operator>>(istream &is, const lnum &l);
    //<<
    friend ostream &operator<<(ostream &os, const lnum &l);
};
size_t lnum::precision = 0;
#endif

lnum.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
//str to attr
const lnum::attr lnum::StrToAttr(const string &str) const
{
    attr n;
    n.prec = precision;
    n.str = str;
    string &nstr = n.str;
    if (nstr[0] == '-')
    {
        nstr.erase(0, 1);
    }
    if (int(nstr.find('.')) != -1)
    {
        nstr.erase(nstr.find('.'), 1);
    }
    if (nstr == string(nstr.size(), '0'))
    {
        nstr.clear();
        nstr.push_back('0');
    }
    else //if (nstr != string(nstr.size(), '0'))
    {
        size_t s = nstr.size(), i = 0;
        if (str[0] == '-')
        {
            n.sign = -1;
        }
        if (int(str.find('.')) != -1)
        {
            n.dpsize = str.size() - str.find('.') - 1;
            if (nstr[0] == '0')
            {

                for (; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                if (i >= s - n.dpsize)
                {
                    nstr.erase(0, s - n.dpsize - 1);
                }
                else if (i < s - n.dpsize)
                {
                    nstr.erase(0, i);
                }
            }
        }
        else
        {
            if (nstr[0] == '0')
            {
                for (i = 0; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                nstr.erase(0, i);
            }
        }
        if (n.prec > n.dpsize)
        {
            nstr.insert(nstr.end(), n.prec - n.dpsize, '0');
        }
        else if (n.prec < n.dpsize)
        {
            nstr.erase(nstr.size() - (n.dpsize - n.prec));
            n.dpsize = n.prec;
        }
        n.vec.clear();
        for (char c : nstr)
        {
            n.vec.push_back(c - 48);
        }
        n.nsize = nstr.size();
    }
    return n;
}
//attr to str
const string lnum::AttrToStr() const
{
    string str = num.str;
    const size_t &prec = num.prec, &dpsize = num.dpsize;
    if (str.size() != 1 && prec != 0)
    {
        if (dpsize == prec)
        {
            str.insert(str.size() - prec, 1, '.');
        }
        else if (dpsize < prec && dpsize != 0)
        {
            str.erase(str.size() - (prec - dpsize));
            str.insert(str.size() - dpsize, 1, '.');
        }
        else if (dpsize == 0)
        {
            str.erase(str.size() - prec);
        }
    }
    if (num.sign == -1)
    {
        str.insert(0, 1, '-');
    }
    return str;
}
//进1
lnum &lnum::Carry()
{
    vector<int> &v = num.vec;
    v.insert(v.begin(), 1, 0);
    size_t s = v.size();
    for (size_t i = s - 1; i > 0; i--)
    {
        int t = v[i];
        v[i] = t % 10;
        v[i - 1] += t / 10;
    }
    return *this;
}
//借1
void lnum::Borrow(vector<int> &v) const
{
    size_t s = v.size();
    for (size_t i = 1; i < s; i++)
    {
        v[i - 1] -= 1;
        v[i] += 10;
    }
}
//对齐
const lnum::align lnum::AlignVec(const vector<int> &m, const vector<int> &n) const
{
    align agn;
    vector<int> &fst = agn.first, &sec = agn.second;
    fst = m, sec = n;
    size_t fsize = fst.size(), ssize = sec.size();
    if (fsize < ssize)
    {
        fst.insert(fst.begin(), ssize - fsize, 0);
    }
    else if (fsize > ssize)
    {
        sec.insert(sec.begin(), fsize - ssize, 0);
    }
    return agn;
}
//反转正负
const lnum lnum::Negative() const
{
    lnum r = *this;
    r.num.sign = -num.sign;
    return r;
}
//clear zero vec
lnum &lnum::ClearZeroVec()
{
    const size_t &prec = num.prec;
    vector<int> &v = num.vec;
    if (v == vector<int>(v.size(), 0))
    {
        v.clear();
        v.push_back(0);
    }
    else if (v[0] == 0)
    {
        size_t s = v.size(), i = 0;
        if (prec != 0)
        {

            for (; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            if (i >= s - prec)
            {
                v.erase(v.begin(), v.begin() + (s - prec - 1));
            }
            else if (i < s - prec)
            {
                v.erase(v.begin(), v.begin() + i);
            }
        }
        else
        {
            for (i = 0; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            v.erase(v.begin(), v.begin() + i);
        }
    }
    return *this;
}
//vec to str
lnum &lnum::VecToStr()
{
    num.str.clear();
    for (int i : num.vec)
    {
        num.str.push_back(i + 48);
    }
    return *this;
}
//------------------------------------------------------------------------
//设置精度
void lnum::SetPrecision(const size_t i)
{
    lnum::precision = i;
}
//展示信息
void lnum::Showinfo() const
{
    std::cout << "有效数位: ";
    for (int i : num.vec)
    {
        std::cout << i << " ";
    }
    std::cout << std::endl;
    std::cout << "正负符号: " << num.sign << "\n"
              << "小数位数: " << num.dpsize << "\n"
              << "小数精度: " << num.prec << "\n"
              << "有效位数: " << num.nsize << std::endl;
}
//>
const bool lnum::operator>(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign < l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst > sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//<
const bool lnum::operator<(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign > l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst < sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//==
const bool lnum::operator==(const lnum &l) const
{
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    return fst == sec && num.sign == l.num.sign;
}
//>=
const bool lnum::operator>=(const lnum &l) const
{
    return !(*this < l);
}
//<=
const bool lnum::operator<=(const lnum &l) const
{
    return !(*this > l);
}
//+
const lnum lnum::operator+(const lnum &l) const
{

    lnum r;
    attr &lr = r.num;
    const align &agn = AlignVec(num.vec, l.num.vec);
    const int &tsgn = num.sign, &lsgn = l.num.sign;
    const vector<int> &fst = agn.first, &sec = agn.second, &tvec = num.vec, &lvec = l.num.vec;
    lr.prec = num.prec;
    lr.dpsize = num.dpsize >= l.num.dpsize ? num.dpsize : l.num.dpsize;
    size_t s = fst.size() <= sec.size() ? fst.size() : sec.size();
    lr.vec = fst >= sec ? fst : sec;
    for (size_t i = 0; i < s; i++)
    {
        lr.vec[i] += (tsgn * lsgn) * ((sec <= fst ? sec : fst)[i]);
    }
    if ((fst > sec && tsgn < lsgn) || (fst < sec && tsgn > lsgn) || (tsgn == -1 && lsgn == -1))
    {
        lr.sign = -1;
    }
    Borrow(lr.vec);
    r.Carry().ClearZeroVec().VecToStr();
    lr.nsize = lr.vec.size();
    return r;
}
//-
const lnum lnum::operator-(const lnum &l) const
{
    lnum r;
    if (l.num.vec != vector<int>(1,0))
    {
        r = *this + l.Negative();
    }
    else
    {
        r = *this;
    }
    return r;
}
//>>
istream &operator>>(istream &is, lnum &l)
{
    string str;
    (is >> str).get();
    l = str;
    return is;
}
//<<
ostream &operator<<(ostream &os, const lnum &l)
{
    os << l.AttrToStr();
    return os;
}

test.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.cpp"
int main(int argc, char const *argv[])
{
    lnum::SetPrecision(3);
    lnum a, b;
    int i = 0;
    while (i != 7)
    {
        std::cout << "input data:\n";
        std::cin >> a >> b;
        std::cout << "a = " << a << "\n"
                  << "b = " << b << "\n"
                  << "a + b = " << (a + b) << "\n"
                  << "a - b = " << (a - b) << std::endl;
        i++;
    }
    return 0;
}
======================================================
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-scgixi2r.ntb --stdout=Microsoft-MIEngine-Out-luutv21b.4xa --stderr=Microsoft-MIEngine-Error-pjehcj35.j3v --pid=Microsoft-MIEngine-Pid-bxz2vs3q.lkd --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
input data:
0
123456789
a = 0
b = 123456789     
a + b = 123456789
a - b = -123456789
input data:      
123456789
0
a = 123456789
b = 0
a + b = 123456789
a - b = 123456789
input data:
-123456789
0
a = -123456789
b = 0
a + b = -123456789
a - b = -123456789
input data:
-123456789
0
a = -123456789
b = 0
a + b = -123456789
a - b = -123456789
input data:
-123456789
123456789
a = -123456789
b = 123456789
a + b = 0
a - b = -246913578
input data:
0
-123456789
a = 0
b = -123456789
a + b = -123456789
a - b = 123456789
input data:
999999999
555555555
a = 999999999
b = 555555555
a + b = 1555555554
a - b = 444444444


E:\Users\admin\Documents\VScode\Code>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-29 13:02:13 | 显示全部楼层
加减完成

lnum.h
#ifndef LNUM_H
#define LNUM_H
#include <iostream>
#include <string>
#include <vector>
using std::istream;
using std::ostream;
using std::string;
using std::vector;
class lnum
{
    struct attr
    {
        int sign = 1;          //符号;
        size_t dpsize = 0;     //小数位;
        size_t prec = 0;       //精度
        size_t nsize = 1;      //有效位数
        string str = "0";      //过渡字符串
        vector<int> vec = {0}; //数组;
    };
    struct align
    {
        vector<int> first;

        vector<int> second;
    };

private:
    //小数精度;
    static size_t precision;
    //数据;
    attr num;
    //str to attr
    const attr StrToAttr(const string &str) const;
    //attr to str
    const string AttrToStr() const;
    //进1
    lnum &Carry();
    //借1
    lnum Borrow();
    //对齐
    const align AlignVec(const vector<int> &m, const vector<int> &n) const;
    //反转正负
    const lnum Negative() const;
    //clear zero vec
    lnum &ClearZeroVec();
    //vec to str
    lnum &VecToStr();

public:
    lnum(const char *cstr = "0") : num(StrToAttr(cstr)){}
    lnum(const string &str) : num(StrToAttr(str)) {}
    lnum(const char c) : num(StrToAttr(string(1, c))) {}
    lnum(const lnum &l) : num(l.num) {}
    //------------------------------------------------------------------
    //设置精度
    static void SetPrecision(const size_t i);
    //展示信息
    void Showinfo() const;
    //>
    const bool operator>(const lnum &l) const;
    //<
    const bool operator<(const lnum &l) const;
    //==
    const bool operator==(const lnum &l) const;
    //>=
    const bool operator>=(const lnum &l) const;
    //<=
    const bool operator<=(const lnum &l) const;
    //+
    const lnum operator+(const lnum &l) const;
    //-
    const lnum operator-(const lnum &l) const;
    //>>
    friend istream &operator>>(istream &is, const lnum &l);
    //<<
    friend ostream &operator<<(ostream &os, const lnum &l);
};
size_t lnum::precision = 0;
#endif

lnum.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
//str to attr
const lnum::attr lnum::StrToAttr(const string &str) const
{
    attr n;
    n.prec = precision;
    n.str = str;
    string &nstr = n.str;
    size_t &ndps = n.dpsize, &npre = n.prec;
    if (nstr[0] == '-')
    {
        nstr.erase(0, 1);
    }
    if (int(nstr.find('.')) != -1)
    {
        nstr.erase(nstr.find('.'), 1);
    }
    if (nstr == string(nstr.size(), '0'))
    {
        nstr.clear();
        nstr.push_back('0');
    }
    else //if (nstr != string(nstr.size(), '0'))
    {
        size_t s = nstr.size(), i = 0;
        if (str[0] == '-')
        {
            n.sign = -1;
        }
        if (int(str.find('.')) != -1)
        {
            ndps = str.size() - str.find('.') - 1;
            if (nstr[0] == '0')
            {

                for (; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                if (i >= s - ndps)
                {
                    nstr.erase(0, s - ndps - 1);
                }
                else if (i < s - ndps)
                {
                    nstr.erase(0, i);
                }
            }
        }
        else
        {
            if (nstr[0] == '0')
            {
                for (i = 0; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                nstr.erase(0, i);
            }
        }
        if (npre > ndps)
        {
            nstr.insert(nstr.end(), npre - ndps, '0');
        }
        else if (npre < ndps)
        {
            nstr.erase(nstr.size() - (ndps - npre));
            ndps = npre;
        }
        n.vec.clear();
        for (char c : nstr)
        {
            n.vec.push_back(c - 48);
        }
        n.nsize = nstr.size();
    }
    return n;
}
//attr to str
const string lnum::AttrToStr() const
{
    string t = num.str, &str = t;
    const size_t &prec = num.prec, &dpsize = num.dpsize;
    if (str.size() != 1 && prec != 0)
    {
        if (dpsize == prec)
        {
            str.insert(str.size() - prec, 1, '.');
        }
        else if (dpsize < prec && dpsize != 0)
        {
            str.erase(str.size() - (prec - dpsize));
            str.insert(str.size() - dpsize, 1, '.');
        }
        else if (dpsize == 0)
        {
            str.erase(str.size() - prec);
        }
    }
    if (num.sign == -1)
    {
        str.insert(0, 1, '-');
    }
    return str;
}
//进1
lnum &lnum::Carry()
{
    vector<int> &v = num.vec;
    v.insert(v.begin(), 1, 0);
    size_t s = v.size();
    for (size_t i = s - 1; i > 0; i--)
    {
        int t = v[i];
        v[i] = t % 10;
        v[i - 1] += t / 10;
    }
    return *this;
}
//借1
lnum lnum::Borrow()
{
    vector<int> &v = num.vec;
    size_t s = v.size();
    for (size_t i = 1; i < s; i++)
    {
        v[i - 1] -= 1;
        v[i] += 10;
    }
    return *this;
}
//对齐
const lnum::align lnum::AlignVec(const vector<int> &m, const vector<int> &n) const
{
    align agn;
    vector<int> &fst = agn.first, &sec = agn.second;
    fst = m, sec = n;
    size_t fsize = fst.size(), ssize = sec.size();
    if (fsize < ssize)
    {
        fst.insert(fst.begin(), ssize - fsize, 0);
    }
    else if (fsize > ssize)
    {
        sec.insert(sec.begin(), fsize - ssize, 0);
    }
    return agn;
}
//反转正负
const lnum lnum::Negative() const
{
    lnum r = *this;
    r.num.sign = -num.sign;
    return r;
}
//clear zero vec
lnum &lnum::ClearZeroVec()
{
    const size_t &prec = num.prec;
    vector<int> &v = num.vec;
    if (v == vector<int>(v.size(), 0))
    {
        v.clear();
        v.push_back(0);
    }
    else if (v[0] == 0)
    {
        size_t s = v.size(), i = 0;
        if (prec != 0)
        {

            for (; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            if (i >= s - prec)
            {
                v.erase(v.begin(), v.begin() + (s - prec - 1));
            }
            else if (i < s - prec)
            {
                v.erase(v.begin(), v.begin() + i);
            }
        }
        else
        {
            for (i = 0; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            v.erase(v.begin(), v.begin() + i);
        }
    }
    return *this;
}
//vec to str
lnum &lnum::VecToStr()
{
    num.str.clear();
    for (int i : num.vec)
    {
        num.str.push_back(i + 48);
    }
    return *this;
}
//------------------------------------------------------------------------
//设置精度
void lnum::SetPrecision(const size_t i)
{
    lnum::precision = i;
}
//展示信息
void lnum::Showinfo() const
{
    std::cout << "有效数位: ";
    for (int i : num.vec)
    {
        std::cout << i << " ";
    }
    std::cout << std::endl;
    std::cout << "正负符号: " << num.sign << "\n"
              << "小数位数: " << num.dpsize << "\n"
              << "小数精度: " << num.prec << "\n"
              << "有效位数: " << num.nsize << std::endl;
}
//>
const bool lnum::operator>(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign < l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst > sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//<
const bool lnum::operator<(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign > l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst < sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//==
const bool lnum::operator==(const lnum &l) const
{
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    return fst == sec && num.sign == l.num.sign;
}
//>=
const bool lnum::operator>=(const lnum &l) const
{
    return !(*this < l);
}
//<=
const bool lnum::operator<=(const lnum &l) const
{
    return !(*this > l);
}
//+
const lnum lnum::operator+(const lnum &l) const
{

    lnum r;
    attr &lr = r.num;
    const align &agn = AlignVec(num.vec, l.num.vec);
    const int &tsgn = num.sign, &lsgn = l.num.sign;
    const vector<int> &fst = agn.first, &sec = agn.second, &tvec = num.vec, &lvec = l.num.vec;
    lr.prec = num.prec;
    lr.dpsize = (num.dpsize >= l.num.dpsize ? num.dpsize : l.num.dpsize);
    size_t s = (fst.size() <= sec.size() ? fst.size() : sec.size());
    lr.vec = (fst >= sec ? fst : sec);
    for (size_t i = 0; i < s; i++)
    {
        lr.vec[i] += (tsgn * lsgn) * ((sec <= fst ? sec : fst)[i]);
    }
    if ((fst > sec && tsgn < lsgn) || (fst < sec && tsgn > lsgn) || (tsgn == -1 && lsgn == -1))
    {
        lr.sign = -1;
    }
    if (tsgn*lsgn == -1)
    {
        r.Borrow();
    }
    r.Carry().ClearZeroVec().VecToStr();
    lr.nsize = lr.vec.size();
    return r;
}
//-
const lnum lnum::operator-(const lnum &l) const
{
    lnum r;
    if (l.num.vec != vector<int>(1,0))
    {
        r = *this + l.Negative();
    }
    else
    {
        r = *this;
    }
    return r;
}
//>>
istream &operator>>(istream &is, lnum &l)
{
    string str;
    (is >> str).get();
    l = str;
    return is;
}
//<<
ostream &operator<<(ostream &os, const lnum &l)
{
    os << l.AttrToStr();
    return os;
}

test.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.cpp"
#include <ctime>
int main(int argc, char const *argv[])
{
    //lnum::SetPrecision();
    lnum a, b;
    int s = 0;
    char m[5], n[5];
    m[4] = '\0';
    n[4] = '\0';
    srand(time(NULL));
    while (s != 10)
    {
        for (size_t i = 0; i < 4; i++)
        {
            m[i] = 48+(rand() % 9);
            n[i] = 48 + (rand() % 9);
        }
        a = m;
        b = n;
        std::cout << "a = " << a << "\n"
                  << "b = " << b << "\n"
                  << "a + b = " << (a + b) << "\n"
                  << "a - b = " << (a - b) << std::boolalpha << "\n"
                  << "a >  b: " << (a > b) << "\n"
                  << "a == b: " << (a == b) << "\n"
                  << "a <  b: " << (a < b)<<std::endl;
        s++;
    }
    return 0;
}
===================================================
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-eboo5i2a.scs --stdout=Microsoft-MIEngine-Out-r4tarar5.3rq --stderr=Microsoft-MIEngine-Error-qhegr14x.fpe --pid=Microsoft-MIEngine-Pid-fmhxlme1.czb --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
a = 5332     
b = 401      
a + b = 5733
a - b = 4931
a >  b: true
a == b: false
a <  b: false
a = 58      
b = 664      
a + b = 722  
a - b = -606
a >  b: false
a == b: false
a <  b: true
a = 4535
b = 4804
a + b = 9339
a - b = -269
a >  b: false
a == b: false
a <  b: true
a = 3751
b = 580
a + b = 4331
a - b = 3171
a >  b: true
a == b: false
a <  b: false
a = 1725
b = 8817
a + b = 10542
a - b = -7092
a >  b: false
a == b: false
a <  b: true
a = 6707
b = 2004
a + b = 8711
a - b = 4703
a >  b: true
a == b: false
a <  b: false
a = 1604
b = 626
a + b = 2230
a - b = 978
a >  b: true
a == b: false
a <  b: false
a = 6284
b = 4865
a + b = 11149
a - b = 1419
a >  b: true
a == b: false
a <  b: false
a = 1810
b = 3604
a + b = 5414
a - b = -1794
a >  b: false
a == b: false
a <  b: true
a = 4671
b = 8577
a + b = 13248
a - b = -3906
a >  b: false
a == b: false
a <  b: true


E:\Users\admin\Documents\VScode\Code>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-29 18:32:34 | 显示全部楼层
num.h

#ifndef LNUM_H
#define LNUM_H
#include <iostream>
#include <string>
#include <vector>
using std::istream;
using std::ostream;
using std::string;
using std::vector;
class lnum
{
    struct attr
    {
        int sign = 1;          //符号;
        size_t dpsize = 0;     //小数位;
        size_t prec = 0;       //精度
        size_t nsize = 1;      //有效位数
        string str = "0";      //过渡字符串
        vector<int> vec = {0}; //数组;
    };
    struct align
    {
        vector<int> first;
        vector<int> second;
    };

private:
    //小数精度;
    static size_t precision;
    //数据;
    attr num;
    //str to attr
    const attr StrToAttr(const string &str) const;
    //attr to str
    const string AttrToStr() const;
    //进1
    lnum &Carry();
    //借1
    lnum Borrow();
    //对齐
    const align AlignVec(const vector<int> &m, const vector<int> &n) const;
    //clear zero vec
    lnum &ClearZeroVec();
    //vec to str
    lnum &VecToStr();

public:
    lnum(const char *cstr = "0") : num(StrToAttr(cstr)){}
    lnum(const string &str) : num(StrToAttr(str)) {}
    lnum(const char c) : num(StrToAttr(string(1, c))) {}
    lnum(const lnum &l) : num(l.num) {}
    //------------------------------------------------------------------
    //设置精度
    static void SetPrecision(const size_t i);
    //展示信息
    void Showinfo() const;
    //>
    const bool operator>(const lnum &l) const;
    //<
    const bool operator<(const lnum &l) const;
    //==
    const bool operator==(const lnum &l) const;
    //>=
    const bool operator>=(const lnum &l) const;
    //<=
    const bool operator<=(const lnum &l) const;
    //+
    const lnum operator+(const lnum &l) const;
    //-
    const lnum operator-(const lnum &l) const;
    const lnum operator-() const;
    //>>
    friend istream &operator>>(istream &is, const lnum &l);
    //<<
    friend ostream &operator<<(ostream &os, const lnum &l);
};
size_t lnum::precision = 0;
#endif

lnum.cpp

#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
//str to attr
const lnum::attr lnum::StrToAttr(const string &str) const
{
    attr n;
    n.prec = precision;
    n.str = str;
    string &nstr = n.str;
    size_t &ndps = n.dpsize, &npre = n.prec;
    if (nstr[0] == '-')
    {
        nstr.erase(0, 1);
    }
    if (int(nstr.find('.')) != -1)
    {
        nstr.erase(nstr.find('.'), 1);
    }
    if (nstr == string(nstr.size(), '0'))
    {
        nstr.clear();
        nstr.push_back('0');
    }
    else //if (nstr != string(nstr.size(), '0'))
    {
        size_t s = nstr.size(), i = 0;
        if (str[0] == '-')
        {
            n.sign = -1;
        }
        if (int(str.find('.')) != -1)
        {
            ndps = str.size() - str.find('.') - 1;
            if (nstr[0] == '0')
            {

                for (; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                if (i >= s - ndps)
                {
                    nstr.erase(0, s - ndps - 1);
                }
                else if (i < s - ndps)
                {
                    nstr.erase(0, i);
                }
            }
        }
        else
        {
            if (nstr[0] == '0')
            {
                for (i = 0; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                nstr.erase(0, i);
            }
        }
        if (npre > ndps)
        {
            nstr.insert(nstr.end(), npre - ndps, '0');
        }
        else if (npre < ndps)
        {
            nstr.erase(nstr.size() - (ndps - npre));
            ndps = npre;
        }
        n.vec.clear();
        for (char c : nstr)
        {
            n.vec.push_back(c - 48);
        }
        n.nsize = nstr.size();
    }
    return n;
}
//attr to str
const string lnum::AttrToStr() const
{
    string t = num.str, &str = t;
    const size_t &prec = num.prec, &dpsize = num.dpsize;
    if (str.size() != 1 && prec != 0)
    {
        if (dpsize == prec)
        {
            str.insert(str.size() - prec, 1, '.');
        }
        else if (dpsize < prec && dpsize != 0)
        {
            str.erase(str.size() - (prec - dpsize));
            str.insert(str.size() - dpsize, 1, '.');
        }
        else if (dpsize == 0)
        {
            str.erase(str.size() - prec);
        }
    }
    if (num.sign == -1)
    {
        str.insert(0, 1, '-');
    }
    return str;
}
//进1
lnum &lnum::Carry()
{
    vector<int> &v = num.vec;
    v.insert(v.begin(), 1, 0);
    size_t s = v.size();
    for (size_t i = s - 1; i > 0; i--)
    {
        int t = v[i];
        v[i] = t % 10;
        v[i - 1] += t / 10;
    }
    return *this;
}
//借1
lnum lnum::Borrow()
{
    vector<int> &v = num.vec;
    size_t s = v.size();
    for (size_t i = 1; i < s; i++)
    {
        v[i - 1] -= 1;
        v[i] += 10;
    }
    return *this;
}
//对齐
const lnum::align lnum::AlignVec(const vector<int> &m, const vector<int> &n) const
{
    align agn;
    vector<int> &fst = agn.first, &sec = agn.second;
    fst = m, sec = n;
    size_t fsize = fst.size(), ssize = sec.size();
    if (fsize < ssize)
    {
        fst.insert(fst.begin(), ssize - fsize, 0);
    }
    else if (fsize > ssize)
    {
        sec.insert(sec.begin(), fsize - ssize, 0);
    }
    return agn;
}
//clear zero vec
lnum &lnum::ClearZeroVec()
{
    const size_t &prec = num.prec;
    vector<int> &v = num.vec;
    if (v == vector<int>(v.size(), 0))
    {
        v.clear();
        v.push_back(0);
    }
    else if (v[0] == 0)
    {
        size_t s = v.size(), i = 0;
        if (prec != 0)
        {

            for (; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            if (i >= s - prec)
            {
                v.erase(v.begin(), v.begin() + (s - prec - 1));
            }
            else if (i < s - prec)
            {
                v.erase(v.begin(), v.begin() + i);
            }
        }
        else
        {
            for (i = 0; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            v.erase(v.begin(), v.begin() + i);
        }
    }
    return *this;
}
//vec to str
lnum &lnum::VecToStr()
{
    num.str.clear();
    for (int i : num.vec)
    {
        num.str.push_back(i + 48);
    }
    return *this;
}
//------------------------------------------------------------------------
//设置精度
void lnum::SetPrecision(const size_t i)
{
    lnum::precision = i;
}
//展示信息
void lnum::Showinfo() const
{
    std::cout << "有效数位: ";
    for (int i : num.vec)
    {
        std::cout << i << " ";
    }
    std::cout << std::endl;
    std::cout << "正负符号: " << num.sign << "\n"
              << "小数位数: " << num.dpsize << "\n"
              << "小数精度: " << num.prec << "\n"
              << "有效位数: " << num.nsize << std::endl;
}
//>
const bool lnum::operator>(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign < l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst > sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//<
const bool lnum::operator<(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign > l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst < sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//==
const bool lnum::operator==(const lnum &l) const
{
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    return fst == sec && num.sign == l.num.sign;
}
//>=
const bool lnum::operator>=(const lnum &l) const
{
    return !(*this < l);
}
//<=
const bool lnum::operator<=(const lnum &l) const
{
    return !(*this > l);
}
//+
const lnum lnum::operator+(const lnum &l) const
{

    lnum r;
    attr &lr = r.num;
    const align &agn = AlignVec(num.vec, l.num.vec);
    const int &tsgn = num.sign, &lsgn = l.num.sign;
    const vector<int> &fst = agn.first, &sec = agn.second, &tvec = num.vec, &lvec = l.num.vec;
    lr.prec = num.prec;
    lr.dpsize = (num.dpsize >= l.num.dpsize ? num.dpsize : l.num.dpsize);
    size_t s = (fst.size() <= sec.size() ? fst.size() : sec.size());
    lr.vec = (fst >= sec ? fst : sec);
    for (size_t i = 0; i < s; i++)
    {
        lr.vec[i] += (tsgn * lsgn) * ((sec <= fst ? sec : fst)[i]);
    }
    if ((fst > sec && tsgn < lsgn) || (fst < sec && tsgn > lsgn) || (tsgn == -1 && lsgn == -1))
    {
        lr.sign = -1;
    }
    if (tsgn*lsgn == -1)
    {
        r.Borrow();
    }
    r.Carry().ClearZeroVec().VecToStr();
    lr.nsize = lr.vec.size();
    return r;
}
//符号取反
const lnum lnum::operator-() const
{
    const vector<int> &tvec = num.vec;
    lnum r = *this;
    if (tvec != vector<int>(tvec.size(), 0))
    {
        r.num.sign = -num.sign;
    }
    return r;
}
//-
const lnum lnum::operator-(const lnum &l) const
{
    lnum r;
    if (l.num.vec != vector<int>(1,0))
    {
        r = *this + (-l);
    }
    else
    {
        r = *this;
    }
    return r;
}
//>>
istream &operator>>(istream &is, lnum &l)
{
    string str;
    (is >> str).get();
    l = str;
    return is;
}
//<<
ostream &operator<<(ostream &os, const lnum &l)
{
    os << l.AttrToStr();
    return os;
}


test.cpp

#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.cpp"
#include <ctime>
#include <cmath>
int main(int argc, char const *argv[])
{
    //lnum::SetPrecision();
    lnum a, b;
    int s = 0;
    char m[5], n[5];
    m[4] = '\0';
    n[4] = '\0';
    srand(time(NULL));
    while (s != 10)
    {
        for (size_t i = 0; i < 4; i++)
        {
            m[i] = (48+(rand() % 9));
            n[i] = 48 + (rand() % 9);
            if (m[i] % 3 == 0)
            {
                m[0] = '-';
            }
            if (n[2] % 2== 0)
            {
                n[0] = '-';
            }
        }
        a = m;
        b = n;
        std::cout << "a = " << a << "  "
                  << "b = " << b << "\n"
                  << a << " +  " << b << " = " << (a + b) << "\n"
                  << a << " -  " << b << " = " << (a - b) << std::boolalpha << "\n"
                  << a << " >  " << b << " : " << (a > b) << "\n"
                  << a << " == " << b << " : " << (a == b) << "\n"
                  << a << " <  " << b << " : " << (a < b) << "\n"
                  << std::endl;
        s++;
    }
    return 0;
}
========================================================
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-ul1mqoep.0q0 --stdout=Microsoft-MIEngine-Out-unmtncle.kyu --stderr=Microsoft-MIEngine-Error-5vrplf5c.qy3 --pid=Microsoft-MIEngine-Pid-ofqfqron.oyu --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
a = -862  b = -133
-862 +  -133 = -995
-862 -  -133 = -729
-862 >  -133 : false
-862 == -133 : false
-862 <  -133 : true

a = -317  b = 4114  
-317 +  4114 = 3797
-317 -  4114 = -4431
-317 >  4114 : false
-317 == 4114 : false
-317 <  4114 : true

a = -766  b = 4011
-766 +  4011 = 3245
-766 -  4011 = -4777
-766 >  4011 : false
-766 == 4011 : false
-766 <  4011 : true

a = -533  b = -286
-533 +  -286 = -819
-533 -  -286 = -247
-533 >  -286 : false
-533 == -286 : false
-533 <  -286 : true

a = -28  b = -830
-28 +  -830 = -858
-28 -  -830 = 802
-28 >  -830 : true
-28 == -830 : false
-28 <  -830 : false

a = 7858  b = 2216
7858 +  2216 = 10074
7858 -  2216 = 5642
7858 >  2216 : true
7858 == 2216 : false
7858 <  2216 : false

a = -722  b = -20
-722 +  -20 = -742
-722 -  -20 = -702
-722 >  -20 : false
-722 == -20 : false
-722 <  -20 : true

a = -670  b = -360
-670 +  -360 = -1030
-670 -  -360 = -310
-670 >  -360 : false
-670 == -360 : false
-670 <  -360 : true

a = -231  b = -226
-231 +  -226 = -457
-231 -  -226 = -5
-231 >  -226 : false
-231 == -226 : false
-231 <  -226 : true

a = -782  b = -116
-782 +  -116 = -898
-782 -  -116 = -666
-782 >  -116 : false
-782 == -116 : false
-782 <  -116 : true



E:\Users\admin\Documents\VScode\Code>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-29 20:08:49 | 显示全部楼层
本帖最后由 bin554385863 于 2019-12-29 20:26 编辑

lnum.h
#ifndef LNUM_H
#define LNUM_H
#include <iostream>
#include <string>
#include <vector>
using std::istream;
using std::ostream;
using std::string;
using std::vector;
class lnum
{
    struct attr
    {
        int sign = 1;          //符号;
        size_t dpsize = 0;     //小数位;
        size_t prec = 0;       //精度
        size_t nsize = 1;      //有效位数
        string str = "0";      //过渡字符串
        vector<int> vec = {0}; //数组;
    };
    struct align
    {
        vector<int> first;
        vector<int> second;
    };

private:
    //小数精度;
    static size_t precision;
    //数据;
    attr num;
    //str to attr
    const attr StrToAttr(const string &str) const;
    //attr to str
    const string AttrToStr() const;
    //进1
    lnum &Carry();
    //借1
    lnum Borrow();
    //对齐
    const align AlignVec(const vector<int> &m, const vector<int> &n) const;
    //clear zero vec
    lnum &ClearZeroVec();
    //vec to str
    lnum &VecToStr();
    
public:
    lnum(const char *cstr = "0") : num(StrToAttr(cstr)){}
    lnum(const string &str) : num(StrToAttr(str)){}
    lnum(const lnum &l) : num(l.num) {}
    //------------------------------------------------------------------
    //设置精度
    static void SetPrecision(const size_t i);
    //展示信息
    void Showinfo() const;
    //>
    const bool operator>(const lnum &l) const;
    //<
    const bool operator<(const lnum &l) const;
    //==
    const bool operator==(const lnum &l) const;
    //>=
    const bool operator>=(const lnum &l) const;
    //<=
    const bool operator<=(const lnum &l) const;
    //+
    const lnum operator+(const lnum &l) const;
    //-
    const lnum operator-(const lnum &l) const;
    const lnum operator-() const;
    //+=
    const lnum &operator+=(const lnum &l);
    //++
    const lnum &operator++();
    //-=
    const lnum &operator-=(const lnum &l);
    //--
    const lnum &operator--();
    //odject++
    friend const lnum &operator++(lnum &l, const int i);
    //object--
    friend const lnum &operator--(lnum &l, const int i);
    //>>
    friend istream &operator>>(istream &is, const lnum &l);
    //<<
    friend ostream &operator<<(ostream &os, const lnum &l);
};
size_t lnum::precision = 0;
#endif

lnum.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
//str to attr
const lnum::attr lnum::StrToAttr(const string &str) const
{
    attr n;
    n.prec = precision;
    n.str = str;
    string &nstr = n.str;
    size_t &ndps = n.dpsize, &npre = n.prec;
    if (nstr[0] == '-')
    {
        nstr.erase(0, 1);
    }
    if (int(nstr.find('.')) != -1)
    {
        nstr.erase(nstr.find('.'), 1);
    }
    if (nstr == string(nstr.size(), '0'))
    {
        nstr.clear();
        nstr.push_back('0');
    }
    else //if (nstr != string(nstr.size(), '0'))
    {
        size_t s = nstr.size(), i = 0;
        if (str[0] == '-')
        {
            n.sign = -1;
        }
        if (int(str.find('.')) != -1)
        {
            ndps = str.size() - str.find('.') - 1;
            if (nstr[0] == '0')
            {

                for (; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                if (i >= s - ndps)
                {
                    nstr.erase(0, s - ndps - 1);
                }
                else if (i < s - ndps)
                {
                    nstr.erase(0, i);
                }
            }
        }
        else
        {
            if (nstr[0] == '0')
            {
                for (i = 0; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                nstr.erase(0, i);
            }
        }
        if (npre > ndps)
        {
            nstr.insert(nstr.end(), npre - ndps, '0');
        }
        else if (npre < ndps)
        {
            nstr.erase(nstr.size() - (ndps - npre));
            ndps = npre;
        }
        n.vec.clear();
        for (char c : nstr)
        {
            n.vec.push_back(c - 48);
        }
        n.nsize = nstr.size();
    }
    return n;
}
//attr to str
const string lnum::AttrToStr() const
{
    string t = num.str, &str = t;
    const size_t &prec = num.prec, &dpsize = num.dpsize;
    if (str.size() != 1 && prec != 0)
    {
        if (dpsize == prec)
        {
            str.insert(str.size() - prec, 1, '.');
        }
        else if (dpsize < prec && dpsize != 0)
        {
            str.erase(str.size() - (prec - dpsize));
            str.insert(str.size() - dpsize, 1, '.');
        }
        else if (dpsize == 0)
        {
            str.erase(str.size() - prec);
        }
    }
    if (num.sign == -1)
    {
        str.insert(0, 1, '-');
    }
    return str;
}
//进1
lnum &lnum::Carry()
{
    vector<int> &v = num.vec;
    v.insert(v.begin(), 1, 0);
    size_t s = v.size();
    for (size_t i = s - 1; i > 0; i--)
    {
        int t = v[i];
        v[i] = t % 10;
        v[i - 1] += t / 10;
    }
    return *this;
}
//借1
lnum lnum::Borrow()
{
    vector<int> &v = num.vec;
    size_t s = v.size();
    for (size_t i = 1; i < s; i++)
    {
        v[i - 1] -= 1;
        v[i] += 10;
    }
    return *this;
}
//对齐
const lnum::align lnum::AlignVec(const vector<int> &m, const vector<int> &n) const
{
    align agn;
    vector<int> &fst = agn.first, &sec = agn.second;
    fst = m, sec = n;
    size_t fsize = fst.size(), ssize = sec.size();
    if (fsize < ssize)
    {
        fst.insert(fst.begin(), ssize - fsize, 0);
    }
    else if (fsize > ssize)
    {
        sec.insert(sec.begin(), fsize - ssize, 0);
    }
    return agn;
}
//clear zero vec
lnum &lnum::ClearZeroVec()
{
    const size_t &prec = num.prec;
    vector<int> &v = num.vec;
    if (v == vector<int>(v.size(), 0))
    {
        v.clear();
        v.push_back(0);
    }
    else if (v[0] == 0)
    {
        size_t s = v.size(), i = 0;
        if (prec != 0)
        {

            for (; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            if (i >= s - prec)
            {
                v.erase(v.begin(), v.begin() + (s - prec - 1));
            }
            else if (i < s - prec)
            {
                v.erase(v.begin(), v.begin() + i);
            }
        }
        else
        {
            for (i = 0; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            v.erase(v.begin(), v.begin() + i);
        }
    }
    return *this;
}
//vec to str
lnum &lnum::VecToStr()
{
    num.str.clear();
    for (int i : num.vec)
    {
        num.str.push_back(i + 48);
    }
    return *this;
}
//------------------------------------------------------------------------
//设置精度
void lnum::SetPrecision(const size_t i)
{
    lnum::precision = i;
}
//展示信息
void lnum::Showinfo() const
{
    std::cout << "有效数位: ";
    for (int i : num.vec)
    {
        std::cout << i << " ";
    }
    std::cout << std::endl;
    std::cout << "正负符号: " << num.sign << "\n"
              << "小数位数: " << num.dpsize << "\n"
              << "小数精度: " << num.prec << "\n"
              << "有效位数: " << num.nsize << std::endl;
}
//>
const bool lnum::operator>(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign < l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst > sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//<
const bool lnum::operator<(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign > l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst < sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//==
const bool lnum::operator==(const lnum &l) const
{
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    return fst == sec && num.sign == l.num.sign;
}
//>=
const bool lnum::operator>=(const lnum &l) const
{
    return !(*this < l);
}
//<=
const bool lnum::operator<=(const lnum &l) const
{
    return !(*this > l);
}
//+
const lnum lnum::operator+(const lnum &l) const
{

    lnum r;
    attr &lr = r.num;
    const align &agn = AlignVec(num.vec, l.num.vec);
    const int &tsgn = num.sign, &lsgn = l.num.sign;
    const vector<int> &fst = agn.first, &sec = agn.second, &tvec = num.vec, &lvec = l.num.vec;
    lr.prec = num.prec;
    lr.dpsize = (num.dpsize >= l.num.dpsize ? num.dpsize : l.num.dpsize);
    size_t s = (fst.size() <= sec.size() ? fst.size() : sec.size());
    lr.vec = (fst >= sec ? fst : sec);
    for (size_t i = 0; i < s; i++)
    {
        lr.vec[i] += (tsgn * lsgn) * ((sec <= fst ? sec : fst)[i]);
    }
    if ((fst > sec && tsgn < lsgn) || (fst < sec && tsgn > lsgn) || (tsgn == -1 && lsgn == -1))
    {
        lr.sign = -1;
    }
    if (tsgn*lsgn == -1)
    {
        r.Borrow();
    }
    r.Carry().ClearZeroVec().VecToStr();
    lr.nsize = lr.vec.size();
    return r;
}
//符号取反
const lnum lnum::operator-() const
{
    const vector<int> &tvec = num.vec;
    lnum r = *this;
    if (tvec != vector<int>(tvec.size(), 0))
    {
        r.num.sign = -num.sign;
    }
    return r;
}
//-
const lnum lnum::operator-(const lnum &l) const
{
    lnum r;
    if (l.num.vec != vector<int>(1,0))
    {
        r = *this + (-l);
    }
    else
    {
        r = *this;
    }
    return r;
}
//+=
const lnum &lnum::operator+=(const lnum &l)
{
    *this = *this + l;
    return *this;
}
//前置++
const lnum &lnum::operator++()
{
    return *this += "1";
}
//-=
const lnum &lnum::operator-=(const lnum &l)
{
    *this = *this - l;
    return *this;
}
//前置--
const lnum &lnum::operator--()
{
    return *this -= "1";
}
//后置++
const lnum &operator++(lnum &l, const int i)
{
    return ++l;
}
//后置--
const lnum &operator--(lnum &l, const int i)
{
    return --l;
}
//>>
istream &operator>>(istream &is, lnum &l)
{
    string str;
    (is >> str).get();
    l = str;
    return is;
}
//<<
ostream &operator<<(ostream &os, const lnum &l)
{
    os << l.AttrToStr();
    return os;
}

test.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.cpp"
#include <ctime>
#include <cmath>
int main(int argc, char const *argv[])
{
    int s = 0;
    lnum::SetPrecision(6);
    lnum a, b;
    char n[20], m[20];
    n[19] = m[19] = '\0';
    srand(time(NULL));
    while (s != 30)
    {
        for (size_t i = 0; i < 19; i++)
        {
            n[i] = rand() % 9 + 48;
            
            m[i] = rand() % 9 + 48;
        }
        n[rand() % 15 + 2] = '.';
        m[rand() % 17 + 2] = '.';
        if (s % 2 == 0)
        {
            n[0] = '-';
        }
        if (s % 3 == 0)
        {
            m[0] = '-';
        }
        a = m;
        b = n;
        std::cout << "第" << s << "组" << std::endl;
        std::cout << a << " - " << b << " = " << (a - b) << "\n"
                  << a << " + " << b << " = " << (a + b) << "\n\n"
                  << std::boolalpha
                  << a << " >  " << b << ": " << (a > b) << "\n\n"
                  << a << " == " << b << ": " << (a == b) << "\n\n"
                  << a << " <  " << b << ": " << (a < b) << "\n\n"
                  << a << " >= " << b << ": " << (a >= b) << "\n\n"
                  << a << " <= " << b << ": " << (a <= b) << "\n"
                  << "------------------------------------------------"
                  << "\n\n";
        s++;
    }
    return 0;
}
============================================
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-ftgy2k0d.itp --stdout=Microsoft-MIEngine-Out-ypcddczr.vhp --stderr=Microsoft-MIEngine-Error-aa1im5qa.s4b --pid=Microsoft-MIEngine-Pid-hnjifgnz.bpc --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
第0组
-3310042360415.7776 - -524736183.452446 = -3309517624232.325154
-3310042360415.7776 + -524736183.452446 = -3310567096599.230046

-3310042360415.7776 >  -524736183.452446: false

-3310042360415.7776 == -524736183.452446: false

-3310042360415.7776 <  -524736183.452446: true

-3310042360415.7776 >= -524736183.452446: false

-3310042360415.7776 <= -524736183.452446: true
------------------------------------------------

第1组
28831215742566646.3 - 5015516270.023648 = 28831210727050376.276352
28831215742566646.3 + 5015516270.023648 = 28831220758082916.323648

28831215742566646.3 >  5015516270.023648: true

28831215742566646.3 == 5015516270.023648: false

28831215742566646.3 <  5015516270.023648: false

28831215742566646.3 >= 5015516270.023648: true

28831215742566646.3 <= 5015516270.023648: false
------------------------------------------------

第2组
8060338.678225 - -84420108108320.115 = 84420116168658.793225
8060338.678225 + -84420108108320.115 = -84420100047981.436775

8060338.678225 >  -84420108108320.115: true

8060338.678225 == -84420108108320.115: false

8060338.678225 <  -84420108108320.115: false

8060338.678225 >= -84420108108320.115: true

8060338.678225 <= -84420108108320.115: false
------------------------------------------------

第3组
-571270373.546517 - 608762417821245.015 = -608762989091618.561517
-571270373.546517 + 608762417821245.015 = 608761846550871.468483

-571270373.546517 >  608762417821245.015: false

-571270373.546517 == 608762417821245.015: false

-571270373.546517 <  608762417821245.015: true

-571270373.546517 >= 608762417821245.015: false

-571270373.546517 <= 608762417821245.015: true
------------------------------------------------

第4组
128261185.021763 - -650.223463 = 128261835.245226
128261185.021763 + -650.223463 = 128260534.798300

128261185.021763 >  -650.223463: true

128261185.021763 == -650.223463: false

128261185.021763 <  -650.223463: false

128261185.021763 >= -650.223463: true

128261185.021763 <= -650.223463: false
------------------------------------------------

第5组
61616.436721 - 33043218771.088144 = -33043157154.651423
61616.436721 + 33043218771.088144 = 33043280387.524865

61616.436721 >  33043218771.088144: false

61616.436721 == 33043218771.088144: false

61616.436721 <  33043218771.088144: true

61616.436721 >= 33043218771.088144: false

61616.436721 <= 33043218771.088144: true
------------------------------------------------

第6组
-870615721.325035 - -185213.632376 = -870430507.692659
-870615721.325035 + -185213.632376 = -870800934.957411

-870615721.325035 >  -185213.632376: false

-870615721.325035 == -185213.632376: false

-870615721.325035 <  -185213.632376: true

-870615721.325035 >= -185213.632376: false

-870615721.325035 <= -185213.632376: true
------------------------------------------------

第7组
1124767517571.72682 - 55438616262.520006 = 1069328901309.206814
1124767517571.72682 + 55438616262.520006 = 1180206133834.246826

1124767517571.72682 >  55438616262.520006: true

1124767517571.72682 == 55438616262.520006: false

1124767517571.72682 <  55438616262.520006: false

1124767517571.72682 >= 55438616262.520006: true

1124767517571.72682 <= 55438616262.520006: false
------------------------------------------------

第8组
323674318651447.714 - -705624841320.74112 = 324379943492768.45512
323674318651447.714 + -705624841320.74112 = 322968693810126.97288

323674318651447.714 >  -705624841320.74112: true

323674318651447.714 == -705624841320.74112: false

323674318651447.714 <  -705624841320.74112: false

323674318651447.714 >= -705624841320.74112: true

323674318651447.714 <= -705624841320.74112: false
------------------------------------------------

第9组
-1144665306707771.6 - 481634332632.706008 = -1145146941040404.306008
-1144665306707771.6 + 481634332632.706008 = -1144183672375138.893992

-1144665306707771.6 >  481634332632.706008: false

-1144665306707771.6 == 481634332632.706008: false

-1144665306707771.6 <  481634332632.706008: true

-1144665306707771.6 >= 481634332632.706008: false

-1144665306707771.6 <= 481634332632.706008: true
------------------------------------------------

第10组
8732.058657 - -782.778583 = 9514.837240
8732.058657 + -782.778583 = 7949.280074

8732.058657 >  -782.778583: true

8732.058657 == -782.778583: false

8732.058657 <  -782.778583: false

8732.058657 >= -782.778583: true

8732.058657 <= -782.778583: false
------------------------------------------------

第11组
25861.444410 - 7026250123802.44388 = -7026250097940.999470
25861.444410 + 7026250123802.44388 = 7026250149663.888290

25861.444410 >  7026250123802.44388: false

25861.444410 == 7026250123802.44388: false

25861.444410 <  7026250123802.44388: true

25861.444410 >= 7026250123802.44388: false

25861.444410 <= 7026250123802.44388: true
------------------------------------------------

第12组
-447.758342 - -22164118630338.377 = 22164118629890.618658
-447.758342 + -22164118630338.377 = -22164118630786.135342

-447.758342 >  -22164118630338.377: true

-447.758342 == -22164118630338.377: false

-447.758342 <  -22164118630338.377: false

-447.758342 >= -22164118630338.377: true

-447.758342 <= -22164118630338.377: false
------------------------------------------------

第13组
8747523615.320310 - 2623811116402745.74 = -2623802368879130.419690
8747523615.320310 + 2623811116402745.74 = 2623819863926361.060310

8747523615.320310 >  2623811116402745.74: false

8747523615.320310 == 2623811116402745.74: false

8747523615.320310 <  2623811116402745.74: true

8747523615.320310 >= 2623811116402745.74: false

8747523615.320310 <= 2623811116402745.74: true
------------------------------------------------

第14组
21.417337 - -841.238130 = 862.655467
21.417337 + -841.238130 = -819.820793

21.417337 >  -841.238130: true

21.417337 == -841.238130: false

21.417337 <  -841.238130: false

21.417337 >= -841.238130: true

21.417337 <= -841.238130: false
------------------------------------------------

第15组
-1540252.622237 - 3301483542760336.28 = -3301483544300588.902237
-1540252.622237 + 3301483542760336.28 = 3301483541220083.657763

-1540252.622237 >  3301483542760336.28: false

-1540252.622237 == 3301483542760336.28: false

-1540252.622237 <  3301483542760336.28: true

-1540252.622237 >= 3301483542760336.28: false

-1540252.622237 <= 3301483542760336.28: true
------------------------------------------------

第16组
804744277.731238 - -57630625470.512714 = 58435369748.243952
804744277.731238 + -57630625470.512714 = -56825881192.781476

804744277.731238 >  -57630625470.512714: true

804744277.731238 == -57630625470.512714: false

804744277.731238 <  -57630625470.512714: false

804744277.731238 >= -57630625470.512714: true

804744277.731238 <= -57630625470.512714: false
------------------------------------------------

第17组
54588025.143467 - 37181153624615.0134 = -37181099036589.869933
54588025.143467 + 37181153624615.0134 = 37181208212640.156867

54588025.143467 >  37181153624615.0134: false

54588025.143467 == 37181153624615.0134: false

54588025.143467 <  37181153624615.0134: true

54588025.143467 >= 37181153624615.0134: false

54588025.143467 <= 37181153624615.0134: true
------------------------------------------------

第18组
-47.444014 - -253.203555 = 205.759541
-47.444014 + -253.203555 = -300.647569

-47.444014 >  -253.203555: true

-47.444014 == -253.203555: false

-47.444014 <  -253.203555: false

-47.444014 >= -253.203555: true

-47.444014 <= -253.203555: false
------------------------------------------------

第19组
6633216310805.67205 - 561852.867423 = 6633215748952.804627
6633216310805.67205 + 561852.867423 = 6633216872658.539473

6633216310805.67205 >  561852.867423: true

6633216310805.67205 == 561852.867423: false

6633216310805.67205 <  561852.867423: false

6633216310805.67205 >= 561852.867423: true

6633216310805.67205 <= 561852.867423: false
------------------------------------------------

第20组
6658841.357124 - -17131451.306243 = 23790292.663367
6658841.357124 + -17131451.306243 = -10472609.949119

6658841.357124 >  -17131451.306243: true

6658841.357124 == -17131451.306243: false

6658841.357124 <  -17131451.306243: false

6658841.357124 >= -17131451.306243: true

6658841.357124 <= -17131451.306243: false
------------------------------------------------

第21组
-200550.220533 - 3284472785364.15405 = -3284472985914.374583
-200550.220533 + 3284472785364.15405 = 3284472584813.933517

-200550.220533 >  3284472785364.15405: false

-200550.220533 == 3284472785364.15405: false

-200550.220533 <  3284472785364.15405: true

-200550.220533 >= 3284472785364.15405: false

-200550.220533 <= 3284472785364.15405: true
------------------------------------------------

第22组
301083560.047325 - -8.655267 = 301083568.702592
301083560.047325 + -8.655267 = 301083551.392058

301083560.047325 >  -8.655267: true

301083560.047325 == -8.655267: false

301083560.047325 <  -8.655267: false

301083560.047325 >= -8.655267: true

301083560.047325 <= -8.655267: false
------------------------------------------------

第23组
148175.157835 - 286266.700022 = -138091.542187
148175.157835 + 286266.700022 = 434441.857857

148175.157835 >  286266.700022: false

148175.157835 == 286266.700022: false

148175.157835 <  286266.700022: true

148175.157835 >= 286266.700022: false

148175.157835 <= 286266.700022: true
------------------------------------------------

第24组
-2621.331256 - -52857260523.474251 = 52857257902.142995
-2621.331256 + -52857260523.474251 = -52857263144.805507

-2621.331256 >  -52857260523.474251: true

-2621.331256 == -52857260523.474251: false

-2621.331256 <  -52857260523.474251: false

-2621.331256 >= -52857260523.474251: true

-2621.331256 <= -52857260523.474251: false
------------------------------------------------

第25组
8773827850628.74812 - 61.476741 = 8773827850567.271379
8773827850628.74812 + 61.476741 = 8773827850690.224861

8773827850628.74812 >  61.476741: true

8773827850628.74812 == 61.476741: false

8773827850628.74812 <  61.476741: false

8773827850628.74812 >= 61.476741: true

8773827850628.74812 <= 61.476741: false
------------------------------------------------

第26组
7351440008672337.43 - -7380726730082.0154 = 7358820735402419.4454
7351440008672337.43 + -7380726730082.0154 = 7344059281942255.4146

7351440008672337.43 >  -7380726730082.0154: true

7351440008672337.43 == -7380726730082.0154: false

7351440008672337.43 <  -7380726730082.0154: false

7351440008672337.43 >= -7380726730082.0154: true

7351440008672337.43 <= -7380726730082.0154: false
------------------------------------------------

第27组
-13383677307.082306 - 3452747742011.25402 = -3466131419318.336326
-13383677307.082306 + 3452747742011.25402 = 3439364064704.171714

-13383677307.082306 >  3452747742011.25402: false

-13383677307.082306 == 3452747742011.25402: false

-13383677307.082306 <  3452747742011.25402: true

-13383677307.082306 >= 3452747742011.25402: false

-13383677307.082306 <= 3452747742011.25402: true
------------------------------------------------

第28组
767613420.170572 - -85462.430530 = 767698882.601102
767613420.170572 + -85462.430530 = 767527957.740042

767613420.170572 >  -85462.430530: true

767613420.170572 == -85462.430530: false

767613420.170572 <  -85462.430530: false

767613420.170572 >= -85462.430530: true

767613420.170572 <= -85462.430530: false
------------------------------------------------

第29组
31813500071.436147 - 6551343.075474 = 31806948728.360673
31813500071.436147 + 6551343.075474 = 31820051414.511621

31813500071.436147 >  6551343.075474: true

31813500071.436147 == 6551343.075474: false

31813500071.436147 <  6551343.075474: false

31813500071.436147 >= 6551343.075474: true

31813500071.436147 <= 6551343.075474: false
------------------------------------------------


E:\Users\admin\Documents\VScode\Code>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-30 15:06:20 | 显示全部楼层
本帖最后由 bin554385863 于 2019-12-30 15:26 编辑

2019年12月30日15:00:34

添加新数据
bool  iszero; 判断一个数字字符串是否为0;

struct attr
    {
        int sign = 1; //符号
        bool iszero = true;//默认为true
        size_t dpsize = 0;     //小数位;
        size_t prec = 0;       //精度
        size_t nsize = 1;      //有效位数
        string str = "0";      //过渡字符串
        vector<int> vec = {0}; //数组;
    };

对应新函数

const bool IsZero(const lnum &l) const;

const bool lnum::IsZero(const lnum &l) const
{
    return !((!num.iszero) || (!l.num.iszero));
}

lnum.h
#ifndef LNUM_H
#define LNUM_H
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
using std::istream;
using std::ostream;
using std::string;
using std::stringstream;
using std::vector;
class lnum
{
    struct attr
    {
        int sign = 1; //符号
        bool iszero = true;
        size_t dpsize = 0;     //小数位;
        size_t prec = 0;       //精度
        size_t nsize = 1;      //有效位数
        string str = "0";      //过渡字符串
        vector<int> vec = {0}; //数组;
    };
    struct align
    {
        vector<int> first;
        vector<int> second;
    };

private:
    //小数精度;
    static size_t precision;
    //数据;
    attr num;
    //str to attr
    const attr StrToAttr(const string &str) const;
    //attr to str
    const string AttrToStr() const;
    //进1
    lnum &Carry();
    //借1
    lnum Borrow();
    //对齐
    const align AlignVec(const vector<int> &m, const vector<int> &n) const;
    //clear zero vec
    lnum &ClearZeroVec();
    //vec to str
    lnum &VecToStr();
    //iszero
    const bool IsZero(const lnum &l) const;

public:
    lnum(const char *cstr = "0") : num(StrToAttr(cstr)) {}
    lnum(const string &str) : num(StrToAttr(str)) {}
    lnum(const lnum &l) : num(l.num) {}
    //------------------------------------------------------------------
    //设置精度
    static void SetPrecision(const size_t i);
    //展示信息
    void Showinfo() const;
    //>
    const bool operator>(const lnum &l) const;
    //<
    const bool operator<(const lnum &l) const;
    //==
    const bool operator==(const lnum &l) const;
    //>=
    const bool operator>=(const lnum &l) const;
    //<=
    const bool operator<=(const lnum &l) const;
    //+
    const lnum operator+(const lnum &l) const;
    //-
    const lnum operator-(const lnum &l) const;
    const lnum operator-() const;
    //+=
    const lnum &operator+=(const lnum &l);
    //++
    const lnum &operator++();
    //-=
    const lnum &operator-=(const lnum &l);
    //--
    const lnum &operator--();
    //odject++
    friend const lnum &operator++(lnum &l, const int i);
    //object--
    friend const lnum &operator--(lnum &l, const int i);
    //>>
    friend istream &operator>>(istream &is, const lnum &l);
    //<<
    friend ostream &operator<<(ostream &os, const lnum &l);
};
size_t lnum::precision = 0;
#endif
lnum.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
//str to attr
const lnum::attr lnum::StrToAttr(const string &str) const
{
    attr n;
    n.prec = precision;
    n.str = str;
    string &nstr = n.str;
    size_t &ndps = n.dpsize, &npre = n.prec;
    if (nstr[0] == '-')
    {
        nstr.erase(0, 1);
    }
    if (int(nstr.find('.')) != -1)
    {
        nstr.erase(nstr.find('.'), 1);
    }
    if (nstr == string(nstr.size(), '0'))
    {
        nstr.clear();
        nstr.push_back('0');
    }
    else //if (nstr != string(nstr.size(), '0'))
    {
        
        n.iszero = false;//iszero
        size_t s = nstr.size(), i = 0;
        if (str[0] == '-')
        {
            n.sign = -1;
        }
        if (int(str.find('.')) != -1)
        {
            ndps = str.size() - str.find('.') - 1;
            if (nstr[0] == '0')
            {

                for (; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                if (i >= s - ndps)
                {
                    nstr.erase(0, s - ndps - 1);
                }
                else if (i < s - ndps)
                {
                    nstr.erase(0, i);
                }
            }
        }
        else
        {
            if (nstr[0] == '0')
            {
                for (i = 0; i < s; i++)
                {
                    if (nstr[i] != '0')
                    {
                        break;
                    }
                }
                nstr.erase(0, i);
            }
        }
        if (npre > ndps)
        {
            nstr.insert(nstr.end(), npre - ndps, '0');
        }
        else if (npre < ndps)
        {
            nstr.erase(nstr.size() - (ndps - npre));
            ndps = npre;
        }
        n.vec.clear();
        for (char c : nstr)
        {
            n.vec.push_back(c - 48);
        }
        n.nsize = nstr.size();
    }
    return n;
}
//attr to str
const string lnum::AttrToStr() const
{
    string t = num.str, &str = t;
    const size_t &prec = num.prec, &dpsize = num.dpsize;
    if (prec != 0 && str.size() != 1)
    {
        if (dpsize == prec)
        {
            str.insert(str.size() - prec, 1, '.');
        }
        else if (dpsize < prec && dpsize != 0)
        {
            str.erase(str.size() - (prec - dpsize));
            str.insert(str.size() - dpsize, 1, '.');
        }
        else if (dpsize == 0)
        {
            str.erase(str.size() - prec);
        }
    }
    if (num.sign == -1)
    {
        str.insert(0, 1, '-');
    }
    return str;
}
//进1
lnum &lnum::Carry()
{
    vector<int> &v = num.vec;
    v.insert(v.begin(), 1, 0);
    size_t s = v.size();
    for (size_t i = s - 1; i > 0; i--)
    {
        int t = v[i];
        v[i] = t % 10;
        v[i - 1] += t / 10;
    }
    return *this;
}
//借1
lnum lnum::Borrow()
{
    vector<int> &v = num.vec;
    size_t s = v.size();
    for (size_t i = 1; i < s; i++)
    {
        v[i - 1] -= 1;
        v[i] += 10;
    }
    return *this;
}
//对齐
const lnum::align lnum::AlignVec(const vector<int> &m, const vector<int> &n) const
{
    align agn;
    vector<int> &fst = agn.first, &sec = agn.second;
    fst = m, sec = n;
    size_t fsize = fst.size(), ssize = sec.size();
    if (fsize < ssize)
    {
        fst.insert(fst.begin(), ssize - fsize, 0);
    }
    else if (fsize > ssize)
    {
        sec.insert(sec.begin(), fsize - ssize, 0);
    }
    return agn;
}
//clear zero vec
lnum &lnum::ClearZeroVec()
{
    const size_t &prec = num.prec;
    vector<int> &v = num.vec;
    
    if (num.iszero)//v == vector<int>(v.size(), 0)
    {
        v.clear();
        v.push_back(0);
    }
    else if (v[0] == 0)
    {
        size_t s = v.size(), i = 0;
        if (prec != 0)
        {

            for (; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            if (i >= s - prec)
            {
                v.erase(v.begin(), v.begin() + (s - prec - 1));
            }
            else if (i < s - prec)
            {
                v.erase(v.begin(), v.begin() + i);
            }
        }
        else
        {
            for (i = 0; i < s; i++)
            {
                if (v[i] != 0)
                {
                    break;
                }
            }
            v.erase(v.begin(), v.begin() + i);
        }
    }
    return *this;
}
//vec to str
lnum &lnum::VecToStr()
{
    num.str.clear();
    for (int i : num.vec)
    {
        num.str.push_back(i + 48);
    }
    return *this;
}
//iszero
const bool lnum::IsZero(const lnum &l) const
{
    return !((!num.iszero) || (!l.num.iszero));
}
//------------------------------------------------------------------------
//设置精度
void lnum::SetPrecision(const size_t i)
{
    lnum::precision = i;
}
//展示信息
void lnum::Showinfo() const
{
    std::cout << "有效数位: ";
    for (int i : num.vec)
    {
        std::cout << i << " ";
    }
    std::cout << std::endl;
    std::cout << "正负符号: " << num.sign << "\n"
              << std::boolalpha << "是否为零: " << num.iszero << "\n"
              << "小数位数: " << num.dpsize << "\n"
              << "小数精度: " << num.prec << "\n"
              << "有效位数: " << num.nsize << std::endl;
}
//>
const bool lnum::operator>(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign < l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst > sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//<
const bool lnum::operator<(const lnum &l) const
{
    bool f = true;
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    if (num.sign > l.num.sign)
    {
        f = false;
    }
    if (num.sign == l.num.sign)
    {
        f = fst < sec;
        if (num.sign == -1)
        {
            f = !f;
        }
    }
    return f;
}
//==
const bool lnum::operator==(const lnum &l) const
{
    align agn = AlignVec(num.vec, l.num.vec);
    const vector<int> &fst = agn.first, &sec = agn.second;
    return fst == sec && num.sign == l.num.sign;
}
//>=
const bool lnum::operator>=(const lnum &l) const
{
    return !(*this < l);
}
//<=
const bool lnum::operator<=(const lnum &l) const
{
    return !(*this > l);
}
//+
const lnum lnum::operator+(const lnum &l) const
{

    lnum r;
    attr &lr = r.num;
    lr.iszero = this->IsZero(l);
    const align &agn = AlignVec(num.vec, l.num.vec);
    const int &tsgn = num.sign, &lsgn = l.num.sign;
    const vector<int> &fst = agn.first, &sec = agn.second, &tvec = num.vec, &lvec = l.num.vec;
    lr.prec = num.prec;
    lr.dpsize = (num.dpsize >= l.num.dpsize ? num.dpsize : l.num.dpsize);
    size_t s = (fst.size() <= sec.size() ? fst.size() : sec.size());
    lr.vec = (fst >= sec ? fst : sec);
    for (size_t i = 0; i < s; i++)
    {
        lr.vec[i] += (tsgn * lsgn) * ((sec <= fst ? sec : fst)[i]);
    }
    if ((fst > sec && tsgn < lsgn) || (fst < sec && tsgn > lsgn) || (tsgn == -1 && lsgn == -1))
    {
        lr.sign = -1;
    }
    if (tsgn * lsgn == -1)
    {
        r.Borrow();
    }
    r.Carry().ClearZeroVec().VecToStr();
    lr.nsize = lr.vec.size();
    return r;
}
//符号取反
const lnum lnum::operator-() const
{
    const vector<int> &tvec = num.vec;
    lnum r = *this;
    if (num.iszero == false)//(tvec != vector<int>(tvec.size(), 0))
    {
        r.num.sign = -num.sign;
    }
    return r;
}
//-
const lnum lnum::operator-(const lnum &l) const
{
    lnum r;
    r.num.iszero = this->IsZero(l);
    if (l.num.iszero == false)//(l.num.vec != vector<int>(1, 0))
    {
        r = *this + (-l);
    }
    else
    {
        r = *this;
    }
    return r;
}
//+=
const lnum &lnum::operator+=(const lnum &l)
{
    *this = *this + l;
    return *this;
}
//前置++
const lnum &lnum::operator++()
{
    return *this += "1";
}
//-=
const lnum &lnum::operator-=(const lnum &l)
{
    *this = *this - l;
    return *this;
}
//前置--
const lnum &lnum::operator--()
{
    return *this -= "1";
}
//后置++
const lnum &operator++(lnum &l, const int i)
{
    return ++l;
}
//后置--
const lnum &operator--(lnum &l, const int i)
{
    return --l;
}
//>>
istream &operator>>(istream &is, lnum &l)
{
    string str;
    (is >> str).get();
    l = str;
    return is;
}
//<<
ostream &operator<<(ostream &os, const lnum &l)
{
    os << l.AttrToStr();
    return os;
}
test.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.cpp"
#include <ctime>
#include <cmath>
int main(int argc, char const *argv[])
{
    int s = 0;
    lnum::SetPrecision(6);
    lnum a, b;
    char n[9], m[9];
    n[8] = m[8] = '\0';
    srand(time(NULL));
    while (s != 12)
    {
        for (size_t i = 0; i < 8; i++)
        {
            n[i] = rand() % 9 + 48;

            m[i] = rand() % 9 + 48;
        }
        n[rand() % 6 + 1] = '.';
        m[rand() % 6 + 1] = '.';
        if (s % 2 == 0)
        {
            n[0] = '-';
        }
        if (s % 3 == 0)
        {
            m[0] = '-';
        }
        a = m;
        b = n;
        std::cout << "第" << s << "组" << std::endl;
        std::cout << a << " - " << b << " = " << (a - b) << "\n"
                  << a << " + " << b << " = " << (a + b) << "\n\n"
                  << std::boolalpha
                  << a << " >  " << b << ": " << (a > b) << "\n\n"
                  << a << " == " << b << ": " << (a == b) << "\n\n"
                  << a << " <  " << b << ": " << (a < b) << "\n\n"
                  << a << " >= " << b << ": " << (a >= b) << "\n\n"
                  << a << " <= " << b << ": " << (a <= b) << "\n"
                  << "------------------------------------------------"
                  << "\n\n";
        s++;
    }
    return 0;
}
---------------------------------------------------------------------------------------------
Microsoft Windows [版本 10.0.18363.535]
(c) 2019 Microsoft Corporation。保留所有权利。

E:\Users\admin\Documents\VScode\Code>c:\Users\admin\.vscode\extensions\ms-vscode.cpptools-0.26.2\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-r4eqqtl4.g4x --stdout=Microsoft-MIEngine-Out-tdxveute.slr --stderr=Microsoft-MIEngine-Error-mmf3t4g3.0vo --pid=Microsoft-MIEngine-Pid-ube4pyyu.txb --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
第0组
-65066.5 - -28541.3 = -36525.2
-65066.5 + -28541.3 = -93607.8

-65066.5 >  -28541.3: false   

-65066.5 == -28541.3: false   

-65066.5 <  -28541.3: true   

-65066.5 >= -28541.3: false

-65066.5 <= -28541.3: true
------------------------------------------------

第1组
45.31752 - 86.42017 = -41.10265
45.31752 + 86.42017 = 131.73769

45.31752 >  86.42017: false

45.31752 == 86.42017: false

45.31752 <  86.42017: true

45.31752 >= 86.42017: false

45.31752 <= 86.42017: true
------------------------------------------------

第2组
5.601777 - -65.7413 = 71.343077
5.601777 + -65.7413 = -60.139523

5.601777 >  -65.7413: true

5.601777 == -65.7413: false

5.601777 <  -65.7413: false

5.601777 >= -65.7413: true

5.601777 <= -65.7413: false
------------------------------------------------

第3组
-6618.72 - 581607.6 = -588226.32
-6618.72 + 581607.6 = 574988.88

-6618.72 >  581607.6: false

-6618.72 == 581607.6: false

-6618.72 <  581607.6: true

-6618.72 >= 581607.6: false

-6618.72 <= 581607.6: true
------------------------------------------------

第4组
36812.51 - -27147.1 = 63959.61
36812.51 + -27147.1 = 9665.41

36812.51 >  -27147.1: true

36812.51 == -27147.1: false

36812.51 <  -27147.1: false

36812.51 >= -27147.1: true

36812.51 <= -27147.1: false
------------------------------------------------

第5组
445684.7 - 4821.640 = 440863.060
445684.7 + 4821.640 = 450506.340

445684.7 >  4821.640: true

445684.7 == 4821.640: false

445684.7 <  4821.640: false

445684.7 >= 4821.640: true

445684.7 <= 4821.640: false
------------------------------------------------

第6组
-.650710 - -4.45786 = 3.807150
-.650710 + -4.45786 = -5.108570

-.650710 >  -4.45786: true

-.650710 == -4.45786: false

-.650710 <  -4.45786: false

-.650710 >= -4.45786: true

-.650710 <= -4.45786: false
------------------------------------------------

第7组
154.2605 - 350.4061 = -196.1456
154.2605 + 350.4061 = 504.6666

154.2605 >  350.4061: false

154.2605 == 350.4061: false

154.2605 <  350.4061: true

154.2605 >= 350.4061: false

154.2605 <= 350.4061: true
------------------------------------------------

第8组
47.87481 - -5507.32 = 5555.19481
47.87481 + -5507.32 = -5459.44519

47.87481 >  -5507.32: true

47.87481 == -5507.32: false

47.87481 <  -5507.32: false

47.87481 >= -5507.32: true

47.87481 <= -5507.32: false
------------------------------------------------

第9组
-653.76 - 680005.7 = -680659.46
-653.76 + 680005.7 = 679351.94

-653.76 >  680005.7: false

-653.76 == 680005.7: false

-653.76 <  680005.7: true

-653.76 >= 680005.7: false

-653.76 <= 680005.7: true
------------------------------------------------

第10组
462520.0 - -28804.2 = 491324.2
462520.0 + -28804.2 = 433715.8

462520.0 >  -28804.2: true

462520.0 == -28804.2: false

462520.0 <  -28804.2: false

462520.0 >= -28804.2: true

462520.0 <= -28804.2: false
------------------------------------------------

第11组
746407.4 - 150352.6 = 596054.8
746407.4 + 150352.6 = 896760.0

746407.4 >  150352.6: true

746407.4 == 150352.6: false

746407.4 <  150352.6: false

746407.4 >= 150352.6: true

746407.4 <= 150352.6: false
------------------------------------------------


E:\Users\admin\Documents\VScode\Code>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-5 05:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表