string numAdd(const string& strA, const string& strB)
{
//转换
//////////////////////////////////////////////////////////////////////////
int nLenA = strA.length();
int nLenB = strB.length();
//数据最大位数
int nLenSum = nLenA > nLenB ? nLenA : nLenB;
//由高位相加后可能会向前进一位
++nLenSum;
size_t sz = nLenSum * sizeof(short);
//给分配相同大小(位数)的数据,方便对齐计算
short* pSum = (short*)malloc(sz); memset(pSum,0,sz); //计算结果数组
short* pA = (short*)malloc(sz); memset(pA,0,sz); //加数A数组
short* pB = (short*)malloc(sz); memset(pB,0,sz); //加数B数组
//按数组下标从大到小(数据位数从低到高)转换。
for (int i = nLenA-1, j = nLenSum - 1; i>=0; i--,j--)
{
pA[j] = strA[i] - '0';
}
for (int i = nLenB-1, j = nLenSum - 1; i>=0; i--,j--)
{
pB[j] = strB[i] - '0';
}
//计算
//////////////////////////////////////////////////////////////////////////
for (int j = nLenSum - 1; j > 0; j--)
{
//计算
pSum[j] += (pA[j] + pB[j])%10;
//向前进位
pSum[j-1] += (pA[j] + pB[j])/10;
}
//转换
//////////////////////////////////////////////////////////////////////////
char* pszSum = (char*)malloc(sizeof(char) * (nLenSum + 1));
int nFlag = 0;
//去除前置0
for (; nFlag<nLenSum; nFlag++)
{
if(pSum[nFlag] != 0)
break;
}
int nszFlag = 0;
//转换
for (; nFlag < nLenSum; nFlag++, nszFlag++)
{
pszSum[nszFlag] = pSum[nFlag] + '0';
}
pszSum[nszFlag] = '\0';
string strSum(pszSum);
//释放资源
free(pA);
free(pB);
free(pSum);
free(pszSum);
return strSum;
}