[Template applet] adds a large number in decimal format (positive integer version + negative number version), including validity check and negative number of digits

Source: Internet
Author: User

[Template applet] adds a large number in decimal format (positive integer version + negative number version), including validity check and negative number of digits

To adapt to different purposes, the big number algorithm is written into two versions, namely, the version that only processes positive integers and the version that contains negative numbers. You can select the version as needed.

Version 1: only positive integers can be processed
1 // Add large numbers (decimal positive integer), use string to process 2 # include <iostream> 3 # include <string> 4 # include <algorithm> 5 6 using namespace std; 7 8 // check the validity of the input data. The number must be within the range of 9 bool IsVaild (const string & num1, const string & num2) 10 {11 for (auto val: num1) 12 {13 if (! (Val> = '0' & val-'0' <= '9') 14 return false; 15} 16 for (auto val: num2) 17 {18 if (! (Val> = '0' & val <= '9') 19 return false; 20} 21 return true; 22} 23 24 string greatNumberAdd (string num1, string num2) 25 {26 const size_t len1 = num1.length (); 27 const size_t len2 = num2.length (); 28 const size_t n = len1> len2? Len1: len2; 29 reverse (num1.begin (), num1.end (); 30 reverse (num2.begin (), num2.end (); 31 32 string result; 33 int carry = 0; 34 for (size_t I = 0; I <n; ++ I) 35 {36 const int num1i = I <len1? Num1 [I]-'0': 0; 37 const int num2i = I <len2? Num2 [I]-'0': 0; 38 const int val = (num1i + num2i + carry) % 10; 39 carry = (num1i + num2i + carry)/10; 40 result. insert (result. begin (), val + '0'); 41} 42 if (1 = carry) // insert '1' 43 result if there is an advance at the beginning. insert (result. begin (), '1'); 44 45 return result; 46} 47 48 int main () 49 {50 string num1, num2; 51 while (cin> num1> num2) 52 {53 if (IsVaild (num1, num2) 54 cout <greatNumberAdd (num1, num2) <endl; 55 else56 cout <"illegal input data" <endl; 57} 58 return 0; 59}
Version 2: handling positive and negative integers (STL encoding style)
1 // Add large numbers (decimal positive and negative integers). Use string to process 2 # include <iostream> 3 # include <string> 4 # include <algorithm> 5 6 using namespace std; 7 8 // check the validity of input data. The number must be within the range of 9 bool IsVaild (const string & num1, const string & num2) 10 {11 for (size_t I = 0; I <num1.length (); ++ I) 12 {13 if (0 = I & '-' = num1 [0] & ++ I <num1.length ()) {}; // the first digit can be '-'. I is directly added here to continue to Judge 14 if (! (Num1 [I]> = '0' & num1 [I]-'0' <= '9') 15 return false; 16} 17 for (size_t I = 0; I <num2.length (); ++ I) 18 {19 if (0 = I & '-' = num2 [0] & ++ I <num2.length ()){}; // The first digit can be '-'. Here, I is added directly and 20 if (! (Num2 [I]> = '0' & num2 [I]-'0' <= '9') 21 return false; 22} 23 return true; 24} 25 26 // helper function: num1-num2 27 string _ greatNumberMinu (string num1, string num2) 28 {29 const size_t len1 = num1.length (); 30 const size_t len2 = num2.length (); 31 const size_t n = len1> len2? Len1: len2; 32 reverse (num1.begin (), num1.end (); 33 reverse (num2.begin (), num2.end (); 34 35 string result; 36 int carry = 0; // borrow 37 for (size_t I = 0; I <n; ++ I) 38 {39 const int num1i = I <len1? Num1 [I]-'0': 0; 40 const int num2i = I <len2? Num2 [I]-'0': 0; 41 const int val = (num1i-carry-num2i> = 0 )? (Num1i-carry-num2i): (num1i-carry-num2i + 10); 42 carry = (num1i-carry-num2i> = 0 )? 0: 1; 43 result. insert (result. begin (), val + '0'); 44} 45 int firstIndex_notEqualTo_0 = 0; // locate the first position not 0 (if the front is 0, erase it) 46 for (; firstIndex_notEqualTo_0 <result. length (); ++ firstIndex_notEqualTo_0) 47 {48 if (result [firstIndex_notEqualTo_0]! = '0') 49 break; 50} 51 if (firstIndex_notEqualTo_0> 0) // (if both are 0, erase) 52 result. erase (0, firstIndex_notEqualTo_0); 53 54 return result; 55} 56 57 // helper function: called when the symbol is positive or negative, here, num1 and num2 do not contain the symbol bits (which are greatly reduced and determined in this function). sign is the 58 string _ greatNumberMinu (string num1, string num2, bool flag) 59 {60 string result; 61 const size_t len1 = num1.length (); 62 const size_t len2 = num2.length (); 63 64 if (len1> len2) 65 {66 result = _ _ GreatNumberMinu (num1, num2); 67 if (flag) 68 result. insert (result. begin (), '-'); // Add '-' 69} 70 else if (len1 <len2) 71 {72 result =__ greatNumberMinu (num2, num1); 73 if (! Flag) 74 result. insert (result. begin (), '-'); // Add '-'75} 76 else at the beginning // len1 = len2 77 {78 if (num1> num2) // string comparison 79 {80 result =__ greatNumberMinu (num1, num2); 81 if (flag) 82 result. insert (result. begin (), '-'); // Add '-'83} 84 else 85 {86 result =__ greatNumberMinu (num2, num1); 87 if (! Flag) 88 result. insert (result. begin (), '-'); // Add '-'89} 90} 91 92 return result at the beginning; 93} 94 95 // helper function: it is called when the symbols are both positive and negative. num1 and num2 do not contain the 96 string _ greatNumberAdd (string num1, string num2) 97 {98 const size_t len1 = num1.length (); 99 const size_t len2 = num2.length (); 100 const size_t n = len1> len2? Len1: len2; 101 reverse (num1.begin (), num1.end (); 102 reverse (num2.begin (), num2.end (); 103 104 string result; 105 int carry = 0; // carry 106 for (size_t I = 0; I <n; ++ I) 107 {108 const int num1i = I <len1? Num1 [I]-'0': 0; 109 const int num2i = I <len2? Num2 [I]-'0': 0; 110 const int val = (num1i + num2i + carry) % 10; 111 carry = (num1i + num2i + carry)/10; 112 result. insert (result. begin (), val + '0'); 113} 114 if (1 = carry) // insert '1' 115 result if there is an advance at the beginning. insert (result. begin (), '1'); 116 117 return result; 118} 119 120 // The entry for adding large numbers. First, judge the symbol (two positive, two negative, one positive, and one negative ), call the auxiliary function 121 string greatNumberAdd (string num1Andsign, string num2Andsign) 122 {123 string result; 124 if ('-' = num1Andsign [0] & '-' = num2Andsign [0]) // two negative 125 {126 result = _ greatNumberAdd (num1Andsign. substr (1, num1Andsign. length ()-1), num2Andsign. substr (1, num2Andsign. length ()-1); 127 result. insert (result. begin (),'-'); // Add '-' 128} 129 else if ('-' = num1Andsign [0] | '-' = num2Andsign [0]) // one positive and one negative 130 {131 if ('-' = num1Andsign [0]) 132 result = _ greatNumberMinu (num1Andsign. substr (1, num1Andsign. length ()-1), num2Andsign, true); 133 else if ('-' = num2Andsign [0]) 134 result = _ greatNumberMinu (num1Andsign, num2Andsign. substr (1, num2Andsign. length ()-1), false); 135} 136 else137 result = _ greatNumberAdd (num1Andsign, num2Andsign); // two positive 138 return result; 140} 141 142 int main () 143 {144 string num1, num2; 145 while (cin> num1> num2) 146 {147 if (IsVaild (num1, num2 )) 148 cout <greatNumberAdd (num1, num2) <endl; 149 else150 cout <"the input data is invalid" <endl; 151} 152 return 0; 153}

For ease of understanding, the program running flowchart of version 2 is drawn (the illustration is clear and function parameters are omitted ):

 

 

Reference source description:

1, positive integer version, for reference to the Niuke network "like" User ideas, thank you here, the original question see https://www.nowcoder.com/questionTerminal/5821836e0ec140c1aa29510fd05f45fc? OrderByHotValue = 0 & mutiTagIds = 578_579 & page = 2 & onlyReference = false.

2, including negative version, refer to the implementation of this article ideas: http://blog.csdn.net/to_be_better/article/details/50375420

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.