瞎tam写
本帖最后由 bin554385863 于 2019-12-20 00:06 编辑#include <iostream>
#include <string>
#include <vector>
class num
{
typedef std::ostream ostream;
typedef std::string string;
typedef std::vector<int> vector;
private:
string STR;
vector VEC;
size_t LEN, pLEN;
//小数位数
size_t const psize(const char *cstr) const
{
string &&str = cstr;
size_t r = 0;
if (int(str.find('.')) != -1)
{
r = str.size() - str.find('.') - 1;
}
return r;
}
//翻转正负
void neg(vector &v) const
{
for (int &i : v)
{
i = -i;
}
}
//str to vec
vector const StrToVec(const char *cstr) const
{
vector v;
string &&str = cstr;
size_t s = psize(cstr);
if (s != 0)
{
str.erase(str.size() - s -1, 1);
}
char c = str;
if (c != '-')
{
for(char i:str)
{
v.push_back( i - 48);
}
}
if (c == '-')
{
str.erase(str.find('-'), 1);
for(char i:str)
{
v.push_back( 48 - i);
}
}
return v;
}
public:
num(char *cstr = "0"):STR(cstr), VEC(StrToVec(cstr)), pLEN(psize(cstr)), LEN(VEC.size()){}
void showinfo()
{
std::cout<<"权值: ";
for(int i:VEC)
{
std::cout<<i<<" ";
}
std::cout<<std::endl;
std::cout<<"有效位数: "<<LEN<<"\n有效小数: "<<pLEN<<std::endl;
}
};
int main(int argc, char const *argv[])
{
num n = "-3.141592653";
n.showinfo();
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-s2kuope4.iyq --stdout=Microsoft-MIEngine-Out-cju20ufx.uoc --stderr=Microsoft-MIEngine-Error-btvph03z.hko --pid=Microsoft-MIEngine-Pid-wamu1j4d.w31 --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
权值: -3 -1 -4 -1 -5 -9 -2 -6 -5 -3
有效位数: 10
有效小数: 9
E:\Users\admin\Documents\VScode\Code>
#include <iostream>
#include <string>
#include <vector>
class num
{
typedef std::ostream ostream;
typedef std::string string;
typedef std::vector<int> vector;
private:
string STR;
vector VEC;
size_t LEN, pLEN;
//小数位数
size_t const psize(const char *cstr) const
{
string &&str = cstr;
size_t r = 0;
if (int(str.find('.')) != -1)
{
r = str.size() - str.find('.') - 1;
}
return r;
}
//翻转正负
void neg(vector &v) const
{
for (int &i : v)
{
i = -i;
}
}
//str to vec
vector const StrToVec(const char *cstr) const
{
vector v;
string &&str = cstr;
size_t s = psize(cstr);
if (s != 0)
{
str.erase(str.size() - s - 1, 1);
}
char c = str;
if (c != '-')
{
for (char i : str)
{
v.push_back(i - 48);
}
}
if (c == '-')
{
str.erase(str.find('-'), 1);
for (char i : str)
{
v.push_back(48 - i);
}
}
return v;
}
//消零
void ClearZero(vector &v) const
{
size_t inx = 0, sz = v.size();
if (v == 0)
{
for (size_t i = 0; i < sz; i++)
{
if (v != 0)
{
inx = i;
break;
}
}
v.erase(v.begin(), v.begin() + inx);
}
if (v == 0)
{
for (size_t i = sz - 1; i > -1; i--)
{
if (v != 0)
{
inx = i;
}
}
v.erase(v.begin() + inx + 2);
}
}
//进位
void carry(vector &v) const
{
v.insert(v.begin(), 1, 0);
size_t s = v.size();
for (size_t i = s-1; i > 0; i--)
{
v %= 10;
v += v / 10;
}
ClearZero(v);
}
public:
num(char *cstr = "0") : STR(cstr), VEC(StrToVec(cstr)), pLEN(psize(cstr)), LEN(VEC.size()) {}
num(const num &n) : STR(n.STR), VEC(n.VEC), pLEN(n.pLEN), LEN(n.LEN) {}
void showinfo()
{
std::cout << "权值: ";
for (int i : VEC)
{
std::cout << i << " ";
}
std::cout << std::endl;
std::cout << "有效位数: " << LEN << "\n有效小数: " << pLEN << std::endl;
}
num const operator+(const num &n) const
{
num r;
if (pLEN == n.pLEN)
{
r.VEC = LEN >= n.LEN ? VEC : n.VEC;
size_t sz = LEN < n.LEN ? LEN : n.LEN;
for (size_t i = 0; i < sz; i++)
{
*(r.VEC.rbegin() + i) += *(n.VEC.rbegin() + i);
}
}
else
{
vector minplen = (pLEN < n.pLEN ? VEC : n.VEC), maxplen = (pLEN > n.pLEN ? VEC : n.VEC);
size_t s = (pLEN > n.pLEN ? pLEN : n.pLEN) - (pLEN < n.pLEN ? pLEN : n.pLEN);
minplen.insert(minplen.end(), s, 0);
size_t sz = minplen.size() <= maxplen.size() ? minplen.size() : maxplen.size();
r.VEC = minplen.size() >= maxplen.size() ? minplen : maxplen;
vector& a = minplen.size() < maxplen.size() ? minplen : maxplen;
for (size_t i = 0; i < sz; i++)
{
*(r.VEC.rbegin() + i) += *(a.rbegin() + i);
}
}
carry(r.VEC);
r.LEN = r.VEC.size();
r.pLEN = pLEN > n.pLEN ? pLEN : n.pLEN;
return r;
}
~num() {}
};
int main(int argc, char const *argv[])
{
num a = "3.14159", b = "123", n;
n = a + b;
n.showinfo();
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-z5ghz2pg.2bm --stdout=Microsoft-MIEngine-Out-4nzyiall.aev --stderr=Microsoft-MIEngine-Error-sfwzztyx.enc --pid=Microsoft-MIEngine-Pid-0y4opmqz.uw3 --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
权值: 1 2 6 1 4 1 5 9
有效位数: 8
有效小数: 5
E:\Users\admin\Documents\VScode\Code> 本帖最后由 bin554385863 于 2019-12-21 20:43 编辑
#include <iostream>
#include <string>
#include <vector>
class num
{
typedef std::ostream ostream;
typedef std::string string;
typedef std::vector<int> vector;
private:
string STR;
vector VEC;
size_t LEN, pLEN;
//小数位数
size_t const psize(const char *cstr) const;
//翻转正负
void neg(vector &v) const;
//str to vec
vector const StrToVec(const char *cstr) const;
//vec to str
const string VecToStr() const;
//消零
void ClearZero(vector &v) const;
//进位
void carry(vector &v) const;
public:
num(char *cstr = "0") : STR(cstr), VEC(StrToVec(cstr)), pLEN(psize(cstr)), LEN(VEC.size()) {}
num(const num &n) : STR(n.STR), VEC(n.VEC), pLEN(n.pLEN), LEN(n.LEN) {}
//有效位数
const size_t size() const { return LEN; }
//有效小数
const size_t psize() const { return pLEN; }
//信息
void showinfo() const;
//重载+
num const operator+(const num &n) const;
//重载<<
friend ostream &operator<<(ostream &os, const num &n)
{
os << n.STR;
return os;
}
~num() {}
};
//小数位数
size_t const num::psize(const char *cstr) const
{
string &&str = cstr;
size_t r = 0;
if (int(str.find('.')) != -1)
{
r = str.size() - str.find('.') - 1;
}
return r;
}
//翻转正负
void num::neg(vector &v) const
{
for (int &i : v)
{
i = -i;
}
}
//str to vec
num::vector const num::StrToVec(const char *cstr) const
{
vector v;
string &&str = cstr;
size_t s = psize(cstr);
if (s != 0)
{
str.erase(str.size() - s - 1, 1);
}
char c = str;
if (c != '-')
{
for (char i : str)
{
v.push_back(i - 48);
}
}
if (c == '-')
{
str.erase(str.find('-'), 1);
for (char i : str)
{
v.push_back(48 - i);
}
}
return v;
}
//vce to str
const num::string num::VecToStr() const
{
string str;
vector t = VEC;
int f = t;
if (f < 0)
{
neg(t);
}
for (int i : t)
{
str.push_back(i + 48);
}
if (pLEN != 0)
{
str.insert(str.size() - pLEN, 1, '.');
}
if (f < 0)
{
str.insert(0, 1, '-');
}
return str;
}
//消零
void num::ClearZero(vector &v) const
{
size_t inx = 0, sz = v.size();
if (v == 0)
{
for (size_t i = 0; i < sz; i++)
{
if (v != 0)
{
inx = i;
break;
}
}
v.erase(v.begin(), v.begin() + inx);
}
}
//进位
void num::carry(vector &v) const
{
v.insert(v.begin(), 1, 0);
size_t s = v.size();
for (size_t i = s - 1; i > 0; i--)
{
int t = v;
v = t % 10;
v += t / 10;
}
ClearZero(v);
}
//信息
void num::showinfo() const
{
std::cout << "权值: ";
for (int i : VEC)
{
std::cout << i << " ";
}
std::cout << std::endl;
std::cout << "有效位数: " << LEN << "\n有效小数: " << pLEN << std::endl;
}
//重载+
num const num::operator+(const num &n) const
{
num r;
if (pLEN == n.pLEN)
{
r.VEC = LEN >= n.LEN ? VEC : n.VEC;
size_t sz = LEN < n.LEN ? LEN : n.LEN;
for (size_t i = 0; i < sz; i++)
{
*(r.VEC.rbegin() + i) += *(n.VEC.rbegin() + i);
}
}
else
{
vector minplen = (pLEN < n.pLEN ? VEC : n.VEC), maxplen = (pLEN > n.pLEN ? VEC : n.VEC);
size_t s = (pLEN > n.pLEN ? pLEN : n.pLEN) - (pLEN < n.pLEN ? pLEN : n.pLEN);
minplen.insert(minplen.end(), s, 0);
size_t sz = minplen.size() <= maxplen.size() ? minplen.size() : maxplen.size();
r.VEC = minplen.size() >= maxplen.size() ? minplen : maxplen;
vector &a = minplen.size() < maxplen.size() ? minplen : maxplen;
r.VEC.insert(r.VEC.begin(), 1, 0);
for (size_t i = 0; i < sz; i++)
{
*(r.VEC.rbegin() + i) += *(a.rbegin() + i);
}
}
carry(r.VEC);
r.LEN = r.VEC.size();
r.pLEN = pLEN > n.pLEN ? pLEN : n.pLEN;
r.STR = r.VecToStr();
return r;
}
int main(int argc, char const *argv[])
{
num a = "99999999999999999.99999", b = "55555555555555555.55555", n;
n = a + b;
std::cout << a<<" + "<<b<<" = "<<n << std::endl;
n.showinfo();
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-gb0eycep.1uf --stdout=Microsoft-MIEngine-Out-emx4iwzt.db3 --stderr=Microsoft-MIEngine-Error-5pankwpf.1w1 --pid=Microsoft-MIEngine-Pid-q4rjlrln.0e4 --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
99999999999999999.99999 + 55555555555555555.55555 = 155555555555555555.55554
权值: 1 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4
有效位数: 23
有效小数: 5
E:\Users\admin\Documents\VScode\Code> 本帖最后由 bin554385863 于 2019-12-22 14:36 编辑
2019年12月22日14:30:55
推倒重写
#include <iostream>
#include <string>
#include <vector>
class num
{
typedef std::ostream ostream;
typedef std::string string;
typedef std::vector<int> vector;
private:
string STR;
vector VEC;
size_t LEN, pLEN;
//1:小数位数
size_t const psize(const char *cstr) const;
//2:翻转正负
vector const neg(const vector &v) const;
//3:str to vec
vector const StrToVec(const char *cstr) const;
//4:vec to str
const string VecToStr(const vector &t, size_t plen) const;
//5:消零
void ClearZero(vector &v) const;
//6:进位
void Carry(vector &v) const;
//7:退位
vector Abdicate(const vector &n) const;
//8:小数位对齐
vector pLenAlign(const vector &n, const vector &m, size_t nplen, size_t mplen) const;
//9:重载 vector +
friend vector operator+(const vector &m, const vector &n);
public:
//10:字符串构造
num(const char *cstr);
//11:vec构造
num(const vector &v, size_t vplen);
//12:copy构造
num(const num &n);
//13:长度
const size_t size() const { return LEN; }
//14:小数位长度
const size_t psize() const { return pLEN; }
//15:信息展示
void showinfo() const;
//16:重载+
num const operator+(const num &n) const;
//17:重载-
num const operator-(const num &n) const;
//18:重载<<
friend ostream &operator<<(ostream &os, const num &n);
~num() {}
};
//1:小数位数
size_t const num::psize(const char *cstr) const
{
string &&str = cstr;
size_t r = 0;
if (int(str.find('.')) != -1)
{
r = str.size() - str.find('.') - 1;
}
return r;
}
//2:翻转vector正负
num::vector const num::neg(const vector &v) const
{
vector r = v;
for (int &i : r)
{
i = -i;
}
return r;
}
//3:STR to VEC
num::vector const num::StrToVec(const char *cstr) const
{
vector v;
string &&str = cstr;
size_t s = psize(cstr);
if (s != 0)
{
str.erase(str.size() - s - 1, 1);
}
char c = str;
if (c != '-')
{
for (char i : str)
{
v.push_back(i - 48);
}
}
if (c == '-')
{
str.erase(str.find('-'), 1);
for (char i : str)
{
v.push_back(48 - i);
}
}
return v;
}
//4:VEC to STR
const num::string num::VecToStr(const vector &n, size_t plen) const
{
string str;
vector t = n;
int f = t;
if (f < 0)
{
t = neg(t);
}
for (int i : t)
{
str.push_back(i + 48);
}
if (plen != 0)
{
str.insert(str.size() - plen, 1, '.');
}
if (f < 0)
{
str.insert(0, 1, '-');
}
return str;
}
//5:消零
void num::ClearZero(vector &v) const
{
size_t inx = 0, sz = v.size();
if (v == 0)
{
for (size_t i = 0; i < sz; i++)
{
if (v != 0)
{
inx = i;
break;
}
}
v.erase(v.begin(), v.begin() + inx);
}
}
//6:进位
void num::Carry(vector &v) const
{
v.insert(v.begin(), 1, 0);
size_t s = v.size();
for (size_t i = s - 1; i > 0; i--)
{
int t = v;
v = t % 10;
v += t / 10;
}
ClearZero(v);
}
//7:退位
num::vector num::Abdicate(const vector &n) const
{
vector r = n;
size_t s = r.size();
for (size_t i = 1; i < s; i++)
{
r -= 1;
r += 10;
}
return r;
}
//8:小数位对齐,返回初始小数位最短的数组
num::vector num::pLenAlign(const vector &n, const vector &m, const size_t nplen, const size_t mplen) const
{
vector r;
if (nplen == mplen)
{
r = n.size() < m.size() ? n : m;
}
else
{
r = nplen < mplen ? n : m;
size_t s = (nplen > mplen ? nplen : mplen) - (nplen < mplen ? nplen : mplen);
r.insert(r.end(), s, 0);
}
return r;
}
//9:重载 vector +
num::vector operator+(const num::vector &m, const num::vector &n)
{
num::vector r = m.size() > n.size() ? m : n;
const num::vector &t = m.size() < n.size() ? m : n;
size_t s = m.size() < n.size() ? m.size() : n.size();
for (size_t i = 0; i < s; i++)
{
*(r.rbegin() + i) += *(t.rbegin() + i);
}
return r;
}
//------------------------------------------------------
int main(int argc, char const *argv[])
{
return 0;
}
本帖最后由 bin554385863 于 2019-12-25 02:50 编辑
2019年12月25日02:37:27
再次推倒重写
lnum.h
#ifndef LNUM_H
#define LNUM_H
#include <iostream>
#include <string>
#include <vector>
using std::ostream;
using std::string;
using std::vector;
class lnum
{
typedef vector<int>vecint;
struct Num
{
int sign;
vecint vec;
size_t nlen;
size_t dplen;
string str;
};
struct align
{
vecint shrter;
vecint longer;
};
private:
Num num;
//1:符号
const int Sign(const char *cstr) const;
//2:vecor
const vecint VecInt(const char *cstr) const;
//3:dplen
const size_t Dplen(const char *cstr) const;
//4:vec to str
const string VECtoSTR(const vecint &v, const int sign, const size_t dplen) const;
//5:clear zero
void ClearZero(vecint &v) const;
//6:满十进1
void Carry(vecint &v) const;
//7:借1退10
const vecint Borrow(const vecint &v) const;
//8:小数对齐
const align Align(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const;
public:
lnum(const char *cstr = "0") : num({Sign(cstr), VecInt(cstr), VecInt(cstr).size(), Dplen(cstr), cstr}){}
lnum(const lnum &l) : num(l.num){}
//?
void showinfo() const;
~lnum(){}
};
#endif
lnum.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
//1:符号
const int lnum::Sign(const char *cstr) const
{
int sign = 1;
if (cstr == '-')
{
sign = -1;
}
return sign;
}
//2:vecor
const lnum::vecint lnum::VecInt(const char *cstr) const
{
string s = cstr;
vecint v;
if (s == '-')
{
s.erase(0, 1);
}
if ((s.find('.')) != -1)
{
s.erase(s.find('.'), 1);
}
for(char c: s)
{
v.push_back(c - 48);
}
return v;
}
//3:dplen
const size_t lnum::Dplen(const char *cstr) const
{
string s = cstr;
size_t dplen = 0;
if (int(s.find('.')) != -1)
{
dplen = s.size() - s.find('.') - 1;
}
return dplen;
}
//4:vec to str
const string lnum::VECtoSTR(const vecint &v, const int sign, const size_t dplen) const
{
string s;
for(int i: v)
{
s.push_back(48 + i);
}
if (dplen != 0)
{
s.insert(s.size() - dplen, 1, '.');
}
if (sign == -1)
{
s.insert(0, 1, '-');
}
return s;
}
//5:clear zero
void lnum::ClearZero(vecint &v) const
{
size_t i = 0, s = v.size();
if (v == 0)
{
for (; i < s; i++)
{
if (v != 0)
{
break;
}
}
v.erase(v.begin(), v.begin() + i);
}
}
//6:满十进1
void lnum::Carry(vecint &v) const
{
v.insert(v.begin(), 1, 0);
size_t s = v.size();
for (size_t i = s-1; i > 0; i--)
{
int n = v;
v = n % 10;
v += n / 10;
}
ClearZero(v);
}
//7:借1退10
const lnum::vecint lnum::Borrow(const vecint &v) const
{
size_t s = v.size();
vecint r = v;
for (size_t i = 1; i < s; i++)
{
r -= 1;
r += 10;
}
return r;
}
//8:小数对齐
const lnum::align lnum::Align(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const
{
align agn;
if (mdpl == ndpl)
{
agn.shrter = m.size() <= n.size() ? m : n;
agn.longer = n.size() >= m.size() ? n : m;
}
else
{
vecint sdpl = mdpl < ndpl ? m : n;
const vecint &ldpl = mdpl > ndpl ? m : n;
size_t s = (mdpl > ndpl ? mdpl : ndpl) - (mdpl < ndpl ? mdpl : ndpl);
sdpl.insert(sdpl.end(), s, 0);
agn.shrter = sdpl.size() <= ldpl.size() ? sdpl : ldpl;
agn.longer = ldpl.size() >= sdpl.size() ? ldpl : sdpl;
}
return agn;
}
//?
void lnum::showinfo() const
{
std::cout << "正/负: " << num.sign << "\n"
<< "原数字: " << num.str << "\n"
<< "数字组成: ";
for (int i : num.vec)
{
std::cout << i << " ";
}
std::cout << "\n有效位数: " << num.nlen << "\n"
<< "小数位数: " << num.dplen;
}
int main(int argc, char const *argv[])
{
lnum a = "123456789.123";
a.showinfo();
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-21qtfmow.b5h --stdout=Microsoft-MIEngine-Out-ij3z1l0n.zqh --stderr=Microsoft-MIEngine-Error-vnwcmfmw.kdy --pid=Microsoft-MIEngine-Pid-kpqzxghx.1h4 --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
正/负: -1
原数字: -123456789.123
数字组成: 1 2 3 4 5 6 7 8 9 1 2 3
有效位数: 12
小数位数: 3
E:\Users\admin\Documents\VScode\Code> 所以老哥你在写大数运算。。? Croper 发表于 2019-12-25 10:49
所以老哥你在写大数运算。。?
是啊,顺利写完差不多就把我之前学的差不多理顺了,接下来才能静下心学继承 本帖最后由 Croper 于 2019-12-25 13:33 编辑
bin554385863 发表于 2019-12-25 13:15
是啊,顺利写完差不多就把我之前学的差不多理顺了,接下来才能静下心学继承
我也写过一个,感觉还是底层2进制比较好,位移运算比乘除法运算快多了,而且除法只需要考虑减法就行。 Croper 发表于 2019-12-25 13:32
我也写过一个,感觉还是底层2进制比较好,位移运算比乘除法运算快多了,而且除法只需要考虑减法就行。
目前只考虑加减,乘除还在思索,其他的计算等以后学了算法和数据结构再说,目前能力不足以支撑 本帖最后由 bin554385863 于 2019-12-25 17:48 编辑
lnum.h
2019年12月25日15:51:11
#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
{
typedef vector<int> vecint;
struct Num
{
int sign;
vecint vec;
size_t nlen;
size_t dplen;
string str;
};
struct align
{
vecint shrter;
vecint longer;
};
private:
Num num;
//0:STR to lnum
const Num STRtoNUM(const string &s) const;
//1:vec to str
const string VECtoSTR(const vecint &v, const int sign, const size_t dplen) const;
//2:clear zero
void ClearZero(vecint &v) const;
//3:满十进1
void Carry(vecint &v) const;
//4:借1退10
const vecint Borrow(const vecint &v) const;
//5:vec小数位对齐
const vecint vecAlign(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const;
//6:小数对齐
const align Align(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const;
//7:大于比较
const bool GreaterThan(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const;
//8:小于比较
const bool LessThan(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const;
//9:相等比较
const bool Equal(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const;
public:
lnum(const string &s):num(STRtoNUM(s)){}
lnum(const char *cstr = "0"):num(STRtoNUM(cstr)){}
lnum(const lnum &l) : num(l.num) {}
//?
void showinfo() const;
//?:overload >
const bool operator>(lnum &l) const;
//?:overload <
const bool operator<(lnum &l) const;
//?:overload ==
const bool operator==(lnum &l) const;
//over load >>
friend istream &operator>>(istream &is, lnum &n);
//overload <<
friend ostream &operator<<(ostream &os, const lnum &l);
~lnum() {}
};
#endif
lnum.cpp
#include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
//0:STR to lnum
const lnum::Num lnum::STRtoNUM(const string &s) const
{
Num n = {1, {0}, 1, 0, s};
string t = s;
if (t == '-')
{
n.sign = -1;
t.erase(0, 1);
}
if (int(t.find('.')) != -1)
{
n.dplen = t.size() - t.find('.') - 1;
t.erase(t.find('.'), 1);
}
n.vec.clear();
for (char c : t)
{
n.vec.push_back(c - 48);
}
n.nlen = n.vec.size();
return n;
}
//1:vec to str
const string lnum::VECtoSTR(const vecint &v, const int sign, const size_t dplen) const
{
string s;
for(int i: v)
{
s.push_back(48 + i);
}
if (dplen != 0)
{
s.insert(s.size() - dplen, 1, '.');
}
if (sign == -1)
{
s.insert(0, 1, '-');
}
return s;
}
//2:clear zero
void lnum::ClearZero(vecint &v) const
{
size_t i = 0, s = v.size();
if (v == 0)
{
for (; i < s; i++)
{
if (v != 0)
{
break;
}
}
v.erase(v.begin(), v.begin() + i);
}
}
//3:满十进1
void lnum::Carry(vecint &v) const
{
v.insert(v.begin(), 1, 0);
size_t s = v.size();
for (size_t i = s-1; i > 0; i--)
{
int n = v;
v = n % 10;
v += n / 10;
}
ClearZero(v);
}
//4:借1退10
const lnum::vecint lnum::Borrow(const vecint &v) const
{
size_t s = v.size();
vecint r = v;
for (size_t i = 1; i < s; i++)
{
r -= 1;
r += 10;
}
return r;
}
//5vec对齐
const lnum::vecint lnum::vecAlign(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const
{
vecint r;
if (mdpl != ndpl)
{
r = mdpl < ndpl ? m : n;
size_t s = (mdpl > ndpl ? mdpl : ndpl) - (mdpl < ndpl ? mdpl : ndpl);
r.insert(r.end(), s, 0);
}
return r;
}
//6:小数对齐
const lnum::align lnum::Align(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const
{
align agn;
if (mdpl == ndpl)
{
agn.shrter = m.size() <= n.size() ? m : n;
agn.longer = n.size() >= m.size() ? n : m;
}
else
{
const vecint &sdpl = vecAlign(m, mdpl, n, ndpl);
const vecint &ldpl = mdpl > ndpl ? m : n;
agn.shrter = sdpl.size() <= ldpl.size() ? sdpl : ldpl;
agn.longer = ldpl.size() >= sdpl.size() ? ldpl : sdpl;
}
return agn;
}
//7:大于比较
const bool lnum::GreaterThan(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const
{
bool f = true;
if (msign < nsign)
{
f = false;
}
else if (msign == nsign)
{
if (mdpl == ndpl)
{
if (m.size() != n.size())
{
f = m.size() > n.size();
if (msign = -1)
{
f = m.size() < n.size();
}
}
else
{
f = m > n;
if (msign == -1)
{
f = m < n;
}
}
}
else
{
if (mdpl < ndpl)
{
const vecint &tm = vecAlign(m, mdpl, n, ndpl);
if (tm.size() != n.size())
{
f = tm.size() > n.size();
if (msign = -1)
{
f = tm.size() < n.size();
}
}
else
{
f = tm > n;
if (msign == -1)
{
f = tm < n;
}
}
}
else if (ndpl < mdpl)
{
const vecint &tn = vecAlign(m, mdpl, n, ndpl);
if (m.size() != tn.size())
{
f = m.size() > tn.size();
if (msign == -1)
{
f = m.size() < tn.size();
}
}
else
{
f = m > tn;
if (msign == -1)
{
f = m < tn;
}
}
}
}
}
return f;
}
//8:小于比较
const bool lnum::LessThan(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const
{
bool f = true;
if (GreaterThan(m, mdpl, msign, n, ndpl, nsign) || Equal(m, mdpl, msign, n, ndpl, nsign))
{
f = false;
}
return f;
}
//9:相等比较
const bool lnum::Equal(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const
{
bool f = true;
if ((msign != nsign) || (mdpl != ndpl) || m.size() != n.size() || m != n)
{
f = false;
}
return f;
}
//?
void lnum::showinfo() const
{
std::cout << "正/负: " << num.sign << "\n"
<< "原数字: " << num.str << "\n"
<< "数字组成: ";
for (int i : num.vec)
{
std::cout << i << " ";
}
std::cout << "\n有效位数: " << num.nlen << "\n"
<< "小数位数: " << num.dplen;
}
//?:overload >
const bool lnum::operator>(lnum &l) const
{
return GreaterThan(num.vec, num.dplen, num.sign, l.num.vec, l.num.dplen, l.num.sign);
}
//?:overload <
const bool lnum::operator<(lnum &l) const
{
return LessThan(num.vec, num.dplen, num.sign, l.num.vec, l.num.dplen, l.num.sign);
}
//?:overload ==
const bool lnum::operator==(lnum &l) const
{
return Equal(num.vec, num.dplen, num.sign, l.num.vec, l.num.dplen, l.num.sign);
}
//over load >>
istream &operator>>(istream &is, lnum &n)
{
string str;
is >> str;
n = str;
return is;
}
//overload <<
ostream &operator<<(ostream &os, const lnum &l)
{
os << l.num.str;
return os;
}
int main(int argc, char const *argv[])
{
lnum a, b;
int i = 0;
while (i != 5)
{
std::cout << std::endl;
std::cout << "input data" << std::endl;
std::cin >> a >> b;
std::cout << "a = " << a << "\n"
<< "b = " << b << "\n"
<< "a > b: " << (a > 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-ex4w2dlu.upv --stdout=Microsoft-MIEngine-Out-pyje5tvg.zas --stderr=Microsoft-MIEngine-Error-ot1h5uhy.d0z --pid=Microsoft-MIEngine-Pid-01azs4my.aje --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
input data
-13.12 -12
a = -13.12
b = -12
a > b: 0
a == b: 0
a < b: 1
input data
123 123
a = 123
b = 123
a > b: 0
a == b: 1
a < b: 0
input data
-987.256 -987.256
a = -987.256
b = -987.256
a > b: 0
a == b: 1
a < b: 0
input data
12345678999999 888888888888
a = 12345678999999
b = 888888888888
a > b: 1
a == b: 0
a < b: 0
input data
-123456789 -987654321
a = -123456789
b = -987654321
a > b: 1
a == b: 0
a < b: 0
input data
987654321 654321
a = 987654321
b = 654321
a > b: 1
a == b: 0
a < b: 0
input data
22222222222 9999999999
a = 22222222222
b = 9999999999
a > b: 1
a == b: 0
a < b: 0
input data
1.23456789 1.02345678
a = 1.23456789
b = 1.02345678
a > b: 1
a == b: 0
a < b: 0
input data
-1.123654 12
a = -1.123654
b = 12
a > b: 0
a == b: 0
a < b: 1
input data
-7 9
a = -7
b = 9
a > b: 0
a == b: 0
a < b: 1
E:\Users\admin\Documents\VScode\Code> 本帖最后由 bin554385863 于 2019-12-28 01:13 编辑
2019年12月28日01:02:21
改进版
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
void Carry(vector<int> &v) const;
//借1
vector<int> Borrow(const vector<int> &v) const;
//对齐
align AlignVec(const vector<int> &m, const vector<int> &n) 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;
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 == '-')
{
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 == '-')
{
n.sign = -1;
}
if (int(str.find('.')) != -1)
{
n.dpsize = str.size() - str.find('.') - 1;
if (nstr == '0')
{
for (; i < s; i++)
{
if (nstr != '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')
{
for (i = 0; i < s; i++)
{
if (nstr != '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;
if (str.size() != 1 && num.prec != 0)
{
if (num.dpsize == num.prec)
{
str.insert(str.size() - num.prec, 1, '.');
}
else if (num.dpsize < num.prec && num.dpsize != 0)
{
str.erase(str.size() - (num.prec - num.dpsize));
str.insert(str.size() - num.dpsize, 1, '.');
}
else if (num.dpsize == 0)
{
str.erase(str.size() - num.prec);
}
}
if (num.sign == -1)
{
str.insert(0, 1, '-');
}
return str;
}
//进1
void lnum::Carry(vector<int> &v) const
{
size_t s = v.size();
v.insert(v.begin(), 1, 0);
for (size_t i = s - 1; i > 0; i++)
{
int t = v;
v = t % 10;
v += t / 10;
}
if (v == 0)
{
v.erase(v.begin(), v.begin() + 1);
}
}
//借1
vector<int> lnum::Borrow(const vector<int> &v) const
{
size_t s = v.size();
vector<int> t = v;
for (size_t i = 1; i < s; i++)
{
t -= 1;
t += 10;
}
return t;
}
//对齐
lnum::align lnum::AlignVec(const vector<int> &m, const vector<int> &n) const
{
align agn;
agn = {m, n};
size_t fsize = agn.first.size(), ssize = agn.second.size();
if (fsize < ssize)
{
agn.first.insert(agn.first.begin(), ssize - fsize, 0);
}
else if (fsize > ssize)
{
agn.second.insert(agn.second.begin(), fsize - ssize, 0);
}
return agn;
}
//------------------------------------------------------------------------
//设置精度
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);
if (num.sign < l.num.sign)
{
f = false;
}
if (num.sign == l.num.sign)
{
f = agn.first > agn.second;
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);
if (num.sign > l.num.sign)
{
f = false;
}
if (num.sign == l.num.sign)
{
f = agn.first < agn.second;
if (num.sign == -1)
{
f = !f;
}
}
return f;
}
//==
const bool lnum::operator==(const lnum &l) const
{
align agn = AlignVec(num.vec, l.num.vec);
return agn.first == agn.second && num.sign == l.num.sign;
}
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;
size_t i = 0;
while (i != 5)
{
std::cout << "input data!" << std::endl;
std::cin >> a >> b;
std::cout << "a =" << a << "\n"
<< "b =" << b << "\n"
<< "a >b = " << (a > b) << "\n"
<< "a <b = " << (a < b) << "\n"
<< "a == b = " << (a == b) << "\n"
<< 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-cmg22gzr.2if --stdout=Microsoft-MIEngine-Out-450tsfj2.vr2 --stderr=Microsoft-MIEngine-Error-uvivdufv.b1n --pid=Microsoft-MIEngine-Pid-xusp5p5w.s1o --dbgExe=D:\MinGW\bin\gdb.exe --interpreter=mi
input data!
0 0.0000
a =0
b =0
a >b = 0
a <b = 0
a == b = 1
input data!
123.3698 12.3
a =123.369
b =12.3
a >b = 1
a <b = 0
a == b = 0
input data!
123456 987.0123
a =123456
b =987.012
a >b = 1
a <b = 0
a == b = 0
input data!
12 0.336
a =12
b =0.336
a >b = 1
a <b = 0
a == b = 0
input data!
0.236 -2
a =0.236
b =-2
a >b = 1
a <b = 0
a == b = 0
E:\Users\admin\Documents\VScode\Code>
页:
[1]