C++ primer plus第十二章编程练习答案

1.对于下面的声明:

class Cow{
char name[20] ;
char + hobby;

double weight ;

public:
Cow();
Cow(const char * nm,const char * ho,double wt);

Cow(const Cow c&);

~Cow();
Cow  operator=(const Cow & c);

void ShowCow() const; // display all cow data

给这个类提供实现,并编写一个使用所有成员函数的小程序。

#include 
#include "cow.h"

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

    Cow temp1;
    Cow temp2("MZZDX", "Programming", 30);
    Cow temp3(temp2);

    cout << "Here are some cows:" << endl;
    cout << "The first:" << endl;
    temp1.ShowCow();
    cout << "The second:" << endl;
    temp2.ShowCow();
    cout << "The third:" << endl;
    temp3.ShowCow();
    temp1 = temp3;
    cout << "After assignment for temp1:" << endl;
    temp1.ShowCow();

    return 0;
}
#ifndef COW_H_
#define COW_H_

class Cow
{
private:
    char name[20];
    char *hobby;
    double weight;

public:
    Cow();
    Cow(const char *nm, const char *ho, double wt);
    Cow(const Cow &c);
    ~Cow();
    Cow &operator=(const Cow &c);
    void ShowCow() const;
};

#endif
#include 
#include "cow.h"
#include 

Cow::Cow()
{
    std::strcpy(name, "\0");
    hobby = new char[1];
    hobby[0] = '\0';
    weight = 0.0;
}

Cow::Cow(const char *nm, const char *ho, double wt)
{
    std::strncpy(name, nm, 20);
    name[19] = '\0';
    hobby = new char[strlen(ho) + 1];
    std::strcpy(hobby, ho);
    weight = wt;
}

Cow::Cow(const Cow &c)
{
    std::strcpy(name, c.name);
    hobby = new char[strlen(c.hobby) + 1];
    std::strcpy(hobby, c.hobby);
    weight = c.weight;
}

Cow::~Cow()
{
    delete[] hobby;
}

Cow &Cow::operator=(const Cow &c)
{
    if (this != &c)
    {
        delete[] hobby;
        std::strcpy(name, c.name);
        hobby = new char[strlen(c.hobby) + 1];
        std::strcpy(hobby, c.hobby);
        weight = c.weight;
    }
    return *this;
}

void Cow::ShowCow() const
{
    std::cout << "Cow name: " << name << std::endl;
    std::cout << "Cow hobby: " << hobby << std::endl;
    std::cout << "Cow weight: " << weight << "\n\n";
}

2.通过完成下面的工作来改进String 类声明(即将 Stringl.h升级为 String2.h)。
a.对+运算符进行重载,使之可将两个学符串合并成1个。
b.提供一个Stringlow()成员函数,将字符审中所有的字母字符转换为小写(别忘了cctype 系列字符函数)

c.提供String()成员函数,将字符串中所有字母字符转换成大写。
d,提供一个这样的成员函数,它接受一个 char 参数,返回该字符在字符串中出现的次数使用下面的程序来测试您的工作:

// pe122.cPP
#include
using namespace std;
#include "string2.h"
int main(]
String sl("and I am a C++ student.");
String s2 -"please enter your name: ";
String s3;
// overloaded << operator
cout ec s2:
cin >> s3;// overloaded >> operators2="My name is"+s3;// overloaded =,+ operatorscout << B2 <<, n";
g2-g2+81:
s2,stringup();// converts string to uppercasecout c<"The string\n"cc s2 cc"ncontaing" sl = "red";// String(const char ).// then String & operatora(const String5)string rgb[3] =String(sl],String("green"),String("blue"));cout <<"Entcr the name of a primary color for mixing light:";String ans;
bool success = false;
while (cin >> ans]
ans.stringlow():/ converts string to lowercasefor (inti=0;i<3;i++)if (ans == rqb[i]) // overloaded == operatorcout c< "That's right!\n";success = true;break:
if (success)
break;
else

cout ce "Try again!\n";
cout <<"Bye\n";return 0;

输出应与下面相似:
Please enter your name: Fretta Farbo
My name is Pretta Farbo.
The string
MY NAME IS FRETTA FARBO AND I AM A C++ STUDENTcontains 6"A' characters in it.
Enter the name of a primary color for mixing light: yellowTry again!
blUE
That'sright!
Bye

#include 
using namespace std;
#include "string2.h"

