大資料乘法

來源:互聯網
上載者:User

標籤:

一:代碼實現
//大資料乘法運算#include <stdio.h>#include <stdlib.h>#include <string.h>int BigDataMul(char *num01, char *num02){int res = 0, i = 0, j = 0;int length01 = 0, length02 = 0, totallength = 0;if(NULL == num01 || NULL == num02) {res = -1;return res;}length01 = strlen(num01);length02 = strlen(num02);totallength = length01 + length02;int *presult = (int *)malloc(sizeof(int) * totallength);memset(presult, 0, sizeof(int) * totallength);if(NULL == presult) {res = -2;return res;}//累乘for(i = 0; i < length01; i++){for(j = 0; j < length02; j++) {//將第一位空出來,以防產生進位,注意這個第0位置,表示最高位presult[i + j + 1] += (num01[i] - ‘0‘) * (num02[j] - ‘0‘);}}//累加 倒序for(i = totallength - 1; i >= 0; i--) {//大於10 表示產生進位if(presult[i] >= 10) {presult[i-1] += presult[i] / 10;//取出進位presult[i] %= 10;//取出個位元}}i = 0;//去除前面的0,因為可能沒有產生進位,前面的可能是0while(0 == presult[i]) {i++;}//可能與0相乘的話,結果全為0if(i == totallength + 1) {printf("0\n");goto IsZero;}char *plastresult = (char *)malloc(sizeof(char) * totallength);memset(plastresult, 0, sizeof(char) * totallength);if(NULL == plastresult) {res = -2;return res;}for(j = 0; i < totallength; i++, j++) {plastresult[j]= presult[i] + ‘0‘;}for(i = 0; i < totallength; i++) {printf("%c",plastresult[i]);}free(plastresult);plastresult = NULL;IsZero:free(presult);presult = NULL;return res;}int main(int argc, char *argv[]){BigDataMul("22222","9");return 0;}

  

  

 

大資料乘法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.