C编写两个64位数的乘积
如题。另外我们平常使用的计算机是不是32位的,不能识别long long int比如运行:
main()
{
long long int a;
scanf("%lld",&a);
printf("%lld",a);
}
运行结果不对,请大家赐教》》》
你那样直接利用基本数据类型是不行的! 我最近写的 这超整形 就可以满足你的要求! #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; }