大数相乘

来源:互联网 发布:聚友网络客服电话郑州 编辑:程序博客网 时间:2024/06/01 10:59
#include <iostream>#include <string>using namespace std;class Mul{public:    Mul(){}    ~Mul()    {        delete m_ipX;        delete m_ipY;        delete m_ipOut;    }    void Input()    {        string x,y;        cin>>x;        cin>>y;        m_iXLen=x.length();        m_iYLen=y.length();        m_ipX=new int(m_iXLen);        m_ipY=new int(m_iYLen);        m_ipOut=new int(m_iXLen+m_iYLen);        for(int i=0;i<m_iXLen;i++)        {            m_ipX[m_iXLen-1-i]=x[i]-'0';        }        for(int i=0;i<m_iYLen;i++)        {            m_ipY[m_iYLen-1-i]=y[i]-'0';        }        for(int i=0;i<m_iXLen+m_iYLen;i++)        {            m_ipOut[i]=0;        }    }    void cal()    {        for(int i=0;i<m_iYLen;i++)        {            for(int j=0;j<m_iXLen;j++)            {                m_ipOut[i+j]+=m_ipX[j]*m_ipY[i];            }        }        for(int i=0;i<m_iYLen+m_iXLen;i++)        {            if(m_ipOut[i]>=10)            {                m_ipOut[i+1]+=m_ipOut[i]/10;                m_ipOut[i]%=10;            }        }    }    void print()    {        cout<<"X: ";        for(int i=0;i<m_iXLen;i++)        {            cout<<m_ipX[m_iXLen-1-i];        }        cout<<endl<<"Y: ";        for(int i=0;i<m_iYLen;i++)        {            cout<<m_ipY[m_iYLen-1-i];        }        cout<<endl<<"Out: ";        for(int i=m_iXLen+m_iYLen-1;i>=0;i--)        {            cout<<m_ipOut[i];        }        cout<<endl;    }private:    int* m_ipX;    int* m_ipY;    int m_iXLen;    int m_iYLen;    int* m_ipOut;};int main(){    Mul mul;    mul.Input();    mul.cal();    mul.print();    return 0;}
0 0
原创粉丝点击