C++ primer第十章课后练习

在这里插入图片描述

//头文件
#ifndef Account_H_
#define Account_H_
#include<string>
class Account
{
private:
	std::string user;
	long account;
	double save;
public:
	Account();//default constructors
	Account(const std::string& co, long ac = 0, double money = 0.0);
	~Account();//do-nothing destructor
	void show() const;
	void stock(double money);
	void take(double money);
};
#endif
//constructors
Account::Account()
{
	user = "no name";
	account = 0;
	save = 0;
}
Account::Account(const std::string& co, long ac , double money )
{
	user = co;
	if (ac == 0)
	{
		std::cout << "no user" << std::endl;
		account = 0;
	}
	else
	{
		account = ac;
	}
	save = money;
}
//class destrutor
Account::~Account()
{}
void Account:: show() const
{
	using std::cout;
	using std::endl;
	cout << "user:" << user<<endl;
	cout<< "Account:" << account<<endl;
	cout << "save " << save<< "yuan."<<endl;

}
void Account::stock(double money)
{
	save = save + money;
}
void Account::take(double money)
{
	save = save - money;
}
//主函数
#include "Account_10_1.h"
int main()
{
	
	Account a = Account("user1", 11111111, 100.1);
	a.show();
	a.stock(100);
	a.show();
	a.take(30);
	a.show();
}

在这里插入图片描述
在这里插入图片描述

//head
#ifndef Person_H_
#define Person_H_
#include <string>
using namespace std;
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;
	void FormalShow() const;

};

#endif // !

//function
#include <iostream>
#include <cstring>
#include "10_2.h"

Person::Person(const string& ln, const char* fn)
{
    lname = ln;
    strcpy_s(fname, fn);
}

void Person::Show() const
{
    std::cout << "The name format is:" << endl;
    std::cout << fname << "(firstname), ";
    std::cout << lname << "(lastname)." << endl;
    return;
}

void Person::FormalShow() const
{
    std::cout << "The name format is:" << endl;
    std::cout << lname << "(lastname), ";
    std::cout << fname << "(firstname)." << endl;
    return;
}

//main
#include <iostream>
#include "10_2.h"

int main()
{
    using std::cout;
    using std::endl;

    Person one;
    Person two("Smythecraft");
    Person three("Dimwiddy", "Sam");
    one.Show();
    one.FormalShow();
    cout << endl;
    two.Show();
    two.FormalShow();
    cout << endl;
    three.Show();
    three.FormalShow();
    cout << endl;

    return 0;
}

在这里插入图片描述

#ifndef GOLF_H_
#define GOLF_H_
class GOLF
{
private:
    static const int Len = 40;
    char fullname[Len];
    int handicap;
public:
    GOLF( const char* name, int hc);

    GOLF ();
    ~GOLF();

    void set_handicap( int hc);

    void showgolf();

};


#endif

#include"10_3.h"
#include<cstring>
#include<iostream>
using namespace std;

GOLF::GOLF(const char* name, int hc)
{
	strncpy_s(this->fullname, name, Len);//指定长度字符串复制到数组中
	this->fullname[Len - 1] = '\0';
	this->handicap = hc;
}


GOLF::GOLF()
{
        char tempname[40];
        int temphandicap;

        cout << "Please enter the fullname(enter to quit): ";
        cin.getline(tempname, Len);
        cout << "Please enter the handicap: ";
        while (!(cin >> temphandicap))
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Please enter an number: ";
        }
        cin.get();
        *this = GOLF(tempname, temphandicap); //调用默认构造函数创建一个临时对象赋值给调用对象;
    
}
GOLF::~GOLF() {};

void GOLF::set_handicap(int hc)
{
    this->handicap = hc;

}

void GOLF::showgolf()
{
    cout << "fullname:" << this->fullname << endl;
    cout << "handicap:" << this->handicap;
}
#include <iostream>
#include "10_3.h"

int main()
{
    using std::cout;
    using std::endl;

    GOLF temp1;
    GOLF temp2("Cloverx", 666);

    cout << "The starting information1:" << endl;
    temp1.showgolf();
    temp1.set_handicap(998);
    cout << "After changing the handicap1:" << endl;
    temp1.showgolf();
    cout << "The starting information2:" << endl;
    temp2.showgolf();
    temp2.set_handicap(888);
    cout << "After changing the handicap2:" << endl;
    temp2.showgolf();

    return 0;
}

在这里插入图片描述

#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(double ar[], int n = 0); //默认构造函数;
        Sales();                       //用户自定义构造函数;
        void show_sales() const;
    };
}

#endif

#include <iostream>
#include "sales.h"

