c++ primer plus第十章编程答案

//编程练习1
//BankCount.h
#include 
#include "BankCount.h"

int main()
{
    BankCount china = BankCount("mahuateng","5647895664335456",12546.30);
    china.show();
    china.deposit(345.6);
    china.show();
    china.withdraw(1234.3);
    china.show();

    return 0;
}

//BankCount.cpp

#include "BankCount.h"
#include 
#include 

 BankCount::BankCount(const char * client, const char * num, double bal )
 {
     strncpy(name,client,39);
     name[39] = '\0';
     strncpy(acctnum,num,24);
     acctnum[25] = '\0';
     balance = bal;
 }

void BankCount::show(void) const
{
    std::cout << "Name: " << name <
#include "BankCount.h"

int main()
{
    BankCount china = BankCount("mahuateng","5647895664335456",12546.30);
    china.show();
    china.deposit(345.6);
    china.show();
    china.withdraw(1234.3);
    china.show();

    return 0;
}
//编程练习2
//name.h
#ifndef NAME_H_
#define NAME_H_

#include

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 // NAME_H_

//name.cpp
#include "name.h"
#include 
#include 


Person::Person(const std::string &ln,const char * fn )
{
    lname = ln;
    strncpy(fname,fn,24);
    fname[24] = '\0';
}

void Person::Show() const
{
    std::cout << fname << " " << lname <
#include "name.h"

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

   Person one;
   one.Show();
   one.FormalShow();
    cout << endl;

   Person two("Smythecraft");
   two.Show();
   two.FormalShow();
    cout << endl;

   Person three("Dimwiddy","Sam");
   three.Show();
   three.FormalShow();
       return 0;
}
//编程练习3
//golf.h
#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 hand(int hc);
    void showgolf();


} ;
#endif // GOLF_H_


//golf.cpp
#include
#include 
#include "golf.h"

golf::golf( const char *name ,int hc)
{
    strcpy(fullname ,name);
    handicap = hc;
}
golf::golf()
{
    using std::cout;
    using std::cin;
    char temp[Len];
    int ha ;
     cout << "Enter fullname: ";
    cin.get(temp,Len);
    cout << "Enter handicap: ";
    cin >> ha;
    *this = golf(temp,ha);//用带有两个参数的构造函数初始化

}
golf::~golf()
{

}

 void golf::hand( int hc)
 {
      handicap = hc;
 }
 void golf::showgolf()
 {
     std::cout << "\nfullname : " << fullname << std::endl;
      std::cout << "handicapL : " << handicap << std::endl;
 }

//main.cpp

#include 
#include "golf.h"

int main()
{
    golf g1;
    g1.showgolf();
    golf g2 = golf ("girl",3);
      g2.showgolf();
    g2.hand(0);
    g2.showgolf();
    return 0;
}

//编程练习4
#ifndef SALE_H_
#define SALE_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();
        void showSales();
    };
}
#endif // SALE_H_

//sale.cpp
#include 
#include 
#include "sale.h"

