天天看點

[算法題] 大數相乘

[算法題] 大數相乘
[算法題] 大數相乘
#include "oj.h"
#include <stdio.h>
#include <string>
#define OK     0
#define ERROR -1

/* 函數聲明 */
void calc1(char* pcStrA, int iLenA, int* piTmp, int num); 
void accumulate(int iIndex, int *piResult, int iLenResult, int *piTemp, int iLenTemp);
char* BignumMultiply(char* pcNumA,int iLenA,char* pcNumB,int iLenB,char* pcResult,int iLenResult); 

/*=============================================================== 
            調用calc1和accumulate函數計算大數相乘 
===============================================================*/
char* BignumMultiply
(
    char* pcNumA,
    int iLenA,
    char* pcNumB,
    int iLenB,
    char* pcResult,
    int iLenResult
) 
{ 
    int i     = 0;
    int j     = 0;
    int num   = 0;
    int index = 0;
    int *piTmp    = NULL;
    int *piResult = NULL;
    
    /* 配置設定臨時結果的存放空間 */ 
    piTmp=(int*)malloc((iLenA+1)*sizeof(int)); 
    piResult=(int*)malloc(iLenResult*sizeof(int)); 
    memset(piTmp, 0, (iLenA+1)*sizeof(int));
    memset(piResult, 0, iLenResult*sizeof(int));

    for (i = iLenB - 1; i>=0; i--)
    { 
        /* 擷取乘數pcNumB中第i位的值 */
        num = pcNumB[i] - '0'; 
        
        /* 計算被乘數與第i位的乘積,結果儲存在piTmp整型數組中 */ 
        calc1(pcNumA,iLenA,piTmp,num); 
        
        /* 将piTmp數組中的值加到piResult數組中 */
        index++;
        accumulate(index,piResult,iLenResult,piTmp,iLenA+1); 
    } 
    printf("\n%s\n", pcResult);

    /* 去掉piResult中第一個非零數字前的零 */
    i = 0;
    while (piResult[i++]==0); 
    
    /* 将整形數組piResult中的值轉化成字元串存入pcResult中 */ 
    index = 0; 
    for (j = i - 1; j < iLenResult; j++, index++)
        pcResult[index] = piResult[j] + '0';
    if (iLenResult == i - 1)
    {
        pcResult[1] = '\0'; 
    }
    else
    {
        pcResult[index] = '\0'; 
    }
    
    free(piTmp); 
    free(piResult); 
    return pcResult;
} 

/*===============================================================
                計算被乘數與乘數的某一位的乘積 
===============================================================*/ 
void calc1
(
    char *pcStrA, 
    int iLenA, 
    int *piTmp, 
    int num
) 
{ 
    /* d兩個位的乘積結果,remainder餘數,carry進位 */ 
    int i         = 0;
    int result    = 0;
    int remainder = 0;
    int carry     = 0; 
    
    /* 從被乘數字元串'\0'的前一位算起 */ 
    for (i = iLenA - 1; i >= 0; i--) 
    { 
        result = pcStrA[i] - '0';
        result *= num; 
        remainder = (result + carry) % 10;
        carry = (result + carry) / 10; 
        piTmp[i+1] = remainder; 
    } 
    if (carry) 
        piTmp[0] = carry;
    else 
        piTmp[0] = 0; 
} 

/*=============================================================== 
将被乘數與乘數中一位數字的乘積結果計入res數組中 
==============================================================*/ 
void accumulate
(
    int iIndex,
    int *piResult,
    int iLenResult,
    int *piTemp,
    int iLenTemp
) 
{ 
    int i = 0;
    int j = 0;
    int m = 0;
    int n = 0;
    int remainder = 0;    //餘數
    static int carry=0; 
    for (j = iLenTemp - 1, i = 0; j >= 0; j--, i++) 
    { 
        m = piTemp[j]; 
        n = piResult[iLenResult-iIndex-i]; 
        if (m + n + carry >= 10)
        { 
            remainder = (m + n + carry) % 10; 
            carry = 1;
        }
        else 
        { 
            remainder = m + n +carry; 
            carry = 0; 
        }
        piResult[iLenResult - iIndex - i] = remainder; 
    } 
}


/*****************************************************************************
 Prototype    : multiply
 Description  : 兩個任意長度的長整數相乘, 輸出結果
 Input Param  : 
                const std::string strMultiplierA  乘數A
                const std::string strMultiplierB  乘數B
 Output       : 
                std::string strRst            乘法結果
 Return Value : 
                int                       0  正确  
                                         -1  異常
*****************************************************************************/
int multiply (const std::string strMultiplierA,const std::string strMultiplierB, std::string &strRst) 
{
    int i = 0;
    int j = 0;
    int lenA = 0;
    int lenB = 0;
    int lenResult = 0;
    char *pcNumA = NULL; 
    char *pcNumB = NULL;
    char *pcResult = NULL; /* 計算兩個字元串的長度,及存儲結果所需要的空間 */ 

    lenA = (int)strMultiplierA.length();
    lenB = (int)strMultiplierB.length(); 
    if (0 == lenA || 0 == lenB)
    {
        return ERROR;
    }

    pcNumA = (char*)strMultiplierA.c_str();
    pcNumB = (char*)strMultiplierB.c_str();
    lenResult = lenA + lenB + 1; /* 配置設定并初始化字元串數組 */
    pcResult = (char*)malloc(lenResult * sizeof(char)); 
    memset(pcResult, 0, lenResult);
    for (i = 0; i < lenResult-1; i++) 
        *(pcResult + i) = '0'; /* 計算并輸出計算結果 */

    printf("The result is: %s",BignumMultiply(pcNumA,lenA,pcNumB,lenB,pcResult,lenResult));
    for (i = 0; i < (int)strlen(pcResult); i++)
    {
        strRst += pcResult[i];
    }
    //printf("\n%s\n", pcResult);
    free(pcResult);
    return OK;
}      

View Code

繼續閱讀