Using string C + + for high-precision addition operations

Source: Internet
Author: User

For large numbers of operations, with a long long int still cannot solve, this time need to consider through the simulation and array storage to achieve high-precision operation.

This article discusses the use of C + + string to achieve high-precision operations.

The amount you enter first is stored directly as a string, set to S1 and S2.

Next, you design a reversal function that reverses the entire string (to facilitate subsequent calculations).

String reversestr (String input) {    string output = "";    for (int i = 0; i < input.length (); i++) {        Output.insert (Output.begin (), input[i]);    }    return output;}
The principle is simple, just need to take out the string from front to back each character, each time in front of the head inserted, also get the reverse order.


The following discussion and the calculation method, first the S1 and S2 inversion, so that from the front to back to traverse the string is from low to high order, set the carry variable is carry, initialized to 0, first processing the public parts of S1 and S2, starting from the low, each get the bit and temp, plus the front carry carry, Then calculate the standard carry TEMP/10, and the standard and the temp%10, insert it into the head of the result string, you can get the sum of the public part.

Next for S1 or S2 more out of the section, separate processing, and finally note that if the processing end of the backward bit is not 0, to go to the next bit, the specific code is as follows:

First define the two functions used for the mutual transfer of char and int:

int Chartoint (char c) {    return C-' 0 ';} char Inttochar (int n) {    return ' 0 ' + N;}

Next is the calculation code for the and:

String Sumofstr (String _s1, String _s2) {string S1 = Reversestr (_S1);    String s2 = Reversestr (_S2); int pMax = S1.length () > S2.length ()?    S2.length (): S1.length ();    String sumstr = "";    int p;    int carry = 0;    int temp = 0;        for (p = 0; p < PMax; p++) {temp = Chartoint (s1[p]) + chartoint (S2[p]) + carry;        carry = TEMP/10;    Sumstr.insert (Sumstr.begin (), Inttochar (temp% 10));            } if (P < s1.length ()) {for (; p < s1.length (); p++) {temp = Chartoint (S1[p]) + carry;            carry = TEMP/10;        Sumstr.insert (Sumstr.begin (), Inttochar (temp% 10));            }} if (P < s2.length ()) {for (; p < s2.length (); p++) {temp = Chartoint (S2[p]) + carry;            carry = TEMP/10;        Sumstr.insert (Sumstr.begin (), Inttochar (temp% 10));        }} if (Carry > 0) {sumstr.insert (Sumstr.begin (), Inttochar (carry% 10));    Carry/= 10; } return sumstr;}


Using string C + + for high-precision addition operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.