| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2351 人关注过本帖
标题:自己写的大整数相乘的程序
只看楼主 加入收藏
di494383142
Rank: 1
等 级:新手上路
帖 子:8
专家分:4
注 册:2010-12-20
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:9 
自己写的大整数相乘的程序
偶是新手。。。
#include<stdio.h>                 
#include<string.h>
int main(){
    char w[200],e[200];             // 存输入数
    int x[200],y[200];              // 存数字放
int    val[400],val1[400],sum1,sum2;  // 存放结果,计数等
    int i,j,sum,l1,l2,h,l3;
    gets(w);
    gets(e);
   l1=strlen(w);                 //计算位数
   for(i=0;i<l1;i++){            //字符转换成数字并存入数组     
       x[i]=w[i]-'0';
   }
   sum1=0;
   for(i=l1-1;i>=0;i--){          //计算后到0的个数
       if(x[i]==0)
         sum1++;
       else
           break;
   }
   l2=strlen(e);                  //计算位数
   for(i=0;i<l2;i++){                 //字符转换成数字并存入数组
        y[i]=e[i]-'0';
   }  
   sum2=0;
   for(i=l2-1;i>=0;i--){         //计算后到0的个数
       if(y[i]==0)
           sum2++;
       else
           break;
   }
   for(i=0;i<l1+l2;i++){       //数组初始化
        val[i]=0;
        val1[i]=0;
   }

   for(i=0;i<l2;i++){             // 错位相乘结果相加放入数组VAL[]
       for(j=0;j<l1;j++){
           sum=y[i]*x[j];
         val[i+j]=sum+val[i+j];
       }
   }
   h=0;
   for(i=l1+l2-1;i>=0;i--){      //算除之外的元素个数
            if(val[i]!=0)
                h++;
   }
   for(i=h-1,j=0;i>=0;j++,i--){        //错位放入VAL1[]数组
 val1[j]=val[i];
   }
   for(j=0;j<l1+12;j++){               //拆分结果数组
       if(val1[j]>=10){
            val1[j+1]=(val1[j]/10)+val1[j+1];
             val1[j]=val1[j]%10;
       }
   }   
   l3=0;
   for(j=l1+l2-1;j>=0;j--){           //计算前导0个数

     if(val1[j]==0)
         l3++;
     else
         break;

   }
   for(j=l1+l2-1-l3;j>=0;j--){           //去掉前导0并输出结果

       printf("%d",val1[j]);
   }
   for(j=sum2+sum1;j>0;j--){            //输出后导0
       printf("0");
   }
   printf("\n");
   
    return 0;
}
搜索更多相关主题的帖子: 整数 相乘 
2010-12-21 21:38
五当家
Rank: 12Rank: 12Rank: 12
等 级:火箭侠
威 望:2
帖 子:1112
专家分:3674
注 册:2010-10-20
收藏
得分:10 
怎么啦?没错呀.