int main()
{
    String s1(" and I am a C++ student.");
    String s2 = "Please enter your name: ";
    String s3;

    cout << s2;
    cin >> s3;
    s2 = "My name is " + s3;
    cout << s2 << ".\n";
    s2 = s2 + s1;
    s2.stringup();
    cout << "The string \n";
    cout << s2 << "\ncontains " << s2.has('A');
    cout << " 'A' characters in it.\n";
    s1 = "red";

    String rgb[3] = {String(s1), String("green"), String("blue")};
    cout << "Enter the name of a primary color for mixing light: ";
    String ans;
    bool success = false;
    while (cin >> ans)
    {
        ans.stringlow();
        for (int i = 0; i < 3; i++)
        {
            if (ans == rgb[i])
            {
                cout << "That's right!\n";
                success = true;
                break;
            }
        }
        if (success)
        {
            break;
        }
        else
        {
            cout << "Try again!\n";
        }
    }
    cout << "Bye\n";

    return 0;
}
#ifndef STRING2_H_
#define STRING2_H_
#include 
using std::istream;
using std::ostream;

class String
{
private:
    char *str;                    //字符串首字符指针;
    int len;                      //记录字符串长度;
    static int num_strings;       //记录构造的字符串数目;
    static const int CINLIM = 80; //限制输入字符个数;

public:
    String(const char *s);
    String();
    String(const String &st);
    ~String();
    int length() const { return len; }
    String &operator=(const String &st);
    String &operator=(const char *s);
    String operator+(const char *s);
    char &operator[](int i);
    const char &operator[](int i) const;
    friend bool operator<(const String &st1, const String &st2);
    friend bool operator>(const String &st1, const String &st2);
    friend bool operator==(const String &st1, const String &st2);
    friend ostream &operator<<(ostream &os, const String &st);
    friend istream &operator>>(istream &is, String &st);
    friend String operator+(const char *s, const String &st);
    friend String operator+(const String &st1, const String &st2);
    void stringlow();
    void stringup();
    unsigned has(const char s) const;
    static int HowMany();
};

#endif
#include 
#include 
#include "string2.h"
using std::cin;
using std::cout;

int String::num_strings = 0;

int String::HowMany()
{
    return num_strings;
}

String::String(const char *s)
{
    len = std::strlen(s);
    str = new char[len + 1];
    std::strcpy(str, s);
    num_strings++;
}

String::String()
{
    len = 4;
    str = new char[1];
    str[0] = '\0';
    num_strings++;
}

String::String(const String &st)
{
    num_strings++;
    len = st.len;
    str = new char[len + 1];
    std::strcpy(str, st.str);
}

String::~String()
{
    --num_strings;
    delete[] str;
}

String &String::operator=(const String &st)
{
    if (this == &st)
    {
        return *this;
    }
    delete[] str;
    len = st.len;
    str = new char[len + 1];
    std::strcpy(str, st.str);
    return *this;
}

String &String::operator=(const char *s)
{
    delete[] str;
    len = std::strlen(s);
    str = new char[len + 1];
    std::strcpy(str, s);
    return *this;
}

String String::operator+(const char *s)
{
    int length = strlen(s) + len;
    char *temp = new char[length + 1];
    strcpy(temp, str);
    strcat(temp, s);
    String new_str(temp);
    delete[] temp;
    return new_str;
}

char &String::operator[](int i)
{
    return str[i];
}

const char &String::operator[](int i) const
{
    return str[i];
}

bool operator<(const String &st1, const String &st2)
{
    return (std::strcmp(st1.str, st2.str) < 0);
}

bool operator>(const String &st1, const String &st2)
{
    return st2 < st1;
}

bool operator==(const String &st1, const String &st2)
{
    return (std::strcmp(st1.str, st2.str) == 0);
}

ostream &operator<<(ostream &os, const String &st)
{
    os << st.str;
    return os;
}

istream &operator>>(istream &is, String &st)
{
    char temp[String::CINLIM];
    is.get(temp, String::CINLIM);
    if (is)
    {
        st = temp;
    }
    while (is && is.get() != '\n')
        continue;
    return is;
}

String operator+(const char *s, const String &st)
{
    int length = strlen(s) + st.len;
    char *temp = new char[length + 1];
    strcpy(temp, s);
    strcat(temp, st.str);
    String new_str(temp);
    delete[] temp;
    return new_str;
}

String operator+(const String &st1, const String &st2)
{
    int length = st1.len + st2.len;
    char *temp = new char[length + 1];
    strcpy(temp, st1.str);
    strcat(temp, st2.str);
    String new_str(temp);
    delete[] temp;
    return new_str;
}

