C + + implements Big Data multiplication

Source: Internet
Author: User

1. Test environment vs2013 Windows 7

Use the method of multiplicative, and then the unified carry, and finally convert it to a string, output.

Program code:

#define _CRT_SECURE_NO_WARNINGS#INCLUDE&LT;IOSTREAM&GT;STRUCT Bigdatamutliplie{private:char Data_a[100];char Data_ B[100];int len_a;int len_b;bool negative;bool detect_data () {len_a = strlen (data_a); len_b = strlen (Data_b); if (len_a = = 0 | | Len_b = = 0) {return false;} for (int i = 0; i < len_a; i++) {if (!) ( Data_a[i] >= ' 0 ' &&data_a[i] <= ' 9 ')) {return false;}} for (int i = 0; i < Len_b; i++) {if (!) ( Data_b[i] >= ' 0 ' &&data_b[i] <= ' 9 ') {return false;}}} Public:bigdatamutliplie () {memset (data_a, 0, sizeof (data_a)), memset (data_b, 0, sizeof (data_b)); len_a = 0;len_b = 0;} BOOL Init_data (const char *data_a, const char *data_b) {this->negative = false;if (!data_a | |!data_b) {return false;} if (*data_a = = '-' | | *data_a = = ' + ') {strcpy (this->data_a, data_a + 1); if (*data_a = = '-') {negative =!negative;}} else{strcpy (This->data_a, data_a);} if (*data_b = = '-' | | *data_b = = ' + ') {strcpy (this->data_b, Data_b + 1); if (*data_b = = '-') {negative =!negative;}} else{strcpy (ThiS->data_b, Data_b);}} char * MULTIPLIE_AB () {if (!detect_data ()) {return NULL;} int * Int_res = new int[(len_a + len_b) *sizeof (int)];//two numbers multiplied by maximum no more than two number of numbers added 99*99char * str_res = new char[(len_a + len_b + 2) *sizeof (char)]; Multiple applications two characters of space a stored symbol, a save ' memset ' (str_res, 0, (len_a + Len_b + 2) *sizeof (char)), memset (int_res, 0, (len_a + len_b) *sizeof ( int));//Take a multiplicative way and then unify the carry for (int i = 0; i < len_a; i++) for (int j = 0; J < Len_b; J + +) {int_res[i + j + 1] + = (data_a[i ]-' 0 ') * (Data_b[j]-' 0 '); The first bit is reserved for the save character carry}//processing carry for (int index = len_a + Len_b + 2-1; index >= 0; index--) {if (Int_res[index] >=) {int_re S[index-1] + = Int_res[index]/10;int_res[index] = Int_res[index]% 10;}} Int j = 0, I = 0;while (int_res[j] = = 0)//Find position not starting at 0 {j + +;} if (negative) {str_res[i++] = '-';} The int_res array is from 0-len_a + Len_b//str_res is removed from the symbol bit start to Len_a + Len_b + 1for (; i < (len_a + Len_b + 1) && J < (le N_a + Len_b); i++, J + +) {Str_res[i] = int_res[j] + ' 0 ';} Str_res[len_a + Len_b + 1] = ';d elete[] Int_res;return str_res;}; void Main () {Bigdatamutliplie Data;char *data_a = " -9999999999999"; char *data_b = "999"; Char *str_res;data.init_data ( Data_a, data_b); str_res = Data.multiplie_ab (); if (str_res) {std::cout << data_a << "*" << data_b &LT;&L T "=" << str_res << std::endl;delete[] str_res;} data_a = " -87654321";d ata_b = "+12345678";d ata.init_data (Data_a, data_b); str_res = Data.multiplie_ab (); if (str_res) { Std::cout << data_a << "*" << data_b << "=" << Data.multiplie_ab () << Std::endl;de Lete[] str_res;} data_a = " -314123123123123";d ata_b = " -64356343653564";d ata.init_data (Data_a, data_b); str_res = Data.multiplie_ab ();  if (str_res) {std::cout << data_a << "*" << data_b << "=" << Data.multiplie_ab () << Std::endl;delete[] str_res;} System ("Pause");}



C + + implements Big Data multiplication

Related Article

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.