《C++ Primer Plus》——编程练习答案(8)

第十章

10.10.1

#ifndef BAND_H_
#define BAND_H_
#include<string>
using namespace std;
class Band
{
public:
    Band(string bandName = "NULL", string account="NULL", double deposit = 0.0);
    ~Band();
    void ShowBand();
    void PushBand(double Indesposit);
    void PopBand(double Outdesposit);
private:
    std::string m_bandName;
    std::string m_account;
    double m_deposit;
};
#endif
#include <iostream>
#include "Band.h"
using namespace std;
Band::Band(string bandName, string account, double deposit)
{
    m_bandName = bandName;
    m_account = account;
    m_deposit = deposit;
}

Band::~Band()
{
}
void Band::ShowBand()
{
    cout << "BandName: " << m_bandName << endl;
    cout << "Bandaccount: " << m_account << endl;
    cout << "Desposit: " << m_deposit << endl;
}
void Band::PushBand(double Indesposit)
{
    m_deposit += Indesposit;
}
void Band::PopBand(double Outdesposit)
{
    m_deposit += Outdesposit;
}
#include <iostream>
#include "Band.h"
int main()
{
    Band My("Arthur Yong", "18826451338", 15000.0);
    My.ShowBand();
    My.PushBand(1500.5);
    My.PopBand(3578.9);
    My.ShowBand();
    cin.get();
    return 0;

}

10.10.2

#ifndef PERSON_H_
#define PERSON_H_
#include <string>
using namespace std;
class Person
{
public:
    Person(){ m_lname = ""; m_fname[0] = ‘\0‘; }
    Person(const string &ln, const char *fn = "Heyyou");
    void Show() const;
    void FormalShow() const;
private:
    static const int LIMIT = 25;
    string m_lname;
    char   m_fname[LIMIT];
};
#endif
#include <iostream>
#include "Person.h"
using namespace std;
Person::Person(const string &ln, const char *fn)
{
    m_lname = ln;
    strcpy_s(m_fname, fn);
}

void Person::Show() const
{
    cout << m_fname << " " << m_lname << endl;
}

void Person::FormalShow() const
{
    cout <<  m_lname << " " << m_fname<< endl;
}
#include<iostream>
#include"Person.h"
using namespace std;
int main()
{
    Person one;
    Person two("Smywiddy");
    Person three("Dimwiddy", "Sam");
    two.Show();
    cout << endl;
    three.FormalShow();
    cin.get();
    return 0;
}

10.10.3

#ifndef GOLF_H_
#define GOLF_H_
class golf
{
public:
    golf();
    golf(const char *fullname,int handcap);
    ~golf();
    golf setgolf(const char *fullname,int handcap);
    void showgolf() const;

private:
    static const int m_len = 40;
    char m_fullname[m_len];
    int m_handcap;

};
#endif
#include <iostream>
#include "golf.h"

golf::golf()
{
    m_fullname[0] = ‘\0‘;
    m_handcap = 0;
}
golf::golf(const char *fullname, int handcap)
{
    strcpy_s(m_fullname, fullname);
    m_handcap = handcap;
}
golf::~golf()
{}
golf golf::setgolf(const char *fullname, int handcap)
{
    golf temp = golf(fullname, handcap);
    //golf temp(fullname,handcap);
    strcpy_s(m_fullname, temp.m_fullname);
    m_handcap = temp.m_handcap;
    return *this;
}
void golf::showgolf() const
{
    using namespace std;
    cout << "fullname : " << m_fullname << endl;
    cout << "handcap: " << m_handcap << endl;
}
#include <iostream>
#include "golf.h"
int main()
{
    using namespace std;
    golf A;
    golf B("Arthur Yong", 3);
    B.showgolf();
    A.setgolf("Avalon Y", 5);
    A.showgolf();
    cin.get();
    return 0;

}

10.10.4

#ifndef CUSTOMER_H_
#define CUSTOMER_H_
struct customer
{
    char fullname[35];
    double payment;

};

#endif
#ifndef STACK_H_
#define STACK_H_
template <typename T>
class Stack
{
public:
    Stack(int size = Stack_init_size);
    //构造函数
    ~Stack();
    Stack(const Stack<T> &s);
    //拷贝构造函数
    const Stack<T>& operator=(const Stack<T> &s);
    //赋值构造函数
    void ClearStack();
    //清空栈
    bool StackEmpty();
    //若栈为空返回TRUE否则返回FALSE
    int StackLength();
    //返回栈的长度,即入栈元素个数
    bool GetTop(T &elem);
    //若栈不空,用elem返回栈顶元素,并返回true,否则返回ERROR
    void Push(T elem);
    //插入元素elem为新的栈顶元素
    bool Pop(T &elem);
    //若栈不为空,则删除栈顶元素,用elem返回其值,并返回true,否则返回error

private:
    T *base;
    T *top;
    int m_size;
    enum StackSize
    {
        Stack_init_size = 100,          //存储空间初始分配量
        Stack_incerement = 10            //存储空间分配增加量
    };
};
template <typename T>
Stack<T>::Stack(int size)
{
    base = new T[Stack_init_size];
    if (base==NULL)
    {
        exit(1);
    }
    top = base;
    m_size = size;
}

