zoukankan      html  css  js  c++  java
  • C++ Primer Plus章节编程练习(第十章)

    1、为复习题5描述的类提供方法定义,并编写一个小程序来演示所有特性。

       复习题5:定义一个类来表示银行账户。数据成员包括储户姓名、账号(使用字符串)和存款。成员函数执行如下操作

      ~创建一个对象并将其初始化;

      ~显示储户姓名、账号和存款;

      ~存入参数指定的存款;

      ~取出参数指定的存款。

    //account.h
    #ifndef ACCOUNT_H
    #define ACCOUNT_H
    #include <string>
    class Account {
    private:
        std::string name;
        std::string number;
        double deposit;
    public:
        Account(std::string _name, std::string _number = "Error", double _deposit = 0);
        void show() const;
        void save_money(double money);
        void draw_money(double money);
    };
    #endif // !ACCOUNT_H
    
    
    //Account.cpp
    #include "stdafx.h"
    #include "account.h"
    #include <iostream>
    #include <string>
    Account::Account(std::string _name, std::string _number, double _deposit) {
        name = _name;
        number = _number;
        deposit = _deposit;
    }
    
    void Account::show() const{
        using std::cout;
        cout << "Name: " << name << "
    "
            << "Number: " << number << "
    "
            << "Deposit: " << deposit << "
    ";
    }
    
    void Account::save_money(double money) {
        if (number == "Error")
            std::cout << "Wrong ! ";
        else
            deposit += money;
    }
    
    void Account::draw_money(double money) {
        if (number == "Error")
            std::cout << "Wrong !";
        else if (deposit < money)
            std::cout << "You have no enough money!";
        else
            deposit -= money;
    }
    
    
    //main.cpp
    #include "stdafx.h"
    #include "account.h"
    int main()
    {
        Account test = Account("Tony Hust", "M201876177", 5000.00);
        test.show();
        test.save_money(998.37);
        test.show();
        test.draw_money(100000.00);
        test.show();
        test.draw_money(2554.73);
        test.show();
        return 0;
    }

    2、下面是一个非常简单的类定义,它使用了一个string对象和一个字符数组,让您能够比较它们的用法。请提供为定义的方法的代码,以完成这个类的实现。再编写一个使用这个类的程序。它使用了三种可能的构造函数调用(没有参数、一个参数和两个参数)以及两种显示方法。

    //person.h
    #ifndef PERSON_H
    #define PERSON_H
    #include <string>
    class Person {
    private:
        static const int LIMIT = 25;
        std::string lname;
        char fname[LIMIT];
    public:
        Person() { lname = "", fname[0] = ''; }
        Person(const std::string & ln, const char * fn = "Heyyou");
        void Show() const;
        void FormalShow() const;
    };
    #endif // !PERSON_H
    
    
    //Person.cpp
    #include "stdafx.h"
    #include "person.h"
    #include <iostream>
    #include <string>
    Person::Person(const std::string & ln, const char * fn) {
        lname = ln;
        strncpy_s(fname, fn, LIMIT);
    }
    
    void Person::Show() const{
        std::cout << "Name: " << fname << " " << lname << std::endl;
    }
    
    void Person::FormalShow() const{
        std::cout << "Name: " << lname << ", " << fname << std::endl;
    }
    
    
    //main.cpp
    #include "stdafx.h"
    #include "person.h"
    #include <iostream>
    
    int main()
    {
        Person one;
        Person two("Smythecraft");
        Person three("Dimwiddy", "Sam");
        one.Show();
        one.FormalShow();
        std::cout << std::endl;
        two.Show();
        two.FormalShow();
        std::cout << std::endl;
        three.Show();
        three.FormalShow();
        return 0;
    }

     3、完成第九章的编程练习1,但要用正确的golf类声明替换那里的代码。用合适参数的构造函数替换setgolf(golf &, const char *, int),以提供初始值。保留setgolf()的交互版本,但要用构造函数来实现它(例如,setgolf()的代码应该获得数据,将数据传递给构造来创建一个临时对象,并将其赋给调用对象,即*this)。

    //golf.h
    #ifndef GOLF_H
    #define GOLF_H
    #include <cstring>
    class Golf {
    private:
        static const int Len = 40;
        char fullname[Len];
        int handicap;
    public:
        Golf(const char * na, int ha) { strncpy_s(fullname, na, Len); handicap = ha; }
        int setgolf(const char *na, int ha);
        int setgolf() const;
        void sethandicap(const int ha);
        void showgolf() const;
    };
    #endif // !GOLF_H
    
    
    //Golf.cpp
    #include "stdafx.h"
    #include "golf.h"
    #include <iostream>
    int Golf::setgolf(const char * na, int ha) {
        Golf g = Golf(na, ha);
        *this = g;
        if (fullname[0] == '')
            return 0;
        return 1;
    }
    int Golf::setgolf() const {
        if (fullname[0] == '')
            return 0;
        return 1;
    }
    void Golf::sethandicap(const int ha) {
        handicap = ha;
    }
    void Golf::showgolf() const {
        std::cout << "fullname: " << fullname << " , and handicap: " << handicap << std::endl;
    }

    4、完成第九章的编程练习4,但将Sales结构及相关的函数转换为一个类及其方法。用构造函数替换setSales(sales &, double [ ], int)函数。用构造函数实现setSales(Sales &)方法的交互版本。将类保留再名称空间SALES中。

    //sales.h
    #ifndef SALES_H
    #define SALES_H
    namespace SALES {
        class Sales {
        private:
            static const int QUARTERS = 4;
            double sales[QUARTERS];
            double average;
            double max;
            double min;
        public:
            Sales(const double ar[], int n);
            Sales(Sales & s);
            void showSales() const;
        };
    }
    #endif // !SALES_H
    
    
    //Sales.cpp
    #include "stdafx.h"
    #include "sales.h"
    #include <iostream>
    using namespace SALES;
    Sales::Sales(const double ar[], int n) {
        int i;
        for (i = 0; i < n && i < 4; i++)    //赋值
            sales[i] = ar[i];
    
        for (int j = i; j < 4; j++)    //将未赋值的设置为0
            sales[j] = 0;
    
        double total = 0;
        for (int j = 0; j < i; j++)    //total为所有有效值的总和
            total += sales[j];
        average = total / i;    //设置平均值
    
        max = min = sales[0];    //最大最小值初始化为第一个值
    
        for (int j = 0; j < i; j++)    //设置最大最小值
        {
            if (sales[j] > max)max = sales[j];
            if (sales[j] < min)min = sales[j];
        }
    }
    
    Sales::Sales(Sales & s) {
        *this = s;
    }
    
    void Sales::showSales()const
    {
        using namespace std;
        std::cout << "输出:" << std::endl;
        for (int i = 0; i < 4 && sales[i] != 0; i++)
            std::cout << "s.sales[" << i << "] = " << sales[i] << std::endl;
        std::cout << "average = " << average << std::endl;
        std::cout << "max = " << max << std::endl;
        std::cout << "min = " << min << std::endl;
    }

    5、考虑下面的结构声明:

    struct customer{

      char fullname[35];

      double payment;

    };

    编写一个程序,它从栈中添加和删除customer结构(栈用Stack类声明表示)。每次customer结构被修改删除时,其payment的值都被加入到总数中,并报告总数。注意:应该可以直接使用Stack类而不做修改。

    //stack.h
    #ifndef STACK_H
    #define STACK_H
    
    struct customer {
        char fullname[35];
        double payment;
    };
    
    typedef customer Item;
    
    class Stack
    {
    private:
        enum { MAX = 10 };
        Item items[MAX];
        int top;
    public:
        Stack();
        bool isempty() const;
        bool isfull() const;
        bool push(const Item & item);
        bool pop(Item & item);
        ~Stack();
    };
    
    #endif // !STACK_H
    
    
    //Stack.cpp
    #include "stdafx.h"
    #include "Stack.h"
    
    Stack::Stack()
    {
        top = 0;
    }
    
    bool Stack::isempty() const {
        return top == 0;
    }
    
    bool Stack::isfull() const {
        return top == MAX;
    }
    
    bool Stack::push(const Item & item) {
        if (top < MAX) {
            items[top++] = item;
            return true;
        }
        else
            return false;
    }
    
    bool Stack::pop(Item & item) {
        if (top > 0) {
            item = items[--top];
            return true;
        }
        else
            return false;
    }
    
    Stack::~Stack()
    {
    }

    6、下面是一个类声明(见Move.h),请提供成员函数的定义和测试这个类的程序

    //Move.h
    #ifndef MOVE_H
    #define MOVE_H
    class Move
    {
    private:
        double x;
        double y;
    public:
        Move(double a = 0, double b = 0);
        void showmove() const;
        Move add(const Move & m) const;
        void reset(double a = 0, double b = 0);
        ~Move();
    };
    #endif // !MOVE_H
    
    
    //Move.cpp
    #include "stdafx.h"
    #include "Move.h"
    #include <iostream>
    Move::Move(double a , double b)
    {
        x = a;
        y = b;
    }
    void Move::showmove() const {
        std::cout << "X: " << x << std::endl;
        std::cout << "Y: " << y << std::endl;
    }
    Move Move::add(const Move & m) const {
        return Move(x + m.x, y + m.y);
    }
    void Move::reset(double a, double b) {
        x = a;
        y = b;
    }
    Move::~Move()
    {
    }
    
    
    //main.cpp
    #include "stdafx.h"
    #include "Move.h"
    #include <iostream>
    using namespace std;
    int main()
    {
        Move one = Move(3.5, 7.2);
        Move two = Move(4.1, 8.9);
        Move three = one.add(two);
        three.showmove();
        three.reset(1.1, 2.2);
        three.showmove();
        return 0;
    }

    7、Betelgeusean plorg有这些特征:

      数据:

      ~plorg的名称不超过19个字符;

      ~plorg有满意指数(CI),这是一个整数;

      操作

      ~新的plorg将有名称,其CI值为50;

      ~plorg的CI可以修改;

      ~plorg可以报告其名称和CI;

      ~plorg的默认名称为“Plorga”

      请编写一个Plorg类声明(包括数据成员和成员函数原型)来表示plorg,并编写成员函数的函数定义。然后编写一个小程序,以演示Plorg类的所有特性。

    //Plorg.h
    #pragma once
    #ifndef PLORG_H
    #define PLORG_H
    #include <iostream>
    #include <cstring>
    class Plorg
    {
    private:
        enum{Len = 19};
        char name[Len];
        int CI;
    public:
        Plorg(const char * na = "Plorga", int _CI = 50) { strncpy_s(name, na, Len); CI = _CI; }
        void setCI(int _CI);
        void show() const;
        ~Plorg();
    };
    #endif !PLORG_H
    
    //Plorg.cpp
    #include "stdafx.h"
    #include "Plorg.h"
    #include <iostream>
    using namespace std;
    void Plorg::setCI(int _CI) {
        CI = _CI;
    }
    
    void Plorg::show() const {
        cout << "Name: " << name << endl;
        cout << "CI: " << CI << endl;
    }
    Plorg::~Plorg()
    {
    }
    
    //main.cpp #include "stdafx.h" #include "Plorg.h" int main() { Plorg _p; _p.show(); Plorg p = Plorg("#noten", 48); p.show(); p.setCI(42); p.show(); return 0; }

    8、可以将简单列表描述成下面这样:

      ~可存储0个或多个某种类型的列表;

      ~可创建空列表;

      ~可在列表中添加数据项;

      ~可确定列表是否为空;

      ~可确定列表是否为满;

      ~可访问列表中每一个数据项,并对它执行某种操作。

      可以看到,这个列表确实很简单,例如,它不允许插入或删除数据项。请设计一个List类来表示这种抽象类型。

  • 相关阅读:
    Codeforces 1255B Fridge Lockers
    Codeforces 1255A Changing Volume
    Codeforces 1255A Changing Volume
    leetcode 112. 路径总和
    leetcode 129. 求根到叶子节点数字之和
    leetcode 404. 左叶子之和
    leetcode 104. 二叉树的最大深度
    leetcode 235. 二叉搜索树的最近公共祖先
    450. Delete Node in a BST
    树的c++实现--建立一棵树
  • 原文地址:https://www.cnblogs.com/SChenqi/p/9778218.html
Copyright © 2011-2022 走看看