鱼C论坛

 找回密码
 立即注册
查看: 2268|回复: 10

[技术交流] 瞎tam写

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

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

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

x
本帖最后由 bin554385863 于 2019-12-20 00:06 编辑
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. class num
  5. {
  6.     typedef std::ostream ostream;
  7.     typedef std::string string;
  8.     typedef std::vector<int> vector;

  9. private:
  10.     string STR;
  11.     vector VEC;
  12.     size_t LEN, pLEN;
  13.     //小数位数
  14.     size_t const psize(const char *cstr) const
  15.     {
  16.         string &&str = cstr;
  17.         size_t r = 0;
  18.         if (int(str.find('.')) != -1)
  19.         {
  20.             r = str.size() - str.find('.') - 1;
  21.         }
  22.         return r;
  23.     }
  24.     //翻转正负
  25.     void neg(vector &v) const
  26.     {
  27.         for (int &i : v)
  28.         {
  29.             i = -i;
  30.         }
  31.     }
  32.     //str to vec
  33.     vector const StrToVec(const char *cstr) const
  34.     {
  35.         vector v;
  36.         string &&str = cstr;
  37.         size_t s = psize(cstr);
  38.         if (s != 0)
  39.         {
  40.             str.erase(str.size() - s -1, 1);
  41.         }
  42.         char c = str[0];
  43.         if (c != '-')
  44.         {
  45.             for(char i:str)
  46.             {
  47.                 v.push_back( i - 48);
  48.             }
  49.         }
  50.         if (c == '-')
  51.         {
  52.             str.erase(str.find('-'), 1);
  53.             for(char i:str)
  54.             {
  55.                 v.push_back( 48 - i);
  56.             }
  57.         }
  58.         return v;
  59.     }
  60.     public:
  61.     num(char *cstr = "0"):STR(cstr), VEC(StrToVec(cstr)), pLEN(psize(cstr)), LEN(VEC.size()){}
  62.     void showinfo()
  63.     {
  64.         std::cout<<"权值: ";
  65.         for(int i:VEC)
  66.         {
  67.             std::cout<<i<<" ";
  68.         }
  69.         std::cout<<std::endl;
  70.         std::cout<<"有效位数: "<<LEN<<"\n有效小数: "<<pLEN<<std::endl;
  71.     }
  72. };
  73. int main(int argc, char const *argv[])
  74. {
  75.     num n = "-3.141592653";
  76.     n.showinfo();
  77.     return 0;
  78. }
复制代码

===================================================================
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>

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-12-21 02:23:27 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. class num
  5. {
  6.     typedef std::ostream ostream;
  7.     typedef std::string string;
  8.     typedef std::vector<int> vector;

  9. private:
  10.     string STR;
  11.     vector VEC;
  12.     size_t LEN, pLEN;
  13.     //小数位数
  14.     size_t const psize(const char *cstr) const
  15.     {
  16.         string &&str = cstr;
  17.         size_t r = 0;
  18.         if (int(str.find('.')) != -1)
  19.         {
  20.             r = str.size() - str.find('.') - 1;
  21.         }
  22.         return r;
  23.     }
  24.     //翻转正负
  25.     void neg(vector &v) const
  26.     {
  27.         for (int &i : v)
  28.         {
  29.             i = -i;
  30.         }
  31.     }
  32.     //str to vec
  33.     vector const StrToVec(const char *cstr) const
  34.     {
  35.         vector v;
  36.         string &&str = cstr;
  37.         size_t s = psize(cstr);
  38.         if (s != 0)
  39.         {
  40.             str.erase(str.size() - s - 1, 1);
  41.         }
  42.         char c = str[0];
  43.         if (c != '-')
  44.         {
  45.             for (char i : str)
  46.             {
  47.                 v.push_back(i - 48);
  48.             }
  49.         }
  50.         if (c == '-')
  51.         {
  52.             str.erase(str.find('-'), 1);
  53.             for (char i : str)
  54.             {
  55.                 v.push_back(48 - i);
  56.             }
  57.         }
  58.         return v;
  59.     }
  60.     //消零
  61.     void ClearZero(vector &v) const
  62.     {
  63.         size_t inx = 0, sz = v.size();
  64.         if (v[0] == 0)
  65.         {
  66.             for (size_t i = 0; i < sz; i++)
  67.             {
  68.                 if (v[i] != 0)
  69.                 {
  70.                     inx = i;
  71.                     break;
  72.                 }
  73.             }
  74.             v.erase(v.begin(), v.begin() + inx);
  75.         }
  76.         if (v[sz - 1] == 0)
  77.         {
  78.             for (size_t i = sz - 1; i > -1; i--)
  79.             {
  80.                 if (v[i] != 0)
  81.                 {
  82.                     inx = i;
  83.                 }
  84.             }
  85.             v.erase(v.begin() + inx + 2);
  86.         }
  87.     }
  88.     //进位
  89.     void carry(vector &v) const
  90.     {
  91.         v.insert(v.begin(), 1, 0);
  92.         size_t s = v.size();
  93.         for (size_t i = s-1; i > 0; i--)
  94.         {
  95.             v[i] %= 10;
  96.             v[i-1] += v[i] / 10;
  97.         }
  98.         ClearZero(v);
  99.     }

  100. public:
  101.     num(char *cstr = "0") : STR(cstr), VEC(StrToVec(cstr)), pLEN(psize(cstr)), LEN(VEC.size()) {}
  102.     num(const num &n) : STR(n.STR), VEC(n.VEC), pLEN(n.pLEN), LEN(n.LEN) {}
  103.     void showinfo()
  104.     {
  105.         std::cout << "权值: ";
  106.         for (int i : VEC)
  107.         {
  108.             std::cout << i << " ";
  109.         }
  110.         std::cout << std::endl;
  111.         std::cout << "有效位数: " << LEN << "\n有效小数: " << pLEN << std::endl;
  112.     }
  113.     num const operator+(const num &n) const
  114.     {
  115.         num r;
  116.         if (pLEN == n.pLEN)
  117.         {
  118.             r.VEC = LEN >= n.LEN ? VEC : n.VEC;
  119.             size_t sz = LEN < n.LEN ? LEN : n.LEN;
  120.             for (size_t i = 0; i < sz; i++)
  121.             {
  122.                 *(r.VEC.rbegin() + i) += *(n.VEC.rbegin() + i);
  123.             }
  124.         }
  125.         else
  126.         {
  127.             vector minplen = (pLEN < n.pLEN ? VEC : n.VEC), maxplen = (pLEN > n.pLEN ? VEC : n.VEC);
  128.             size_t s = (pLEN > n.pLEN ? pLEN : n.pLEN) - (pLEN < n.pLEN ? pLEN : n.pLEN);
  129.             minplen.insert(minplen.end(), s, 0);
  130.             size_t sz = minplen.size() <= maxplen.size() ? minplen.size() : maxplen.size();
  131.             r.VEC = minplen.size() >= maxplen.size() ? minplen : maxplen;
  132.             vector& a = minplen.size() < maxplen.size() ? minplen : maxplen;
  133.             for (size_t i = 0; i < sz; i++)
  134.             {
  135.                 *(r.VEC.rbegin() + i) += *(a.rbegin() + i);
  136.             }
  137.             
  138.         }
  139.         carry(r.VEC);
  140.         r.LEN = r.VEC.size();
  141.         r.pLEN = pLEN > n.pLEN ? pLEN : n.pLEN;
  142.         return r;
  143.     }
  144.     ~num() {}
  145. };
  146. int main(int argc, char const *argv[])
  147. {
  148.     num a = "3.14159", b = "123", n;
  149.     n = a + b;
  150.     n.showinfo();
  151.     return 0;
  152. }