template <typename T>
Stack<T>::Stack(const Stack<T> &s)
{
    base = new T[s.m_size];
    if (base == NULL)
    {
        exit(1);
    }
    top = base;
    m_size = s.m_size;
    T * p1 =s.base ;
    T * p2 = base;
    while (p1!=s.top)
    {
        *p2 = *p1;
        p1++;
        p2++;
    }
    top = p2;

}
template <typename T>
const Stack<T>& Stack<T>::operator=(const Stack<T> &s)
{
    delete[] base;
    base = new T[s.m_size];
    if (base == NULL)
    {
        exit(1);
    }
    top = base;
    m_size = s.m_size;
    T * p1 = s.base;
    T * p2 = base;
    while (p1 != s.top)
    {
        *p2 = *p1;
        p1++;
        p2++;
    }
    top = p2;
    return *this;
}

template <typename T>
Stack<T>::~Stack()
{
    delete[] base;
}

template <typename T>
int Stack<T>::StackLength()
{
    int n = 0;
    T * p = top;
    while (p!= base)
    {
        n++;
        p--;
    }
    return n;
}
/*
template <typename T>
void Stack<T>::ClearStack()
{

    while (top != base)
    {
        top--;
        *top = 0;

    }

}*/
template <typename T>
bool Stack<T>::StackEmpty()
{
    if (top == base)
    {
        return true;
    }
    return false;
}
template <typename T>
bool Stack<T>::GetTop(T &elem)
{
    if (StackEmpty())
    {
        return false;
    }
    else
    {

        top--;
        elem = *top;
//      *top = 0;
        return true;
    }

}
template <typename T>
void Stack<T>::Push(T elem)
{
    int size_increment;
    if (top - base>Stack_init_size)
    {
        m_size = m_size + Stack_incerement;
        T *temp = new T[m_size];
        T *p = base;
        T *q = temp;
        while ((p + 1) != top)
        {
            *q = *p;
            q++;
            p++;
        }
        delete[] base;
        base = temp;
        *q = elem;
        q++;
    }
    else
    {
        *top = elem;
        top++;
    }

}

template <typename T>
bool Stack<T>::Pop(T &elem)
{
    if (top == base)
    {
        return false;
    }
    top--;
    elem = *top;
//  *top = 0;
    return true;
}

#endif
#include <iostream>
#include "Stack.h"
#include"customer.h"
#include <cstdlib>

int main()
{
    using namespace std;
    Stack<customer> T;
    customer temp[2]=
    {
        { "Arthur Yong",1500.5 }, {"Avalon Y",2000.5}
    };
    T.Push(temp[0]);
    T.Push(temp[1]);
    customer m;
    T.Pop(m);
    cout << m.fullname << "  " << m.payment << endl;
    system("pause");
    return 0;

}

10.10.6

#ifndef MOVE_H_
#define MOVE_H_

using namespace std;
class Move
{
public:
    Move(double x=0.0, double y=0.0){ m_x = x; m_y = y; };
    ~Move(){};
    void showmove() const;
    Move add(const Move & s) const;
    void reset(double a = 0.0, double b = 0.0);

private:
    double m_x;
    double m_y;

};
#endif
#include <iostream>
#include "Move.h"
void Move::showmove() const
{
    cout << "m_x=:  " << m_x << "   " << "m_y=:  " << m_y << endl;
}

Move Move::add(const Move & s) const
{
    double temp_x = m_x + s.m_x;
    double temp_y = m_y + s.m_y;
    Move temp=Move(temp_x,temp_y);
    return temp;

}

void  Move::reset(double a , double b)
{
    m_x = a;
    m_y = b;
}
#include <iostream>
#include "Move.h"

int main()
{
    Move a(2.5,3.5);
    Move b=Move(1.5,2.3);
    Move c;
    a.showmove();
    b.showmove();
    c = a.add(b);
    c.showmove();
    c.reset(5.5, 5.5);
    c.showmove();
    cin.get();
    return 0;
}

10.10.7

