无语
勤奋,踏实,在生活中学习,在学习中快乐……

    用C++写了个大数相乘,下面是源码:

/* high precision mutiply */

#include
<iostream>
#include
<string>
#include
<iomanip>
class HighMutiply{
    
private:
        
const int MAX;
        
int *_mutiplier;
        
int *_mutipliee;
        
int *_result;
        
int _lenlier;
        
int _lenliee;
        
int _lenResult;
    
public:
        HighMutiply(
int max=1000)
        :MAX(max),_lenliee(
0),_lenlier(0),_lenResult(0)
        {
            _mutiplier 
= new int[MAX];
            _mutipliee 
= new int[MAX];
            _result 
= new int[MAX * MAX];
        }

//将输入字符串按4位一组转化为整型数组
        void stringToInt(char *s1,char *s2)
        {
            
char temp[5];
            
int len_s1= strlen(s1);
            
int len_s2= strlen(s2);
            
while(len_s1>4)
            {
                strncpy(temp,
&s1[len_s1-4],4);
                temp[
5]='\0';
                _mutiplier[_lenlier
++]=atoi(temp);
                len_s1 
-= 4;
                s1[len_s1]
='\0';    
            }
            
if(len_s1>0)
                _mutiplier[_lenlier
++]=atoi(s1);
                
            
while(len_s2>4)
            {
                strncpy(temp,
&s2[len_s2-4],4);
                temp[
5]='\0';
                _mutipliee[_lenliee
++]=atoi(temp);
                len_s2 
-= 4;
                s2[len_s2]
='\0';    
            }
            
if(len_s2>0)
                _mutipliee[_lenliee
++]=atoi(s2);
        }

//计算,结果存入_result[]中
        void Calculation()
        {
            
int temp,carry;
            
int i,j,count;
            
for(i=0;i<MAX*MAX;i++)
            {
                _result[i]
=0;
            }
            
for(i = 0, carry = 0; i < _lenliee; i++ )
            {
                
                
for( j = 1, count = j+i-1; j <= _lenlier; j++)
                {
                    temp 
= _mutipliee[i] * _mutiplier[j-1+ carry + _result[count];
                    _result[count
++=  temp % 10000;
                    carry 
= temp / 10000;//存储进位
                }
                
while( carry )
                {
                    _result[count
+++= (carry % 10000);
                    carry 
= carry / 10000;
                }
            }
            _lenResult
=count;        
        }
        
void display()
        {
            
int temp,j;
            cout 
<< _mutiplier[_lenlier-1];
            
for(int i = _lenlier-2; i >= 0; i--)
            {
                
if(_mutiplier[i] == 0)
                {
                    cout
<<"0000";
                }
                
else
                {
                    
for(j=4,temp=_mutiplier[i];temp;j--,temp/=10)
                    {}
                    
while(-->= 0)
                    {
                        cout
<<"0";
                    }
                    cout 
<< _mutiplier[i];
                }
            }
            cout 
<< " * " << _mutipliee[_lenliee-1];
            
            
for(int i = _lenliee-2; i >= 0; i--)
            {
                
if(_mutipliee[i] == 0)
                {
                    cout
<<"0000";
                }
                
else
                {
                    
for(j=4,temp=_mutipliee[i];temp;j--,temp/=10)
                    {}
                    
while(-->= 0)
                    cout
<<"0";
                    cout 
<< _mutipliee[i];
                }
            }
            cout 
<< " =" << endl << _result[_lenResult-1];
            
for(int i = _lenResult-2; i >= 0; i--)
            {
                
if( _result[i] == 0 )
                {
                    cout
<<"0000";
                }
                
else
                {
                    
for(j=4,temp=_result[i];temp;j--,temp/=10)
                    {}
                    
while(-->= 0)
                        cout
<<"0";
                    cout 
<< _result[i];
                }
            }
            cout
<<endl;
        }
        
~HighMutiply()
        {
            delete[] _mutipliee;
            delete[] _mutiplier;
            delete[] _result;
        }
};

int main()
{
    
string s1,s2;
    cin
>>s1>>s2;
    
int l1=s1.length();
    
int l2=s2.length();
    
char *c1=new char[l1];
    
char *c2=new char[l2];
    strcpy(c1,s1.c_str());
    strcpy(c2,s2.c_str());
    
    HighMutiply hi;
     hi.stringToInt(c1,c2);    
    hi.Calculation();
    hi.display();
    
    delete[] c1;
    delete[] c2;
    
    
return 0;
}


    本来输入字符串想使用string类,但在VC6.0中运行老出现错误,查了MSDN后,发现是VC中存在的固有错误,后来将其改为C风格的字符串了。 程序在C-Free4.0中编译通过。
posted on 2008-04-07 14:10  程、诚、成  阅读(5794)  评论(0编辑  收藏  举报