怎样自己定义一个类实现超过64位整数的4则运算 用BigInteger類,可以直接進行運算 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 BigInteger完全已经实现了任意位数的整数运算了,不用自己写啦 听说是用string来用我见过那贴子你可以找一下 我给一个C++的 我很久以前写的 代码比较乱 大概帮不上你什么忙 不过我还是帖出来:#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;class l_int{public: vector<short> data; l_int(){ data.push_back(0);} l_int(string str){ data.clear(); int j; for(j=0;j<str.length();j++) if (str[j]!='0') break; for(int i=str.length()-1;i>=j;i--) data.push_back( int(str[i])-48 ); } void set_value(string str){ data.clear(); for(int i=str.length()-1;i>=0;i--) data.push_back( int(str[i])-48 ); } l_int(int a){ data.clear(); do{ short k =a % 10; a = int(a/10); data.push_back(k); }while(a>0); } ~l_int(){ data.clear(); } l_int operator =(l_int a){ this->data = a.data; return *this; } bool operator==(l_int a){ if (this->data==a.data) return true; return false; } l_int operator +(l_int a); l_int operator -(l_int a); l_int operator *(l_int a); l_int semi_mul(l_int a, short n, int seg); int compare( l_int , l_int ); void print(){ vector<short>::iterator lst; lst = data.end(); do{ lst--; cout<<*lst; }while(lst!=data.begin()); }};l_int l_int::operator +(l_int a ){ vector<short> data; l_int b = *this; vector<short>::iterator lst,lst2; if (a.data.size()>b.data.size()) swap(a,b); lst = a.data.begin(); lst2 = b.data.begin(); data.clear(); while(lst!=a.data.end()){ data.push_back(*lst+*lst2); lst++; lst2++; } while(lst2!=b.data.end()){ data.push_back(*lst2); lst2++; } lst = data.begin(); while( lst!=data.end() ){ if (*lst>=10){ lst2 = lst+1; if( lst2!=data.end() ) (*lst2)++; else data.push_back(1); *lst -= 10; } lst++; } l_int res; res.data = data; return res;}l_int l_int::semi_mul(l_int a, short n, int seg){ vector<short>::iterator lst; vector<short> data; data.push_back(0); if ((n==0)||(a.data==data)){ a.data = data; return a; } for(lst = a.data.begin(); lst!=a.data.end();lst++) (*lst) *= n; data.clear(); for( int i=0;i<seg;i++) data.push_back(0); int ad=0; for(lst = a.data.begin();lst!=a.data.end(); lst++){ ad = ad+(*lst); *lst = ad % 10; ad = int(ad/10); data.push_back(*lst); } if (ad!=0) data.push_back(ad); bool flag = true; for(lst = data.begin(); lst!=data.end(); lst++) if (*lst!=0){ flag = false; break; } if (flag) { data.clear(); data.push_back(0); } a.data = data; return a;}l_int l_int::operator *( l_int a ){ l_int res(0); vector<short>::iterator lst,lst2; int seg = 0; for(lst=a.data.begin();lst!=a.data.end();lst++){ res = res + semi_mul(*this , *lst ,seg); seg++; } return res;}//remind should return a positive integer;l_int l_int::operator -(l_int a ){ if ( compare(*this,a)==-1) cout<<" ERROR! "<<endl; vector<short>::iterator lst; for( lst = a.data.begin(); lst!= a.data.end(); lst++) *lst *= -1; l_int res = *this + a ; for(lst = res.data.begin();lst!=res.data.end();lst++){ if (*lst<0){ *(lst+1) -=1; *lst += 10; } } lst = res.data.end(); do{ lst--; if( *lst == 0 ) lst = res.data.erase(lst); }while(lst!=res.data.begin()); return res;}//return 1 if bigger , 0 if equal , -1 if smallerint l_int::compare(l_int a , l_int b){ int i = a.data.size(); int j = b.data.size(); if ( i>j )return 1; else if ( i < j) return -1; else { vector<short>::iterator lst,lst2; lst = a.data.end()-1; lst2 = b.data.end(); do{ if ( *lst > *lst2) return 1; else if( *lst<*lst2) return -1; }while(lst!=a.data.begin()); } return 0;}int main(){ string str; char ch[1000]; /*while( cin.getline(ch,1000) ){ str = ch; if(str=="") continue; l_int a(str); cin.getline(ch,1000); str = ch; l_int b(str); a = a*b; a.print(); cout<<endl; }*/ l_int a(9); l_int r(1); for(int i=0;i<1000;i++) { r = r*a; r.print(); cout<<endl<<endl; int j; cout<<r.data.size()<<"!!!!"<<endl; } int j; cin>>j; return 0;} JTextPane 取消自动换行 请教一些工作方面的问题 J2SE中的指针模型 为开发小巧软件,50分征求大家意见 JXL难题,求各位大侠 java程序的并发性能问题--请教了解Java运行机制的高手! 谁能给一个java流分析文本内容的程序? 100分 关于字体 有没有人用过Jtest的,我怎么用的时候不能进行测试呢? 那里有关于JAVA数据库方面的书 如何做一个程序的启动画面〉? 有关native调用的问题
我见过那贴子
你可以找一下
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;class l_int{
public:
vector<short> data;
l_int(){ data.push_back(0);} l_int(string str){
data.clear();
int j;
for(j=0;j<str.length();j++)
if (str[j]!='0') break; for(int i=str.length()-1;i>=j;i--)
data.push_back( int(str[i])-48 );
} void set_value(string str){
data.clear();
for(int i=str.length()-1;i>=0;i--)
data.push_back( int(str[i])-48 );
} l_int(int a){
data.clear();
do{
short k =a % 10;
a = int(a/10);
data.push_back(k);
}while(a>0);
} ~l_int(){
data.clear();
} l_int operator =(l_int a){
this->data = a.data;
return *this;
} bool operator==(l_int a){
if (this->data==a.data) return true;
return false;
} l_int operator +(l_int a);
l_int operator -(l_int a);
l_int operator *(l_int a);
l_int semi_mul(l_int a, short n, int seg);
int compare( l_int , l_int ); void print(){
vector<short>::iterator lst;
lst = data.end();
do{
lst--;
cout<<*lst;
}while(lst!=data.begin());
}
};l_int l_int::operator +(l_int a )
{
vector<short> data;
l_int b = *this; vector<short>::iterator lst,lst2; if (a.data.size()>b.data.size()) swap(a,b); lst = a.data.begin();
lst2 = b.data.begin();
data.clear();
while(lst!=a.data.end()){
data.push_back(*lst+*lst2); lst++;
lst2++;
} while(lst2!=b.data.end()){
data.push_back(*lst2);
lst2++;
} lst = data.begin();
while( lst!=data.end() ){
if (*lst>=10){
lst2 = lst+1;
if( lst2!=data.end() ) (*lst2)++;
else data.push_back(1); *lst -= 10;
}
lst++;
} l_int res;
res.data = data;
return res;
}l_int l_int::semi_mul(l_int a, short n, int seg)
{
vector<short>::iterator lst;
vector<short> data;
data.push_back(0); if ((n==0)||(a.data==data)){
a.data = data;
return a;
} for(lst = a.data.begin(); lst!=a.data.end();lst++)
(*lst) *= n; data.clear();
for( int i=0;i<seg;i++) data.push_back(0); int ad=0;
for(lst = a.data.begin();lst!=a.data.end(); lst++){ ad = ad+(*lst);
*lst = ad % 10;
ad = int(ad/10);
data.push_back(*lst);
}
if (ad!=0) data.push_back(ad); bool flag = true;
for(lst = data.begin(); lst!=data.end(); lst++)
if (*lst!=0){
flag = false;
break;
} if (flag) {
data.clear();
data.push_back(0);
}
a.data = data;
return a;
}l_int l_int::operator *( l_int a )
{
l_int res(0); vector<short>::iterator lst,lst2;
int seg = 0;
for(lst=a.data.begin();lst!=a.data.end();lst++){
res = res + semi_mul(*this , *lst ,seg);
seg++;
}
return res;
}//remind should return a positive integer;
l_int l_int::operator -(l_int a )
{
if ( compare(*this,a)==-1) cout<<" ERROR! "<<endl; vector<short>::iterator lst; for( lst = a.data.begin(); lst!= a.data.end(); lst++)
*lst *= -1; l_int res = *this + a ; for(lst = res.data.begin();lst!=res.data.end();lst++){
if (*lst<0){
*(lst+1) -=1;
*lst += 10;
}
} lst = res.data.end();
do{
lst--;
if( *lst == 0 )
lst = res.data.erase(lst);
}while(lst!=res.data.begin());
return res;
}//return 1 if bigger , 0 if equal , -1 if smaller
int l_int::compare(l_int a , l_int b)
{
int i = a.data.size();
int j = b.data.size(); if ( i>j )return 1;
else if ( i < j) return -1;
else {
vector<short>::iterator lst,lst2;
lst = a.data.end()-1;
lst2 = b.data.end();
do{
if ( *lst > *lst2) return 1;
else if( *lst<*lst2) return -1;
}while(lst!=a.data.begin());
}
return 0;
}int main()
{
string str;
char ch[1000];
/*while( cin.getline(ch,1000) ){
str = ch;
if(str=="") continue;
l_int a(str);
cin.getline(ch,1000);
str = ch;
l_int b(str); a = a*b;
a.print();
cout<<endl;
}*/
l_int a(9);
l_int r(1);
for(int i=0;i<1000;i++)
{
r = r*a;
r.print();
cout<<endl<<endl;
int j;
cout<<r.data.size()<<"!!!!"<<endl;
}
int j;
cin>>j;
return 0;
}