经验积累中............
2010-12-21 21:40
di494383142
Rank: 1
等 级:新手上路
帖 子:8
专家分:4
注 册:2010-12-20
收藏
得分:0 
只是觉得自己写的太过繁琐,谁更有好算法指教一下,谢谢。。。。
2010-12-21 21:47
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:10 
程序代码:
#ifndef __BIGINT__H__
#define __BIGINT__H__
#include<iostream>
#include<deque>
#include<string>
#include<cassert>
#include<exception>
class BigInt
{
public:
    explicit BigInt(int i);
    BigInt(const BigInt &other);
    BigInt(const std::string &);
    ~BigInt();
    friend BigInt operator += (BigInt &, const BigInt &);
    friend BigInt operator -= (BigInt &, const BigInt &);
    friend BigInt operator *= (BigInt &,  int );
    friend BigInt operator *= (BigInt &,  const BigInt &);
    friend BigInt operator +(const BigInt & , const BigInt & );
    friend BigInt operator -(const BigInt & , const BigInt & );
    friend BigInt operator - (const BigInt &);
    friend BigInt operator *(const BigInt & , int);
    friend BigInt operator *( const int  ,const BigInt &);
    friend BigInt operator *(const BigInt & , const BigInt & );
    friend bool operator == (const BigInt &,  const BigInt & );
    friend bool operator >  ( const BigInt &, const BigInt &);
    friend bool operator < ( const BigInt &, const BigInt & );
    friend bool operator >= ( const BigInt &, const BigInt &);
    friend bool operator <= ( const BigInt &, const BigInt &);
    BigInt & operator = (const BigInt &);
    friend std::ostream & operator << ( std::ostream & out, BigInt &bg);
    friend BigInt fab(const BigInt &);
    friend BigInt pow ( const BigInt &,int );
protected:
    void trim(){
    while( 0 == dt.back() )
    {
        dt.pop_back();
    }
    }
private:
    bool sign;
    static const unsigned int Base;
    std::deque<unsigned int> dt;
};
const unsigned int BigInt::Base =10000;

BigInt::BigInt(int i=0)
{
    sign= i >= 0? true: false;
    i = i > 0 ? i : -i;
    assert(dt.empty());
    for ( ; i > 0; i /= Base)
    {
    dt.push_back(i%Base);
    }
}
BigInt::BigInt(const BigInt &other)
{
    assert(this->dt.empty());
    dt=other.dt;
    sign=other.sign;
}

BigInt & BigInt::operator = (const BigInt & other)
{
    this->sign=other.sign;
    this->dt=other.dt;
    return *this;
}
BigInt::BigInt( const std::string & s)
{
    try{
    std::string::const_reverse_iterator it=s.rbegin();
    for( ; it!=s.rend(); it++)
    {
        if ( * it == '-' || (* it <='9' && * it >='0'))
        {
        if( *it != '-')
            dt.push_back((unsigned)( *it-'0' ));
        else
            sign=false;
        }
        else
        throw std::exception();
    }
    }catch ( std::exception e)
    {
    std::cerr<<e.what()<<std::endl;
    }
}

BigInt::~BigInt()
{
    dt.clear();
}


std::ostream & operator << ( std::ostream & out, BigInt &bg)
{
    if( 0 == bg.dt.size())
        out<< unsigned(0);
    if( !bg.sign)
        out<<"-";
    std::deque<unsigned int>::const_reverse_iterator rit = bg.dt.rbegin();
    for( ; rit != bg.dt.rend() ; rit++ )
    {
        out<<*rit;
    }
    return out;
}
BigInt operator - (const BigInt & bg)
{
    BigInt ret(bg);
    ret.sign=!ret.sign;
    return ret;
}

bool operator == ( const BigInt& op1 , const BigInt &op2)
{
    return op1.sign==op2.sign && op1.dt== op2.dt;
}

bool operator > ( const BigInt &op1, const BigInt & op2)
{
    if( op1.sign> op2.sign)
        return true;
    if ( op1 == op2 )
        return false;
    if( op1.dt.size()== op2.dt.size())
    {
        std::deque<unsigned>::const_reverse_iterator it,jt;
        it=op1.dt.rbegin();
        jt=op2.dt.rbegin();
        while( *it == *jt && it != op1.dt.rend() && jt !=op2.dt.rend() )
        {
            it++;
            jt++;
        }
        if( op1.sign > 0 )
            return *it > *jt;
        else
            return *it < *jt;
    }
    else
    {
        if( op1.sign > 0 )
            return op1.dt.size()>op2.dt.size()? true:false;
        else
            return op1.dt.size()>op2.dt.size()? false: true;
    }
}

bool operator >= ( const BigInt & op1, const BigInt & op2 )
{
    return op1 == op2 || op1 > op2;
}

bool operator <= ( const BigInt & op1, const BigInt & op2 )
{
    return op1 == op2 || op1 < op2;
}

