大数相乘
相乘的两个数是字符串格式的,所以,除了内存限制外,理论上可以支持无限大的数相乘。
计算方法:对于两数A和B,数B的每一位分别和数A相乘,再将乘积累加。
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void MultiplyBigData(const char *pData1, const char *pData2, char *pResult, int nLen);
int main()
{
int nData1Len = 100;
int nData2Len = 100;
int nResultLen = nData1Len + nData2Len;
char *pszData1 = (char*)malloc(nData1Len);
char *pszData2 = (char*)malloc(nData2Len);
char *pszResult = (char*)malloc(nResultLen);
memset(pszData1, 0, nData1Len);
memset(pszData2, 0, nData2Len);
memset(pszResult, 0, nResultLen);
printf("Please Input Data1:");
scanf("%s", pszData1);
printf("Please Input Data2:");
scanf("%s", pszData2);
MultiplyBigData(pszData1, pszData2, pszResult, nResultLen);
printf("Data1 * Data2 = %s * %s = %s \n\n", pszData1, pszData2, pszResult);
free(pszData1);
free(pszData2);
free(pszResult);
return 0;
}
/* 字符串反转 */
void ReserseString(const char *pStr, char *pDesStr)
{
int i = 0;
int nStrLen = strlen(pStr);
for (i=0; i<nStrLen; i++)
{
*(pDesStr+i) = *(pStr+nStrLen-i-1);
}
*(pDesStr+i) = 0;
}
/* 数字字符 转为 数字 */
byte* StringToData(char *pszData)
{
int nLen = strlen(pszData);
int i = 0;
byte *pDataTemp = (byte*)pszData;
for (i=0; i<nLen; i++)
{
*pDataTemp = *pDataTemp - '0';
pDataTemp++;
}
return (byte*)pszData;
}
/* 数字 转为 数字字符 */
char* DataToString(byte *pucData, int nBuffLen)
{
int nDataLen = nBuffLen;
while (*(pucData+nDataLen-1) == 0)
{
nDataLen--;
}
int i = 0;
byte *pDataTemp = pucData;
for (i=0; i<nDataLen; i++)
{
*pDataTemp = *pDataTemp + '0';
pDataTemp++;
}
return (char*)pucData;
}
/* 字符串数相乘 */
void MultiplyBigData(const char *pszData1, const char *pszData2, char *pszResult, int nBuffLen)
{
byte *pucResult = (byte*)malloc(nBuffLen);
int nData1Len = strlen(pszData1);
int nData2Len = strlen(pszData2);
byte *pucData1 = (byte*)malloc(nData1Len+1);
byte *pucData2 = (byte*)malloc(nData2Len+1);
int n1 = 0;
int n2 = 0;
int nMul = 0;
int *pnMulRes = (int*)malloc(nBuffLen);
memset(pucResult, 0, nBuffLen);
memset(pnMulRes, 0, nBuffLen);
ReserseString(pszData1, (char*)pucData1); // 高位在后面,方便计算时向高位扩展
ReserseString(pszData2, (char*)pucData2);
pucData1 = StringToData((char*)pucData1);
pucData2 = StringToData((char*)pucData2);
/*两数相乘,且按位相加*/
for (n2=0; n2<nData2Len; n2++)
{
for (n1=0; n1<nData1Len; n1++)
{
nMul = pucData1[n1]* pucData2[n2];
pnMulRes[n2+n1] += nMul%10; /*值可能超过10*/
pnMulRes[n2+n1+1] += nMul/10;
}
}
/*每位数字范围调整为0-9*/
int i = 0;
int nResLen = n2+n1;
for (i=0; i<nResLen; i++)
{
pucResult[i] = pnMulRes[i] % 10;
pnMulRes[i+1] += pnMulRes[i] / 10;
}
DataToString(pucResult, nBuffLen);
ReserseString((char*)pucResult, pszResult);
free(pnMulRes);
free(pucResult);
}
#include <stdlib.h>
#include <windows.h>
void MultiplyBigData(const char *pData1, const char *pData2, char *pResult, int nLen);
int main()
{
int nData1Len = 100;
int nData2Len = 100;
int nResultLen = nData1Len + nData2Len;
char *pszData1 = (char*)malloc(nData1Len);
char *pszData2 = (char*)malloc(nData2Len);
char *pszResult = (char*)malloc(nResultLen);
memset(pszData1, 0, nData1Len);
memset(pszData2, 0, nData2Len);
memset(pszResult, 0, nResultLen);
printf("Please Input Data1:");
scanf("%s", pszData1);
printf("Please Input Data2:");
scanf("%s", pszData2);
MultiplyBigData(pszData1, pszData2, pszResult, nResultLen);
printf("Data1 * Data2 = %s * %s = %s \n\n", pszData1, pszData2, pszResult);
free(pszData1);
free(pszData2);
free(pszResult);
return 0;
}
/* 字符串反转 */
void ReserseString(const char *pStr, char *pDesStr)
{
int i = 0;
int nStrLen = strlen(pStr);
for (i=0; i<nStrLen; i++)
{
*(pDesStr+i) = *(pStr+nStrLen-i-1);
}
*(pDesStr+i) = 0;
}
/* 数字字符 转为 数字 */
byte* StringToData(char *pszData)
{
int nLen = strlen(pszData);
int i = 0;
byte *pDataTemp = (byte*)pszData;
for (i=0; i<nLen; i++)
{
*pDataTemp = *pDataTemp - '0';
pDataTemp++;
}
return (byte*)pszData;
}
/* 数字 转为 数字字符 */
char* DataToString(byte *pucData, int nBuffLen)
{
int nDataLen = nBuffLen;
while (*(pucData+nDataLen-1) == 0)
{
nDataLen--;
}
int i = 0;
byte *pDataTemp = pucData;
for (i=0; i<nDataLen; i++)
{
*pDataTemp = *pDataTemp + '0';
pDataTemp++;
}
return (char*)pucData;
}
/* 字符串数相乘 */
void MultiplyBigData(const char *pszData1, const char *pszData2, char *pszResult, int nBuffLen)
{
byte *pucResult = (byte*)malloc(nBuffLen);
int nData1Len = strlen(pszData1);
int nData2Len = strlen(pszData2);
byte *pucData1 = (byte*)malloc(nData1Len+1);
byte *pucData2 = (byte*)malloc(nData2Len+1);
int n1 = 0;
int n2 = 0;
int nMul = 0;
int *pnMulRes = (int*)malloc(nBuffLen);
memset(pucResult, 0, nBuffLen);
memset(pnMulRes, 0, nBuffLen);
ReserseString(pszData1, (char*)pucData1); // 高位在后面,方便计算时向高位扩展
ReserseString(pszData2, (char*)pucData2);
pucData1 = StringToData((char*)pucData1);
pucData2 = StringToData((char*)pucData2);
/*两数相乘,且按位相加*/
for (n2=0; n2<nData2Len; n2++)
{
for (n1=0; n1<nData1Len; n1++)
{
nMul = pucData1[n1]* pucData2[n2];
pnMulRes[n2+n1] += nMul%10; /*值可能超过10*/
pnMulRes[n2+n1+1] += nMul/10;
}
}
/*每位数字范围调整为0-9*/
int i = 0;
int nResLen = n2+n1;
for (i=0; i<nResLen; i++)
{
pucResult[i] = pnMulRes[i] % 10;
pnMulRes[i+1] += pnMulRes[i] / 10;
}
DataToString(pucResult, nBuffLen);
ReserseString((char*)pucResult, pszResult);
free(pnMulRes);
free(pucResult);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
posted on 2016-09-11 14:08 Waaaaaall-E 阅读(176) 评论(0) 编辑 收藏 举报