C + + Large-number precision calculation (with decimal points) __c++

Source: Internet
Author: User
Tags arithmetic
Header file:
Wtnumber.h:interface for the Cwtnumber class. #if!defined (afx_wtnumber_h__92e62b40_ 491A_4A75_AB89_FFB160DB2343__INCLUDED_) #define AFX_WTNUMBER_H__92E62B40_491A_4A75_AB89_FFB160DB2343__INCLUDED_ # If _msc_ver > 1000 #pragma once #endif//_msc_ver > 1000 #define INT_BIT_MAX 255 #define FLOAT_BIT_MAX 255 Class                     Cwtnumber {private:int intbits;                   /* Integer digit */int floatbits;                   /* Decimal effective digit/char infinite;                       /* Infinity */char sign;       /* Symbol */char Intpart[int_bit_max];   /* Integer part */char Floatpart[float_bit_max];
* * Small number of parts/private:char* M_SZ;
    Public:/* arithmetic function pointer type. */typedef void (*PFNCALC) (const cwtnumber&,const cwtnumber&,cwtnumber&);
    Cwtnumber ();
    Cwtnumber (const char* sznum);
    ~cwtnumber ();
    /* Convert to String * * char* ToString ();
    void Freestring ();
    /* Initialize Wtnumber to 0.*/void Initwtnumbertozero ();
    /* Determine how many character spaces are required to store the Wtnumber conversion string. * * int strlenbywtnumber ();
    /* Converts from string to wtnumber.*/void Strtowtnumber (const char *arr);
    /* Converts from Wtnumber to string. */void Wtnumbertostr (char *szbuf);
    /* Adjust digit, delete the highest integer digit is 0 and the lowest decimal place is the digit of 0. */void Adjustbits ();
    /* Move the decimal point, delta=0 does not move, delta<0 to the left, delta>0 to the right to move. * * void movefloatpoint (int delta);
    /* Make infinity/void Makeinfinite ();
    /* Compare 2 number size */int wtcompare (const cwtnumber& n) const;
    /* To determine whether 0/int iszero () const;

    /* Assignment Number Overload * * cwtnumber& operator= (const cwtnumber& N);
    /* operator overload */Cwtnumber operator+ (const cwtnumber& N);
    Cwtnumber operator-(const cwtnumber& N);
    Cwtnumber operator* (const cwtnumber& N);
    Cwtnumber operator/(const cwtnumber& N);
    cwtnumber& operator+= (const cwtnumber& N);
    cwtnumber& operator-= (const cwtnumber& N);
    cwtnumber& operator*= (const cwtnumber& N); cwtnumber& operator/=(const cwtnumber& N);
    BOOL Operator> (const cwtnumber& N);
    BOOL Operator>= (const cwtnumber& N);
    BOOL operator< (const cwtnumber& N);
    BOOL Operator<= (const cwtnumber& N);
    BOOL operator== (const cwtnumber& N);
    BOOL Operator!= (const cwtnumber& N);
    /* addition */static void Wtadd (const cwtnumber& n1,const cwtnumber& n2,cwtnumber& Res);
    /* multiplication */static void wtmultiply (const cwtnumber& n1,const cwtnumber& n2,cwtnumber& Res);
    /* Subtraction */static void Wtsubtract (const cwtnumber& n1,const cwtnumber& n2,cwtnumber& Res);
    /* Division */static void Wtdivide (const cwtnumber& n1,const cwtnumber& n2,cwtnumber& Res);
/* The result is returned according to the arithmetic function */static char *result (const char *val1,const char *val2,pfncalc pfncalc);


}; #endif//!defined (AFX_WTNUMBER_H__92E62B40_491A_4A75_AB89_FFB160DB2343__INCLUDED_)

