Addition with string

I've done my code for addition using string.
I'd really happy if you guys can guide me with the optimization. It's not really necessary but if you are interested.

Algorithm based on Complement Method.
See: http://en.wikipedia.org/wiki/Method_of_complements

The input format should be:
<sign><integer><point><decimal>

Example: +0.123456 or -987654.0

Main function:
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
string add (string a, string b) {
    string str;
    string temp;
    short  carry  = 0;
    short  result = 0;
    bool   c_sign = 0;
    long   l      = 0;

    // padding 0 to decimal part
    l = ( a.size()-a.find(".") ) - ( b.size()-b.find(".") );
    if (l > 0) b.resize(b.size() + l, '0');
    if (l < 0) a.resize(a.size() - l, '0');

    // padding 0 to integer part
    l = a.size()-b.size();
    if (l > 0) b.insert(1,  l, '0');
    if (l < 0) a.insert(1, -l, '0');

    // covert sign to digit, convert number to 9's complement
    if (a[0] == '-') { a[0]='0'; a = complement(a); carry++; } else a[0]='0';
    if (b[0] == '-') { b[0]='0'; b = complement(b); carry++; } else b[0]='0';

    // this will be the carry if the result is negative
    // the actual process requires you to subtract 1 from the result
    // using this the program will only calculate with carry 1 less than current
    c_sign = ( carry==2 ? 1:0 );

    // adding
    for (long i=a.size()-1; i>=0; i--) {
        if (a[i] == '.') {
            str += '.';
            continue;
        }

        result  = a[i] + b[i] + carry - '0' - '0';
        carry   = result/10;
        result %= 10;
        str    += result + '0';
    }

    // reverse the result, since the addition was (*must) done from back
    for (long i=str.size()-1; i>=0; i--) temp += str[i];
    str = temp;

    // signing
    if (str[0] < '5') str = "+" + temp;
    else { // redo adding
        carry = c_sign;
        str.clear();
        temp.clear();
        for (long i=a.size()-1; i>=0; i--) {
            if (a[i] == '.') {
                str += '.';
                continue;
            }

            result  = a[i] + b[i] + carry - '0' - '0';
            carry   = result/10;
            result %= 10;
            str    += result + '0';
        }
        for (long i=str.size()-1; i>=0; i--) temp += str[i];
        str = "+" + temp;
        str = complement(str);
    }

    return compact(str);
}

Complement function: convert base-10 numbers to it's 9 complement.
Example: 1350268947 > 8649731052
1
2
3
4
5
6
7
string complement (string s) {
    for (long l=0; l<s.size(); l++) {
        if  ( isdigit(s[l]) ) s[l] = '9' - s[l] + '0';
    }

    return s;
}

This code is used to crop leading/trailing zeros.
1
2
3
4
5
6
7
8
9
10
11
12
string compact (string s) {
    long l;
    l = s.find_first_not_of("0", 1);
    if (s[l] == '.') l--;
    s.erase(1, l-1);

    l = s.find_last_not_of("0");
    if (s[l] == '.') l++;
    s.resize(l+1);

    return s;
}

Here's my main:
1
2
3
4
5
6
7
int main() {
    string a = "+12345.0";
    string b = "-0.12345";
    cout << a << endl << b << endl;
    cout << add(a, b); // +12344.87655
    return 0;
}

I'll post the multiplication later...
Topic archived. No new replies allowed.