namespace SALES
{
    Sales::Sales(double ar[], int n) //默认构造函数;
    {
        double total = 0.0;
        double max = ar[0];
        double min = ar[0];
        for (int i = 1; i < n; i++)
        {
            this->sales[i] = ar[i];
            total += ar[i];
            if (ar[i] > max)
            {
                max = ar[i];
            }
            if (ar[i] < min)
            {
                min = ar[i];
            }
        }
        this->min = min;
        this->max = max;
        this->average = total / n;
    }

    Sales::Sales()
    {
        using namespace std;
        int len;
        cout << "Enter the length of sales(<= 4 and > 0): ";
        while (!(cin >> len) || len > 4 || len <= 0)
        {
            cin.clear();
            while (cin.get() != '\n')
                continue;
            cout << "Please enter a number(<= 4 and > 0): ";
        }
        double *temp = new double[len];
        cout << "Please enter the sales:" << endl;
        for (int i = 0; i < len; i++)
        {
            cout << "Please enter the content #" << i + 1 << ": ";
            while (!(cin >> temp[i]))
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
                cout << "Please enter a number: ";
            }
        }
        *this = Sales(temp, len); //调用默认构造函数赋值给调用对象;
        delete[] temp;
    }

    void Sales::show_sales() const
    {
        std::cout << "Sales average: " << this->average << std::endl;
        std::cout << "Sales max: " << this->max << std::endl;
        std::cout << "Sales min: " << this->min << std::endl;
        return;
    }
}

#include <iostream>
#include "sales.h"

int main()
{
    using namespace SALES;
    double temp[4] = {1.0, 2.0, 3.0, 4.0};
    Sales objects[2] = {Sales(temp, 4), Sales()}; //首元素默认初始化,次元素用户初始化;

    std::cout << "The first object information:" << std::endl;
    objects[0].show_sales();
    std::cout << "The second object information:" << std::endl;
    objects[1].show_sales();
    std::cout << "Bye." << std::endl;

    return 0;
}

在这里插入图片描述

#ifndef STACK_H_
#define STACK_H_

typedef struct customer
{
    char fullname[35];
    double payment;
} 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);
};

#endif

#include "stack.h"

Stack::Stack()
{
    top = 0;
}

bool Stack::isempty() const
{
    return 0 == top;
}

bool Stack::isfull() const
{
    return MAX == top;
}

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

#include <iostream>
#include <cctype>
#include "stack.h"

int main()
{
    using namespace std;
    char ch;
    Stack st;
    Item temp;
    double total = 0.0;

    cout << "a to add a customer." << endl;
    cout << "d to delete a customer." << endl;
    cout << "q to exit the menu." << endl;
    cout << "Please enter your choice: ";
    while (cin >> ch && tolower(ch) != 'q')
    {
        while (cin.get() != '\n')
            continue;
        if (tolower(ch) != 'a' && tolower(ch) != 'd') //处理错误选择;
        {
            cout << "Please enter a, d or q: ";
            continue;
        }
        switch (tolower(ch))
        {
        case 'a':
        {
            cout << "Enter the customer's fullname: ";
            cin.getline(temp.fullname, 35);
            cout << "Enter the customer's payment: ";
            while (!(cin >> temp.payment)) //处理错误非数值输入;
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
                cout << "Please enter an number: ";
            }
            if (st.isfull())
            {
                cout << "Can't add new customer." << endl;
            }
            else
            {
                st.push(temp);
            }
            break;
        }
        case 'd':
        {
            if (st.isempty())
            {
                cout << "No any customer.\n";
            }
            else
            {
                st.pop(temp);
                total += temp.payment; //累计payment的数值;
                cout << "Customer " << temp.fullname << " will quit." << endl;
                cout << "Now the total payments are: " << total << endl; //报告当前total的总数;
            }
            break;
        }
        }
        cout << "\n\n\n";
        cout << "a to add a customer." << endl;
        cout << "d to delete a customer." << endl;
        cout << "q to exit the menu." << endl;
        cout << "Please enter your choice: ";
    }
    cout << "Bye." << endl;

    return 0;
}

在这里插入图片描述

#ifndef MOVE_H_
#define MOVE_H_

class Move
{
private:
    double x;
    double y;

public:
    Move(double a = 0.0, double b = 0.0);
    void showmove() const;
    Move add(const Move &m) const;//Move是返回类型
    void reset(double a = 0.0, double b = 0.0);
};

#endif

#include <iostream>
#include "move.h"

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

Move Move::add(const Move &m) const
{
    Move temp;
    temp.x = m.x + this->x; //加上调用对象的x值;
    temp.y = m.y + this->y; //加上调用对象的y值;
    return temp;            //返回值为一个新对象;
}