#ifndef PLORG_H_
#define PLORG_H_
class plorg
{
public:
    plorg();
    plorg(const char *plorgname, int plorgCI);
    ~plorg();
    void SetCI(int plorgCI);
    void Showplorg();

private:
    static  const int len = 20;
    char m_plorgname[len];
    int m_plorgCI;
};
#endif
#include <iostream>
#include "Plorg.h"

plorg::plorg()
{
    strcpy_s(m_plorgname, "Plorga");
    m_plorgCI = 0;
}
plorg::plorg(const char *plorgname, int plorgCI)
{
    strcpy_s(m_plorgname,plorgname);
    m_plorgCI = plorgCI;
}
plorg::~plorg()
{
}

void plorg::SetCI(int plorgCI)
{
    m_plorgCI = plorgCI;
}
void plorg::Showplorg()
{
    using namespace std;
    cout << "Plorg Name: " << m_plorgname << endl;
    cout << "Plorg CI:   " << m_plorgCI << endl;
}
#include <iostream>
#include"Plorg.h"
int main()
{
    using namespace std;
    plorg A;
    A.Showplorg();
    plorg B("Arthur Yong", 5);
    B.Showplorg();
    cin.get();
    return 0;
}

时间: 2024-11-23 10:20:05

《C++ Primer Plus》——编程练习答案(8)的相关文章

c primer plus 编程练习答案第四章

最近在研读c primer plus,小白一枚,把自己做的答案写出来,望指正. 编程环境 visual studio 2010.第五章. 1 /* Programming Exercise 5-1 */ 2 #include<stdio.h> 3 #define N 60 4 int main(void) 5 {int a,b,c; 6 while(1) 7 {printf("please enter minute\n"); 8 scanf("%d",&

c++ primer plus(第6版)中文版 第九章编程练习答案

首先,说明下环境: linux:fedora14: IDE:eclipse: python:python2.7 python框架:django web服务器:apache web服务器的python模块:mod_wsgi 写在前面: 之前用的windows下面的xampp,写的php后台,现在想转向linux下面的python,跟以前一样,选择apache和eclipse作为自己的开发工具. eclipse的python配置, 参见之前的博客:http://blog.csdn.net/zy416

c++ primer plus(第6版)中文版 第十三章编程练习答案

第十三章编程练习答案 13.1根据Cd基类,完成派生出一个Classic类,并测试 //13.1根据Cd基类,完成派生出一个Classic类,并测试 #include <iostream> #include <cstring> using namespace std; // base class class Cd { char performers[50]; char label[20]; int selections; // number of selections double

c++ primer plus(第6版)中文版 第十章编程练习答案

第十章编程练习答案 10.1为复习题5类提供定义,并演示 //10.1为复习题5类提供定义,并演示 #include <iostream> using namespace std; class BankAccount { string m_name; string m_num; unsigned m_balance; public: BankAccount (string name, string num, unsigned balance) { m_name = name; m_num =

c++ primer plus(第6版)中文版 第八章编程练习答案

第八章编程练习答案 8.1编写一个输出字符串的函数,有一个默认参数表示输出次数,默认为1.(原题太扯啦,题意基础上小改动) //8.1编写一个输出字符串的函数,有一个默认参数表示输出次数,默认为1.(原题太扯啦,题意基础上小改动) #include <iostream> using namespace std; void show (const char* str, int time=1) { unsigned n=(time>0)?time:-time; for (unsigned i

c++ primer plus(第6版)中文版 第十二章编程练习答案

第十二章编程练习答案 12.1根据以下类声明,完成类,并编小程序使用它 //12.1根据以下类声明,完成类,并编小程序使用它 #include <iostream> #include <cstring> using namespace std; class Cow{ char name[20]; char * hobby; double weight; public: Cow(); Cow(const char * nm, const char * ho, double wt);

c++ primer plus(第6版)中文版 第七章编程练习答案

第七章编程练习答案 7.1编写一个程序,用户不停输入两数,直到有0出现为止,计算调和平均数 //7.1编写一个程序,用户不停输入两数,直到有0出现为止,计算调和平均数 #include <iostream> using namespace std; double average (unsigned x, unsigned y) { return (2.0 * x * y / (x + y)); } int main () { while (true) { unsigned x, y; cout

C++Primer Plus第6版 4.13编程练习答案

1.答案: #include <iostream> #include <string> int main() { using namespace std; char* fname = new char[10]; char* lname = new char[6]; char grade; int age; cout<< "What is your first name? "; cin.getline(fname,10); cout<< &

c++primer 第四章编程练习答案

4.13.1 #include<iostream> struct students { char firstname[20]; char lastname[20]; char grade; int age; }; int main() { using namespace std; students student1; cout << "What is your fistname? "; cin.get(student1.firstname, 20).get();