复制代码

==============================================================
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>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-21 18:22:19 | 显示全部楼层
本帖最后由 bin554385863 于 2019-12-21 20:43 编辑
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. class num
  5. {
  6.     typedef std::ostream ostream;
  7.     typedef std::string string;
  8.     typedef std::vector<int> vector;

  9. private:
  10.     string STR;
  11.     vector VEC;
  12.     size_t LEN, pLEN;
  13.     //小数位数
  14.     size_t const psize(const char *cstr) const;
  15.     //翻转正负
  16.     void neg(vector &v) const;
  17.     //str to vec
  18.     vector const StrToVec(const char *cstr) const;
  19.     //vec to str
  20.     const string VecToStr() const;
  21.     //消零
  22.     void ClearZero(vector &v) const;
  23.     //进位
  24.     void carry(vector &v) const;

  25. public:
  26.     num(char *cstr = "0") : STR(cstr), VEC(StrToVec(cstr)), pLEN(psize(cstr)), LEN(VEC.size()) {}
  27.     num(const num &n) : STR(n.STR), VEC(n.VEC), pLEN(n.pLEN), LEN(n.LEN) {}
  28.     //有效位数
  29.     const size_t size() const { return LEN; }
  30.     //有效小数
  31.     const size_t psize() const { return pLEN; }
  32.     //信息
  33.     void showinfo() const;
  34.     //重载+
  35.     num const operator+(const num &n) const;
  36.     //重载<<
  37.     friend ostream &operator<<(ostream &os, const num &n)
  38.     {
  39.         os << n.STR;
  40.         return os;
  41.     }
  42.     ~num() {}
  43. };

  44. //小数位数
  45. size_t const num::psize(const char *cstr) const
  46. {
  47.     string &&str = cstr;
  48.     size_t r = 0;
  49.     if (int(str.find('.')) != -1)
  50.     {
  51.         r = str.size() - str.find('.') - 1;
  52.     }
  53.     return r;
  54. }
  55. //翻转正负
  56. void num::neg(vector &v) const
  57. {
  58.     for (int &i : v)
  59.     {
  60.         i = -i;
  61.     }
  62. }
  63. //str to vec
  64. num::vector const num::StrToVec(const char *cstr) const
  65. {
  66.     vector v;
  67.     string &&str = cstr;
  68.     size_t s = psize(cstr);
  69.     if (s != 0)
  70.     {
  71.         str.erase(str.size() - s - 1, 1);
  72.     }
  73.     char c = str[0];
  74.     if (c != '-')
  75.     {
  76.         for (char i : str)
  77.         {
  78.             v.push_back(i - 48);
  79.         }
  80.     }
  81.     if (c == '-')
  82.     {
  83.         str.erase(str.find('-'), 1);
  84.         for (char i : str)
  85.         {
  86.             v.push_back(48 - i);
  87.         }
  88.     }
  89.     return v;
  90. }
  91. //vce to str
  92. const num::string num::VecToStr() const
  93. {
  94.     string str;
  95.     vector t = VEC;
  96.     int f = t[0];
  97.     if (f < 0)
  98.     {
  99.         neg(t);
  100.     }
  101.     for (int i : t)
  102.     {
  103.         str.push_back(i + 48);
  104.     }
  105.     if (pLEN != 0)
  106.     {
  107.         str.insert(str.size() - pLEN, 1, '.');
  108.     }
  109.     if (f < 0)
  110.     {
  111.         str.insert(0, 1, '-');
  112.     }
  113.     return str;
  114. }
  115. //消零
  116. void num::ClearZero(vector &v) const
  117. {
  118.     size_t inx = 0, sz = v.size();
  119.     if (v[0] == 0)
  120.     {
  121.         for (size_t i = 0; i < sz; i++)
  122.         {
  123.             if (v[i] != 0)
  124.             {
  125.                 inx = i;
  126.                 break;
  127.             }
  128.         }
  129.         v.erase(v.begin(), v.begin() + inx);
  130.     }
  131. }
  132. //进位
  133. void num::carry(vector &v) const
  134. {
  135.     v.insert(v.begin(), 1, 0);
  136.     size_t s = v.size();
  137.     for (size_t i = s - 1; i > 0; i--)
  138.     {
  139.         int t = v[i];
  140.         v[i] = t % 10;
  141.         v[i - 1] += t / 10;
  142.     }
  143.     ClearZero(v);
  144. }
  145. //信息
  146. void num::showinfo() const
  147. {
  148.     std::cout << "权值: ";
  149.     for (int i : VEC)
  150.     {
  151.         std::cout << i << " ";
  152.     }
  153.     std::cout << std::endl;
  154.     std::cout << "有效位数: " << LEN << "\n有效小数: " << pLEN << std::endl;
  155. }
  156. //重载+
  157. num const num::operator+(const num &n) const
  158. {
  159.     num r;
  160.     if (pLEN == n.pLEN)
  161.     {
  162.         r.VEC = LEN >= n.LEN ? VEC : n.VEC;
  163.         size_t sz = LEN < n.LEN ? LEN : n.LEN;
  164.         for (size_t i = 0; i < sz; i++)
  165.         {
  166.             *(r.VEC.rbegin() + i) += *(n.VEC.rbegin() + i);
  167.         }
  168.     }
  169.     else
  170.     {
  171.         vector minplen = (pLEN < n.pLEN ? VEC : n.VEC), maxplen = (pLEN > n.pLEN ? VEC : n.VEC);
  172.         size_t s = (pLEN > n.pLEN ? pLEN : n.pLEN) - (pLEN < n.pLEN ? pLEN : n.pLEN);
  173.         minplen.insert(minplen.end(), s, 0);
  174.         size_t sz = minplen.size() <= maxplen.size() ? minplen.size() : maxplen.size();
  175.         r.VEC = minplen.size() >= maxplen.size() ? minplen : maxplen;
  176.         vector &a = minplen.size() < maxplen.size() ? minplen : maxplen;
  177.         r.VEC.insert(r.VEC.begin(), 1, 0);
  178.         for (size_t i = 0; i < sz; i++)
  179.         {
  180.             *(r.VEC.rbegin() + i) += *(a.rbegin() + i);
  181.         }
  182.     }
  183.     carry(r.VEC);
  184.     r.LEN = r.VEC.size();
  185.     r.pLEN = pLEN > n.pLEN ? pLEN : n.pLEN;
  186.     r.STR = r.VecToStr();
  187.     return r;
  188. }
  189. int main(int argc, char const *argv[])
  190. {
  191.     num a = "99999999999999999.99999", b = "55555555555555555.55555", n;
  192.     n = a + b;
  193.     std::cout << a<<" + "<<b<<" = "<<n << std::endl;
  194.     n.showinfo();
  195.     return 0;
  196. }