void Move::reset(double a, double b)
{
    x = a;
    y = b;
    return;
}

在这里插入图片描述

#ifndef PLORG_H_
#define PLORG_H_

class Plorg
{
private:
    char fullname[20];
    int ci;

public:
    Plorg(); //默认构造函数;
    void reset_ci();
    void show_plorg() const;
    void create_new_plorg(const char *newname);
};

#endif

#include <iostream>
#include <cstring>
#include "plorg.h"

Plorg::Plorg()
{
    strcpy(fullname, "Plorga");
}

void Plorg::reset_ci()
{
    using std::cin;
    using std::cout;
    int my_ci;

    cout << "Please enter an new ci number: ";
    while (!(cin >> my_ci)) //修改CI;
    {
        cin.clear();
        while (cin.get() != '\n')
            continue;
        cout << "Please enter an number: ";
    }
    this->ci = my_ci;
    return;
}

void Plorg::show_plorg() const
{
    using std::cout;
    using std::endl;

    cout << "The plorg name is: " << this->fullname << endl;
    cout << "The plorg ci is: " << this->ci << endl;
    return;
}

void Plorg::create_new_plorg(const char *newname)
{
    strncpy(fullname, newname, 20); //新名称不超过20个字符;
    fullname[19] = '\0';
    this->ci = 50;
    return;
}

#include <iostream>
#include "plorg.h"

int main()
{
    using std::cout;
    using std::endl;
    Plorg temp;

    cout << "The starting plorg information:" << endl;
    temp.show_plorg();
    temp.create_new_plorg("Cloverx");
    cout << "\nAfter changing name and ci:" << endl;
    temp.show_plorg();
    temp.reset_ci();
    cout << "\nAfter changing ci:" << endl;
    temp.show_plorg();
    cout << "Bye." << endl;

    return 0;
}

在这里插入图片描述

#ifndef LIST_H_
#define LIST_H_
typedef int Item;//类型为int
class LIST 
{
private:
	static const int MAX = 10;
	Item items[MAX];
	int index;
public:
	LIST();
	void add_data(Item item);
	bool is_empty();
	bool is_full();
	void visit(void(*pf)(Item&));

};
#endif // !LIST_H_

#include"10_8.h"
using namespace std;
LIST::LIST()
{
	this->index = 0;
};
void LIST::add_data(Item item)
{
	this->items[index++] = item;
	return;
}
bool LIST::is_empty()
{
	return 0 == index;
}
bool LIST::is_full()
{
	return MAX == index;
}
void LIST::visit(void(*pf)(Item &))//调用函数
{
	for (int i = 0; i < this->index; i++)
	{
		(*pf)(this->items[i]);
	}
	return;
}
#include <iostream>
#include <cctype>
#include "10_8.h"

void traverse(Item& item);

int main()
{
    using namespace std;
    char ch;
    Item temp;
    LIST mylist;

    cout << "The list include following functions:" << endl;
    cout << "a to add an number." << endl;
    cout << "v to visit every number." << endl;
    cout << "q to exit the menu." << endl;
    cout << "Please enter your choice: ";
    while (cin >> ch && tolower(ch) != 'q')
    {
        while (cin.get() != '\n')
            continue;
        if (tolower(ch) != 'a' && tolower(ch) != 'v') //处理错误选择;
        {
            cout << "Please enter a, v or q: ";
            continue;
        }
        switch (tolower(ch))
        {
        case 'a':
        {
            cout << "Please enter an number: ";
            while (!(cin >> temp)) //处理错误非数值输入;
            {
                cin.clear();
                while (cin.get() != '\n')
                    continue;
                cout << "Please enter an number again: ";
            }
            if (mylist.is_full())
            {
                cout << "The list is full. Can't add new number." << endl;
            }
            else
            {
                mylist.add_data(temp);
                cout << "Add number " << temp << " successfully." << endl;
            }
            break;
        }
        case 'v':
        {
            if (mylist.is_empty())
            {
                cout << "No number.\n";
            }
            else
            {
                cout << "Visit every number:" << endl;
                mylist.visit(traverse);
            }
            break;
        }
        }
        cout << "\n\n\n";
        cout << "The list include following functions:" << endl;
        cout << "a to add an number." << endl;
        cout << "v to visit every number." << endl;
        cout << "q to exit the menu." << endl;
        cout << "Please enter your choice: ";
    }
    cout << "Bye." << endl;

    return 0;
}

void traverse(Item& item)
{
    std::cout << item << ' ';
    return;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值