Source file:
WTNumber.cpp:implementation of the Cwtnumber class. #include <string.h> #include < stdlib.h> #include "WTNumber.h"/////////////////////////////////////////////////////////////////////// Construction/destruction//////////////////////////////////////////////////////////////////////CWTNumber::
Cwtnumber () {Initwtnumbertozero ();}
    Cwtnumber::cwtnumber (const char* sznum) {Initwtnumbertozero ();
Strtowtnumber (Sznum);
    } cwtnumber::~cwtnumber () {freestring ();} void Cwtnumber::freestring () {if (M_SZ) delete []M_SZ;
M_sz=null; } void Cwtnumber::initwtnumbertozero () {memset (this,0,sizeof (Cwtnumber));} int Cwtnumber::strlenbywtnumber () {i
    NT len = floatbits+intbits+1;    if (intbits==0) len++;    /*. 1--> 0.1*/if (floatbits) len++; /* '.'        */if (sign) len++;    /* '-' */if (infinite) return 11;
* * #INFINITE/return len; } void Cwtnumber::sTrtowtnumber (const char *arr) {char *point;
    Initwtnumbertozero ();
        if (*arr== '-')/* If it is a negative number * * arr++;
    Sign=1;
    } point=strchr (arr, '. ');    if (point)/* Find the decimal */{int n=intbits=point-arr;    /* Calculates the integer digital/while (n)/* integer digits not ==0 the loop/{intpart[intbits-n]=arr[n-1]-' 0 ';
        * * will be the number of low existing low standard elements * * n--;
            while (*++point) {floatpart[floatbits]=*point-' 0 ';
        floatbits++;
        } else/* Description does not write a decimal point, all integers. * * {int n=intbits=strlen (arr);
            while (n) {intpart[intbits-n]=arr[n-1]-' 0 ';
        n--;
    } adjustbits (); 
    /* processing-conditions of 0 and 0/if (floatbits==0) {if (intbits==0 | | intbits==1&&intpart[0]==0) sign=0;
    } void Cwtnumber::wtnumbertostr (char *szbuf) {int n=intbits,c;
    memset (Szbuf,0,strlenbywtnumber ());
       if (sign)/* If it is a negative number * *szbuf++= '-';
        } if (infinite) {strcat (szbuf, "#INFINITE");
    Return
        while (n) {szbuf[intbits-n]=intpart[n-1]+ ' 0 ';
    n--;    } c=0;
        /* Whether to add 0*/if (intbits==0) {strcat (szbuf, "0");
    C=1;
    } if (floatbits) strcat (Szbuf, ".");
    n=0;
        while (n<floatbits) {szbuf[intbits+1+n+c]=floatpart[n]+ ' 0 ';
    n++;
    } void Cwtnumber::adjustbits () {while (intbits>1&&intpart[intbits-1]==0) intbits--;
while (floatbits&&floatpart[floatbits-1]==0) floatbits--;
        } void Cwtnumber::movefloatpoint (int delta) {/* delta<0 moves the decimal point to the left, Delta>0 moves the decimal point to the right (Delta) {
        Cwtnumber N=*this;
        Initwtnumbertozero ();
        Sign=n.sign;
            if (delta<0) {int i;
            Delta=-delta;
            for (i=delta;i<n.intbits;i++) {intpart[intbits++]=n.intpart[i]; for (i=delta-1;i>=0;i--) {Floatpart[floatbits++]=n.intpart[i];
            for (i=0;i<n.floatbits;i++) {floatpart[floatbits++]=n.floatpart[i];
            } else {int i;
            for (i=delta;i<n.floatbits;i++)/* Processing decimal parts/{Floatpart[floatbits++]=n.floatpart[i]; for (i=delta-1;i>=0;i--)/* Decimal to Integer part * * {Intpart[intbits++]=n.floa
            Tpart[i];
            for (i=0;i<n.intbits;i++)/* Integer part */{intpart[intbits++]=n.intpart[i];
}} adjustbits ();

} void Cwtnumber::makeinfinite () {infinite=1;} int Cwtnumber::wtcompare (const cwtnumber& N) Const {/* First is the comparison symbol */if (sign==0&&n.sign!=0)/* PN1 is a positive number,    PN2 is a negative number */return 1;
      /* >*/else if (sign!=0&&n.sign==0)/* PN1 is a negative number, PN2 is a positive number * *  return-1; /* <*/else/////{///////////* Compare Integer part/if (intbits>n.intbits)//PN1 integer digit multiple */RE
        Turn sign?-1:1;
        else if (intbits<n.intbits) return sign?1:-1;    else//* integer digit same */{int i=intbits-1; /* refers to the highest bit */while (i>=0) {if (Intpart[i]>n.intpart[i]) Retu
                RN Sign?-1:1;
                else if (Intpart[i]<n.intpart[i]) return sign?1:-1;
            else i--;
            }/* The integer part is the same, compared to the decimal part of the */for (i=0;i<floatbits&&i<n.floatbits;)
                {if (Floatpart[i]>n.floatpart[i]) return sign?-1:1;
                else if (Floatpart[i]<n.floatpart[i]) return sign?1:-1;
            else i++;
            } if (i<floatbits) return sign?-1:1;
           if (i<n.floatbits) return sign?1:-1; return 0;
    /* Equality */}} int Cwtnumber::iszero () const {if (floatbits==0&&intbits==0) return 1;
    if (floatbits==0&&intbits==1&&intpart[0]==0) return 1;
return 0; } void Cwtnumber::wtadd (const cwtnumber& n1,const cwtnumber& n2,cwtnumber& Res) {Res.initwtnumbertozero (
    );
        if (n1.sign^n2.sign)/* XOR/{Cwtnumber rn2=n2;
        Rn2.sign=n1.sign;
    Wtsubtract (N1,rn2,res);
        else */* the same number */{int maxfloatbits=n1.floatbits>n2.floatbits?n1.floatbits:n2.floatbits;    int addbit=0;
        /* Carry value */int i,j;
            for (i=maxfloatbits-1;i>=0;i--) {int value=n1.floatpart[i]+n2.floatpart[i]+addbit;    ADDBIT=VALUE/10; * To see if more than 10.
        Set Carry value * * RES.FLOATPART[I]=VALUE%10;
        } res.floatbits=maxfloatbits; * * To this, the decimal part of the calculation completed./For (j=0;j<n1.intbits| | j<n2.intbits;j++) {int value=n1.intpart[j]+n2.intpart[j]+addbit;
            ADDBIT=VALUE/10;
            res.intpart[j]=value%10;
        res.intbits++;
            } if (addbit>0) {res.intpart[j]=addbit;
        res.intbits++;    } res.sign=n1.sign;
    /* Decision Symbol */res.adjustbits (); } void Cwtnumber::wtmultiply (const cwtnumber& n1,const cwtnumber& n2,cwtnumber& Res) {Cwtnumber z1=n1,
    Z2=N2;
    Cwtnumber sum;
    int i,j; Sum.
    Initwtnumbertozero ();
    Res.initwtnumbertozero (); Z1.
    Movefloatpoint (z1.floatbits); Z2.
    Movefloatpoint (z2.floatbits);    /* COMPUTE Z1*Z2 * * for (i=0;i<z2.intbits;i++) {cwtnumber tmp;
        /* Store temporary product/int addbit=0;
        Tmp.intbits=z1.intbits+i;
            for (j=0;j<z1.intbits;j++) {int value = Z2.intpart[i]*z1.intpart[j]+addbit;
            ADDBIT=VALUE/10;
        tmp.intpart[j+i]=value%10;
   } if (addbit) {tmp.intpart[j+i]=addbit;         tmp.intbits++;
        } wtadd (Sum,tmp,res);
    Sum=res;
    } res=sum;
    Res.movefloatpoint (-(n1.floatbits+n2.floatbits));
/* Judging symbol, the number is negative/if (n1.sign^n2.sign) res.sign=1; } void Cwtnumber::wtsubtract (const cwtnumber& n1,const cwtnumber& n2,cwtnumber& Res) {Res.initwtnumberto
    Zero ();
        if (n1.sign^n2.sign)/* Cwtnumber rn2=n2;
        Rn2.sign=n1.sign;
    Wtadd (N1,rn2,res); else *///{int cmp=n1.
        Wtcompare (N2);
        int swapflag,i,maxfloatbits,subtractbit;    if (cmp==0) return;
        /* Equality there is no need to subtract. * * swapflag=n1.sign==0?cmp==-1:cmp==1;
        Const cwtnumber* pn1=&n1;
        Const cwtnumber* pn2=&n2;
            if (swapflag) {const Cwtnumber *T=PN1;
            PN1=PN2;
        pn2=t;
        } maxfloatbits=pn1->floatbits>pn2->floatbits?pn1->floatbits:pn2->floatbits;    subtractbit=0; /* Abdication Value */* First calculate the decimal part of the * * (i=maxfloatbits-1;i>=0;i--) {if (pn1->floatpart[i]-subtractbit<pn2-&gt
                ; floatpart[i]) {int value=pn1->floatpart[i]-pn2->floatpart[i]-subtractbit+10;
                Subtractbit=1;
            Res.floatpart[i]=value;
                else {int value=pn1->floatpart[i]-pn2->floatpart[i]-subtractbit;
                subtractbit=0;
            Res.floatpart[i]=value;
        }} res.floatbits=maxfloatbits; /* This decimal part is calculated./For (i=0;i<pn1->intbits| |
                i<pn2->intbits;i++) {if (Pn1->intpart[i]-subtractbit<pn2->intpart[i]) {
                int value=pn1->intpart[i]-pn2->intpart[i]-subtractbit+10;
                Subtractbit=1;
            Res.intpart[i]=value; else {int value=pn1->intpart[i]-pn2->intpart[I]-subtractbit;
                subtractbit=0;
            Res.intpart[i]=value;
        } res.intbits++; } res.sign=swapflag?!    N1.sign:n1.sign;
    /* Decision Symbol */res.adjustbits (); } void Cwtnumber::wtdivide (const cwtnumber& n1,const cwtnumber& n2,cwtnumber& Res) {Cwtnumber z1=n1,z2=
    N2;
    int deta=z2.floatbits-z1.floatbits; Z1.
    Movefloatpoint (z1.floatbits); Z2.
    Movefloatpoint (z2.floatbits);
    Res.initwtnumbertozero (); if (N1.
    Iszero ()) return; if (N2.
        Iszero ()) {res.sign=n1.sign;
        Res.makeinfinite ();
    return;                } z1.sign=z2.sign=0;
            /* Uniform symbol for ease of comparison size */while (z1.intbits!=z2.intbits) {/* ensures digital parity, which takes a lot of time/if (z1.intbits<z2.intbits) { Z1.
            Movefloatpoint (1);
        deta--; else {Z2.
            Movefloatpoint (1);
        deta++; }} while (res.floatbits< (INT_BIT_MAX/2)) {INT cmp=z1.
      Wtcompare (Z2);  int n=10;
        Cwtnumber Mulres,subres; if (cmp==-1) {/*z1<z2*/z1.
            Movefloatpoint (1);
            res.floatpart[res.floatbits++]=0;
        Continue
            else if (cmp==0) {/*z1==z2*/res.floatpart[res.floatbits++]=1;
        Break
            do {* * * Find business/cwtnumber tmp;
            Tmp.intpart[0]=--n;
            Tmp.intbits=1;
        Wtmultiply (Z2,tmp,mulres); while (Cmp=mulres.
        Wtcompare (z1)) ==1);
        Res.floatpart[res.floatbits++]=n;
        if (cmp==0) break;
        Wtsubtract (Z1,mulres,subres); Subres.
        Movefloatpoint (1);
    Z1=subres;
    } res.movefloatpoint (1);
    Res.movefloatpoint (DETA);
/* Judging symbol, the number is negative/if (n1.sign^n2.sign) res.sign=1;
    } char *cwtnumber::result (const char *val1,const char *val2,pfncalc pfncalc) {Cwtnumber n1,n2,res; N1.
    Strtowtnumber (VAL1); N2.
    Strtowtnumber (VAL2);
    Pfncalc (N1,n2,res); return Res.
ToString (); } char* CwtnumBer::tostring () {freestring ();
    M_sz=new Char[strlenbywtnumber ()];
    Wtnumbertostr (M_SZ);
return M_SZ;
        } cwtnumber& cwtnumber::operator= (const cwtnumber& N) {if (this!=&n) {freestring ();
        memcpy (this,&n,sizeof (Cwtnumber));
        if (N.M_SZ) {m_sz=strdup (N.M_SZ);
} return *this;
    } cwtnumber cwtnumber::operator+ (const cwtnumber& N) {cwtnumber res;
    Cwtnumber::wtadd (*this,n,res);
return res;
    } cwtnumber cwtnumber::operator-(const cwtnumber& N) {cwtnumber res;
    Cwtnumber::wtsubtract (*this,n,res);
return res;
    } cwtnumber cwtnumber::operator* (const cwtnumber& N) {cwtnumber res;
    Cwtnumber::wtmultiply (*this,n,res);
return res;
    } cwtnumber cwtnumber::operator/(const cwtnumber& N) {cwtnumber res;
    Cwtnumber::wtdivide (*this,n,res);
return res;
    } cwtnumber& cwtnumber::operator+= (const cwtnumber& N) {cwtnumber n1=*this,n2=n; CWtnumber::wtadd (N1,n2,*this);
return *this;
    } cwtnumber& cwtnumber::operator-= (const cwtnumber& N) {cwtnumber n1=*this,n2=n;
    Cwtnumber::wtsubtract (N1,n2,*this);
return *this;
    } cwtnumber& cwtnumber::operator*= (const cwtnumber& N) {cwtnumber n1=*this,n2=n;
    Cwtnumber::wtmultiply (N1,n2,*this);
return *this;
    } cwtnumber& cwtnumber::operator/= (const cwtnumber& N) {cwtnumber n1=*this,n2=n;
    Cwtnumber::wtdivide (N1,n2,*this);
return *this;  BOOL Cwtnumber::operator> (const cwtnumber& N) {return wtcompare (n) ==1;} bool Cwtnumber::operator>= (const cwtnumber& N) {return wtcompare (n)!=-1: bool cwtnumber::operator< (const cwtnumber& N) {return Wtco
Mpare (n) ==-1; BOOL Cwtnumber::operator<= (const cwtnumber& N) {return wtcompare (n)!=1;} bool cwtnumber::operator== (const C wtnumber& N) {return wtcompare (n) ==0: bool cwtnumber::operator!= (const cwtnumber& N) {return wtcomPare (n)!=0; }

Use Demo:
#include <stdio.h>
#include "WTNumber.h"

int main ()
{
    char sz1[256]= "";
    Char sz2[256]= "";
    Puts ("Please enter two digits:");
    while (scanf ("%s%s", SZ1,SZ2)!=-1) {Cwtnumber N1 (sz1)
        , N2 (SZ2);
        printf ("Addition of two numbers: \n%s\n", (N1+N2). ToString ());
        printf ("Subtract two results: \n%s\n", (N1-N2). ToString ());
        printf ("Multiply by two results: \n%s\n", (N1*N2). ToString ());
        printf ("Two number division result: \n%s\n", (N1/N2). ToString ());
        Puts ("Please enter two digits:");
    }
    return 0;
}

Run Result:
Please enter two digits:
13 7
The result of adding two numbers:
20
Subtract the result of two numbers:
6
Multiply the results by two numbers:
91
Divide the result of two numbers:
1.85714285714285714285714285714285714285714285714285714285714285714285714285714285714285714285714285714285714285714285714 285714

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.