bool operator < ( const BigInt & op1, const BigInt & op2)
{
    if( op1.sign >  op2.sign)
    return true;
    else if( op1.sign < 0 && op2.sign <0 )
    {
    return fab(op1) > fab(op2) ;
    }
    else
    return ! (op1 > op2);
}

BigInt operator += ( BigInt & op1,const BigInt & op2 )
{
    if (op1.sign != op2.sign)
    {
    BigInt tmp(op2);
    tmp=-tmp;
    op1-=tmp;
    return op1;
    }
    BigInt bg,other;
    bg=fab(op1)>fab(op2)?op1:op2;
    other= bg==op1? op2: op1;
    unsigned int carry = 0;
    std::deque<unsigned int>::iterator it;
    std::deque<unsigned int>::const_iterator jt;
    for( it = bg.dt.begin(),jt = other.dt.begin(); it < bg.dt.end() || jt < other.dt.end() || carry > 0 ; )
    {
    if( it < bg.dt.end() && jt < other.dt.end() )
    {
        carry += * it + *jt ;
        *it = carry % BigInt::Base;
        carry /= BigInt::Base;
        it ++;
        jt ++;
    }
    else if ( it < bg.dt.end() )
    {
        carry += *it;
        *it = carry % BigInt::Base;
        carry /= BigInt::Base;
        it ++;
    }
    else if( jt< other.dt.end())
    {
        carry += *jt;
        bg.dt.push_back(carry % BigInt::Base);
        carry /= BigInt::Base;
        jt++;
    }
    else
    {
        while ( carry > 0 )
        {
        bg.dt.push_back(carry%BigInt::Base);
        carry /= BigInt::Base;
        }
    }
    }
    op1=bg;
    return op1;
}

BigInt operator + ( const BigInt & op1, const BigInt & op2 )
{
    BigInt ret(op1);
    ret+=op2;
    return ret;
}

BigInt operator -=( BigInt & bg, const BigInt &other )
{
    if ( bg.sign != other.sign )
    {
    BigInt tmp(other);
    tmp=-tmp;
    bg+=tmp;
    return bg;
    }
    int carry=0;
    BigInt op1,op2;
    if ( bg.dt.size() == other.dt.size() )
    {
    op1 = bg.dt.back() >= other.dt.back() ? bg: other;
    op2 = bg.dt.back() >= other.dt.back() ? other: bg;
    }
    else
    {
    op1 = bg.dt.size() > other.dt.size()? bg:other;
    op2 = bg.dt.size() > other.dt.size()? other:bg;
    }
    op1.sign= (op1 == bg );
    assert( op1.dt.size() >= op2.dt.size() );
    std::deque<unsigned int>::iterator it;
    std::deque<unsigned int>::const_iterator jt;
    try
    {
    for( it =  op1.dt.begin(),jt = op2.dt.begin(); it< op1.dt.end()||jt<op2.dt.end()|| carry < 0; )
    {
        if( it < op1.dt.end() && jt<op2.dt.end() )
        {
        carry += (int) *it- (int)*jt;
        *it= carry >=0 ? (unsigned)(carry % BigInt::Base) :(unsigned) (carry + BigInt::Base)% BigInt::Base;
        carry = carry >= 0 ? carry/BigInt::Base: -((-carry + BigInt::Base)/(BigInt::Base));
        it++;
        jt++;
        }
        else if ( it < op1.dt.end() )
        {
        carry += (int)* it;
        *it= carry >=0 ?(unsigned) (carry % BigInt::Base) :(unsigned) (carry + BigInt::Base)% BigInt::Base;
        carry = carry >= 0 ? carry/BigInt::Base: -((-carry + BigInt::Base)/(BigInt::Base));
        it++;
        }
        else if( jt < op2.dt.end() )
        {
        throw std::exception();
        }
    }
    }catch( std::exception e )
    {
    std::cerr<<e.what()<<std::endl;
    }
    op1.trim();
    bg=op1;
    return bg;
}

