高精压4位之C++类实现

用C++重写了一份,Pascal版本在这里
  1. 字符串转高精数组
  2. 高精+高精
  3. 高精+低精
  4. 高精-高精(答案必须为非负数)
  5. 高精*高精
  6. 高精*低精
  7. 高精div低精
  8. 高精数之间的<、>、=判断
  9. 高精数的输出
  10. 以上全都是压4位
  11. 一个小小的demo
可以发现操作符重载之后运算符的优先级和结合性不变。 参数表中的const引用很重要,否则复制数组会浪费很多时间。 程序如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
using namespace std;

int n;
class Bigint
{
  int data[200];
public:
  Bigint(): data() { }
  Bigint(const std::string& s): data() { operator=(s); }
  int operator[](const int i) const { return data[i]; }
  int& operator[](const int i) { return data[i]; }
  Bigint& operator=(const std::string&);
  int Length() const { return data[0]; }
  int& Length() { return data[0]; }
};
Bigint& Bigint::operator=(const std::string& str)
{
  memset(data, 0, sizeof(data));
  int k = str.size()-4;
  while (k > -4)
  {
    ++Length();
    std::stringstream ss;
    if (k <= 0)
    {
      ss << str.substr(0, 4+k);
      ss >> data[Length()];
    }
    else
    {
      ss << str.substr(k, 4);
      ss >> data[Length()];
    }
    k -= 4;
  }
  return *this;
}
Bigint operator+(const Bigint& a, const Bigint& b)
{
  Bigint c;
  int x = 0;
  c.Length() = std::max(a.Length(), b.Length());
  for (int i = 1; i <= c.Length(); ++i)
  {
    x = x/10000+a[i]+b[i];
    c[i] = x%10000;
  }
  if (x >= 10000)
    c[++c.Length()] = 1;
  return c;
}
Bigint operator+(const Bigint& a, const int b)
{
  Bigint c(a);
  int x = b+a[1];
  for (int i = 1; i <= c.Length(); ++i)
  {
    c[i] = x%10000;
    x = x/10000+a[i+1];
    if (!x)
      break;
  }
  return c;
}
Bigint operator-(const Bigint& a, const Bigint& b)
{
  Bigint c;
  int x = 0;
  for (int i = 1; i <= a.Length(); ++i)
  {
    x = x/10000+a[i]-b[i]+c[i];
    if (x < 0)
      x += 10000, --c[i+1];
    c[i] = x;
  }
  c.Length() = a.Length();
  while (c.Length() && !c[c.Length()]) --c.Length();
  return c;
}
Bigint operator*(const Bigint& a, const Bigint& b)
{
  Bigint c;
  for (int i = 1; i <= a.Length(); ++i)
  {
    int x = 0;
    for (int j = 1; j <= b.Length(); ++j)
    {
      x = x+a[i]*b[j]+c[i+j-1];
      c[i+j-1] = x%10000;
      x /= 10000;
    }
    c[i+b.Length()] = x;
  }
  c.Length() = a.Length()+b.Length();
  while (c.Length() && !c[c.Length()]) --c.Length();
  return c;
}
Bigint operator*(const Bigint& a, const int b)
{
  Bigint c;
  int x = 0;
  for (int i = 1; i <= a.Length(); ++i)
  {
    x += a[i]*b;
    c[i] = x%10000;
    x /= 10000;
  }
  c.Length() = a.Length();
  while (x)
  {
    c[++c.Length()] = x%10000;
    x /= 10000;
  }
  return c;
}
Bigint operator/(const Bigint& a, const int b)
{
  Bigint c;
  int x = 0;
  for (int i = a.Length(); i > 0; --i)
  {
    c[i] = (x*10000+a[i])/b;
    x = (x*10000+a[i])%b;
  }
  c.Length() = a.Length();
  while (c.Length() && !c[c.Length()]) --c.Length();
  return c;
}
inline bool operator==(const Bigint& a, const Bigint& b)
{
  if (a.Length() != b.Length())
    return false;
  for (int i = 1; i <= a.Length(); ++i)
    if (a[i] != b[i])
      return false;
  return true;
}
inline bool operator>(const Bigint& a, const Bigint& b)
{
  if (a.Length() != b.Length())
    return a.Length() > b.Length();
  for (int i = 1; i <= a.Length(); ++i)
    if (a[i] != b[i])
      return a[i] > b[i];
  return false;
}
inline bool operator<(const Bigint& a, const Bigint& b)
{
  if (a.Length() != b.Length())
    return a.Length() < b.Length();
  for (int i = 1; i <= a.Length(); ++i)
    if (a[i] != b[i])
      return a[i] < b[i];
  return false;
}
void Print(const Bigint& a)
{
  cout << a[a.Length()];
  for (int i = a.Length()-1; i >= 1; --i)
  {
    if (a[i] < 10) cout << "000"; else
    if (a[i] < 100) cout << "00"; else
    if (a[i] < 1000) cout << "0";
    cout << a[i];
  }
  cout << endl;
}
int main()
{
  ios::sync_with_stdio(false);

  Bigint a("1234567890");
  Bigint b("12345678901");
  Bigint c("123456789012");
  Bigint d("1234567890123");

  Print(a);
  Print(b);
  Print(c);
  Print(d);

  Print(b+a);
  Print(c-b);
  Print(d*c);
  Print(d/100);
  Print(a*768);
  Print(a+22);
  cout << ((a+b)*c < (d-c)*a) << endl
       << (c*100 == d) << endl
       << (a+d > b+c) << endl;
  a = "2";
  b = "3";
  c = "4";
  Print(a+b*c);
  Print((a+b)*c);
}

Comments