using SALES::Sales;//类Sales在后面可以直接用

     Sales::Sales (const double ar[],int n)
    {
        int min_num = 4;
        if(min_num > n)
        {
            min_num = n;
            std::cout <<"array not full!\n";
            for (int i = n;i < 4 ;i++)
            {
                sales[i] = 0.0;
            }
        }
        double sum = 0.0;
        max = ar[0];
         min = ar[0];
        for (int i = 0; i < min_num;i++)
        {
            sales[i] = ar[i];
            sum += sales[i];
            if( max < sales[i])
            {
                max = sales[i];
            }
            if(min > sales[i])
            {
                min = sales[i];
            }
        }
        average = sum / min_num ;
    }

  Sales::Sales()
    {
        std::cout << "Enter number into sales: ";
        int i = 0 ;
        double sum = 0.0;
        while( i<4 && (std::cin >> sales[i] ))
        {
            sum += sales[i];
            i++;
            if(i!=4)
            std::cout << "next: ";
        }
        max = sales[0];
        min = sales[0];
        for (int j = 0;j<4;j++)
        {
            if(sales[j]>max)
               {
                  max =sales[j];
               }
              else  if (sales[j]
#include "sale.h"

int main()
{
        double arr[3] = {34,56.4,456.5};
        using SALES::Sales;//添加命名空间
        Sales st1 = Sales(arr,3);
        std::cout << "ONE: \n";
        st1.showSales();
        Sales st2 ;
        std::cout << "TWO: \n";
        st2.showSales();
    return 0 ;
}

//编程练习5(使用书上的stack程序,修改main函数,
//头文件里修改typedef,经典的oop思想)
// stack.h -- class definition for the stack ADT
#ifndef STACK_H_
#define STACK_H_
struct customer
{
    char fullname[35];
    double payment;
};

typedef customer Item;

class Stack
{
private:
    enum {MAX = 10};    // constant specific to class
    Item items[MAX];    // holds stack items
    int top;            // index for top stack item
public:
    Stack();
    bool isempty() const;
    bool isfull() const;
    // push() returns false if stack already is full, true otherwise
    bool push(const Item & item);   // add item to stack
    // pop() returns false if stack already is empty, true otherwise
    bool pop(Item & item);          // pop top into item
};
#endif

// stack.cpp -- Stack member functions
#include "stack.h"
Stack::Stack()    // create an empty 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;
}

// stacker.cpp -- testing the Stack class
#include 
#include   // or ctype.h
#include "stack.h"
int main()
{
    using namespace std;
    Stack st; // create an empty stack
    char ch;
    Item po;
    cout << "Please enter A to add a purchase order,\n"
        << "P to process a PO, or Q to quit.\n";
    while (cin >> ch && toupper(ch) != 'Q')
    {
        while (cin.get() != '\n')
            continue;
        if (!isalpha(ch))
        {
            cout << '\a';
            continue;
        }
        switch(ch)
        {
             case 'A':
             case 'a': cout << "Enter a PO number to add: ";
                       cout << "fullname: ";//修改部分
                       cin.getline(po.fullname,35);
                       cout << "payment: ";
                       cin >> po.payment;
                       if (st.isfull())
                           cout << "stack already full\n";
                       else
                           st.push(po);
                       break;
             case 'P':
             case 'p': static double  sum = 0.0;//使用内部静态变量存储
                       if (st.isempty())
                           cout << "stack already empty\n";
                       else {
                           st.pop(po);
                           sum += po.payment ;
                          // cout << "PO #" << po << " popped\n";
                           cout << "sum: " << sum << endl;
                       }
                       break;
        }
        cout << "Please enter A to add a purchase order,\n"
             << "P to process a PO, or Q to quit.\n";
    }
    cout << "Bye\n";
    return 0;
}
//编程练习6
//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);
};
#endif // MOVE_H_

//move.cpp
#include 
#include "move.h"

Move::Move(double a ,double b )
{
    x = a;
    y = b;
}
void Move::showmove() const
{
    using std::cout;
    using std::endl;
    cout << "x: " << x << endl;
    cout << "y: " << y << endl;
}
Move  Move::add(const Move & m) const
{
    Move temp;
    temp.x = x + m.x;
    temp.y = y + m.y;
    return temp;
    //return Move( x + m.x , y + m.y );
}
void Move::reset(double a ,double b )
{
    x = a;
    y = b;
}

//main.cpp
#include 
#include "move.h"

int main()
{
   Move mo(245,675);
   mo.showmove();

   Move me(25,65);
   me.showmove();

   mo.add(me);
   mo.add(me).showmove();

   me = mo.add(me);
   me.showmove();

   mo.reset();
   mo.showmove();

   me.reset();
   me.showmove();


    return 0;
}
//编程练习7
#ifndef PLORG_H_
#define PLORG_H_

class Plorg
{
  private:
      char NAME[20];
      int CI ;
  public:
    Plorg();
    Plorg(const char * name , int ci = 50);
    void setci(int ci) ;
    void showplorg() const;

};

#endif // PLORG_H_

//plorg.cpp

 #include
 #include
 #include "plorg.h"

 Plorg::Plorg()
 {
     strcpy(NAME,"Plorga");
     CI = 0;
 }
 Plorg::Plorg(const char * name , int ci )
 {
     strncpy(NAME,name,19);
     NAME[19] = '\0';
     CI = ci;
 }
 void Plorg::setci(int ci)
 {
     CI = ci;
 }

 void Plorg::showplorg() const
 {
     using std::cout;
     using std::endl;
     cout << "name: " << NAME << endl;
     cout << "CI: " << CI << endl;
 }

//main.cpp
#include 
#include "plorg.h"

int main()
{
    Plorg p1;
    p1.showplorg();

    Plorg p2("drive", 35);
    p2.showplorg();

    Plorg p3("swimming");
    p3.showplorg();

    p3.setci(78);
    p3.showplorg();

    return 0;
}
//编程练习8 (运用例子stack)
//list.h
#ifndef LIST_H_
#define LIST_H_
#include 
typedef int Item;
class List
{
private:
    static const int SIZE = 10;
    Item lis[SIZE];
    int num;
public:
    List(const Item arr[] = NULL , int n = 0);
    bool add(const Item & ls ) ;
    bool Isempty() const;
    bool Isfull() const;
    void visit(void (*pf)(Item &));

};

void show(Item & ls);

#endif // LIST_H_

//list.cpp
 #include "list.h"

 List::List(const Item arr[] , int n )
 {
     if (NULL == arr)
        {
            num = 0;
            return;
        }

       num = n < SIZE ? n : SIZE;
        {
            for(int i = 0;i < num ;i++)
                lis[i] = arr[i];
        }
 }
  bool List::Isempty() const
  {
      return (num == 0);
  }

  bool List::Isfull() const
  {
      return (num == SIZE);
  }


 bool  List::add(const Item & ls )
 {
     if (num < SIZE)
     {
         lis[num++] = ls;
         return true;
     }
     else
        return false;
 }

 void List::visit(void (*pf)(Item &))
 {
     for (int i = 0;i < num;i++)
        pf(lis[i]);
 }
 void show(Item & ls) //
{
    std::cout << ls  << " " ;

}

//main.cpp
#include 
#include "list.h"

int main()
{
    List one ;

    if(one.Isempty())
    {
        std::cout << "one empty\n";
    }
    else
    {
      one.visit(show);
    }

    int ar[3] = {1,2,3};
   List temp = List(ar,sizeof(ar)/sizeof(ar[0])); //利用sizeof计算长度
   temp.visit(show);                               //ar表示整个数组
   std::cout << std::endl;

   if(!temp.Isempty())
    {
         if(!temp.Isfull())
       {
          temp.add(49);
             temp.visit(show);
       }
       else
       {
          std::cout << " temp full\n";
       }
    }
    else
        std::cout << " temp empty\n";


    return 0;
}


 

你可能感兴趣的:(c++ primer plus第十章编程答案)