BigInt operator -( const BigInt & op1, BigInt &op2)
{
    BigInt ret(op1);
    ret-=op2;
    return ret;
}

BigInt operator *= ( BigInt &  bg, int other)
{
    bg.sign = bg.sign== (other>=0?true:false) ? true:false;
    other = other>=0? other:-other;
    unsigned int carry=0;
    if ( 0 == other )
    {
    bg.dt.clear();
    bg.dt.push_back(0);
    return bg;
    }
    std::deque<unsigned int>::iterator it=bg.dt.begin();
    for( ; it != bg.dt.end(); it ++ )
    {
    carry += *it* other;
    *it= carry % BigInt::Base;
    carry /= BigInt::Base;
    }
    while ( carry > 0 )
    {
    bg.dt.push_back(carry%BigInt::Base);
    carry/=BigInt::Base;
    }
    return bg;
}

BigInt operator * ( int n, BigInt & other)
{
    BigInt bg(other);
    bg*=n;
    return bg;
}

BigInt operator *( const int n,const BigInt & bg)
{
    BigInt ret(bg);
    ret*=n;
    return ret;
}
BigInt operator *=(BigInt& op1, const BigInt & op2)
{
    if ( op1 == BigInt(0) || op2 == BigInt(0) )
    return BigInt (0);
    BigInt out(0);
    BigInt tmp;
    int carry=0;
    for( std::deque<unsigned>::const_iterator it = op2.dt.begin(); it != op2.dt.end();it ++,carry++ )
    {
    tmp= op1* (*it);
    for( int i=0;i<carry; i++)
    {
        tmp.dt.push_front(unsigned(0));
    }
    out+=tmp;
    }
    op1=out;
    op1.sign= !op1.sign ^ op2.sign;
    return op1;
}
BigInt operator * ( const BigInt &op1, const BigInt & op2 )
{
    BigInt ret(op1);
    ret*=op2;
    return ret;
}

BigInt operator * ( const BigInt & op1, int other)
{
    BigInt ret(op1);
    ret*=other;
    return ret;
}

BigInt fab( const BigInt & bg)
{
    BigInt ret(bg);
    ret.sign=true;
    return ret;
}

BigInt pow( const BigInt & bg, int n)
{
    assert( n >= 0 );
    if ( n == 0 )
    return BigInt(0);
    BigInt ret(bg);
    for( int i=0; i < n-1 ; i++)
    {
    ret*=bg;
    }
    return ret;
}

#endif
2010-12-21 22:36
di494383142
Rank: 1
等 级:新手上路
帖 子:8
专家分:4
注 册:2010-12-20
收藏
得分:0 
哎呀 我在oj上提交时发现此题有问题,我实在照不出错在哪了,谁能帮看看呢
2010-12-23 18:36
Devil_W
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:9
帖 子:1160
专家分:1797
注 册:2009-9-14
收藏
得分:0 
把我的代码拿去改改,贴上去看看还work.
2010-12-23 18:48
di494383142
Rank: 1
等 级:新手上路
帖 子:8
专家分:4
注 册:2010-12-20
收藏
得分:0 
谢谢你楼上的你的有些太深奥了,我有些看不懂;
不过我的错找出来了
for(j=0;j<(l1+12)*2;j++){ //拆分结果数组
 if(val1[j]>=10){
 val1[j+1]=(val1[j]/10)+val1[j+1];
 val1[j]=val1[j]%10;
 }
 }
改成这样的就OK了,没有考虑拆分自后回多出约2倍的长度
2010-12-23 19:21
di494383142
Rank: 1
等 级:新手上路
帖 子:8
专家分:4
注 册:2010-12-20
收藏
得分:0 
谢谢你楼上的你的有些太深奥了,我有些看不懂;
不过我的错找出来了
for(j=0;j<(l1+12)*2;j++){ //拆分结果数组
 if(val1[j]>=10){
 val1[j+1]=(val1[j]/10)+val1[j+1];
 val1[j]=val1[j]%10;
 }
 }
