最近接触C++语言,看到大数乘法的题,自己动手写一写,为日后有用做个笔记
其实基本思路还是和我们笔算的方法一致,只是笔算把相乘的结果都罗列出来(错位),然后再相加,考虑进位问题;在程序中我们直接在两个for循环中处理相乘结果相加的问题,再处理进位问题,最后考虑一点就是结果是0与开头可能是0的情况即可,同时注意对入参做合法性判断
觉得比较好的博客罗列如下:
【转】https://blog.csdn.net/u010155023/article/details/47422319
strlen(dataA) = 3
strlen(dataB) = 1
call calloc success!
call multiply success!
strResult:0
equal!
strlen(dataA) = 0
strlen(dataB) = 3
param wrong!
call multiply failed!
strResult:
equal!
strlen(dataA) = 3
strlen(dataB) = 3
call calloc success!
result:271272
strRst:271272
call multiply success!
strResult:271272
equal!
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
/*FUNC*/
int multiply(const std::string strMultiplierA, const std::string strMultiplierB, std::string& strRst)
{
const char* dataA = strMultiplierA.c_str();
const char* dataB = strMultiplierB.c_str();
int dataALen = strlen(dataA);
int dataBLen = strlen(dataB);
std::cout << "strlen(dataA) = " << dataALen << std::endl;
std::cout << "strlen(dataB) = " << dataBLen << std::endl;
/*PARAM CHECK*/
if((0 == dataALen) || (0 == dataBLen))
{
std::cout << "param wrong!" << std::endl;
return (-1);
}
char result[100] = {'\0'};
int* tempResult = (int*)calloc((dataALen + dataBLen), sizeof(int));
if(NULL == tempResult)
{
std::cout << "call calloc failed!" << std::endl;
return (-1);
}
std::cout << "call calloc success!" << std::endl;
/*MULTIPLY*/
int i = 0;
int j = 0;
for(i = 0; i < dataALen; i++)
{
for(j = 0; j < dataBLen; j++)
{
tempResult[i + j + 1] = tempResult[i + j + 1] + (dataA[i] - '0') * (dataB[j] - '0');
}
}
/*PROCESS*/
int k = 0;
for(k = dataALen + dataBLen - 1; k >= 1; k--)
{
if(tempResult[k] >= 10)
{
tempResult[k - 1] = tempResult[k - 1] + tempResult[k] / 10;
tempResult[k] = tempResult[k] % 10;
}
}
/*ABANDON '0' IN THE HEAD*/
for(i = 0; i < dataALen + dataBLen; i++)
{
if(tempResult[i] != 0)
{
break;
}
}
if(i == (dataALen + dataBLen))
{
strRst = "0";
free(tempResult);
return (0);
}
j = 0;
for(j = 0; i < dataALen + dataBLen; i++, j++)
{
result[j] = tempResult[i] + '0';
}
std::cout << "result:" << result << std::endl;
strRst = result;
std::cout << "strRst:" << strRst << std::endl;
free(tempResult);
return (0);
}
/*TEST FUNC*/
void TestCase01(void)
{
int iRet = 0;
std::string sExpectedResult("0");
std::string strResult = "";
iRet = multiply("003", "0", strResult);
if(0 == iRet)
{
std::cout << "call multiply success!" << std::endl;
}
else
{
std::cout << "call multiply failed!" << std::endl;
}
std::cout << "strResult:" << strResult << std::endl;
if(sExpectedResult == strResult)
{
std::cout << "equal!" << std::endl;
}
else
{
std::cout << "not equal!" << std::endl;
}
return;
}
void TestCase02(void)
{
int iRet = 0;
std::string sExpectedResult("");
std::string strResult = "";
iRet = multiply("", "004", strResult);
if(0 == iRet)
{
std::cout << "call multiply success!" << std::endl;
}
else
{
std::cout << "call multiply failed!" << std::endl;
}
std::cout << "strResult:" << strResult << std::endl;
if(sExpectedResult == strResult)
{
std::cout << "equal!" << std::endl;
}
else
{
std::cout << "not equal!" << std::endl;
}
return;
}
void TestCase03(void)
{
int iRet = 0;
std::string sExpectedResult("271272");
std::string strResult = "";
iRet = multiply("762", "356", strResult);
if(0 == iRet)
{
std::cout << "call multiply success!" << std::endl;
}
else
{
std::cout << "call multiply failed!" << std::endl;
}
std::cout << "strResult:" << strResult << std::endl;
if(sExpectedResult == strResult)
{
std::cout << "equal!" << std::endl;
}
else
{
std::cout << "not equal!" << std::endl;
}
return;
}
/*mainFunc*/
int main(void)
{
TestCase01();
TestCase02();
TestCase03();
return (0);
}