-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT42_fudianshujiafa.cpp
87 lines (84 loc) · 2.06 KB
/
T42_fudianshujiafa.cpp
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
#include<bits/stdc++.h>
using namespace std;
// jinwei:小数位进位
string addZhengShu(string s1, int len1, string s2, int len2, int jinwei) {
string res="";
// 4441976058474520796, 9057183, 1
for(int i=0; i<len2; i++) {
int a = (s1[len1-i-1]-'0');
int b = (s2[len2-i-1]-'0');
int temp= a+b+jinwei;
res = (char)(temp%10 + '0') + res;
jinwei = temp/10;
}
for(int i=len2; i<len1; i++) {
int a = (s1[len1-i-1]-'0');
int temp= a+jinwei;
res = (char)(temp%10 + '0') + res;
jinwei = temp/10;
}
if(jinwei!=0) {
res = (char)(jinwei + '0') + res;
}
return res;
}
// str1小数位数idx1大于str2小数部分idx2
string add(string str1, int idx1, string str2, int idx2) {
// cout<< str1<< "("<< idx1<< ")+"<< str2<< "("<< idx2<< ")"<< "=";
int jinwei=0;
// 易错点,直接拼接长出来的小数位
string res=str1.substr(str1.length()-idx1+idx2);
// res:2
// 11.2222,1.035
for(int i=0; i<idx2; i++) {
int a = str1[str1.length()-idx1+idx2-i-1]-'0';
int b = str2[str2.length()-i-1]-'0';
int temp = a+b+jinwei;
res = (char)(temp%10+'0') + res;
jinwei = temp/10;
}
res = '.' + res;
// cout<< res<< endl;
int zLen1 = str1.length()-idx1-1;
int zLen2 = str2.length()-idx2-1;
if(zLen1>zLen2) {
res = addZhengShu(str1.substr(0, zLen1), zLen1, str2.substr(0, zLen2), zLen2, jinwei) + res;
} else {
res = addZhengShu(str2.substr(0, zLen2), zLen2, str1.substr(0, zLen1), zLen1, jinwei) + res;
}
return res;
}
int main() {
string str1, str2;
while(cin>> str1>> str2) {
int idx1=0;
int idx2=0;// 小数点后位数
string res;
for(int i=0; i<str1.length(); i++) {
if(str1[i]=='.') {
// str1.find('.')
idx1 = str1.length()-1-i;
}
}
idx2 = str2.length()-1-str2.find('.');
// for(int i=0; i<str2.length(); i++) {
// if(str2[i]=='.') {
// idx2 = str2.length()-1-i;
// }
// }
if(idx1>idx2) {
res = add(str1, idx1, str2, idx2);
} else {
res = add(str2, idx2, str1, idx1);
}
// 11.00
for(int i=res.length()-1; i>=0; i--) {
if(res[i]!='0' && res[i]!='.') {
res = res.substr(0, i+1);
break;
}
}
cout<< res<< endl;
}
return 0;
}