⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 高精度.txt

📁 上次站长没用!今次打包几个ACM的资料在上传
💻 TXT
📖 第 1 页 / 共 2 页
字号:
//高精度最终较完美解[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 + -