Multiplication of large numbers (1)

Source: Internet
Author: User

Question: multiply two large numbers.

 

See this for the multiplication of large numbers (2.

 

Method 1: store two large numbers in arrays, simulate multiplication, and multiply each bit of an array with a large number, and add the result.
For the code, see string multiply1 (bignumber & bignumber2) in the Code section ).

Method 2: the same is the above idea. The difference is that the stored procedure uses a two-dimensional array to directly access the result of multiplication of each bit, and then processes the process of adding.
For the code, see string multiply2 (bignumber & bignumber2) in the Code section ).

 

#include <iostream>#include <string>#include <cstring>using namespace std;class bignumber{private:string bigno;bool checkout;bignumber();bool checknumber(){for(int count = 0; count < bigno.length(); ++count){if(!isdigit(bigno[count])){return false;}}return true;}public:explicit bignumber(const char tocopy[]){bigno = tocopy;checkout = checknumber();}explicit bignumber(const bignumber &tocopy){bigno = tocopy.bigno;checkout = checknumber();}string showvalue(){return bigno;}bool showcheck(){return checkout;}string multiply1(bignumber &bignumber2){if(!(checkout && bignumber2.showcheck())){cout<<"Values are not all positive numbers."<<endl;return "";}int len1 = bigno.length();int len2 = bignumber2.showvalue().length();int *no1 = new int[len1];memset(no1, 0, len1 * sizeof(int));int *no2 = new int[len2];memset(no2, 0, len2 * sizeof(int));int count = 0;for(count = len1; count > 0; --count){no1[count - 1] = (int)(bigno[count - 1]) - 48;}for(count = len2; count > 0; --count){no2[count - 1] = (int)(bignumber2.showvalue()[count - 1]) - 48;}int *convertpointer = new int[len1 + len2];memset(convertpointer, 0, (len1 + len2) * sizeof(int));for(count = len2; count > 0; --count){for(int countrow = len1; countrow > 0; --countrow){convertpointer[countrow + count - 1] += no1[countrow - 1] * no2[count - 1];}}for(count = len1 + len2; count > 0; --count){if(convertpointer[count - 1] > 9){convertpointer[count - 2] += convertpointer[count - 1] / 10;convertpointer[count - 1] = convertpointer[count - 1] % 10;}}char *resultchar = new char[len1 + len2];bool checkresultzero = false;int countresultzero = 0;for(count = 0; count < len1 + len2; ++count){if(!checkresultzero){if(convertpointer[count] != 0){checkresultzero = true;resultchar[countresultzero++] = convertpointer[count] + '0';}}else{resultchar[countresultzero++] = convertpointer[count] + '0';}}resultchar[countresultzero] = '\0';string result = resultchar;delete[] no1;delete[] no2;delete[] convertpointer;delete[] resultchar;return result;}string multiply2(bignumber &bignumber2){if(!(checkout && bignumber2.showcheck())){cout<<"Values are not all positive numbers."<<endl;return "";}int len1 = bigno.length();int len2 = bignumber2.showvalue().length();int **convertcolumn = new int*[len2 + 1];int countcolumn = 0;int countrow = 0;for(countcolumn = 0; countcolumn < len1 + 1; ++countcolumn){convertcolumn[countcolumn] = new int[len1 + len2 + 1];memset(convertcolumn[countcolumn], 0, sizeof(int) * (len1 + len2 + 1));}for(countcolumn = len2 + 1; countcolumn < len1 + len2 + 1; ++countcolumn){convertcolumn[0][countcolumn] = (int)(bigno[countcolumn - len2 - 1]) - 48;}for(countrow = 1; countrow < len2 + 1; ++countrow){convertcolumn[countrow][0] = (int)(bignumber2.showvalue()[countrow - 1]) - 48;}for(countrow = len2; countrow > 0; --countrow){for(countcolumn = len1; countcolumn > 0; --countcolumn){convertcolumn[countrow][len2 + 1 - countrow + countcolumn] = convertcolumn[0][countcolumn + len2] * convertcolumn[len2 + 1 - countrow][0];}}int *convertpointer = new int[len1 + len2];memset(convertpointer, 0, sizeof(int) * (len1 + len2));int count = 0;for(count = len1 + len2; count > 0; --count){for(countrow = 1; countrow < len2 + 1; ++countrow){convertpointer[count - 1] += convertcolumn[countrow][count];}if(convertpointer[count - 1] > 9){convertpointer[count - 2] += convertpointer[count - 1] / 10;convertpointer[count - 1] = convertpointer[count - 1] % 10;}}char *resultchar = new char[len1 + len2];bool checkresultzero = false;int countresultzero = 0;for(count = 0; count < len1 + len2; ++count){if(!checkresultzero){if(convertpointer[count] != 0){checkresultzero = true;resultchar[countresultzero++] = convertpointer[count] + '0';}}else{resultchar[countresultzero++] = convertpointer[count] + '0';}}resultchar[countresultzero] = '\0';string result = resultchar;for(countcolumn = 0; countcolumn < len1 + 1; ++countcolumn){delete[] convertcolumn[countcolumn];}delete[] convertcolumn;delete[] convertpointer;delete[] resultchar;return result;}};int main(){bignumber aaa("11234567890");bignumber bbb("00123456788");cout<<aaa.showvalue()<<endl<<bbb.showvalue()<<endl;cout<<aaa.multiply1(bbb)<<endl;cout<<aaa.multiply2(bbb)<<endl;return 0;}

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.