这得自己写大数类了,我给你一个模板,用数组的
头文件[code]/*大数运算库头文件:Integer.h
说明:适用于MFC,1024位RSA运算
*/
//允许生成1120位(二进制)的中间结果
#ifndef BI_MAXLEN
#define BI_MAXLEN 35
#define DEC 10
#define HEX 16
class CInteger
{
public:
//大数在0x100000000进制下的长度
unsigned m_nLength;
//用数组记录大数在0x100000000进制下每一位的值
unsigned long m_ulValue[BI_MAXLEN];
CInteger();
~CInteger();
/*基本操作与运算
Mov,赋值运算,可赋值为大数或普通整数,可重载为运算符“=”
Cmp,比较运算,可重载为运算符“==”、“!=”、“>=”、“<=”等
Add,加,求大数与大数或大数与普通整数的和,可重载为运算符“+”
Sub,减,求大数与大数或大数与普通整数的差,可重载为运算符“-”
Mul,乘,求大数与大数或大数与普通整数的积,可重载为运算符“*”
Div,除,求大数与大数或大数与普通整数的商,可重载为运算符“/”
Mod,模,求大数与大数或大数与普通整数的模,可重载为运算符“%”
*/
void Mov(unsigned __int64 A);
void Mov(CInteger& A);
CInteger Add(CInteger& A);
CInteger Sub(CInteger& A);
CInteger Mul(CInteger& A);
CInteger Div(CInteger& A);
CInteger Mod(CInteger& A);
CInteger Add(unsigned long A);
CInteger Sub(unsigned long A);
CInteger Mul(unsigned long A);
CInteger Div(unsigned long A);
unsigned long Mod(unsigned long A);
int Cmp(CInteger& A);
/*输入输出
Get,从字符串按10进制或16进制格式输入到大数
Put,将大数按10进制或16进制格式输出到字符串
*/
void Get(const CString& str, unsigned int system=HEX);
void Put(CString& str, unsigned int system=HEX);
};
#endif[/code]实现文件[code]/*大数运算库源文件:Integer.cpp
说明:适用于MFC,1024位RSA运算
*/
#include "stdafx.h"
#include "Integer.h"
//构造大数对象并初始化为零
CInteger::CInteger()
{
m_nLength=1;
for(int i=0;i<BI_MAXLEN;i++)m_ulValue[i]=0;
}
//解构大数对象
CInteger::~CInteger()
{
}
/*大数比较
调用方式:N.Cmp(A)
返回值:若N<A返回-1;若N=A返回0;若N>A返回1
*/
int CInteger::Cmp(CInteger& A)
{
if(m_nLength>A.m_nLength)return 1;
if(m_nLength<A.m_nLength)return -1;
for(int i=m_nLength-1;i>=0;i--)
{
if(m_ulValue[i]>A.m_ulValue[i])return 1;
if(m_ulValue[i]<A.m_ulValue[i])return -1;
}
return 0;
}
/*
大数赋值
调用方式:N.Mov(A)
返回值:无,N被赋值为A
*/
void CInteger::Mov(CInteger& A)
{
m_nLength=A.m_nLength;
for(int i=0;i<BI_MAXLEN;i++)m_ulValue[i]=A.m_ulValue[i];
}
void CInteger::Mov(unsigned __int64 A)
{
if(A>0xffffffff)
{
m_nLength=2;
m_ulValue[1]=(unsigned long)(A>>32);
m_ulValue[0]=(unsigned long)A;
}
else
{
m_nLength=1;
m_ulValue[0]=(unsigned long)A;
}
for(int i=m_nLength;i<BI_MAXLEN;i++)m_ulValue[i]=0;
}
/*
大数相加
调用形式:N.Add(A)
返回值:N+A
*/
CInteger CInteger::Add(CInteger& A)
{
CInteger X;
X.Mov(*this);
unsigned carry=0;
unsigned __int64 sum=0;
if(X.m_nLength<A.m_nLength)X.m_nLength=A.m_nLength;
for(unsigned i=0;i<X.m_nLength;i++)
{
sum=A.m_ulValue[i];
sum=sum+X.m_ulValue[i]+carry;
X.m_ulValue[i]=(unsigned long)sum;
carry=(unsigned)(sum>>32);
}
X.m_ulValue[X.m_nLength]=carry;
X.m_nLength+=carry;
return X;
}
CInteger CInteger::Add(unsigned long A)
{
CInteger X;
X.Mov(*this);
unsigned __int64 sum;
sum=X.m_ulValue[0];
sum+=A;
X.m_ulValue[0]=(unsigned long)sum;
if(sum>0xffffffff)
{
unsigned i=1;
while(X.m_ulValue[i]==0xffffffff){X.m_ulValue[i]=0;i++;}
X.m_ulValue[i]++;
if(m_nLength==i)m_nLength++;
}
return X;
}
/*
大数相减
调用形式:N.Sub(A)
返回值:N-A
*/
CInteger CInteger::Sub(CInteger& A)
{
CInteger X;
X.Mov(*this);
if(X.Cmp(A)<=0){X.Mov(0);return X;}
unsigned carry=0;
unsigned __int64 num;
unsigned i;
for(i=0;i<m_nLength;i++)
{
if((m_ulValue[i]>A.m_ulValue[i])||((m_ulValue[i]==A.m_ulValue[i])&&(carry==0)))
{
X.m_ulValue[i]=m_ulValue[i]-carry-A.m_ulValue[i];
carry=0;
}
else
{
num=0x100000000+m_ulValue[i];
X.m_ulValue[i]=(unsigned long)(num-carry-A.m_ulValue[i]);
carry=1;
}
}
while(X.m_ulValue[X.m_nLength-1]==0)X.m_nLength--;
return X;
}
CInteger CInteger::Sub(unsigned long A)
{
CInteger X;
X.Mov(*this);
if(X.m_ulValue[0]>=A){X.m_ulValue[0]-=A;return X;}
if(X.m_nLength==1){X.Mov(0);return X;}
unsigned __int64 num=0x100000000+X.m_ulValue[0];
X.m_ulValue[0]=(unsigned long)(num-A);
int i=1;
while(X.m_ulValue[i]==0){X.m_ulValue[i]=0xffffffff;i++;}
X.m_ulValue[i]--;
if(X.m_ulValue[i]==0)X.m_nLength--;
return X;
}
/*
大数相乘
调用形式:N.Mul(A)
返回值:N*A
*/
CInteger CInteger::Mul(CInteger& A)
{
if(A.m_nLength==1)return Mul(A.m_ulValue[0]);
CInteger X;
unsigned __int64 sum,mul=0,carry=0;
unsigned i,j;
X.m_nLength=m_nLength+A.m_nLength-1;
for(i=0;i<X.m_nLength;i++)
{
sum=carry;
carry=0;
for(j=0;j<A.m_nLength;j++)
{
if(((i-j)>=0)&&((i-j)<m_nLength))
{
mul=m_ulValue[i-j];
mul*=A.m_ulValue[j];
carry+=mul>>32;
mul=mul&0xffffffff;
sum+=mul;
}
}
carry+=sum>>32;
X.m_ulValue[i]=(unsigned long)sum;
}
if(carry){X.m_nLength++;X.m_ulValue[X.m_nLength-1]=(unsigned long)carry;}
return X;
}
CInteger CInteger::Mul(unsigned long A)
{
CInteger X;
unsigned __int64 mul;
unsigned long carry=0;
X.Mov(*this);
for(unsigned i=0;i<m_nLength;i++)
{
mul=m_ulValue[i];
mul=mul*A+carry;
X.m_ulValue[i]=(unsigned long)mul;
carry=(unsigned long)(mul>>32);
}
if(carry){X.m_nLength++;X.m_ulValue[X.m_nLength-1]=carry;}
return X;
}
/*
大数相除
调用形式:N.Div(A)
返回值:N/A
*/
CInteger CInteger::Div(CInteger& A)
{
if(A.m_nLength==1)return Div(A.m_ulValue[0]);
CInteger X,Y,Z;
unsigned i,len;
unsigned __int64 num,div;
Y.Mov(*this);
while(Y.Cmp(A)>=0)
{
div=Y.m_ulValue[Y.m_nLength-1];
num=A.m_ulValue[A.m_nLength-1];
len=Y.m_nLength-A.m_nLength;
if((div==num)&&(len==0)){X.Mov(X.Add(1));break;}
if((div<=num)&&len){len--;div=(div<<32)+Y.m_ulValue[Y.m_nLength-2];}
div=div/(num+1);
Z.Mov(div);
if(len)
{
Z.m_nLength+=len;
for(i=Z.m_nLength-1;i>=len;i--)Z.m_ulValue[i]=Z.m_ulValue[i-len];
for(i=0;i<len;i++)Z.m_ulValue[i]=0;
}
X.Mov(X.Add(Z));
Y.Mov(Y.Sub(A.Mul(Z)));
}
return X;
}
CInteger CInteger::Div(unsigned long A)
{
CInteger X;
X.Mov(*this);
if(X.m_nLength==1){X.m_ulValue[0]=X.m_ulValue[0]/A;return X;}
unsigned __int64 div,mul;
unsigned long carry=0;
for(int i=X.m_nLength-1;i>=0;i--)
{
div=carry;
div=(div<<32)+X.m_ulValue[i];
X.m_ulValue[i]=(unsigned long)(div/A);
mul=(div/A)*A;
carry=(unsigned long)(div-mul);
}
if(X.m_ulValue[X.m_nLength-1]==0)X.m_nLength--;
return X;
}
/*
大数求模
调用形式:N.Mod(A)
返回值:N%A
*/
CInteger CInteger::Mod(CInteger& A)
{
CInteger X,Y;
unsigned __int64 div,num;
unsigned long carry=0;
unsigned i,len;
X.Mov(*this);
while(X.Cmp(A)>=0)
{
div=X.m_ulValue[X.m_nLength-1];
num=A.m_ulValue[A.m_nLength-1];
len=X.m_nLength-A.m_nLength;
if((div==num)&&(len==0)){X.Mov(X.Sub(A));break;}
if((div<=num)&&len){len--;div=(div<<32)+X.m_ulValue[X.m_nLength-2];}
div=div/(num+1);
Y.Mov(div);
Y.Mov(A.Mul(Y));
if(len)
{
Y.m_nLength+=len;
for(i=Y.m_nLength-1;i>=len;i--)Y.m_ulValue[i]=Y.m_ulValue[i-len];
for(i=0;i<len;i++)Y.m_ulValue[i]=0;
}
X.Mov(X.Sub(Y));
}
return X;
}
unsigned long CInteger::Mod(unsigned long A)
{
if(m_nLength==1)return(m_ulValue[0]%A);
unsigned __int64 div;
unsigned long carry=0;
for(int i=m_nLength-1;i>=0;i--)
{
div=m_ulValue[i];
div+=carry*0x100000000;
carry=(unsigned long)(div%A);
}
return carry;
}
/*
从字符串按10进制或16进制格式输入到大数
调用格式:N.Get(str,sys)
返回值:N被赋值为相应大数
sys暂时只能为10或16
*/
void CInteger::Get(const CString& str, unsigned int system)
{
int len=str.GetLength(),k;
Mov(0);
for(int i=0;i<len;i++)
{
Mov(Mul(system));
if((str[i]>='0')&&(str[i]<='9'))k=str[i]-48;
else if((str[i]>='A')&&(str[i]<='F'))k=str[i]-55;
else if((str[i]>='a')&&(str[i]<='f'))k=str[i]-87;
else k=0;
Mov(Add(k));
}
}
/*
将大数按10进制或16进制格式输出为字符串
调用格式:N.Put(str,sys)
返回值:无,参数str被赋值为N的sys进制字符串
sys暂时只能为10或16
*/
void CInteger::Put(CString& str, unsigned int system)
{
if((m_nLength==1)&&(m_ulValue[0]==0)){str="0";return;}
str="";
CString t="0123456789ABCDEF";
int a;
char ch;
CInteger X;
X.Mov(*this);
while(X.m_ulValue[X.m_nLength-1]>0)
{
a=X.Mod(system);
ch=t[a];
str.Insert(0,ch);
X.Mov(X.Div(system));
}
}[/code]
温馨提示:内容为网友见解,仅供参考
C++编程中到底要怎么存一个大数啊 比如很大的数相加减!!!请给出说明...
Add,加,求大数与大数或大数与普通整数的和,可重载为运算符“+”Sub,减,求大数与大数或大数与普通整数的差,可重载为运算符“-”Mul,乘,求大数与大数或大数与普通整数的积,可重载为运算符“*”Div,除,求大数与大数或大数与普通整数的商,可重载为运算符“\/”Mod,模,求大数与大数或大数与普通整数的模,可重载...
C++编写一个大数类(1000位以内即可),要求:
\/\/大数和另一个大数的大小比较 bool operator>(const int & t)const; \/\/大数和一个int类型的变量的大小比较 void print(); \/\/输出大数}; BigNum::BigNum(const int b) \/\/将一个int类型的变量转化为大数{ int c,d = b; len = 0; memset(a,0,sizeof(a))...
C++编程:给你一个非常大的整数x,(-10^400 <=x<= 10^400),请统计x的每...
case 2:count[1]++;break;case 9:count[2]++;break;default:break;} } cout<<"1:"<<count[0]<<endl <<"2:"<<count[1]<<endl <<"9:"<<count[2]<<endl;return 0;}
...的最大数,并将所有小于n个数平均值的数输出,C++源代码,上机课啊,真...
楼主,这个是个简单的程序,我给楼主点思路,你可以开一个足够大的数组,然后读入n个数,并按顺序存入到数组里,然后用一个 变量来存他们的和,然后求平均,再一次的遍历数组,把小于平均值的数都输入就行了,楼主要学会自己写,不然是很难学会编程的、、、...
求做一个C++程序 求四个数中的最大数和最小数差值.要求用函数实现该功...
使用的代码,主要的疏漏是比较之后,要使用一个临时变量储存当前较大值,即可,修改之后是:#include<iostream> using namespace std;int max(int x,int y,int z,int g){ int t1,t2;t1=x>y?x:y; \/\/t1 is max in x,y;t2=t1>z?t1:z; \/\/t2 is max in x,y,z;return(t2>g?
c++中两个整型数相乘,要求很大很大的数,超出了取值范围!
include <string> using namespace std;void main(){ string num1,num2;\/\/被乘数和乘数,考虑到大整数,用string装入 cout<<"please input number1 and number2:"<<endl;cin>>num1>>num2;const char *p1=num1.c_str(); \/\/转为char const char *p2=num2.c_str();int length=strlen(...
怎么用C语言编写一个大整数的四则运算器,求解??(急!!)
1.打开CodeBlocks,创建一个新的空白文件,定义头文件和主要功能,然后写程序的主体:2.数百首先,定义所需的变量,将变量定义为浮点数,定义输入函数,定义x和y的计算变量,和c定义为变量选择计算方法。使用switch语句,以c为选择变量,填写计算方法的选择,最学位后在主函数输入输出函数输出计算公式和...
在C语言里输入一个十位数的整形数 怎么求吧它原样输出啊 我做了几...
5分也得拿啊:你用long long 下面我给你写一个 有错误自己改吧:long long a;scanf("%lld",&a);printf("%lld",a);不要不给分啊
C++,求大数 (p^n-1) 的质因子分解,大数分解!其中p为素数,n为整数...
\/*m的p次方减去1等于(m^0+m^1+m^2+...+m^(p-1))*(m-1),若不考虑(m-1),则剩余部分m^0+m^1+m^2+...+m^(p-1)可以用m进制数表示,即111111...1111(m),m进制数,有p个1,然后利用m进制数的除法判断一个数是否能被整除来分解质因数*\/#include <iostream>#include <...
高分!c++编程问题!编写一个递归函数int max(a [ ],intn ),其功能是...
include include<iostream>using namespace std;#define N 10int max(int *a,int n){ if ( n==1 ) return( a[0] ); else { int x=max(a+1,n-1); return( (a[0]>x )?(a[0]):(x) ); }}void main(){ int a[N],i; srand((unsigned int)time(NULL)); for (...