| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 470 人关注过本帖
标题:C编写两个64位数的乘积
只看楼主 加入收藏
xingbo2293
Rank: 1
等 级:新手上路
帖 子:11
专家分:5
注 册:2010-10-22
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:2 
C编写两个64位数的乘积
如题。另外我们平常使用的计算机是不是32位的,不能识别long long int
比如运行:
main()
{
  long long int a;
  scanf("%lld",&a);
  printf("%lld",a);
}

运行结果不对,请大家赐教》》》
搜索更多相关主题的帖子: 乘积 位数 编写 
2010-10-22 12:11
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:10 
程序代码:
你那样直接利用基本数据类型是不行的!
我最近写的 这超整形 就可以满足你的要求!



#include <iostream>
#include <cstring>
const int N=1000;
using namespace std;


class HugeInt
{
private:
    int integer[N];
public:
    HugeInt(long val=0);
    HugeInt(const char* str);
    friend HugeInt MinusHugeInt(HugeInt& h1,HugeInt& h2);
    friend HugeInt operator + (HugeInt& h1,HugeInt& h2);
    friend HugeInt&  operator + (HugeInt& op1,int op2);
    friend HugeInt&  operator + (HugeInt& op1,char* op2);
    friend ostream& operator << (ostream& os,const HugeInt& num);
    bool operator > (const HugeInt& rhs);
    HugeInt& operator = (const HugeInt& rhs);
    HugeInt operator - ( HugeInt& rhs);
    HugeInt Multiply(short n);
    HugeInt Shiftleft(int s);
    friend HugeInt operator * (HugeInt& op1,HugeInt& op2);
       
};

HugeInt::HugeInt(long val)
{
    for(int i=0;i<N;i++)
        integer[i]=0;
    for(int i=N-1;val!=0&&i>=0;i--)
    {
        integer[i]=val%10;
        val/=10;
    }
}

HugeInt::HugeInt(const char* str)
{
    for(int i=0;i<N;i++)
        integer[i]=0;
    for(int i=N-strlen(str),j=0;i<N,j<strlen(str);i++,j++)
        if(isdigit(str[j]))
            integer[i]=str[j]-'0';
}



HugeInt& HugeInt::operator = (const HugeInt& rhs)
{
    if(this!=&rhs)
    {
        for(int i=N-1;i>=0;i--)
            this->integer[i]=rhs.integer[i];
    }
    return *this;
}


HugeInt operator + (HugeInt& h1,HugeInt& h2)
{
    HugeInt h;
    int carry=0;
    for(int i=N-1;i>=0;i--)
    {
        h.integer[i]=h1.integer[i]+h2.integer[i]+carry;
        if(h.integer[i]>=10)
        {
            h.integer[i]%=10;
            carry=1;
        }
        else
            carry=0;
    }
    return h;
}


HugeInt&  operator + (HugeInt& op1,int op2)
{
    return op1+HugeInt(op2);
}

HugeInt&  operator + (HugeInt& op1,char* op2)
{
    return op1+HugeInt(op2);
}

ostream& operator << (ostream& os,const HugeInt& num)
{
    int i;
    for(i=0;(num.integer[i]==0)&&(i<N);i++)
    {}
    if(i==N)
        os<<0;
    else
        for(;i<N;i++)
            os<<num.integer[i];
    return os;
}

bool HugeInt:: operator > (const HugeInt& rhs)
{
    int i,j;
    for(i=0;(this->integer[i]==0)&&(i<N);i++)
    {}
    for(j=0;(rhs.integer[j]==0)&&(j<N);j++)
    {}
    if(i<j)
        return true;
    else if(i>j)
        return false;
    else
    {
        for(int s=i;s<N;s++)
        {
            if(this->integer[i]>rhs.integer[i])
                return true;
            else if(this->integer[i]<rhs.integer[i])
                return false;
        }
        return false;
    }
}


HugeInt MinusHugeInt(HugeInt& h1,HugeInt& h2)
{
    HugeInt temp;
    int carry=0;
    for(int i=N-1;i>=0;i--)
    {
        temp.integer[i]=h1.integer[i]-h2.integer[i]-carry;
        if(temp.integer[i]<0)
        {
            temp.integer[i]+=10;
            carry=1;
        }
        else
            carry=0;
    }
    return temp;
}

HugeInt HugeInt:: operator - ( HugeInt& rhs)
{
    HugeInt temp;
    if(*this>rhs)
        temp=MinusHugeInt(*this,rhs);
    else
    {
        temp=MinusHugeInt(rhs,*this);
        int i;
        for( i=0;(temp.integer[i]==0)&&i<N;i++)
        {}
        temp.integer[i]*=-1;
    }
    return temp;
}

HugeInt HugeInt::Multiply(short n)
{
    HugeInt temp;
    int carry=0;
    for(int i=N-1;i>=0;i--)
    {
        temp.integer[i]=n*this->integer[i]+carry;
        short m=temp.integer[i];
        if(m>=10)
        {
            temp.integer[i]%=10;
            carry=m/10;
        }
        else
            carry=0;
    }
    return temp;
}


HugeInt HugeInt::Shiftleft(int s)             //左移s位
{
    HugeInt temp;
    if(s==0)
        return *this;
    for(int i=N-1;i>N-1-s;i--)
        temp.integer[i]=0;
    int m;
    for(m=0;(this->integer[m]==0)&&(m<N);m++)
    {}
    for(int j=N-1-s,i=N-1;j>=m-s;j--,i--)
        temp.integer[j]=this->integer[i];
    return temp;
}

HugeInt operator * (HugeInt& op1,HugeInt& op2)
{
    HugeInt s,temp,temp1;
    int i,j;
    for(i=0;(op1.integer[i]==0)&&(i<N);i++)
    {}
    for(j=0;(op2.integer[j]==0)&&(j<N);j++)
    {}
    int m=0;
    for(int s1=N-1;s1>=j;s1--)
    {
        temp=op1.Multiply(op2.integer[s1]);
        temp1=temp.Shiftleft(m);
        m++;
        s=s+temp1;
    }
    return s;
}


int main()
{
    char*  s ="35365465476768787845654654765476768798989090324234234";
    char*  s1= "53534546576879898213132132134353454565657676878778985";
    HugeInt a(s), b(s1), c,d,f;
   c=a+b;
    d=a-b;
    f=a*b;
    cout<<c<<endl;
    cout<<d<<endl;
    cout<<f<<endl;
}
    


If You Want Something, Go Get It, Period.
2010-10-22 13:57
xingbo2293
Rank: 1
等 级:新手上路
帖 子:11
专家分:5
注 册:2010-10-22
收藏
得分:0 
回复 2楼 m21wo
大哥不会吧。这可是中兴软创的一个编程题,不会这么麻烦吧?
2010-10-22 15:26
快速回复:C编写两个64位数的乘积
数据加载中...
 
   



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

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