C++;自制double类。

 1 #include<iostream>
  2 #include<stdlib.h>
  3 #include<cstring>
  4 #include <signal.h>
  5 #include<cmath>
  6 using namespace std;
  7 class mydouble
  8 {
  9     private:
 10         int zheng;
 11         int xiao;
 12         int len_x;
 13     public:
 14         mydouble(int z = 0, int x = 0, int lenlen =0)
 15             :zheng ( z) ,xiao ( x ),len_x( len_xiao(x))
 16         {
 17             while( xiao %10 ==0&& xiao >0)
 18             {
 19                 xiao/=10;
 20                 len_x--;
 21             }
 22         }
 23 
 24         mydouble&  operator+( mydouble& zj)
 25         {
 26             zheng = zheng + zj.zheng;
 27             int len_temp = len_x;
 28             if(len_x >= len_xiao(zj.xiao))
 29             {
 30                 xiao = xiao + zj.xiao*pow(10,(len_x  - len_xiao(zj.xiao)));
 31             }
 32             else
 33             {
 34                 xiao = zj.xiao + xiao*pow(10,(len_xiao( zj.xiao)  - len_x));
 35 
 36             }
 37             if( len_xiao( xiao) > len_temp)
 38             {
 39                 zheng++;
 40                 xiao = xiao -pow(10,(len_xiao(xiao)-1));                                                                                                                                                                                                                                                                                         
 41             }
 42             return *this;
 43         }
 44         mydouble&  operator-( mydouble& zj)
 45         {
 46             zheng = zheng - zj.zheng;
 47             int len_temp = len_x;
 48             if(len_x >= len_xiao(zj.xiao))
 49             {
 50                 xiao = xiao - zj.xiao*pow(10,(len_x  - len_xiao(zj.xiao)));
 51             }
 52             else
 53             {
 54                 xiao = xiao*pow(10,(len_xiao( zj.xiao)  - len_x)) - zj.xiao;
 55                 len_x = len_xiao( zj.xiao);
 56 
 57             }
 58             if( xiao<0)
 59             {
 60                 zheng--;
 61                 xiao = -1*xiao;
 62             }
 63             return *this;
 64         }
 65         //化成整数,再除回去
 66         mydouble&  operator*( mydouble& zj)
 67         {
 68             int now = zheng*pow(10,len_x) + xiao;
 69             int low = zj.zheng*pow(10,zj.len_x) + xiao;
 70             now = now * low;
 71             int ppow = pow(10,len_x+zj.len_x);
 72             zheng = now/ppow;
 73             xiao = now% ppow;
 74             len_x = len_x +zj.len_x;
 75         }
 76    //化成整数                           
 77    mydouble&  operator/( mydouble& zj)
 78    {
 79        int now = zheng*pow(10,len_x) + xiao;
 80        int low = zj.zheng*pow(10,zj.len_x) + xiao;
 81        if( len_x > zj.len_x)
 82        {
 83            low= low * pow( 10, len_x - zj.len_x);
 84        }
 85        else
 86        {
 87            now= now * pow( 10,zj.len_x - len_x);
 88 
 89        }
 90        //取4位小数
 91        now = (now * pow(10 ,4))/low;
 92        zheng = now/ pow(10 ,4);
 93        xiao = now % 10000;
 94        len_x = 4;
 95    }
 96 
 97 
 98 
 99         int len_xiao(int xxx)
100         {
101             int len = 0;
102             while(xxx != 0 )
103             {
104                 len++;
105                 xxx/=10;
106             }
107             return len;
108         }
109         void show( )
110         {
111             string qqqq="";
112             int cha =len_x - len_xiao(xiao);
113             while( xiao %10 ==0 && xiao >0)
114             {
115                 xiao/=10;
116                 len_x--;
117             }
118             while( cha >0)
119             {
120                 qqqq = qqqq +  "0";
121                 cha--;
122             }
123             cout << zheng <<'.'<<qqqq<<xiao<< endl;
124         }
125 };
126 int main(int argc, const char *argv[])
127 {
128     mydouble abc(100,1);
129     abc.show();
130     mydouble qwe(2,109999);
131     qwe.show();
132     abc / qwe;
133     abc.show();
134     return 0;
135 }
~                                                                                                                                                                                                                                                                                                                                                    
~                                                             

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值