数据结构、算法与应用 (C++描述) 第二版 1.18
2015-11-16 15:35
225 查看
关于练习1.17,直接用这个就可以了。[数据结构、算法与应用 (C++描述) 第二版 1.16](http://blog.csdn.net/tianluoyuge/article/details/49863769) 关于1.18,重载了**+、-、*、/**和**>>**运算符,对于>>他要求不能写在**公有成员函数**里,所以用**友元**写,**关于+、-运算我写的比较啰嗦,是为了检测正负符号**,因为存储和输出的时候都是按照sign的值来显示符号,而amount里面是正值 **仅供有需要的人以参考,如有错误请纠正我**
头文件:currency.h
#ifndef CURRENCY_H_ #define CURRENCY_H_ #include<iostream> #include<exception> enum signType { plu, minu }; class currency { private: signType sign; long amount; public: currency(signType theSign = plu, unsigned long theDollars = 0, unsigned int theCents = 0); ~currency() { } void operator=(int x); void operator=(double theAmount); signType getSign() const; unsigned long getDollars() const; unsigned int getCents() const; currency operator+(const currency & x) const; currency operator-(const currency & x) const; currency operator*(const currency & x) const; currency operator/(const currency & x) const; friend std::ostream & operator<<(std::ostream & out, const currency &x); friend std::istream & operator>>(std::istream & in, currency & x); }; #endif
头文件实现:currency.cpp
#include"currency.h" currency::currency(signType theSign, unsigned long theDollars, unsigned int theCents) : sign(theSign) { using namespace std; if (theCents > 99) { cerr << "Cents should be < 100"; exit(EXIT_FAILURE); } amount = theDollars * 100 + theCents; if (theSign == minu) amount = -amount; } void currency::operator=(int x) { if (x < 0) { sign = minu; amount = -x; } else { sign = plu; amount = x; } } void currency::operator=(double theAmount) { if (theAmount < 0) { amount = (long)((theAmount - 0.001) * 100); sign = minu; amount = -amount; } else amount = (long)((theAmount + 0.001) * 100); } signType currency::getSign() const { if (amount < 0) return minu; else return plu; } unsigned long currency::getDollars() const { if (amount < 0) return (-amount) / 100; else return amount / 100; } unsigned int currency::getCents() const { if (amount < 0) return -amount - getDollars() * 100; else return amount - getDollars() * 100; } currency currency::operator+(const currency & x) const { currency result; if (sign == plu && x.sign == plu) result.amount = amount + x.amount; else if (sign == plu && x.sign == minu) { if (amount > x.amount) { result.amount = amount - x.amount; result.sign = plu; } else { result.amount = x.amount - amount; result.sign = minu; } } else if (sign == minu && x.sign == plu) { if (amount > x.amount) { result.amount = amount - x.amount; result.sign = minu; } else { result.amount = x.amount - amount; result.sign = plu; } } else if (sign == minu && x.sign == minu) { result.amount = amount + x.amount; result.sign = minu; } return result; } currency currency::operator-(const currency & x) const { currency temp; if (sign == plu && x.sign == plu) { if (amount > x.amount) temp.amount = amount - x.amount; else { temp.amount = x.amount - amount; temp.sign = minu; } } else if (sign == plu && x.sign == minu) { temp.amount = amount + x.amount; temp.sign = plu; } else if (sign == minu && x.sign == plu) { if (amount > x.amount) { temp.amount = amount - x.amount; temp.sign = minu; } else temp.amount = x.amount - amount; } else if (sign == minu && x.sign == minu) { temp.amount = amount + x.amount; temp.sign = minu; } return temp; } currency currency::operator*(const currency & x) const { currency temp; temp.amount = amount * x.amount; if (sign == minu || x.sign == minu) temp.sign = minu; return temp; } currency currency::operator/(const currency & x) const { currency temp; double t1 = amount; double t2 = x.amount; double tt = t1 / t2; temp.amount = tt * 100; if (sign == minu || x.sign == minu) temp.sign = minu; return temp; } std::ostream & operator<<(std::ostream & out, const currency & x) { if (x.sign == minu) out << '-'; long dollars = x.amount / 100; out << '$' << dollars << '.'; int cents = x.amount - dollars * 100; if (cents < 10) out << '0'; out << cents; return out; } std::istream & operator>>(std::istream & is, currency & x) { long temp; is >> temp; if (temp > 0) { x.amount = temp; x.sign = plu; } else { x.amount = -temp; x.sign = minu; } return is; }
测试文件:main.cpp
#include"currency.h" #include<iostream> #include<cstdlib> int main() { using namespace std; currency g, i, j; currency h(plu, 3, 50); g = 25; i = -6.45; cout << "g: " << g << endl; cout << "i: " << i << endl; j = h + g; cout << h << " + " << g << " = " << j << endl; j = i + g + h; cout << i << " + " << g << " + " << h << " = " << j << endl; currency t1; t1 = 100; currency t2; t2 = -50; currency t3; t3 = t1 * t2; cout << "t3: " << t3 << endl; cout << "Enter a number: "; cin >> t3; cout << "t3: " << t3 << endl; t3 = t1 / t2; cout << t1 << " / " << t2 << " = " << t3 << endl; t3 = t1 - t2; cout << t1 << " - " << t2 << " = " << t3 << endl; return 0; }
相关文章推荐
- 数据结构 无序数组java篇
- 数据结构 有序数组java篇
- 数据结构之(2)链表
- 数据结构-二叉树的遍历
- 数据结构、算法与应用 (C++描述) 第二版 1.16
- 数据结构考研复习要点2016
- C. Wilbur and Points(简单贪心加合法性判断)
- 【郝斌数据结构自学笔记】10-11_跨函数使用内存讲解及其示例
- 数据结构、算法与应用 (C++描述) 第二版 1.15
- 数据结构二叉树的遍历
- 【郝斌数据结构自学笔记】9_malloc()动态分配内存概述
- 【郝斌数据结构自学笔记】8_结构体的使用概述
- 【树项目6 - 二叉树的构造——第11周】
- 【2015/11/15】 数据结构学习日志_Day16 链表 我的<LinkList.c>
- 2015年大二上-数据结构-队列(2)- 链队算法库
- HBase系统架构及数据结构(转)
- 数据结构笔记
- 数据结构(Java)——栈的实例
- 数据结构学习(四)二叉树的遍历
- 2015年大二上-数据结构-队列(1)- 顺序环形队列算法库