改成这样的就OK了,没有考虑拆分自后回多出约2倍的长度
2010-12-23 19:21
qwerty2011
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-8-29
收藏
得分:0 
本人初学者,看楼主程序,请问为什么在输入“012”,“012”时,结果是“1”,前导0如何去除,谢谢
2011-09-01 19:54
qwerty2011
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2011-8-29
收藏
得分:0 
#include<stdio.h>                 
#include<string.h>
int main()
{
    char w[200], e[200];             // 存输入数
    int x[200], y[200];              // 存数字放
    int val[400],val1[400],sum1,sum2;  // 存放结果,计数等
    int i,j,sum,l1,l2,h,l3,H;
   
    gets(w);
    gets(e);
   
    l1 = strlen(w);                 //计算位数
    j = 0;
    for(i=0; i<l1; i++)
    {            //字符转换成数字并存入数组
        x[i]=w[i]-'0';
    }
   
           H=0;
    for(i = 0; i<=l1; i++)
    {           //计算前导0个数
        if(x[i]==0)
            H++;
        else
            break;
    }
    for(i = 0;i<=l1;i++)
    {            //去掉前导0
        x[i] = x[i+H];
    }
        l1 = l1 - H;

        sum1=0;
    for(i=l1-1; i>=0 ;i--)
    {          //计算后到0的个数
        if(x[i]==0)
            sum1++;
        else
            break;
    }
   
   
   
    l2=strlen(e);                  //计算位数
    for(i=0; i<l2; i++)
    {                 //字符转换成数字并存入数组
        y[i]=e[i]-'0';
    }
           H=0;
    for(i = 0; i<=l2; i++)
    {           //计算前导0个数
        if(y[i]==0)
            H++;
        else
            break;
    }
    for(i = 0;i<=l2;i++)
    {            //去掉前导0
        y[i] = y[i+H];
    }
        l2 = l2 - H;

       sum2=0;
    for(i=l2-1; i>=0; i--)
    {         //计算后到0的个数
        if(y[i]==0)
            sum2++;
        else
            break;
    }
    for(i=0; i<l1+l2; i++)
    {       //数组初始化
        val[i]=0;
        val1[i]=0;
    }
   
    for(i=0; i<l2; i++)
    {             // 错位相乘结果相加放入数组VAL[]
        for(j=0; j<l1; j++)
        {
            sum=y[i]*x[j];
            val[i+j]=sum+val[i+j];
        }
    }
   
    h=0;
    for(i=l1+l2-1; i>=0; i--)
    {      //算除之外的元素个数
        if(val[i]!=0)
            h++;
    }
    for(i=h-1,j=0; i>=0; j++,i--)
    {        //错位放入VAL1[]数组
        val1[j]=val[i];
    }
    for(j=0; j<(l1+12)*2; j++)
    {               //拆分结果数组
        if(val1[j]>=10)
        {
            val1[j+1]=(val1[j]/10)+val1[j+1];
            val1[j]=val1[j]%10;
        }
    }   
   
    l3=0;
    for(j=l1+l2-1; j>=0; j--)
    {           //计算前导0个数
        if(val1[j]==0)
            l3++;
        else
            break;
    }
   
    for(j=l1+l2-1-l3; j>=0; j--)
    {           //去掉前导0并输出结果
        printf("%d",val1[j]);
    }
    for(j=sum2+sum1; j>0; j--)
    {            //输出后导0
        printf("0");
    }
    printf("\n");
   
    return 0;
}




本人小改一下,这样应该就没问题了,望楼主见谅
2011-09-02 21:10
快速回复:自己写的大整数相乘的程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.096577 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved