c++ primer plus第六版第十章编程练习

  1. 编写银行用户信息类(数据成员包括储户姓名、帐号和存款)成员函数能执行以下操作:
    创建对象并初始化;
    显示储户姓名、帐号和存款;
    存入参数指定的存款;
    取出参数指定的款项。
    头文件asset类声明
#include <iostream>
#include <string>
using std::string;
class asset
{
private:
    string name;
    string account;
    double deposit;

public:
    asset();
    asset(const string &n, const string &a, double d = 100.0);
    ~asset();
    void show_asset() const;
    void Deposit(double d);
    void withdraw(double d);
};

成员函数定义源文件

#include <iostream>
#include <string>
#include "asset.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
asset::asset()
{
    name = "no name";
    account = "no account";
    deposit = 0.0;
}
asset::asset(const string &n, const string &a, double d)
{
    name = n;
    account = a;
    deposit = d;
}
void asset::show_asset() const
{
    using std::ios_base;
    ios_base::fmtflags org =
        cout.setf(ios_base::fixed, ios_base::floatfield);
    std::streamsize prec = cout.precision(2);

    cout << "Name: " << name << " "
         << "Account: " << account << endl;
    cout << "Deposit: $" << deposit << endl;
}
void asset::Deposit(double d)
{
    deposit += d;
}
void asset::withdraw(double d)
{
    if (d > deposit)
        cout << "Your balance is insufficient, please recharge.hhh" << endl;
    else
        deposit -= d;
}
asset::~asset()
{
    cout << "Bye," << name << endl;
}

主文件

#include <iostream>
#include <string>
#include "asset.h"
using namespace std;
int main()
{
    {
        asset p1;
        asset p2 = asset("John wich", "John", 10000);
        p1.show_asset();
        p2.show_asset();
        p1 = asset("Marry Jane", "Mary");
        p1.Deposit(9976);
        p1.show_asset();
        p2.withdraw(50000);
        p2.show_asset();
        p1.Deposit(5000);
        p1.show_asset();
        system("pause");
        return 0;
    }
}

上面我定义了有输出的析构函数是为了检验一下它的使用,不喜欢的可以去掉的,因为编译器会自动隐式生成并调用析构。
在这里插入图片描述

  1. 根据下面的类定义编写实现和使用程序。
#include <iostream>
#include <string>
using std::string;
class person
{
private:
    static const int LIMIT = 25;
    string lname;
    char fname[LIMIT];

public:
    person()
    {
        lname = "";
        fname[0] = '\0';
    };
    person(const string &ln, const char *fn = "Heyyou");
	void show() const;//firstname lastname format
    void formalshow() const;//lastname,firstname format
};

根据上述声明编写实现
实现文件

#include <iostream>
#include <string>
#include <cstring>
#include "person.h"
using std::cin;
using std::cout;
using std::endl;
using std::string;
person::person()
{
    lname = "";
    fname[0] = '\0';
}
person::person(const string &ln, const char *fn)
{
    lname = ln;
    strcpy(fname, fn);
}
void person::show() const
{
    cout << "Here is the name:" << endl;
    cout << fname << " " << lname << endl;
} //firstname lastname format
void person::formalshow() const
{
    cout << "Here is the name:" << endl;
    cout << lname << "," << fname << endl;
} //lastname,firstname format

主文件

#include <iostream>
#include <string>
#include "person.h"
using namespace std;
int main()
{
    person one;
    person two("Smythecraft");
    person three("Parker", "Peter");
    one.show();
    two.formalshow();
    three.show();
    system("pause");
    return 0;
}

在这里插入图片描述

  1. 以类的手法改写第九章的编程练习1

头文件golf.h:

#include <iostream>
class golf
{
private:
    static const int Len = 20;
    char fullname[Len];
    int handicap;

public:
    golf(const char *c, int ha = 0);
    golf();
    void sethandicap(int ha);
    void show() const;
};

实现文件

#include <iostream>
#include <cstring>
#include "golf.h"
using std::cin;
using std::cout;
using std::endl;
golf::golf()
{
    cout << "Please enter the name: ";
    cin.get(fullname, Len);
    cout << "Please enter the handicaps:";
    cin >> this->handicap;
}
golf::golf(const char *c, int ha)
{
    strcpy(fullname, c);
    handicap = ha;
}
void golf::sethandicap(int ha)
{
    handicap = ha;
}
void golf::show() const
{
    cout << "Name: " << fullname << " "
         << "Handicap: " << handicap << endl;
}
#include <iostream>
#include "golf.h"
using namespace std;
int main()
{
    golf g1 = {"Peter Parker", 3};
    golf g2;
    g1.show();
    g2.show();
    cout << endl;
    g1.sethandicap(20);
    g1.show();
    system("pause");
    return 0;
}

在这里插入图片描述
4. 用类改写第九章编程练习4,并包含在名称空间SALES里面

头文件声明

#include <iostream>
namespace SALES
{
    class sales
    {
    private:
        static const int Quarters = 4;
        double sale[Quarters];
        double average;
        double max;
        double min;

    public:
        sales(const double arr[], int n);
        sales();
        void display() const;
    };
}; // namespace SALES

实现cpp文件

#include <iostream>
#include "sales.h"
using std::cin;
using std::cout;
using std::endl;
namespace SALES
{
    sales::sales(const double arr[], int n)
    {
        double temp;
        for (int i = 0; i < n; i++)
        {
            for (int j = 1; j < n; j++)
            {
                temp = (arr[i] < arr[j]) ? arr[i] : arr[j];
            }
            sale[i] = temp;
        }
        average = (sale[0] + sale[1] + sale[2] + sale[3]) / 4;
        max = sale[3];
        min = sale[0];
    }
    sales::sales()
    {
        double total = 0.0;
        double temp = 0.0, ma, mi;
        cout << "Enter the sales:";
        for (int i = 0; i < 4; i++)
        {
            cin >> sale[i];
            total += sale[i];
            cout << "Next:" << endl;
        }
        ma = sale[0];
        mi = sale[0];
        for (int j = 1; j < 4; j++)
        {
            ma = (sale[j] > ma) ? sale[j] : ma;
            mi = (sale[j] < mi) ? sale[j] : mi;
        }
        average = total / 4;
        max = ma;
        min = mi;
    }
    void sales::display() const
    {
        cout << "Here is the sales :" << endl;
        for (int i = 0; i < 4; i++)
        {
            cout << "#" << (i + 1) << " :$" << sale[i] << endl;
        }
        cout << "The max sale is: $" << max << " and the min sale is: $" << min << endl;
        cout << "The average of the 4 quarters is: $" << average << endl;
    }
} // namespace SALES

主文件

#include <iostream>
#include "sales.h"
using namespace std;
using namespace SALES;
int main()
{
    double sales_[8] = {100, 200, 300, 400, 600, 800, 500, 700};
    sales s;
    sales s1(sales_, 8);
    s1.display();
    s.display();
    system("pause");
    return 0;
}

这里出现了一点问题,但我不知道原因在哪,就先放着了。现象就是,在我完成s的构造后,显示输入信息的时候出现了意外输出,就是无论怎么输入,对象s的成员sale[0]一直都是700,然后我在代码里加进去了地址的输出,结果是这样的
在这里插入图片描述
等我搞明白再出后续。
10.4.2后续:
我怀疑是构造函数的问题,然后我改了一下,另建一个默认为0的构造函数,把原本的交互版的构造函数sales改成setsales如下:

#include <iostream>
#include "sales.h"
using std::cin;
using std::cout;
using std::endl;
namespace SALES
{
    sales::sales()
    {
        for (int i = 0; i < Quarters; i++)
            sale[i] = 0.0;
        max = min = average = 0.0;
    }
    sales::sales(const double arr[], int n)
    {
        double temp;
        for (int i = 0; i < n; i++)
        {
            for (int j = 1; j < n; j++)
            {
                temp = (arr[i] < arr[j]) ? arr[i] : arr[j];
            }
            sale[i] = temp;
        }
        average = (sale[0] + sale[1] + sale[2] + sale[3]) / 4;
        max = sale[3];
        min = sale[0];
    }
    void sales::setsales()
    {
        double total = 0.0;
        double ma, mi;
        cout << "Enter the sales:";
        for (int i = 0; i < 4; i++)
        {
            cin >> sale[i];
            total += sale[i];
            cout << "Next:" << endl;
        }
        cin.get();
        ma = sale[0];
        mi = sale[0];
        for (int j = 1; j < 4; j++)
        {
            ma = (sale[j] > ma) ? sale[j] : ma;
            mi = (sale[j] < mi) ? sale[j] : mi;
        }
        average = total / 4;
        max = ma;
        min = mi;
    }
    void sales::display() const
    {
        cout << "Here is the sales :" << endl;
        for (int i = 0; i < 4; i++)
        {
            cout << "#" << (i + 1) << " :$" << sale[i] << endl;
        }
        cout << "The max sale is: $" << max << " and the min sale is: $" << min << endl;
        cout << "The average of the 4 quarters is: $" << average << endl;
    }
} // namespace SALES

测试如下,证明是构造函数的问题,然后问题留给10.4.3解决。
在这里插入图片描述

  1. 使用程序清单10.10中的stack类来来添加和删除customer结构,每次删除时payment值加入到总数中。
    头文件
#include <iostream>
struct customer
{
    char fullname[35];
    double payment;
};
typedef customer Item;

class stack
{
private:
    static const int MAX = 10;
    Item cus[MAX];
    int top;

public:
    stack();
    bool isempty() const;
    bool isfull() const;
    bool push(const Item &cus_);
    bool pop(Item &cus_);
};

实现文件

#include <iostream>
#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 &cus_)
{
    if (top < MAX)
    {
        cus[top++] = cus_;
        return true;
    }
    else
        return false;
}
bool stack::pop(Item &cus_)
{
    if (top > 0)
    {
        cus_ = cus[--top];
        return true;
    }
    else
        return false;
}

主文件

#include <iostream>
#include "stack.h"
using namespace std;
int main()
{
    stack st;
    customer cu;
    double total = 0.0;

    customer cus1 = {"John Wick", 1000};
    if (st.push(cus1))
        cout << cus1.fullname << " has been pushed." << endl;
    else
        cout << "Stack is full." << endl;
    customer cus2 = {"Peter Parker", 100};
    if (st.push(cus2))
        cout << cus2.fullname << " has been pushed." << endl;
    else
        cout << "Stack is full." << endl;

    if (st.pop(cu))
    {
        cout << cu.fullname << " popped." << endl;
        total += cu.payment;
    }
    else
        cout << "Stack is empty." << endl;
    if (st.pop(cu))
    {
        cout << cu.fullname << " popped." << endl;
        total += cu.payment;
    }
    else
        cout << "Stack is empty." << endl;
    cout << "Total paymemt: " << total << endl;
    system("pause");
    return 0;
}

在这里插入图片描述
在使用pop函数中添加了显示,更加明确显示栈的特性。
6. 根据以下类编写函数实现和测试类的程序。

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);
};

函数实现文件

#include <iostream>
#include "move.h"
using std::cout;
using std::endl;
move::move(double a, double b)
{
    x = a;
    y = b;
}
void move::showmove() const
{
    cout << "x=" << x << " ";
    cout << "y=" << y << endl;
}
move move::add(const move &m) const
{
    move temp;
    temp.x = m.x + this->x;
    temp.y = m.y + this->y;
    return temp;
}
void move::reset(double a, double b)
{
    x = a;
    y = b;
}

测试程序

#include <iostream>
#include "move.h"
using namespace std;
int main()
{
    Move s1;
    Move s2(10, 20);
    s1.showmove();
    s2.showmove();
    s1 = Move(10.9, 8.9);
    Move s = s1.add(s2);
    s.showmove();
    system("pause");
    return 0;
}

一个小测试,就不展示输出结果了。
7. 编写Plorg类
包含两个成员数据:plorg的名称和满意指数(CI)
能够进行以下操作:
新的plorg有名称,ci值为50;
ci值可修改;
Plorg可以报告其名称和满意指数;
默认名称为“Plorga”。
类声明如下:

class Plorg
{
private:
    static const int Max = 20;
    char fullname[Max];
    int CI;

public:
    Plorg(char *name = "Plorga", int n = 50);
    void setci(int n);
    void display() const;
};

函数实现文件

#include <iostream>
#include <cstring>
#include "plorg.h"
using std::cout;
using std::endl;
Plorg::Plorg(char *name, int n)
{
    strcpy(fullname, name);
    CI = n;
}
void Plorg::setci(int n)
{
    CI = n;
}
void Plorg::display() const
{
    cout << "The name: " << fullname << " and the Satisfaction Index: " << CI << "." << endl;
}

测试程序

#include <iostream>
#include "plorg.h"
using namespace std;
int main()
{
    Plorg p1;
    Plorg p2("John Wick");
    Plorg p3("Peter Parker", 20);
    p1.display();
    p2.display();
    p3.display();
    p1.setci(90);
    p1.display();
    p1 = Plorg("Marry");
    p1.display();
    system("pause");
    return 0;
}

以上代码编译可行,不过由于构造函数的“-Wwrite-strings”问题,会出现很多警告,哈哈哈。
在这里插入图片描述

  1. 简单列表
    可存储0或多个某种类型的列表;
    可创建空列表;
    可在列表中添加数据项;
    可确定列表是否为空;
    可确定列表是否为满;
    可访问列表中的每一个数据项,并进行某种操作;

头文件类声明

typedef double Item;
class List
{
private:
    static const int Max = 10;
    Item item[Max];
    int top;

public:
    List();
    bool isempty() const;
    bool isfull() const;
    bool add(const Item &i);
    void visit(void (*pf)(Item &));
};
void show(Item &i);//用于visit用的非成员函数

实现文件

#include <iostream>
#include <cstring>
#include "list.h"
using std::cout;
using std::endl;
List::List()
{
    top = 0;
}
bool List::isempty() const
{
    return top == 0;
}
bool List::isfull() const
{
    return top == Max;
}
bool List::add(const Item &i)
{
    if (top < Max)
    {
        item[top++] = i;
        return true;
    }
    else
        return false;
}
void List::visit(void (*pf)(Item &))
{
    for (int i = 0; i < top; i++)
    {
        cout << "#" << (i + 1) << " ";
        pf(item[i]);
    }
}
void show(Item &i)
{
    cout << i << endl;
}

测试程序

#include <iostream>
#include "list.h"
using namespace std;
int main()
{
    List list;
    Item i1 = 10;
    Item i2 = 20;
    Item i3 = -20;
    if (list.isempty())
    {
        cout << "List is empty." << endl;
    }
    list.add(i1);
    list.add(i2);
    list.add(i3);
    if (list.isfull())
        cout << "List is full." << endl;
    else
        cout << "Iist is not full." << endl;
    if (list.isempty())
        cout << "List is empty." << endl;
    else
        cout << "It is not empty." << endl;
    list.visit(show);
    system("pause");
    return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值