zoukankan      html  css  js  c++  java
  • currency类的建立

      1 //currency类
      2 
      3 #include<iostream>
      4 using namespace std;
      5 
      6 enum signType { p = 1, m = -1 };//枚举
      7 
      8 class currency
      9 {
     10     //enum signType { p, m };//枚举
     11 public:
     12     //构造函数
     13     currency(signType theSign = p,
     14         unsigned long theDollors = 0,
     15         unsigned int theCents = 0);//如果此处有{},则此处为构造函数的定义,如果没有则定义放在类外部
     16     //析构函数
     17     ~currency()
     18     {
     19 
     20     }
     21     void setValue(signType, unsigned long, unsigned long);
     22     void setValue(double);
     23     signType getSign();
     24     unsigned long getDollors();
     25     unsigned int getCents();
     26     currency add(const currency &)const;
     27     //此函数不改变传入的参数,所以有第一个const,此函数不改变调用此函数对象,所以有第二个const
     28     currency & increment(const currency &);
     29     void increment2(const currency &);
     30     //此函数不改变传入的参数,所以加const。但是改变了调用此函数的对象。
     31     void input();//输入值返回给调用对象
     32     currency percent(double x);//返回调用对象的占比x
     33     currency subtract(const currency& x);
     34     currency multiply(double x);//返回调用对象的x倍
     35     currency divide(double x);//返回调用对象的1/x倍
     36     friend ostream & operator <<(ostream & output, const currency & x);
     37 private:
     38     /*
     39     成员变量的初始化有两种方式:
     40     1、列表初始化,是真正的初始化,效率高
     41     2、在构造函数体内赋值,不是真正的初始化,效率较慢
     42     常成员变量必须在构造函数的初始化列表中进行初始化
     43     静态变量只能在类外初始化,且不加关键字static
     44     */
     45     signType sign;
     46     unsigned long dollors;
     47     unsigned int cents;
     48 };//此处的分号不能忘记
     49 currency::currency(signType theSign, unsigned long theDollors, unsigned int theCents)
     50 {
     51     setValue(theSign, theDollors, theCents);
     52 }
     53 void currency::setValue(signType theSign, unsigned long theDollors, unsigned long theCents)
     54 {
     55     if (theCents > 99)
     56         throw("cent should be <99");
     57     sign = theSign;
     58     dollors = theDollors;
     59     cents = theCents;
     60 }
     61 void currency::setValue(double theAmount)
     62 {
     63     if (theAmount > 0)
     64         sign = p;
     65     else
     66         sign = m;
     67     dollors = (unsigned long)theAmount;
     68     cents = (theAmount - dollors + 0.001) * 100;
     69 }
     70 signType currency::getSign()
     71 {
     72     return (*this).sign;//注意this是一个指针,不能用this.sign,正确写法为this->sign或者(*this).sign,*比.优先级低,所以加()
     73 }
     74 unsigned long currency::getDollors()
     75 {
     76     return this->dollors;
     77 }
     78 unsigned int currency::getCents()
     79 {
     80     return this->cents;
     81 }
     82 currency currency::add(const currency & x)const
     83 {
     84     long a1, a2, a3;
     85     currency result;
     86     a1 = dollors * 100 + cents;
     87     if (sign == m)
     88         a1 = -a1;
     89     a2 = x.dollors * 100 + x.cents;
     90     if (x.sign == m)
     91         a2 = -a2;
     92     a3 = a1 + a2;
     93     if (a3 < 0)
     94     {
     95         result.sign = m;
     96         a3 = -a3;
     97     }
     98     else
     99     {
    100         result.sign = p;
    101     }
    102     result.dollors = a3 / 100;
    103     result.cents = a3 - result.dollors * 100;
    104     return result;
    105 }
    106 currency & currency::increment(const currency & x)
    107 {
    108     *this = this->add(x);
    109     return *this;
    110 }
    111 void currency::increment2(const currency & x)
    112 {
    113     long a1, a2, a3;
    114     a1 = dollors * 100 + cents;
    115     if (sign == m)
    116         a1 = -a1;
    117     a2 = x.dollors * 100 + x.cents;
    118     if (x.sign == m)
    119         a2 = -a2;
    120     a3 = a1 + a2;
    121     if (a3 < 0)
    122     {
    123         this->sign = m;
    124         a3 = -a3;
    125     }
    126     else
    127     {
    128         this->sign = p;
    129     }
    130     this->dollors = a3 / 100;
    131     this->cents = a3 - this->dollors * 100;
    132 }
    133 void currency::input()//输入值返回给调用对象
    134 {
    135     double mon;
    136     cout << "请输入金额:";
    137     cin >> mon;
    138     if (mon < 0)
    139         sign = m;
    140     else
    141         sign = p;
    142     dollors = (unsigned long)mon;
    143     cents = (mon - dollors + 0.001) * 100;
    144 }
    145 currency currency::subtract(const currency& x)
    146 {
    147     currency result;
    148     long a1, a2, a3;
    149     a1 = dollors * 100 + cents;
    150     if (sign == m)
    151         a1 = -a1;
    152     a2 = x.dollors * 100 + x.cents;
    153     if (x.sign == m)
    154         a2 = -a2;
    155     a3 = a1 - a2;
    156     if (a3 < 0)
    157         result.sign = m;
    158     else
    159         result.sign = p;
    160     if (a3 < 0)
    161         a3 = -a3;
    162     result.dollors = a3 / 100;
    163     result.cents = a3 - result.dollors * 100;
    164     return result;
    165 }
    166 currency currency::percent(double x)//返回调用对象的占比x
    167 {
    168     currency result;
    169     result.sign = sign;
    170     double mon = (dollors * 100 + cents)*x / 100;
    171     result.dollors = mon / 100;
    172     result.cents = mon - result.dollors * 100;
    173     return result;
    174 }
    175 currency currency::multiply(double x)//返回调用对象的x倍
    176 {
    177     currency result;
    178     double a;
    179     if (x > 0)
    180         result.sign = sign;
    181     else
    182     {
    183         if (sign == p)
    184             result.sign = m;
    185         else
    186             result.sign = p;
    187     }
    188     a = (dollors * 100 + cents)*x;
    189     result.dollors = a / 100;
    190     result.cents = a - result.dollors * 100;
    191     return result;
    192 }
    193 currency currency::divide(double x)//返回调用对象的x倍
    194 {
    195     currency result;
    196     double a;
    197     if (x > 0)
    198         result.sign = sign;
    199     else
    200     {
    201         if (sign == p)
    202             result.sign = m;
    203         else
    204             result.sign = p;
    205     }
    206     a = (dollors * 100 + cents) / x;
    207     result.dollors = a / 100;
    208     result.cents = a - result.dollors * 100;
    209     return result;
    210 }
    211 ostream & operator <<(ostream & output, const currency & x)
    212 {
    213     output << " sign: " << x.sign << " dollors: " << x.dollors << " cents: " << x.cents << endl;
    214     return output;
    215 }
    216 int main()
    217 {
    218     //测试setValue(m,dollors,cents)
    219     currency c1;
    220     c1.setValue(p, 3, 52);
    221     cout << " c1 " << c1 << endl;
    222     //测试构造函数
    223     currency c2(m, 3, 52);
    224     //测试setValue(double)
    225     cout << " c2 " << c2 << endl;
    226     currency c3;
    227     c3.setValue(6.52);
    228     cout << " c3 " << c3 << endl;
    229     cout << " c3 " << c3.getSign() << c3.getDollors() << c3.getCents() << endl;
    230     //测试add
    231     currency c4;
    232     c4 = c4.add(c3);
    233     cout << " c4 " << c4 << endl;
    234 
    235     //测试increment
    236     currency c5;
    237     c5.increment(c1);
    238     cout << " c5 " << c5 << endl;
    239 
    240     //测试increment2
    241     currency c6;
    242     c6.increment2(c2);
    243     cout << " c6 " << c6 << endl;
    244 
    245     //测试默认构造函数
    246     currency c7;
    247     cout << " c7 " << c7 << endl;
    248 
    249     //测试input()
    250     c7.input();
    251     cout << " c7 " << c7 << endl;
    252     //测试减法
    253     currency c8 = c3.subtract(c1);
    254     cout << " c8 " << c8 << endl;
    255     //测试百分比
    256     c8 = c1.percent(64.3);
    257     cout << " c8 " << c8 << endl;
    258     //测试乘法
    259     c8 = c2.multiply(2.1);
    260     cout << " c8 " << c8 << endl;
    261     //测试除法
    262     c8 = c1.divide(3.2);
    263     cout << " c8 " << c8 << endl;
    264 
    265 }
  • 相关阅读:
    从0开始学习 GitHub 系列之「02.加入 GitHub」
    从0开始学习 GitHub 系列之「01.初识 GitHub
    用Redis轻松实现秒杀系统
    算法之美
    Android窗口管理服务WindowManagerService显示Activity组件的启动窗口(Starting Window)的过程分析
    6)django-示例(fbv)
    5)django-模板
    4)django-视图view
    3)django-路由系统url
    2)django-请求生命周期
  • 原文地址:https://www.cnblogs.com/hustsss/p/11176840.html
Copyright © 2011-2022 走看看