void String::stringlow()
{
    for (int i = 0; i < len; i++)
    {
        str[i] = tolower(str[i]);
    }
}

void String::stringup()
{
    for (int i = 0; i < len; i++)
    {
        str[i] = toupper(str[i]);
    }
}

unsigned String::has(const char s) const
{
    unsigned count = 0;

    for (int i = 0; i < len; i++)
    {
        count += (s == str[i]);
    }
    return count;
}


3.新编写程序清单 10.7 和程序清单 10.8 描述的 Stok 类,使之使用动分配的内存,而不是 string类对象来存储股票名称。另外,使用重载的 operator<<0定义代替show(成员函数。再使用程序清单 10.9测试新的定义程序。

#include 
#include "stock20.h"

const int STKS = 4;

int main()
{
    Stock stocks[STKS] = 
    {
        Stock("NanoSmart", 12, 20.0),
        Stock("Boffo Objects", 200, 2.0),
        Stock("Monolithic Obelisks", 130, 3.25),
        Stock("Fleep Enterprises", 60, 6.5)
    };

    std::cout << "Stock holdings:\n";
    for (int st = 0; st < STKS; st++)
    {
        std::cout << stocks[st] << std::endl;
    }
    const Stock *top = &stocks[0];
    for (int st = 1; st < STKS; st++)
    {
        top = &top->topval(stocks[st]);
    }
    std::cout << "\nMost valuable holding:\n";
    std::cout << *top;

    return 0;
}
#ifndef STOCK20_H_
#define STOCK20_H_
#include 

class Stock
{
private:
    char *company;
    int shares;
    double share_val;
    double total_val;
    void set_tot() { total_val = shares * share_val; }

public:
    Stock();
    Stock(const char *s, long n = 0, double pr = 0.0);
    ~Stock();
    void buy(long num, double price);
    void sell(long num, double price);
    void update(double price);
    friend std::ostream &operator<<(std::ostream &os, const Stock &st);
    const Stock &topval(const Stock &s) const;
};

#endif
#include 
#include "stock20.h"
using namespace std;
#include 

Stock::Stock()
{
    company = new char[1];
    company[0] = '\0';
    shares = 0;
    share_val = 0.0;
    total_val = 0.0;
}

Stock::Stock(const char *s, long n, double pr)
{
    company = new char[strlen(s) + 1];
    std::strcpy(company, s);
    if (n < 0)
    {
        std::cout << "Number of shares can't be negative; ";
        std::cout << company << " shares set to 0.\n";
        shares = 0;
    }
    else
    {
        shares = n;
    }
    share_val = pr;
    set_tot();
}

Stock::~Stock()
{
    delete[] company;
}

void Stock::buy(long num, double price)
{
    if (num < 0)
    {
        std::cout << "Number of shares purchased can't be negative. ";
        std::cout << "Transaction is aborted.\n";
    }
    else
    {
        shares += num;
        share_val = price;
        set_tot();
    }
}

void Stock::sell(long num, double price)
{
    using std::cout;
    if (num < 0)
    {
        cout << "Number of shares sold can't be negative. ";
        cout << "Transaction is aborted.\n";
    }
    else if (num > shares)
    {
        cout << "You can't sell more than you have! ";
        cout << "Transaction is aborted.\n";
    }
    else
    {
        shares -= num;
        share_val = price;
        set_tot();
    }
}

void Stock::update(double price)
{
    share_val = price;
    set_tot();
}

std::ostream &operator<<(std::ostream &os, const Stock &st)
{
    os << "Company: " << st.company << std::endl;
    os << "Shares: " << st.shares << std::endl;
    os << "Share Price: " << st.share_val << std::endl;
    os << "Total Worth: " << st.total_val << std::endl;
    return os;
}

const Stock &Stock::topval(const Stock &s) const
{
    return s.total_val > total_val ? s : *this;
}

4.请看下面程序清单10.10 定义的 Stack 类的变量:

// stack.h -- class declaration for the stack ADTtypedef unsigned long Item;
class Stack
private:
enum(MAX-10;Item* pitems;int size;
int topi
// constant epecific to clasc
// holds stack items
// number of elements in stack
// index for top stack item
public:
Stack(int n = MAX):// creates stack with n elementsStack(const Stack & st);-Stack();
bool isempty) const;
bool isfull() const;
// push() returns false if stack already is full,true otherwisebool push(const Item & item); // add item to stack// pop() returns false if stack already is empty, true otherwisebool pop(Item &item): // pop top into itemStack & operator=(const Stack  st];

正如私有成员表明的,这个类使用动态分配的数组来保存栈项。请重新编写方法,以适应这种新的表示法,并编写一个程序来演示所有的方法,包括复制构造函数和赋值运算符。

#include 
#include "stack.h"

int main()
{
    using namespace std;
    Stack st;
    Item temp = 1000UL;
    st.push(temp);
    temp = 2000UL;
    st.push(temp);
    temp = 3000UL;
    st.push(temp);
    Stack st1(st);
    Stack st2;
    st2 = st1;
    cout << "Here are some stack contents:" << endl;
    cout << "Stack st:" << endl;
    cout << st;
    cout << "Stack st1:" << endl;
    cout << st1;
    cout << "Stack st2:" << endl;
    cout << st2;
    cout << "Bye\n";

    return 0;
}
#ifndef STACK_H_
#define STACK_H_
#include 

typedef unsigned long Item;

class Stack
{
private:
    enum {MAX = 10};
    Item *pitems;
    int size;
    int top;

public:
    Stack(int n = MAX);
    Stack(const Stack &st);
    ~Stack();
    bool isempty() const;
    bool isfull() const;
    bool push(const Item &item);
    bool pop(Item &item);
    Stack &operator=(const Stack &st);
    friend std::ostream &operator<<(std::ostream &os, const Stack &st);
};

#endif
#include "stack.h"

Stack::Stack(int n)
{
    top = 0;
    if (n > MAX) //长度大于MAX时的情况;
    {
        std::cout << "The length of stack can't exceed 10.\n";
        std::cout << "So initialize the length to 10.\n";
        size = MAX;
    }
    else if (n < 0) //长度小于0的情况;
    {
        std::cout << "The length of stack can't less than 0.\n";
        std::cout << "So initialize the length to 10.\n";
        size = MAX;
    }
    else
    {
        size = n;
    }
    pitems = new Item[size];
}

Stack::Stack(const Stack &st)
{
    size = st.size, top = st.top;
    pitems = new Item[size];
    for (int i = 0; i < top; i++)
    {
        pitems[i] = st.pitems[i];
    }
}

Stack::~Stack()
{
    delete[] pitems;
}

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

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

bool Stack::push(const Item &item)
{
    if (top < MAX)
    {
        pitems[top++] = item;
        return true;
    }
    else
    {
        return false;
    }
}

bool Stack::pop(Item &item)
{
    if (top > 0)
    {
        item = pitems[--top];
        return true;
    }
    else
    {
        return false;
    }
}

Stack &Stack::operator=(const Stack &st)
{
    if (this == &st)
    {
        return *this;
    }
    delete[] pitems;
    size = st.size;
    top = st.top;
    pitems = new Item[size];
    for (int i = 0; i < top; i++)
    {
        pitems[i] = st.pitems[i];
    }
    return *this;
}

std::ostream &operator<<(std::ostream &os, const Stack &st)
{
    for (int i = st.top - 1; i >= 0; i--)
    {
        os << st.pitems[i] << std::endl;
    }
    return os;
}

5.Heather 银行进行的研究表明,ATM 客户不希望排队时间不超过1分钟使用程序清单1210中的模拟,找出要使平均等候时间为1分钟,每小时到达的客户数应为多少(试验时间不短于 100 小时)?

18

6.Heather 银行想知道,如果再开设一台 ATM,情况将如何。请对模拟进行修改,以包含两个队列。假设当第一台ATM 前的排队人数少于第二台ATM 时,客户将排在第一队,否则将排在第二队。然后再找出要使平均等候时间为1分钟,每小时到达的客户数应该为多少(注意,这是一个非线性问题,即将ATM数量加倍,并不能保证每小时处理的客户数量也翻倍,并确保客户等候的时间少于1 分钟)?

#include 
#include 
#include 
#include "queue.h"
using namespace std;
const int MIN_PER_HR = 60;

bool newcustomer(double x);

int main()
{
    srand(time(0));

    cout << "Case Study: Bank of Heather Automatic Teller\n";
    cout << "Enter maximum size of queue: ";
    int qs;
    cin >> qs;
    Queue line1(qs); //构造第一台ATM机;
    Queue line2(qs); //构造第二台ATM机;

    cout << "Enter the number of simulation hours: ";
    int hours;
    cin >> hours;
    long cyclelimit = MIN_PER_HR * hours;

    cout << "Enter the average number of customers per hour: ";
    double perhour;
    cin >> perhour;
    double min_per_cust;
    min_per_cust = MIN_PER_HR / perhour;

    long turnaways = 0;
    long customers = 0;
    long served = 0;
    long sum_line = 0;
    int wait_time1 = 0; //第一台ATM机的等待时间;
    int wait_time2 = 0; //第二台ATM机的等待时间;
    long line_wait = 0;

    for (int cycle = 0; cycle < cyclelimit; cycle++)
    {
        Item temp;
        if (newcustomer(min_per_cust))
        {
            if (line1.isfull() && line2.isfull())
                turnaways++;
            else
            {
                customers++;
                temp.set(cycle);
                if (line1.queuecount() <= line2.queuecount()) //第一台ATM机人数小于第二台;
                {
                    line1.enqueue(temp); //顾客排在第一队;
                }
                else
                {
                    line2.enqueue(temp); //否则排在第二队;
                }
            }
        }
        if (wait_time1 <= 0 && !line1.isempty())
        {
            line1.dequeue(temp);
            wait_time1 = temp.ptime();
            line_wait += cycle - temp.when();
            served++;
        }
        if (wait_time1 > 0)
        {
            wait_time1--;
        }
        sum_line += line1.queuecount();
        if (wait_time2 <= 0 && !line2.isempty())
        {
            line2.dequeue(temp);
            wait_time2 = temp.ptime();
            line_wait += cycle - temp.when();
            served++;
        }
        if (wait_time2 > 0)
        {
            wait_time2--;
        }
        sum_line += line2.queuecount();
    }

    if (customers > 0)
    {
        cout << "customers accepted: " << customers << endl;
        cout << "  customers served: " << served << endl;
        cout << "         turnaways: " << turnaways << endl;
        cout << "average queue size: ";
        cout.precision(2);
        cout.setf(ios_base::fixed, ios_base::floatfield);
        cout << (double)sum_line / cyclelimit << endl;
        cout << " average wait time: ";
        cout << (double)line_wait / served << " minutes\n";
    }
    else
    {
        cout << "No customers!\n";
    }
    cout << "Done!\n";

    return 0;
}

bool newcustomer(double x)
{
    return (std::rand() * x / RAND_MAX < 1);
}
#ifndef QUEUE_H_
#define QUEUE_H_

class Customer
{
private:
    long arrive;
    int processtime;

public:
    Customer() : arrive(0L), processtime(0) {}
    void set(long when);
    long when() const { return arrive; }
    int ptime() const { return processtime; }
};

typedef Customer Item;

class Queue
{
private:
    struct Node{ Item item; struct Node *next; };
    enum { Q_SIZE = 10 };
    Node *front;
    Node *rear;
    int items;
    const int qsize;
    Queue(const Queue &q) : qsize(0) {}
    Queue &operator=(const Queue &q) { return *this; }

public:
    Queue(int qs = Q_SIZE);
    ~Queue();
    bool isempty() const;
    bool isfull() const;
    int queuecount() const;
    bool enqueue(const Item &item);
    bool dequeue(Item &item);
};

#endif
#include "queue.h"
#include 

Queue::Queue(int qs) : qsize(qs)
{
    front = rear = NULL;
    items = 0;
}

Queue::~Queue()
{
    Node *temp;
    while (front != NULL)
    {
        temp = front;
        front = front->next;
        delete temp;
    }
}

bool Queue::isempty() const
{
    return items == 0;
}

bool Queue::isfull() const
{
    return items == qsize;
}

int Queue::queuecount() const
{
    return items;
}

bool Queue::enqueue(const Item &item)
{
    if (isfull())
    {
        return false;
    }
    Node *add = new Node;
    add->item = item;
    add->next = NULL;
    items++;
    if (front == NULL)
    {
        front = add;
    }
    else
    {
        rear->next = add;
    }
    rear = add;
    return true;
}

bool Queue::dequeue(Item &item)
{
    if (front == NULL)
    {
        return false;
    }
    item = front->item;
    items--;
    Node *temp = front;
    front = front->next;
    delete temp;
    if (items == 0)
    {
        rear = NULL;
    }
    return true;
}

void Customer::set(long when)
{
    processtime = std::rand() % 3 + 1;
    arrive = when;
}

你可能感兴趣的:(c++,primer,plus阅读心得,c++,c++,源码,学习,分享)