// BigMult.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <time.h>
#define SZBUF_MAX 1048576
int Calculate(char *pRes, const char *pAryA, const char *pAryB);
void AryMul(int *pRes, const int *pAry, const int nNum);
void AryAdd(int *pRes, const int *pA, const int *pB);
void AryAdd(int *pA, const int *pB);
void CarrySet(int *pAry, const int nEnd);
int AryLen(const int *pAry, const int nEnd);
void AryInv(int *pAry, const int nEnd);
void AryCpy(int *pDest, const int *pSrc, const int nEnd);
void StrToAry(int *pAry, const char *pStr, const int nEnd);
void AryToStr(char *pStr, const int *pAry, const int nEnd);
bool IsNum(const char *pStr);
int StrCmp(const char *pStr1, const char *pStr2);
int StrToNum(const char *pStr);
int StrLen(const char *pStr);
int main(int argc, char* argv[])
{
char *szBufA = new char[SZBUF_MAX];
char *szBufB = new char[SZBUF_MAX];
char *szRes = new char[SZBUF_MAX];
char szSel[256] = "";
char szNum[256] = "";
char szCnt[256] = "";
while(true)
{
cout<<"1.正常乘法"<<endl;
cout<<"2.指定个数的数字相乘"<<endl;
cout<<"0.退出"<<endl;
cin>>szSel;
if(0 == StrCmp(szSel, "0"))
{
break;
}
if(0 == StrCmp(szSel, "1"))
{
while(true)
{
cout<<"请输入被乘数: ";
cin>>szBufA;
cout<<"请输入乘数: ";
cin>>szBufB;
if(IsNum(szBufA) && IsNum(szBufB))
{
break;
}
}
}
else if(0 == StrCmp(szSel, "2"))
{
while(true)
{
cout<<"请输入数字: ";
cin>>szNum;
cout<<"请输入个数: ";
cin>>szCnt;
if(IsNum(szNum) && IsNum(szCnt))
{
if(StrToNum(szCnt) > 500000)
{
cout<<"你等不起!"<<endl;
continue;
}
break;
}
}
int nCount = StrToNum(szCnt);
char ch = szNum[0];
cout<<szCnt<<"个"<<ch<<"乘"<<szCnt<<"个"<<ch<<endl;
for(int i = 0; i < nCount; i++)
{
szBufA[i] = ch;
szBufB[i] = ch;
}
szBufA[i] = '\0';
szBufB[i] = '\0';
}
else
{
continue;
}
int nTime = Calculate(szRes, szBufA, szBufB);
FILE *fp = fopen("运算记录.txt", "ab+");
if(NULL != fp)
{
fprintf(fp, "计算: %s*%s\r\n长度: %d位数\r\n耗时: %d毫秒\r\n结果: %s\r\n\r\n", szBufA, szBufB, strlen(szRes), nTime, szRes);
}
fclose(fp);
cout<<"计算完成! "<<endl;
cout<<"结果: ";
cout<<szRes<<endl;
cout<<"长度: "<<StrLen(szRes)<<"位数"<<endl;
cout<<"耗时: "<<nTime<<"毫秒"<<endl;
cout<<endl;
}
delete [] szBufA;
delete [] szBufB;
delete [] szRes;
return 0;
}
int Calculate(char *pRes, const char *pAryA, const char *pAryB)
{
int tBeg = 0;
int tEnd = 0;
tBeg = clock();
int *pA = new int[SZBUF_MAX];
int *pB = new int[SZBUF_MAX];
int *pMulRes = new int[SZBUF_MAX];
int *pAddRes = new int[SZBUF_MAX];
int *pFinalRes = new int[SZBUF_MAX];
pFinalRes[0] = -1;
int *pTempA = pA + SZBUF_MAX - StrLen(pAryA) - 1;
int *pTempB = pB;
StrToAry(pTempA, pAryA, -1);
StrToAry(pB, pAryB, -1);
AryInv(pTempA, -1);
AryInv(pB, -1);
while(-1 != *pTempB)
{
AryMul(pMulRes, pTempA, *pTempB);
AryAdd(pFinalRes, pMulRes);
/*
AryAdd(pAddRes, pFinalRes, pMulRes);
AryCpy(pFinalRes, pAddRes, -1);
*/
pTempA--;
*pTempA = 0;
pTempB++;
}
CarrySet(pFinalRes, -1);
AryInv(pFinalRes, -1);
AryToStr(pRes, pFinalRes, -1);
delete [] pA;
delete [] pB;
delete [] pAddRes;
delete [] pMulRes;
delete [] pFinalRes;
tEnd = clock();
return tEnd - tBeg;
}
void AryMul(int *pRes, const int *pAry, const int nNum)
{
while(-1 != *pAry)
{
*pRes = *pAry * nNum;
pAry++;
pRes++;
}
*pRes = -1;
}
void AryAdd(int *pRes, const int *pA, const int *pB)
{
while(-1 != *pA || -1 != *pB)
{
if(-1 != *pA && -1 != *pB)
{
*pRes = *pA + *pB;
pA++;
pB++;
}
else if(-1 != *pA)
{
*pRes = *pA;
pA++;
}
else
{
*pRes = *pB;
pB++;
}
pRes++;
}
*pRes = -1;
}
void AryAdd(int *pA, const int *pB)
{
while(-1 != *pA || -1 != *pB)
{
if(-1 == *pA)
{
*pA = 0;
*(pA + 1) = -1;
*pA = *pA + *pB;
}
else
{
*pA = *pA + *pB;
}
pA++;
pB++;
}
*pA = -1;
}
void CarrySet(int *pAry, const int nEnd)
{
int nCarry = 0;
while(-1 != *pAry)
{
*pAry = *pAry + nCarry;
nCarry = *pAry/10;
*pAry = *pAry%10;
pAry++;
}
while(nCarry > 9)
{
*pAry = nCarry % 10;
pAry++;
}
if(nCarry > 0)
{
*pAry = nCarry;
pAry++;
}
*pAry = -1;
}
int AryLen(const int *pAry, const int nEnd)
{
int nLen = 0;
while(-1 != *pAry)
{
nLen++;
pAry++;
}
return nLen;
}
void AryInv(int *pAry, const int nEnd)
{
int nLen = AryLen(pAry, -1);
int *pEnd = pAry + nLen - 1;
int nTemp;
if(nLen > 1)
{
do{
nTemp = *pAry;
*pAry = *pEnd;
*pEnd = nTemp;
pAry++;
pEnd--;
}while(pAry < pEnd);
}
}
void AryCpy(int *pDest, const int *pSrc, const int nEnd)
{
if(NULL != pDest && NULL != pSrc)
{
while(nEnd != (*pDest = *pSrc))
{
pDest++;
pSrc++;
}
}
}
void StrToAry(int *pAry, const char *pStr, const int nEnd)
{
while('\0' != *pStr)
{
*pAry = *pStr - '0';
pAry++;
pStr++;
}
*pAry = nEnd;
}
void AryToStr(char *pStr, const int *pAry, const int nEnd)
{
while(nEnd != *pAry)
{
*pStr = *pAry + '0';
pAry++;
pStr++;
}
*pStr = '\0';
}
bool IsNum(const char *pStr)
{
if(NULL != pStr)
{
while('\0' != *pStr)
{
if(*pStr < '0' || *pStr > '9')
{
return false;
}
pStr++;
}
return true;
}
return false;
}
int StrCmp(const char *pStr1, const char *pStr2)
{
if(pStr1 == pStr2)
{
return 0;
}
if(NULL != pStr1 && NULL != pStr2)
{
while(*pStr1 == *pStr2)
{
if('\0' == *pStr1)
{
return 0;
}
pStr1++;
pStr2++;
}
return *pStr1 > *pStr2 ? 1 : -1;
}
return pStr1 > pStr2 ? 1 : -1;
}
int StrToNum(const char *pStr)
{
int nNum = 0;
if(NULL != pStr)
{
bool fNega = false;
if('-' == *pStr)
{
fNega = true;
pStr++;
}
while('\0' != *pStr)
{
if(*pStr > '9' || *pStr < '0')
{
return 0;
}
nNum = nNum * 10 + (*pStr - '0');
pStr++;
}
if(fNega)
{
nNum = 0 - nNum;
}
}
return nNum;
}
int StrLen(const char *pStr)
{
int nLen = 0;
if(NULL != pStr)
{
while('\0' != *pStr)
{
nLen++;
pStr++;
}
}
return nLen;
}
//32768个9乘32768个9: