C++實現大資料乘法

來源:互聯網
上載者:User

標籤:大資料   乘法   

1.測試環境 vs2013 windows 7

採用累乘的方式實現,然後再統一的進位,最後將其在轉換為字串,輸出。

程式碼:

#define _CRT_SECURE_NO_WARNINGS#include<iostream>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)];//兩個數相乘最大不會超過兩個數的個數相加99*99char  * str_res = new char[(len_a + len_b + 2)*sizeof(char)]; //多申請兩個字元的空間 一個存符號,一個存'\0'memset(str_res, 0, (len_a + len_b + 2)*sizeof(char));memset(int_res, 0, (len_a + len_b)*sizeof(int));//採取累乘的方式然後再統一進位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'); //第一位預留出來用於儲存符進位}//處理進位for (int index = len_a + len_b + 2 - 1; index >= 0; index--){if (int_res[index] >= 10){int_res[index - 1] += int_res[index] / 10;int_res[index] = int_res[index] % 10;}}int j = 0, i = 0;while (int_res[j] == 0) // 找到開始不為0的位置{j++;}if (negative){str_res[i++] = '-';}//int_res 數組是從0-len_a + len_b //str_res 是從除去符號位開始到len_a + len_b + 1for (; i < (len_a + len_b + 1) && j < (len_a + len_b); i++, j++){str_res[i] = int_res[j] + '0';}str_res[len_a + len_b + 1] = '\0';delete[] 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 << " = " << str_res << std::endl;delete[] str_res;}data_a = "-87654321";data_b = "+12345678";data.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;}data_a = "-314123123123123";data_b = "-64356343653564";data.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++實現大資料乘法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.