复制代码

=======================================================================
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>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-22 14:33:26 | 显示全部楼层
本帖最后由 bin554385863 于 2019-12-22 14:36 编辑

2019年12月22日14:30:55
推倒重写
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. class num
  5. {
  6.     typedef std::ostream ostream;
  7.     typedef std::string string;
  8.     typedef std::vector<int> vector;

  9. private:
  10.     string STR;
  11.     vector VEC;
  12.     size_t LEN, pLEN;
  13.     //1:小数位数
  14.     size_t const psize(const char *cstr) const;
  15.     //2:翻转正负
  16.     vector const neg(const vector &v) const;
  17.     //3:str to vec
  18.     vector const StrToVec(const char *cstr) const;
  19.     //4:vec to str
  20.     const string VecToStr(const vector &t, size_t plen) const;
  21.     //5:消零
  22.     void ClearZero(vector &v) const;
  23.     //6:进位
  24.     void Carry(vector &v) const;
  25.     //7:退位
  26.     vector Abdicate(const vector &n) const;
  27.     //8:小数位对齐
  28.     vector pLenAlign(const vector &n, const vector &m, size_t nplen, size_t mplen) const;
  29.     //9:重载 vector +
  30.     friend vector operator+(const vector &m, const vector &n);

  31. public:
  32.     //10:字符串构造
  33.     num(const char *cstr);
  34.     //11:vec构造
  35.     num(const vector &v, size_t vplen);
  36.     //12:copy构造
  37.     num(const num &n);
  38.     //13:长度
  39.     const size_t size() const { return LEN; }
  40.     //14:小数位长度
  41.     const size_t psize() const { return pLEN; }
  42.     //15:信息展示
  43.     void showinfo() const;
  44.     //16:重载+
  45.     num const operator+(const num &n) const;
  46.     //17:重载-
  47.     num const operator-(const num &n) const;
  48.     //18:重载<<
  49.     friend ostream &operator<<(ostream &os, const num &n);
  50.     ~num() {}
  51. };
  52. //1:小数位数
  53. size_t const num::psize(const char *cstr) const
  54. {
  55.     string &&str = cstr;
  56.     size_t r = 0;
  57.     if (int(str.find('.')) != -1)
  58.     {
  59.         r = str.size() - str.find('.') - 1;
  60.     }
  61.     return r;
  62. }
  63. //2:翻转vector正负
  64. num::vector const num::neg(const vector &v) const
  65. {
  66.     vector r = v;
  67.     for (int &i : r)
  68.     {
  69.         i = -i;
  70.     }
  71.     return r;
  72. }
  73. //3:STR to VEC
  74. num::vector const num::StrToVec(const char *cstr) const
  75. {
  76.     vector v;
  77.     string &&str = cstr;
  78.     size_t s = psize(cstr);
  79.     if (s != 0)
  80.     {
  81.         str.erase(str.size() - s - 1, 1);
  82.     }
  83.     char c = str[0];
  84.     if (c != '-')
  85.     {
  86.         for (char i : str)
  87.         {
  88.             v.push_back(i - 48);
  89.         }
  90.     }
  91.     if (c == '-')
  92.     {
  93.         str.erase(str.find('-'), 1);
  94.         for (char i : str)
  95.         {
  96.             v.push_back(48 - i);
  97.         }
  98.     }
  99.     return v;
  100. }
  101. //4:VEC to STR
  102. const num::string num::VecToStr(const vector &n, size_t plen) const
  103. {
  104.     string str;
  105.     vector t = n;
  106.     int f = t[0];
  107.     if (f < 0)
  108.     {
  109.         t = neg(t);
  110.     }
  111.     for (int i : t)
  112.     {
  113.         str.push_back(i + 48);
  114.     }
  115.     if (plen != 0)
  116.     {
  117.         str.insert(str.size() - plen, 1, '.');
  118.     }
  119.     if (f < 0)
  120.     {
  121.         str.insert(0, 1, '-');
  122.     }
  123.     return str;
  124. }
  125. //5:消零
  126. void num::ClearZero(vector &v) const
  127. {
  128.     size_t inx = 0, sz = v.size();
  129.     if (v[0] == 0)
  130.     {
  131.         for (size_t i = 0; i < sz; i++)
  132.         {
  133.             if (v[i] != 0)
  134.             {
  135.                 inx = i;
  136.                 break;
  137.             }
  138.         }
  139.         v.erase(v.begin(), v.begin() + inx);
  140.     }
  141. }
  142. //6:进位
  143. void num::Carry(vector &v) const
  144. {
  145.     v.insert(v.begin(), 1, 0);
  146.     size_t s = v.size();
  147.     for (size_t i = s - 1; i > 0; i--)
  148.     {
  149.         int t = v[i];
  150.         v[i] = t % 10;
  151.         v[i - 1] += t / 10;
  152.     }
  153.     ClearZero(v);
  154. }
  155. //7:退位
  156. num::vector num::Abdicate(const vector &n) const
  157. {
  158.     vector r = n;
  159.     size_t s = r.size();
  160.     for (size_t i = 1; i < s; i++)
  161.     {
  162.         r[i - 1] -= 1;
  163.         r[i] += 10;
  164.     }
  165.     return r;
  166. }
  167. //8:小数位对齐,返回初始小数位最短的数组
  168. num::vector num::pLenAlign(const vector &n, const vector &m, const size_t nplen, const size_t mplen) const
  169. {
  170.     vector r;
  171.     if (nplen == mplen)
  172.     {
  173.         r = n.size() < m.size() ? n : m;
  174.     }
  175.     else
  176.     {
  177.         r = nplen < mplen ? n : m;
  178.         size_t s = (nplen > mplen ? nplen : mplen) - (nplen < mplen ? nplen : mplen);
  179.         r.insert(r.end(), s, 0);
  180.     }
  181.     return r;
  182. }
  183. //9:重载 vector +
  184. num::vector operator+(const num::vector &m, const num::vector &n)
  185. {
  186.     num::vector r = m.size() > n.size() ? m : n;
  187.     const num::vector &t = m.size() < n.size() ? m : n;
  188.     size_t s = m.size() < n.size() ? m.size() : n.size();
  189.     for (size_t i = 0; i < s; i++)
  190.     {
  191.         *(r.rbegin() + i) += *(t.rbegin() + i);
  192.     }
  193.     return r;
  194. }
  195. //------------------------------------------------------
  196. int main(int argc, char const *argv[])
  197. {
  198.     return 0;
  199. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-25 02:47:12 | 显示全部楼层
本帖最后由 bin554385863 于 2019-12-25 02:50 编辑

2019年12月25日02:37:27
再次推倒重写
lnum.h
  1. #ifndef LNUM_H
  2. #define LNUM_H
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. using std::ostream;
  7. using std::string;
  8. using std::vector;
  9. class lnum
  10. {
  11.     typedef vector<int>  vecint;
  12.     struct Num
  13.     {
  14.         int sign;
  15.         vecint vec;
  16.         size_t nlen;
  17.         size_t dplen;
  18.         string str;
  19.     };
  20.     struct align
  21.     {
  22.         vecint shrter;
  23.         vecint longer;
  24.     };
  25.    
  26. private:
  27.     Num num;
  28.     //1:符号
  29.     const int Sign(const char *cstr) const;
  30.     //2:vecor
  31.     const vecint VecInt(const char *cstr) const;
  32.     //3:dplen
  33.     const size_t Dplen(const char *cstr) const;
  34.     //4:vec to str
  35.     const string VECtoSTR(const vecint &v, const int sign, const size_t dplen) const;
  36.     //5:clear zero
  37.     void ClearZero(vecint &v) const;
  38.     //6:满十进1
  39.     void Carry(vecint &v) const;
  40.     //7:借1退10
  41.     const vecint Borrow(const vecint &v) const;
  42.     //8:小数对齐
  43.     const align Align(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const;

  44. public:
  45.     lnum(const char *cstr = "0") : num({Sign(cstr), VecInt(cstr), VecInt(cstr).size(), Dplen(cstr), cstr}){}
  46.     lnum(const lnum &l) : num(l.num){}
  47.     //?
  48.     void showinfo() const;
  49.     ~lnum(){}
  50. };
  51. #endif
复制代码

lnum.cpp
  1. #include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
  2. //1:符号
  3. const int lnum::Sign(const char *cstr) const
  4. {
  5.     int sign = 1;
  6.     if (cstr[0] == '-')
  7.     {
  8.         sign = -1;
  9.     }
  10.     return sign;
  11. }
  12. //2:vecor
  13. const lnum::vecint lnum::VecInt(const char *cstr) const
  14. {
  15.     string s = cstr;
  16.     vecint v;
  17.     if (s[0] == '-')
  18.     {
  19.         s.erase(0, 1);
  20.     }
  21.     if ((s.find('.')) != -1)
  22.     {
  23.         s.erase(s.find('.'), 1);
  24.     }
  25.     for(char c: s)
  26.     {
  27.         v.push_back(c - 48);
  28.     }
  29.     return v;
  30. }
  31. //3:dplen
  32. const size_t lnum::Dplen(const char *cstr) const
  33. {
  34.     string s = cstr;
  35.     size_t dplen = 0;
  36.     if (int(s.find('.')) != -1)
  37.     {
  38.         dplen = s.size() - s.find('.') - 1;
  39.     }
  40.     return dplen;
  41. }
  42. //4:vec to str
  43. const string lnum::VECtoSTR(const vecint &v, const int sign, const size_t dplen) const
  44. {
  45.     string s;
  46.     for(int i: v)
  47.     {
  48.         s.push_back(48 + i);
  49.     }
  50.     if (dplen != 0)
  51.     {
  52.         s.insert(s.size() - dplen, 1, '.');
  53.     }
  54.     if (sign == -1)
  55.     {
  56.         s.insert(0, 1, '-');
  57.     }
  58.     return s;
  59. }
  60. //5:clear zero
  61. void lnum::ClearZero(vecint &v) const
  62. {
  63.     size_t i = 0, s = v.size();
  64.     if (v[0] == 0)
  65.     {
  66.         for (; i < s; i++)
  67.         {
  68.             if (v[i] != 0)
  69.             {
  70.                 break;
  71.             }
  72.         }
  73.         v.erase(v.begin(), v.begin() + i);
  74.     }
  75. }
  76. //6:满十进1
  77. void lnum::Carry(vecint &v) const
  78. {
  79.     v.insert(v.begin(), 1, 0);
  80.     size_t s = v.size();
  81.     for (size_t i = s-1; i > 0; i--)
  82.     {
  83.         int n = v[i];
  84.         v[i] = n % 10;
  85.         v[i - 1] += n / 10;
  86.     }
  87.     ClearZero(v);
  88. }
  89. //7:借1退10
  90. const lnum::vecint lnum::Borrow(const vecint &v) const
  91. {
  92.     size_t s = v.size();
  93.     vecint r = v;
  94.     for (size_t i = 1; i < s; i++)
  95.     {
  96.         r[i - 1] -= 1;
  97.         r[i] += 10;
  98.     }
  99.     return r;
  100. }
  101. //8:小数对齐
  102. const lnum::align lnum::Align(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const
  103. {
  104.     align agn;
  105.     if (mdpl == ndpl)
  106.     {
  107.         agn.shrter = m.size() <= n.size() ? m : n;
  108.         agn.longer = n.size() >= m.size() ? n : m;
  109.     }
  110.     else
  111.     {
  112.         vecint sdpl = mdpl < ndpl ? m : n;
  113.         const vecint &ldpl = mdpl > ndpl ? m : n;
  114.         size_t s = (mdpl > ndpl ? mdpl : ndpl) - (mdpl < ndpl ? mdpl : ndpl);
  115.         sdpl.insert(sdpl.end(), s, 0);
  116.         agn.shrter = sdpl.size() <= ldpl.size() ? sdpl : ldpl;
  117.         agn.longer = ldpl.size() >= sdpl.size() ? ldpl : sdpl;
  118.     }
  119.     return agn;
  120. }
  121. //?
  122. void lnum::showinfo() const
  123. {
  124.     std::cout << "正/负: " << num.sign << "\n"
  125.               << "原数字: " << num.str << "\n"
  126.               << "数字组成: ";
  127.     for (int i : num.vec)
  128.     {
  129.         std::cout << i << " ";
  130.     }
  131.     std::cout << "\n有效位数: " << num.nlen << "\n"
  132.               << "小数位数: " << num.dplen;
  133. }
  134. int main(int argc, char const *argv[])
  135. {
  136.     lnum a = "123456789.123";
  137.     a.showinfo();
  138.     return 0;
  139. }
复制代码

============================================================
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>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-25 10:49:51 | 显示全部楼层
所以老哥你在写大数运算。。?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-25 13:15:41 | 显示全部楼层
Croper 发表于 2019-12-25 10:49
所以老哥你在写大数运算。。?

是啊,顺利写完差不多就把我之前学的差不多理顺了,接下来才能静下心学继承
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-12-25 13:32:32 | 显示全部楼层
本帖最后由 Croper 于 2019-12-25 13:33 编辑
bin554385863 发表于 2019-12-25 13:15
是啊,顺利写完差不多就把我之前学的差不多理顺了,接下来才能静下心学继承


我也写过一个,感觉还是底层2进制比较好,位移运算比乘除法运算快多了,而且除法只需要考虑减法就行。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-25 14:58:59 | 显示全部楼层
Croper 发表于 2019-12-25 13:32
我也写过一个,感觉还是底层2进制比较好,位移运算比乘除法运算快多了,而且除法只需要考虑减法就行。

目前只考虑加减,乘除还在思索,其他的计算等以后学了算法和数据结构再说,目前能力不足以支撑
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-25 15:54:11 | 显示全部楼层
本帖最后由 bin554385863 于 2019-12-25 17:48 编辑

lnum.h
2019年12月25日15:51:11
  1. #ifndef LNUM_H
  2. #define LNUM_H
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. using std::istream;
  7. using std::ostream;
  8. using std::string;
  9. using std::vector;
  10. class lnum
  11. {
  12.     typedef vector<int> vecint;
  13.     struct Num
  14.     {
  15.         int sign;
  16.         vecint vec;
  17.         size_t nlen;
  18.         size_t dplen;
  19.         string str;
  20.     };
  21.     struct align
  22.     {
  23.         vecint shrter;
  24.         vecint longer;
  25.     };

  26. private:
  27.     Num num;
  28.     //0:STR to lnum
  29.     const Num STRtoNUM(const string &s) const;
  30.     //1:vec to str
  31.     const string VECtoSTR(const vecint &v, const int sign, const size_t dplen) const;
  32.     //2:clear zero
  33.     void ClearZero(vecint &v) const;
  34.     //3:满十进1
  35.     void Carry(vecint &v) const;
  36.     //4:借1退10
  37.     const vecint Borrow(const vecint &v) const;
  38.     //5:vec小数位对齐
  39.     const vecint vecAlign(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const;
  40.     //6:小数对齐
  41.     const align Align(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const;
  42.     //7:大于比较
  43.     const bool GreaterThan(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const;
  44.     //8:小于比较
  45.     const bool LessThan(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const;
  46.     //9:相等比较
  47.     const bool Equal(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const;

  48. public:
  49.     lnum(const string &s):num(STRtoNUM(s)){}
  50.     lnum(const char *cstr = "0"):num(STRtoNUM(cstr)){}
  51.     lnum(const lnum &l) : num(l.num) {}
  52.     //?
  53.     void showinfo() const;
  54.     //?:overload >
  55.     const bool operator>(lnum &l) const;
  56.     //?:overload <
  57.     const bool operator<(lnum &l) const;
  58.     //?:overload ==
  59.     const bool operator==(lnum &l) const;
  60.     //over load >>
  61.     friend istream &operator>>(istream &is, lnum &n);
  62.     //overload <<
  63.     friend ostream &operator<<(ostream &os, const lnum &l);
  64.     ~lnum() {}
  65. };
  66. #endif
复制代码


lnum.cpp
  1. #include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
  2. //0:STR to lnum
  3. const lnum::Num lnum::STRtoNUM(const string &s) const
  4. {
  5.     Num n = {1, {0}, 1, 0, s};
  6.     string t = s;
  7.     if (t[0] == '-')
  8.     {
  9.         n.sign = -1;
  10.         t.erase(0, 1);
  11.     }
  12.     if (int(t.find('.')) != -1)
  13.     {
  14.         n.dplen = t.size() - t.find('.') - 1;
  15.         t.erase(t.find('.'), 1);
  16.     }
  17.     n.vec.clear();
  18.     for (char c : t)
  19.     {
  20.         n.vec.push_back(c - 48);
  21.     }
  22.     n.nlen = n.vec.size();
  23.     return n;
  24. }
  25. //1:vec to str
  26. const string lnum::VECtoSTR(const vecint &v, const int sign, const size_t dplen) const
  27. {
  28.     string s;
  29.     for(int i: v)
  30.     {
  31.         s.push_back(48 + i);
  32.     }
  33.     if (dplen != 0)
  34.     {
  35.         s.insert(s.size() - dplen, 1, '.');
  36.     }
  37.     if (sign == -1)
  38.     {
  39.         s.insert(0, 1, '-');
  40.     }
  41.     return s;
  42. }
  43. //2:clear zero
  44. void lnum::ClearZero(vecint &v) const
  45. {
  46.     size_t i = 0, s = v.size();
  47.     if (v[0] == 0)
  48.     {
  49.         for (; i < s; i++)
  50.         {
  51.             if (v[i] != 0)
  52.             {
  53.                 break;
  54.             }
  55.         }
  56.         v.erase(v.begin(), v.begin() + i);
  57.     }
  58. }
  59. //3:满十进1
  60. void lnum::Carry(vecint &v) const
  61. {
  62.     v.insert(v.begin(), 1, 0);
  63.     size_t s = v.size();
  64.     for (size_t i = s-1; i > 0; i--)
  65.     {
  66.         int n = v[i];
  67.         v[i] = n % 10;
  68.         v[i - 1] += n / 10;
  69.     }
  70.     ClearZero(v);
  71. }
  72. //4:借1退10
  73. const lnum::vecint lnum::Borrow(const vecint &v) const
  74. {
  75.     size_t s = v.size();
  76.     vecint r = v;
  77.     for (size_t i = 1; i < s; i++)
  78.     {
  79.         r[i - 1] -= 1;
  80.         r[i] += 10;
  81.     }
  82.     return r;
  83. }
  84. //5vec对齐
  85. const lnum::vecint lnum::vecAlign(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const
  86. {
  87.     vecint r;
  88.     if (mdpl != ndpl)
  89.     {
  90.         r = mdpl < ndpl ? m : n;
  91.         size_t s = (mdpl > ndpl ? mdpl : ndpl) - (mdpl < ndpl ? mdpl : ndpl);
  92.         r.insert(r.end(), s, 0);
  93.     }
  94.     return r;
  95. }
  96. //6:小数对齐
  97. const lnum::align lnum::Align(const vecint &m, const size_t mdpl, const vecint &n, size_t ndpl) const
  98. {
  99.     align agn;
  100.     if (mdpl == ndpl)
  101.     {
  102.         agn.shrter = m.size() <= n.size() ? m : n;
  103.         agn.longer = n.size() >= m.size() ? n : m;
  104.     }
  105.     else
  106.     {
  107.         const vecint &sdpl = vecAlign(m, mdpl, n, ndpl);
  108.         const vecint &ldpl = mdpl > ndpl ? m : n;
  109.         agn.shrter = sdpl.size() <= ldpl.size() ? sdpl : ldpl;
  110.         agn.longer = ldpl.size() >= sdpl.size() ? ldpl : sdpl;
  111.     }
  112.     return agn;
  113. }
  114. //7:大于比较
  115. const bool lnum::GreaterThan(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const
  116. {
  117.     bool f = true;
  118.     if (msign < nsign)
  119.     {
  120.         f = false;
  121.     }
  122.     else if (msign == nsign)
  123.     {
  124.         if (mdpl == ndpl)
  125.         {
  126.             if (m.size() != n.size())
  127.             {
  128.                 f = m.size() > n.size();
  129.                 if (msign = -1)
  130.                 {
  131.                     f = m.size() < n.size();
  132.                 }
  133.                
  134.             }
  135.             else
  136.             {
  137.                 f = m > n;
  138.                 if (msign == -1)
  139.                 {
  140.                     f = m < n;
  141.                 }   
  142.             }
  143.         }
  144.         else
  145.         {
  146.             if (mdpl < ndpl)
  147.             {
  148.                 const vecint &tm = vecAlign(m, mdpl, n, ndpl);
  149.                 if (tm.size() != n.size())
  150.                 {
  151.                     f = tm.size() > n.size();
  152.                     if (msign = -1)
  153.                     {
  154.                         f = tm.size() < n.size();
  155.                     }
  156.                 }
  157.                 else
  158.                 {
  159.                     f = tm > n;
  160.                     if (msign == -1)
  161.                     {
  162.                         f = tm < n;
  163.                     }
  164.                     
  165.                 }
  166.                
  167.             }
  168.             else if (ndpl < mdpl)
  169.             {
  170.                 const vecint &tn = vecAlign(m, mdpl, n, ndpl);
  171.                 if (m.size() != tn.size())
  172.                 {
  173.                     f = m.size() > tn.size();
  174.                     if (msign == -1)
  175.                     {
  176.                         f = m.size() < tn.size();
  177.                     }
  178.                     
  179.                 }
  180.                 else
  181.                 {
  182.                     f = m > tn;
  183.                     if (msign == -1)
  184.                     {
  185.                         f = m < tn;
  186.                     }
  187.                 }
  188.             }
  189.         }
  190.     }
  191.     return f;
  192. }
  193. //8:小于比较
  194. const bool lnum::LessThan(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const
  195. {
  196.     bool f = true;
  197.     if (GreaterThan(m, mdpl, msign, n, ndpl, nsign) || Equal(m, mdpl, msign, n, ndpl, nsign))
  198.     {
  199.         f = false;
  200.     }
  201.     return f;
  202. }
  203. //9:相等比较
  204. const bool lnum::Equal(const vecint &m, const size_t mdpl, int msign, const vecint &n, size_t ndpl, int nsign) const
  205. {
  206.     bool f = true;
  207.     if ((msign != nsign) || (mdpl != ndpl) || m.size() != n.size() || m != n)
  208.     {
  209.         f = false;
  210.     }
  211.     return f;
  212. }
  213. //?
  214. void lnum::showinfo() const
  215. {
  216.     std::cout << "正/负: " << num.sign << "\n"
  217.               << "原数字: " << num.str << "\n"
  218.               << "数字组成: ";
  219.     for (int i : num.vec)
  220.     {
  221.         std::cout << i << " ";
  222.     }
  223.     std::cout << "\n有效位数: " << num.nlen << "\n"
  224.               << "小数位数: " << num.dplen;
  225. }
  226. //?:overload >
  227. const bool lnum::operator>(lnum &l) const
  228. {
  229.     return GreaterThan(num.vec, num.dplen, num.sign, l.num.vec, l.num.dplen, l.num.sign);
  230. }
  231. //?:overload <
  232. const bool lnum::operator<(lnum &l) const
  233. {
  234.     return LessThan(num.vec, num.dplen, num.sign, l.num.vec, l.num.dplen, l.num.sign);
  235. }
  236. //?:overload ==
  237. const bool lnum::operator==(lnum &l) const
  238. {
  239.     return Equal(num.vec, num.dplen, num.sign, l.num.vec, l.num.dplen, l.num.sign);
  240. }
  241. //over load >>
  242. istream &operator>>(istream &is, lnum &n)
  243. {
  244.     string str;
  245.     is >> str;
  246.     n = str;
  247.     return is;
  248. }
  249. //overload <<
  250. ostream &operator<<(ostream &os, const lnum &l)
  251. {
  252.     os << l.num.str;
  253.     return os;
  254. }
  255. int main(int argc, char const *argv[])
  256. {
  257.     lnum a, b;
  258.     int i = 0;
  259.     while (i != 5)
  260.     {
  261.         std::cout << std::endl;
  262.         std::cout << "input data" << std::endl;
  263.         std::cin >> a >> b;
  264.         std::cout << "a = " << a << "\n"
  265.                   << "b = " << b << "\n"
  266.                   << "a > b: " << (a > b) << "\n"
  267.                   << "a == b: " << (a == b) << "\n"
  268.                   << "a < b: " << (a < b) << std::endl;
  269.         i++;
  270.     };
  271.     return 0;
  272. }
复制代码

================================================================
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>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-12-28 01:06:31 | 显示全部楼层
本帖最后由 bin554385863 于 2019-12-28 01:13 编辑

2019年12月28日01:02:21
改进版

lnum.h
  1. #ifndef LNUM_H
  2. #define LNUM_H
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. using std::istream;
  7. using std::ostream;
  8. using std::string;
  9. using std::vector;
  10. class lnum
  11. {
  12.     struct attr
  13.     {
  14.         int sign = 1;      //符号;
  15.         size_t dpsize = 0; //小数位;
  16.         size_t prec = 0;
  17.         size_t nsize = 1;
  18.         string str = "0";//过渡字符串
  19.         vector<int> vec = {0};   //数组;
  20.     };
  21.     struct align
  22.     {
  23.         vector<int> first;
  24.         vector<int> second;
  25.     };

  26. private:
  27.     //小数精度;
  28.     static size_t precision;
  29.     //数据;
  30.     attr num;
  31.     //str to attr
  32.     const attr StrToAttr(const string &str) const;
  33.     //attr to str
  34.     const string AttrToStr() const;
  35.     //进1
  36.     void Carry(vector<int> &v) const;
  37.     //借1
  38.     vector<int> Borrow(const vector<int> &v) const;
  39.     //对齐
  40.     align AlignVec(const vector<int> &m, const vector<int> &n) const;

  41. public:
  42.     lnum(const char *cstr = "0") : num(StrToAttr(cstr)) {}
  43.     lnum(const string &str) : num(StrToAttr(str)) {}
  44.     lnum(const lnum &l) : num(l.num) {}
  45.     //------------------------------------------------------------------
  46.     //设置精度
  47.     static void SetPrecision(const size_t i);
  48.     //展示信息
  49.     void Showinfo() const;
  50.     //>
  51.     const bool operator>(const lnum &l) const;
  52.     //<
  53.     const bool operator<(const lnum &l) const;
  54.     //==
  55.     const bool operator==(const lnum &l) const;
  56.     friend istream &operator>>(istream &is, const lnum &l);
  57.     friend ostream &operator<<(ostream &os, const lnum &l);
  58. };
  59. size_t lnum::precision = 0;
  60. #endif
复制代码


lnum.cpp
  1. #include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.h"
  2. //str to attr
  3. const lnum::attr lnum::StrToAttr(const string &str) const
  4. {
  5.     attr n;
  6.     n.prec = precision;
  7.     n.str = str;
  8.     string &nstr = n.str;
  9.     if (nstr[0] == '-')
  10.     {
  11.         nstr.erase(0, 1);
  12.     }
  13.     if (int(nstr.find('.')) != -1)
  14.     {
  15.         nstr.erase(nstr.find('.'), 1);
  16.     }
  17.     if (nstr == string(nstr.size(), '0'))
  18.     {
  19.         nstr.clear();
  20.         nstr.push_back('0');
  21.     }
  22.     else if (nstr != string(nstr.size(), '0'))
  23.     {
  24.         size_t s = nstr.size(), i = 0;
  25.         if (str[0] == '-')
  26.         {
  27.             n.sign = -1;
  28.         }
  29.         if (int(str.find('.')) != -1)
  30.         {
  31.             n.dpsize = str.size() - str.find('.') - 1;
  32.             if (nstr[0] == '0')
  33.             {
  34.                
  35.                 for (; i < s; i++)
  36.                 {
  37.                     if (nstr[i] != '0')
  38.                     {
  39.                         break;
  40.                     }
  41.                 }
  42.                 if (i >= s - n.dpsize)
  43.                 {
  44.                     nstr.erase(0, s - n.dpsize - 1);
  45.                 }
  46.                 else if (i < s - n.dpsize)
  47.                 {
  48.                     nstr.erase(0, i);
  49.                 }
  50.             }
  51.             
  52.         }
  53.         else
  54.         {
  55.             if (nstr[0] == '0')
  56.             {
  57.                 for (i = 0; i < s; i++)
  58.                 {
  59.                     if (nstr[i] != '0')
  60.                     {
  61.                         break;
  62.                     }
  63.                 }
  64.                 nstr.erase(0, i);
  65.             }
  66.         }
  67.         if (n.prec > n.dpsize)
  68.         {
  69.             nstr.insert(nstr.end(), n.prec - n.dpsize, '0');
  70.         }
  71.         else if (n.prec < n.dpsize)
  72.         {
  73.             nstr.erase(nstr.size() - (n.dpsize - n.prec));
  74.             n.dpsize = n.prec;
  75.         }
  76.         n.vec.clear();
  77.         for(char c:nstr)
  78.         {
  79.             n.vec.push_back(c - 48);
  80.         }
  81.     }
  82.     n.nsize = nstr.size();
  83.     return n;
  84. }
  85. //attr to str
  86. const string lnum::AttrToStr() const
  87. {
  88.     string str = num.str;
  89.     if (str.size() != 1 && num.prec != 0)
  90.     {
  91.         if (num.dpsize == num.prec)
  92.         {
  93.             str.insert(str.size() - num.prec, 1, '.');
  94.         }
  95.         else if (num.dpsize < num.prec && num.dpsize != 0)
  96.         {
  97.             str.erase(str.size() - (num.prec - num.dpsize));
  98.             str.insert(str.size() - num.dpsize, 1, '.');
  99.         }
  100.         else if (num.dpsize == 0)
  101.         {
  102.             str.erase(str.size() - num.prec);
  103.         }
  104.     }
  105.     if (num.sign == -1)
  106.     {
  107.         str.insert(0, 1, '-');
  108.     }
  109.     return str;
  110. }
  111. //进1
  112. void lnum::Carry(vector<int> &v) const
  113. {
  114.     size_t s = v.size();
  115.     v.insert(v.begin(), 1, 0);
  116.     for (size_t i = s - 1; i > 0; i++)
  117.     {
  118.         int t = v[i];
  119.         v[i] = t % 10;
  120.         v[i - 1] += t / 10;
  121.     }
  122.     if (v[0] == 0)
  123.     {
  124.         v.erase(v.begin(), v.begin() + 1);
  125.     }
  126. }
  127. //借1
  128. vector<int> lnum::Borrow(const vector<int> &v) const
  129. {
  130.     size_t s = v.size();
  131.     vector<int> t = v;
  132.     for (size_t i = 1; i < s; i++)
  133.     {
  134.         t[i - 1] -= 1;
  135.         t[i] += 10;
  136.     }
  137.     return t;
  138. }
  139. //对齐
  140. lnum::align lnum::AlignVec(const vector<int> &m, const vector<int> &n) const
  141. {
  142.     align agn;
  143.     agn = {m, n};
  144.     size_t fsize = agn.first.size(), ssize = agn.second.size();
  145.     if (fsize < ssize)
  146.     {
  147.         agn.first.insert(agn.first.begin(), ssize - fsize, 0);
  148.     }
  149.     else if (fsize > ssize)
  150.     {
  151.         agn.second.insert(agn.second.begin(), fsize - ssize, 0);
  152.     }
  153.     return agn;
  154. }
  155. //------------------------------------------------------------------------
  156. //设置精度
  157. void lnum::SetPrecision(const size_t i)
  158. {
  159.     lnum::precision = i;
  160. }
  161. //展示信息
  162. void lnum::Showinfo() const
  163. {
  164.     std::cout << "位值: ";
  165.     for (int i : num.vec)
  166.     {
  167.         std::cout << i;
  168.     }
  169.     std::cout << std::endl;
  170.     std::cout << "正负性: " << num.sign << "\n"
  171.               << "小数位: " << num.dpsize << "\n"
  172.               << "精确度: " << num.prec << "\n"
  173.               << "长  度: " << num.nsize << std::endl;
  174. }
  175. //>
  176. const bool lnum::operator>(const lnum &l) const
  177. {
  178.     bool f = true;
  179.     align agn = AlignVec(num.vec, l.num.vec);
  180.     if (num.sign < l.num.sign)
  181.     {
  182.         f = false;
  183.     }
  184.     if (num.sign == l.num.sign)
  185.     {
  186.         f = agn.first > agn.second;
  187.         if (num.sign == -1)
  188.         {
  189.             f = !f;
  190.         }
  191.     }
  192.     return f;
  193. }
  194. //<
  195. const bool lnum::operator<(const lnum &l) const
  196. {
  197.     bool f = true;
  198.     align agn = AlignVec(num.vec, l.num.vec);
  199.     if (num.sign > l.num.sign)
  200.     {
  201.         f = false;
  202.     }
  203.     if (num.sign == l.num.sign)
  204.     {
  205.         f = agn.first < agn.second;
  206.         if (num.sign == -1)
  207.         {
  208.             f = !f;
  209.         }
  210.     }
  211.     return f;
  212. }
  213. //==
  214. const bool lnum::operator==(const lnum &l) const
  215. {
  216.     align agn = AlignVec(num.vec, l.num.vec);
  217.     return agn.first == agn.second && num.sign == l.num.sign;
  218. }
  219. istream &operator>>(istream &is, lnum &l)
  220. {
  221.     string str;
  222.     (is >> str).get();
  223.     l = str;
  224.     return is;
  225. }
  226. ostream &operator<<(ostream &os, const lnum &l)
  227. {
  228.     os << l.AttrToStr();
  229.     return os;
  230. }
复制代码


lnum_test.cpp
  1. #include "E:\Users\admin\Documents\VScode\Code\My Class\shape.cpp\lnum\lnum.cpp"
  2. int main(int argc, char const *argv[])
  3. {
  4.     lnum::SetPrecision(3);
  5.     lnum a, b;
  6.     size_t i = 0;
  7.     while (i != 5)
  8.     {
  9.         std::cout << "input data!" << std::endl;
  10.         std::cin >> a >> b;
  11.         std::cout << "a =  " << a << "\n"
  12.                   << "b =  " << b << "\n"
  13.                   << "a >  b = " << (a > b) << "\n"
  14.                   << "a <  b = " << (a < b) << "\n"
  15.                   << "a == b = " << (a == b) << "\n"
  16.                   << std::endl;
  17.         i++;
  18.     }
  19.     return 0;
  20. }
复制代码

======================================================
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>
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-12 19:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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