?? 高精度.txt
字號:
//高精度最終較完美解[url]http://acm.zjnu.cn/show.asp?tab=arithmetic&id=67[/url]
//done by smallrain . 05,4,7
#include<iostream>
#include<fstream>
#include<string>
#include"time.h"
using namespace std;
struct cresi //此結構體用來存余數,并打印
{
int *p_resi; //指向內存中存放余數的連續空間
int len_resi; //余數存放空間的長度
void print() //打印余數
{
for(int i=len_resi-1;i>=0;i--)
{
cout<<p_resi[i];
}
cout<<endl;
}
};///:p
struct cresi resi; //定義結構體全局變量,用于處理高精度整數除法的余數
class cata //定義高精度類,可實現任意大數的四則運算
{
public:
cata(int Max=10);
~cata();
bool empty() const;
int length() const; //存放高精度數的數組的長度
void printcata(ofstream &out) const;
void pricata(); //打印高精度數
void del_first(); //刪除高精度數組的首單元
cata &getdata(const string &x); //由于數據讀入用的是string,把字符串轉化為整形數組,并且完成逆置
cata &operator +(const cata &y); //實現高精度的加法,結果存放于被加數中,且返回被加數對象的指針
cata &operator -(const cata &y); //實現高精度的減法,結果存放于被減數中,且返回被減數對象的指針
cata &operator *(const cata &y); //實現高精度的乘法,結果存放于被乘數中,且返回被乘數對象的指針
cata &operator /(const cata &y); //實現高精度的除法,結果存放于被除數中,且返回被除數對象的指針
cata &operator =(const cata &y); //重載賦值運算符,用以實現cata對象的賦值
friend bool dispose_boundary(const cata &m,const cata &n); //處理邊界情況
friend int compare_cata(const cata &x,const cata &y); //比較兩個高精度數的大小
friend void result(cata &m,cata &n,cata &result_combination,cata &result_catalan); //產生大組合數結果和catalan數結果
friend void boundary_result(cata &m,cata &result_combination,cata &result_catalan); //在邊界條件下產生上述結果
private:
int n; //存放高精度數的數組的長度
int displacement; //數組偏移量,用于除法移位
int MaxSize;
int *data; //用于申請動態空間,指向存放高精度數的數組
};///:p
inline cata::cata(int Max) //構造函數
{
MaxSize=Max;
data=new int[MaxSize];
n=0;
displacement=0;
}///:p
inline cata::~cata() //析構函數
{
delete [] data;
}///:p
inline bool cata::empty() const //判斷是否非空,在本程序中無用
{
return n==0;
}///:p
inline bool dispose_boundary(const cata &m,const cata &n)
{
if(((1==n.n)&&(n.data[0]==0))||compare_cata(m,n)==2)
{
return false;
}
else
{
return true;
}
}///:p
inline int cata::length() const //返回高精度數的長度,即是數組長度
{
return n;
}///:p
inline void cata::del_first() //刪除存放高精度數的數組的首單元,在除法中運用
{
/*for(int i=1;i<n;i++)
{
data[i-1]=data[i];
}*/
displacement+=1;
n-=1;
}///:p
void cata::pricata() //打印高精度數到控制臺
{
for(int i=n-1;i>=0;i--)
{
cout<<data[i];
}
cout<<endl;
}///:p
cata &cata::getdata(const string &str)
{
int len_int=str.length(),len_str=len_int;
if(this->n!=len_int)
{
delete [] data;
data=new int [len_int];
if(NULL==data)
{
cout<<"the error take place in getdata()"<<endl;
exit(1);
}
}
for(int j=0;j<len_int;j++) //完成string到int型數組的轉換,并且逆置
{
data[j]=(int)(str[len_str-1-j]-48);
}
n=len_int;
return *this;
}///:p
cata &cata::operator +(const cata &y)
{
int len_x=length(),len_y=y.length();
int len_max=len_x>len_y? len_x:len_y,len_min=len_x<len_y? len_x:len_y;
int *result=new int[len_max+1]; //result存放高精度加法結果
for(int i=0;i<len_max+1;i++) //將result全部置0
{
result[i]=0;
}
int carry=0,temp_1,temp_2; //carry為進位
for(i=0;i<len_min;i++) //按位加,分兩步做
{
temp_2=temp_1=data[i]+y.data[i]+carry;
carry=temp_1/10;
if(carry)
result[i]=temp_2%10;
else result[i]=temp_2;
}
if(len_x>=len_y)
{
for(i=len_min;i<len_max;i++)
{
temp_2=temp_1=data[i]+result[i]+carry;
carry=temp_1/10;
if(carry)
{
result[i]=temp_2%10;
}
else
{
result[i]=temp_2;
}
}
}
else
{
for(i=len_min;i<len_max;i++)
{
temp_2=temp_1=y.data[i]+result[i]+carry;
carry=temp_1/10;
if(carry)
{
result[i]=temp_2%10;
}
else
{
result[i]=temp_2;
}
}
}
if(carry)
{
result[len_max]=carry;
n=len_max+1;
}
else
{
n=len_max;
}
int *temp=result; //指針的交換
result=data;
data=temp;
delete [] result;
return *this;
}///:p
cata &cata::operator -(const cata &y)
{
int len_max=length(),len_y=y.length();
int *result=new int [len_max]; //result用于存放高精度減法的結果
if(NULL==result)
{
cout<<"the error take place in -"<<endl;
exit(1);
}
for(int i=0;i<len_max;i++)
{
result[i]=0;
}
int borrow=0,temp; //borrow位借
int displace=y.displacement;
for(i=0;i<len_y;i++) //按位減,分兩步做
{
temp=data[i]+10-y.data[i+displace]-borrow;
if(temp>=10)
{
temp=temp-10;
borrow=0;
}
else
{
borrow=1;
}
result[i]=temp;
}
for(int j=len_y;j<len_max;j++)
{
temp=data[j]+10-result[j]-borrow;
if(temp>=10)
{
temp=temp-10;
borrow=0;
}
else
{
borrow=1;
}
result[j]=temp;
}
n=len_max;
i=1;
while(result[n-i]==0&&i<n) //由于減法會在高位產生0,i的回溯相當于去掉無用的高位0
{
i++;
}
n=n-i+1; //i回溯后,重新計算n
int *temp_sub=result; //交換指針,保證結果實在*this對象中
result=data;
data=temp_sub;
delete [] result;
return *this;
}///:p
cata &cata::operator *(const cata &y)
{
int len_x=length(),len_y=y.length();
int len_z=len_x+len_y;
int *result=new int [len_z]; //result存放高精度*的結果
int *temp=new int [len_z];
if(NULL==result||NULL==temp)
{
cout<<"the error take place in *"<<endl;
exit(1);
}
for(int j=0;j<len_z;j++)
{
result[j]=0;
}
int carry,temp1,temp2; //carry為進位
for(j=0;j<len_y;j++) //模擬手工方法,實現乘法
{
for(int i=0;i<len_z;i++)
{
temp[i]=0;
}
carry=0;
for(i=0;i<len_x;i++)
{
temp2=temp1=data[i]*y.data[j]+carry;
carry=temp1/10;
if(carry)
{
temp[i+j]=temp2%10;
}
else
{
temp[i+j]=temp2;
}
}
if(carry)
{
temp[len_x+j]=carry;
}
int carry_add=0,temp1_add,temp2_add; //乘法中的加法
for(i=0;i<len_z;i++)
{
temp2_add=temp1_add=temp[i]+result[i]+carry_add;
carry_add=temp1_add/10;
if(carry_add)
{
result[i]=temp2_add%10;
}
else
{
result[i]=temp2_add;
}
}
}
this->n=len_z;
int i=1;
while(result[n-i]==0&&i<n)
{
i++;
}
n=n-i+1;
int *temp_change=result; //交換指針
result=data;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -