Description Input:
Input of two non-negative number, each a number satisfying the following requirements:
(1) contains only 0-9, 0 or a decimal point ".";
(2) length is not greater than 200;
(3) without wrong ruling.
Description Output:
output and two numbers
1. Thoughts
- Beginning the problem too simple, direct string is converted int / double, then a simple digital addition. : Reflection two errors occurred during
'' (1) that is a decimal decimal fraction, the integer part is 0, so long as the judgment s [1] == can know whether it is a decimal, so for granted;
(2) did not notice length is less than 200, i.e., the number string can not speak into int / double, will overflow; - Later look title problem before and think:
(1) Since the length is less than 200, the character string can only add and subtract;
(2) Since the number of inputs may also have decimal integers, it should be divided discussion, ( '.') determined by circumstances s.find: + decimal decimal decimal integer +, + a decimal integer, an integer + integer;
(3) Consideration difficult situation, i.e., "+ decimal decimal", has three main parts calculated : left part decimal integer addition AddInte (), adding the fractional part of the decimal point right AddFrac (), and adding the carry portion after the decimal integer addition; in some circumstances other portions of the three-part calculation, i.e., the case covers the addition of all possible conditions;
(4) which align the two numbers are integers and decimal integer string is right-aligned, the decimal string is left-aligned, the same length taken It is added. Integer carry occurs, and then adding the carry to the higher position; decimal carry occurs, and then adding the carry to the integer part. - Relates to the role of function as follows:
(. 1) the Add (): two strings are added, the current character string is determined by the find ( '.') Will be classified into four cases: + decimal decimal decimal integer + integer + decimal integer + integer; and all the fractional are divided into an integer part and the fractional part, for adding separately;
(2) AddInte (): adding an integer, a string right alignment, alignment section () by adding AddNum; If there is a carry, then the carry bit in the higher performing AddInte () operation; Finally, the two longitudinal stitching together;
(. 3) AddFrac (): decimal addition, the string left aligned, the alignment section by AddNum () with was added; then, the latter half of the splicing together; wherein the integer part to carry reused AddInte () for processing;
(. 4) AddNum (): adding two integers of the same length.
2. Implement
#include <iostream>
#include <string>
using namespace std;
#define MAX(a,b) ((a>b)?a:b)
string AddNum(string s1, string s2){//相同长度的两数相加
string res, s;
int len = s1.size();
int n = 0, c = 0;
for (int i = len - 1; i >= 0; i--){
n = (s1[i] - '0') + (s2[i] - '0') + c;
if (n > 9){
c = n / 10;
s = '0' + (n % 10);
}
else{
c = 0;
s = '0' + n;
}
res = s + res;
}
if (c > 0){
char cr = '0' + c;
res = cr + res;
}
return res;
}
string AddFrac(string s1, string s2){//两个小数部分相加
int l1 = s1.size(), l2 = s2.size();
int len, maxlen, minlen;
string f1, f2, res;
//先确定两个小数的长度
if (l1 > l2){
len = l1;
maxlen = l1;
minlen = l2;
f1 = s1;
f2 = s2;
}
else{
len = l2;
maxlen = l2;
minlen = l1;
f1 = s2;
f2 = s1;
}
//将位数更长的小数分成两部分
string f11 = f1.substr(0, minlen);//与位数更短相加的前半部分
string f12 = f1.substr(minlen, maxlen - minlen);//剩余后半部分
string r1 = AddNum(f11, f2);
int lr = r1.size();
res = r1 + f12;
return res;
}
string AddInte(string s1, string s2){//两个整数部分相加
int l1 = s1.size(), l2 = s2.size();
int len, maxlen, minlen;
string i1, i2, res, n1, n2;
//先确定两个整数的长度
if (l1 > l2){
len = l1;
maxlen = l1;
minlen = l2;
i1 = s1;
i2 = s2;
}
else{
len = l2;
maxlen = l2;
minlen = l1;
i1 = s2;
i2 = s1;
}
//将位数更长的小数分成两部分
string i11 = i1.substr(maxlen - minlen, minlen);//与位数更短相加的后半部分
string i12 = i1.substr(0, maxlen - minlen);//剩余前半部分
string r1 = AddNum(i11, i2);
int lr = r1.size();
if (lr > minlen){//小数部分存在进位
//将小数进位与整数和进行两个整数相加操作
n1 = AddInte(i12, r1.substr(0, lr - minlen));
res = n1 + r1.substr(lr - minlen);
}
else{
res = i12 + r1;
}
return res;
}
string Add(string s1, string s2){
int d1 = s1.find('.');
int d2 = s2.find('.');
string i1, i2;
string f1, f2;
string n1, n2;
string res;
if (d1 > 0 && d2 > 0){//两个都是小数
i1 = s1.substr(0, d1);
f1 = s1.substr(d1+1);
i2 = s2.substr(0, d2);
f2 = s2.substr(d2 + 1);
n1 = AddInte(i1, i2);
n2 = AddFrac(f1, f2);
int ml = MAX(f1.size(), f2.size());
int ln2 = n2.size();
if ( ln2 > ml ){//小数部分存在进位
//将小数进位与整数和进行两个整数相加操作
n1 = AddInte(n1, n2.substr(0, ln2 - ml));
n2 = n2.substr(ln2 - ml);
}
res = n1 + '.' + n2;
}
else if (d1 > 0 && d2 < 0){//第一个是小数,第二个是整数
i1 = s1.substr(0, d1);
f1 = s1.substr(d1 + 1);
i2 = s2;
n1 = AddInte(i1, i2);
n2 = f1;
res = n1 + '.' + n2;
}
else if (d1 < 0 && d2 > 0){//第一个是整数,第二个是小数
i1 = s1;
i2 = s2.substr(0, d2);
f2 = s2.substr(d2 + 1);
n1 = AddInte(i1, i2);
n2 = f2;
res = n1 + '.' + n2;
}
else{//两个都是整数
i1 = s1;
i2 = s2;
n1 = AddInte(i1, i2);
res = n1;
}
return res;
}
int main(){
string s1, s2;
while (cin >> s1 >> s2){
string res;
res = Add(s1, s2);
cout << res << endl;
}
return 0;
}