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 << "\n"
        << "Number: " << number << "\n"
        << "Deposit: " << deposit << "\n";
}

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] = '\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] == '\0')
        return 0;
    return 1;
}
int Golf::setgolf() const {
    if (fullname[0] == '\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類來表示這種抽象類型。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM