数据结构、算法与应用 (C++描述) 第二版 1.16
来源:互联网 发布:刘统勋 知乎 编辑:程序博客网 时间:2024/06/06 01:59
按照惯例,分了三个部分(头文件,头文件实现,main)来写。只是按照他的要求粗略的写了一下,没有优化。 **仅供有需要的人以参考,如有错误请纠正我**
头文件: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 setValue(signType theSign, unsigned long Dollars, unsigned int Cents); void setValue(double theAmount); signType getSign() const; unsigned long getDollars() const; unsigned int getCents() const; currency operator+(const currency & x) const; friend std::ostream & operator<<(std::ostream & out, const currency &x); void input(); void subtract(double x); currency & percent(double x); currency & multiply(double x); currency & divide(double x);};#endif
头文件实现:currency.cpp
#include"currency.h"currency::currency(signType theSign, unsigned long theDollars, unsigned int theCents){ setValue(theSign, theDollars, theCents);}void currency::setValue(signType theSign, unsigned long Dollars, unsigned int Cents){ using namespace std; if (Cents > 99) throw "Cents should be < 100"; amount = Dollars * 100 + Cents; if (theSign == minu) amount = -amount;}void currency::setValue(double theAmount){ if (theAmount < 0) amount = (long)((theAmount - 0.001) * 100); 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; result.amount = amount + x.amount; return result;}std::ostream & operator<<(std::ostream & out, const currency & x){ long theAmount = x.amount; if (theAmount < 0) { out << '-'; theAmount = -theAmount; } long dollars = theAmount / 100; out << '$' << dollars << '.'; int cents = theAmount - dollars * 100; if (cents < 10) out << '0'; out << cents; return out;}void currency::input(){ using std::cin; using std::cout; using std::endl; long dollars; int cents; cout << "Dollars: "; cin >> dollars; cout << "Cents: "; cin >> cents; if ((dollars > 0 && cents < 0) || (dollars < 0 && cents > 0)) throw "Dollars and cents symbols must be the same."; if (dollars < 0) setValue(minu, dollars, cents); else setValue(plu, dollars, cents);}void currency::subtract(double x){ amount = amount - (long)(x * 100); std::cout << "subtract: " << *this << std::endl;}currency & currency::percent(double x){ amount = amount * (x / 100); return *this;}currency & currency::multiply(double x){ amount = amount * x; return *this;}currency & currency::divide(double x){ amount = amount / x; return *this;}
测试文件:main.cpp
#include"currency.h"#include<iostream>#include<cstdlib>int main(){ using namespace std; currency g, i, j; currency h(plu, 3, 50); g.setValue(minu, 2, 25); i.setValue(-6.45); j = h + g; cout << h << " + " << g << " = " << j << endl; j = i + g + h; cout << i << " + " << g << " + " << h << " = " << j << endl; /*异常测试 cout << "Attempting to initialize with cents = 152" << endl; try { i.setValue(plu, 3, 152); } catch (char * e) { cout << "Caught thrown exception : " << e << endl; exit(EXIT_FAILURE); } */ currency test; try { test.input(); } catch (char * e) { cout << "test.input error: " << e << endl; exit(EXIT_FAILURE); } cout << "test: " << test << endl; test.subtract(2.5); cout << "percent: " << test.percent(10) << endl; cout << "multiply: " << test.multiply(5.0) << endl; cout << "divide: " << test.divide(2.0) << endl; return 0;}
0 0
- 数据结构、算法与应用 (C++描述) 第二版 1.16
- 数据结构、算法与应用 (C++描述) 第二版 1.1
- 数据结构、算法与应用(C++描述)第二版 1.3
- 数据结构、算法与应用 (C++描述) 第二版 1.2
- 数据结构、算法与应用 (C++描述) 第二版 1.4
- 数据结构、算法与应用 (C++描述) 第二版 1.5
- 数据结构、算法与应用 (C++描述) 第二版 1.6
- 数据结构、算法与应用(C++描述) 第二版 1.7
- 数据结构、算法与应用 (C++描述) 第二版 1.8
- 数据结构、算法与应用 (C++描述) 第二版 1.9
- 数据结构、算法与应用 (C++描述) 第二版 1.10
- 数据结构、算法与应用 (C++描述) 第二版 1.11
- 数据结构、算法与应用 (C++描述) 第二版 1.12
- 数据结构、算法与应用 (C++描述) 第二版 1.13
- 数据结构、算法与应用 (C++描述) 第二版 1.14
- 数据结构、算法与应用 (C++描述) 第二版 1.15
- 数据结构、算法与应用 (C++描述) 第二版 1.18
- 数据结构、算法与应用 (C++描述) 第二版 1.19
- windows下安装pip
- Date类型数据转化json后,在jsp获取日期显示为[object Object]问题
- 仿滴滴打车底部滑动条代码逻辑实现
- Android-Gradle基础脚本解析
- myeclipse package explorer显示问题及maven配置问题
- 数据结构、算法与应用 (C++描述) 第二版 1.16
- 完美网络
- orcle 序列
- 《人月神话-32周年中文纪念版》-读书笔记-第二章:The Mythical Man-Month(人月神话)
- printf("%d",5.01)和printf("%f",5)的输出结果
- P2P通讯协议定义说明书
- 使用ThreadLocal变量的时机和方法
- C++ 拷贝构造函数和赋值构造函数机制详解
- LoadRunner Mysql性能优化