-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTwoBig.cpp
More file actions
134 lines (115 loc) · 2.17 KB
/
Copy pathTwoBig.cpp
File metadata and controls
134 lines (115 loc) · 2.17 KB
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
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
class A
{
public:
void print()
{
cout << "hello" << endl;
}
};
//一个字符串和一位相乘,模拟相乘过程
string step(string s,char c)
{
int cnt = 0;
int flag = c - '0';
string res;
int n = s.size();
for(int i = n - 1;i >= 0;i--)
{
int tmp = s[i] - '0';
int t = tmp * flag + cnt;
res += (t % 10 + '0');
cnt = (t / 10);
}
if(cnt != 0)
{
res += (cnt % 10) + '0';
}
//cout << res << endl;
//res.reserve();
int l = 0,r = res.size() -1;
while(l < r)
{
swap(res[l++],res[r--]);
}
//cout << res << endl;
return res;
}
//两个字符串相加
string addString(string s1,string s2)
{
int cnt = 0;
string res;
int index1 = s1.size() - 1;
int index2 = s2.size() - 1;
while(index1 >= 0 && index2 >= 0)
{
int tmp = s1[index1--] - '0' + s2[index2--] - '0' + cnt;
res += (tmp % 10) + '0';
cnt = (tmp / 10);
}
while(index1 >= 0)
{
int tmp = s1[index1--] - '0' + cnt;
res += (tmp % 10) + '0';
cnt = (tmp / 10);
}
while(index2 >= 0)
{
int tmp = s2[index2--] - '0' + cnt;
res += (tmp % 10) + '0';
cnt = (tmp / 10);
}
if(cnt != 0)
{
res += (cnt % 10) + '0';
}
//res.reserve();
int l = 0,r = res.size() -1;
while(l < r)
{
swap(res[l++],res[r--]);
}
return res;
}
//大整数相乘
string pro(string s1,string s2)
{
//模拟每一位相乘
string res;
string pre = "";
int times = 0;
for(int i = s2.size() - 1;i >= 0;i--)
{
string tmp = step(s1,s2[i]);
//移位加0
for(int i = 0;i < times;i++)
{
tmp += '0';
}
times++;
if(pre == "")
{
pre = tmp;
}else
{
pre = addString(pre,tmp);
}
}
res = pre;
return res;
}
int main()
{
string s1 = "9999";
string s2 = "9999";
A a;
int n = sizeof(a);
cout << n << endl;
cout << pro(s1,s2) << endl;
system("pause");
return 0;
}