数据结构、算法与应用 (C++描述) 第二版 1.16

按照惯例,分了三个部分(头文件,头文件实现,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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Data Structures, Algorithms, and Applications in C++, Second Edition 出版者的话 译者序 前言 第一部分 预备知识 第1章 C++回顾 1.1 引言 1.2 函数与参数 1.2.1 传值参数 1.2.2 模板函数 1.2.3 引用参数 1.2.4 常量引用参数 1.2.5 返回值 1.2.6 重载函数 1.3 异常 1.3.1 抛出异常 1.3.2 处理异常 1.4 动态存储空间分配 1.4.1 操作符new 1.4.2 一维数组 1.4.3 异常处理 1.4.4 操作符delete 1.4.5 二维数组 1.5 自有数据类型 1.5.1 类currency 1.5.2 一种不同的描述方法 1.5.3 操作符重载 1.5.4 友元和保护性类成员 1.5.5 增加#ifndef、#define和#endif语句 1.6 异常类illegalParameterValue 1.7 递归函数 1.7.1 递归的数学函数 1.7.2 归纳 1.7.3 C++递归函数 1.8 标准模板库 1.9 测试与调试 1.9.1 什么是测试 1.9.2 测试数据的设计 1.9.3 调试 1.10 参考及推荐读物 第2章 程序性能分析 2.1 什么是程序性能 2.2 空间复杂度 2.2.1 空间复杂度的组成 2.2.2 举例 2.3 时间复杂度 2.3.1 时间复杂度的组成 2.3.2 操作计数 2.3.3 最好、最坏和平均操作计数 2.3.4 步数 第3章 渐近记法 3.1 引言 3.2 渐近记法 3.2.1 大Ο记法 3.2.2 渐近记法Ω和Θ 3.3 渐近数学(可选) 3.3.1 大O记法 3.3.2 Ω记法 3.3.3 Θ记法 3.3.4 小ο记法 3.3.5 特性 3.4 复杂度分析举例 3.5 实际复杂度 3.6 参考及推荐读物 第4章 性能测量 4.1 引言 4.2 选择实例的大小 4.3 设计测试数据 4.4 实验设计 4.5 高速缓存 4.5.1 简单计算机模型 4.5.2 缓存未命中对运行时间的影响 4.5.3 矩阵乘法 4.6 参考及推荐读物 第二部分 数据结构 第5章 线性表——数组描述 5.1 数据对象和数据结构 5.2 线性表数据结构 5.2.1 抽象数据类型linearList 5.2.2 抽象类linearList 5.3 数组描述 5.3.1 描述 5.3.2 变长一维数组 5.3.3 类arrayList 5.3.4 C++迭代器 5.3.5 arrayList的一个迭代器 5.4 vector的描述 5.5 在一个数组中实现的多重表 5.6 性能测量 5.7 参考及推荐读物 第6章 线性表——链式描述 6.1 单向链表 6.1.1 描述 6.1.2 结构chainNode 6.1.3 类chain 6.1.4 抽象数据类型linearList的扩充 6.1.5 类extendedChain 6.1.6 性能测量 6.2 循环链表和头节点 6.3 双向链表 6.4 链表用到的词汇表 6.5 应用 6.5.1 箱子排序 6.5.2 基数排序 6.5.3 凸包 6.5.4 并查集 第7章 数组和矩阵 7.1 数组 7.1.1 抽象数据类型 7.1.2 C++数组的索引 7.1.3 行主映射和列主映射 7.1.4 用数组的数组来描述 7.1.5 行主描述和列主描述 7.1.6 不规则二维数组 7.2 矩阵 7.2.1 定义和操作 7.2.2 类matrix 7.3 特殊矩阵 7.3.1 定义和应用 7.3.2 对角矩阵 7.3.3 三对角矩阵 7.3.4 三角矩阵 7.3.5 对称矩阵 7.4 稀疏矩阵 7.4.1 基本概念 7.4.2 用单个线性表描述 7.4.3 用多个线性表描述 7.4.4 性能测量 第8章 栈 8.1 定义和应用 8.2 抽象数据类型 8.3 数组描述 8.3.1 作为一个派生类实现 8.3.2 类arrayStack 8.3.3 性能测量 8.4 链表描述 8.4.1 类derivedLinkedStack 8.4.2 类linkedStack 8.4.3 性能测量 8.5 应用 8.5.1 括号匹配 8.5.2 汉诺塔 8.5.3 列车车厢重排 8.5.4 开关盒布线 8.5.5 离线等价类问题 8.5.6 迷宫老鼠 8.6 参考及推荐读物 第9章 队列 9.1 定义和应用 9.2 抽象数据类型 9.3 数组描述 9.3.1 描述 9.3.2 类arrayQueue 9.4 链表描述 9.5 应用 9.5.1 列车车厢重排 9.5.2 电路布线 9.5.3 图元识别 9.5.4 工厂仿真 9.6 参考及推荐读物 第10章
数据结构算法应用C语言描述第二版)》是一本以C语言为基础,介绍数据结构算法的教材。下面将从内容概述、特点和应用三个方面进行回答。 首先,本书的内容主要包括:线性表、栈和队列、树、图、排序、查找等常见的数据结构算法。通过对这些基本数据结构算法的介绍,读者可以深入理解其原理和实现方式。 其次,本书的特点有以下几点:首先,基于C语言进行讲解,读者可以通过实例代码来理解数据结构算法的实现方式,提升自己的编程能力。其次,采用了结构化的教学方式,将知识点分成小块进行讲解,并通过习题和实例加深读者对知识点的理解。再次,书中每个知识点都有实际应用的示例,读者可以通过实例来了解数据结构算法在实际中的应用场景。 最后,本书的应用主要包括:软件开发、算法设计和数据结构设计等领域。在软件开发中,数据结构算法是必备的基础知识,可以帮助开发人员设计和优化程序。在算法设计领域,本书介绍了常见的排序、查找和图算法等,为读者提供了一些常用算法的基础。在数据结构设计领域,本书详述了线性表、树和图等数据结构的实现方式,读者可以基于这些知识进行数据结构的设计和实现。 总结来说,《数据结构算法应用C语言描述第二版)》是一本基于C语言的数据结构算法教材,通过具体的代码实例和应用示例,帮助读者深入理解数据结构算法的原理和实现方式,并且适用于软件开发、算法设计和数据结构设计等领域的应用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值