Two extra large numbers added

Source: Internet
Author: User

One way to solve the problem of adding extra large numbers is to turn the shaping into a string, not much to say directly on the code, which has been debugged through.


#include <iostream> #include <string> #include <cmath>using namespace std;string Add (String str1, String str2) {int i;string str;int len_str1 = Str1.length (); int len_str2 = Str2.length (); int n = ABS (LEN_STR1-LEN_STR2);//Long  degree alignment if (LEN_STR1 < LEN_STR2) for (i=0; i<n; i++) str1 = "0" + str1;elsefor (i=0; i<n; i++)            str2 = "0" + str2;len_str1 = Str1.length ();//At this time the length of two strings is the same as int cf=0;//carry flag bit int temp;//holds two numbers added and for (i=len_str1-1; i>=0; i--) {temp = str1[i]-' 0 ' + str2[i]-' 0 ' + cf;//converts the string to integer CF = temp/10;temp = Temp%10;str = char (temp+ ' 0 ') + str;} if (cf!=0) str = char (cf+ ' 0 ') + str;return str;} int main () {string str1, str2;cin >> str1;cin >> str2;cout << Add (str1, str2) << Endl;return 0;}


Two extra large numbers added

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.