//--------------------------------------------------------------------------- #ifndef CCalMulityResultH #define CCalMulityResultH //--------------------------------------------------------------------------- #include<string> #include<deque> #include<math.h> using namespace std; class CCalMulityResult { public: CCalMulityResult(){}; void CalMulityResult(const string &sFirst,const string &sSecond,string &sReslult,int &iSumvalue); ~CCalMulityResult(){}; private: void InitValue(); void MulityTwoValue(); void GetMulityResult(); string m_sFirst; string m_sSecond; int m_iPowerTen; int m_MultValue; deque<int> m_dSrcFirstValue; deque<int> m_dSrcSecondValue; deque<int> m_dMultValue; }; #endif
//--------------------------------------------------------------------------- #pragma hdrstop #include "CCalMulityResult.h" //--------------------------------------------------------------------------- void CCalMulityResult::CalMulityResult(const string &sFirst,const string &sSecond,string &sReslult,int &iSumvalue) { deque<int>().swap(m_dSrcFirstValue); deque<int>().swap(m_dSrcSecondValue); deque<int>().swap(m_dMultValue); m_sFirst = sFirst; m_sSecond = sSecond; InitValue(); MulityTwoValue(); iSumvalue = 0; for(int iIndex = 0; iIndex < m_dMultValue.size(); iIndex ++) { char cTemp = m_dMultValue[iIndex] + '0'; sReslult = cTemp + sReslult; iSumvalue = iSumvalue + m_dMultValue[iIndex]; } /*for(int iIndex = 0; iIndex < sReslult.size() / 2; iIndex ++) { char c = sReslult[iIndex]; sReslult[iIndex] = sReslult[sReslult.size() - 1 - iIndex]; sReslult[sReslult.size() - 1 - iIndex] = c; } */ } /*** 初始化参数 ***/ void CCalMulityResult::InitValue() { for(int iIndex = 0; iIndex < m_sFirst.size(); iIndex ++) { m_dSrcFirstValue.push_front(m_sFirst[iIndex] - '0'); } for(int iIndex = 0; iIndex < m_sSecond.size(); iIndex ++) { m_dSrcSecondValue.push_front(m_sSecond[iIndex] - '0'); } } /*** 计算两个数相乘 ***/ void CCalMulityResult::MulityTwoValue() { for(int iIndex = 0; iIndex < m_dSrcSecondValue.size(); iIndex ++) { m_iPowerTen = iIndex; m_MultValue = m_dSrcSecondValue[iIndex]; GetMulityResult(); } } /*** 第二个数的每一位与第一个数相乘 ***/ void CCalMulityResult::GetMulityResult() { deque<int> dTempMulityResult; //相乘 for(int iIndex = 0; iIndex < m_dSrcFirstValue.size(); iIndex ++) { int iMultValue = m_dSrcFirstValue[iIndex] * m_MultValue; dTempMulityResult.push_back(iMultValue); } //取模 for(int iIndex = 0; iIndex < dTempMulityResult.size(); iIndex ++) { if(!(dTempMulityResult[iIndex] < 10)) { int iAddValue = dTempMulityResult[iIndex] / 10; dTempMulityResult[iIndex] = dTempMulityResult[iIndex] % 10; if(iIndex + 1 == dTempMulityResult.size()) { dTempMulityResult.push_back(iAddValue); } else { dTempMulityResult[iIndex + 1 ] = dTempMulityResult[iIndex + 1 ] + iAddValue; } } } //填充之前的0 for(int iIndex = 0; iIndex < m_iPowerTen; iIndex ++) { dTempMulityResult.push_front(0); } //求和 for(int iIndex = 0; iIndex < dTempMulityResult.size(); iIndex ++) { if(m_dMultValue.size() != iIndex) { m_dMultValue[iIndex] = m_dMultValue[iIndex] + dTempMulityResult[iIndex]; } else { m_dMultValue.push_back(dTempMulityResult[iIndex]); } } //求和结果取模 for(int iIndex = 0; iIndex < m_dMultValue.size(); iIndex ++) { if(!(m_dMultValue[iIndex] < 10)) { int iAddValue = m_dMultValue[iIndex] / 10; m_dMultValue[iIndex] = m_dMultValue[iIndex] % 10; if(iIndex + 1 == m_dMultValue.size()) { m_dMultValue.push_back(iAddValue); } else { m_dMultValue[iIndex + 1 ] = m_dMultValue[iIndex + 1 ] + iAddValue; } } } } #pragma package(smart_init)