C++Primer Plus 10章ソースコード

13313 ワード

C++Primer Plus第十章本上ソース
記録は私がC++Primer Plus(第6版)の第10章のソースコードを見ることを見ます
10.1
//10.1
#ifndef STOCK00_H_
#define STOCK00_H_
#include
class Stock
{
    private:
        std::string company;
        long shares;
        double share_val;
        double total_val;
        void set_tot() { total_val = shares * share_val; }
    public:
        void acquire(const std::string &co, long n, double pr);
        void buy(long num, double price);
        void sell(long num, double price);
        void update(double price);
        void show();
};
#endif

10.2
//10.2
#include
#include"stock00.h"
void Stock::acquire(const std::string &co, long n, double pr)
{
    company = co;
    if(n<0)
    {
        std::cout << "Number of shares can't be negative;"
                  << company << " shares set to 0.
"; shares = 0; } else shares = n; share_val = pr; set_tot(); } void Stock::buy(long num, double price) { if(num<0) { std::cout << "Number of shares purchased can't be negative. " << "Transaction is aborted.
"; } 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." << "Transaction is aborted.
"; } else if(num>shares) { cout << "You can't sell more than you have! " << "Transaction is aborted.
"; } else { shares -= num; share_val = price; set_tot(); } } void Stock::update(double price) { share_val = price; set_tot(); } void Stock::show() { std::cout << "Company: " << company << " Shares: " << shares << "
" << " Shar Price: $" << share_val << " Total Worth: $" << total_val << "
"; }

10.3
#include
#include"stock00.cpp"
int main()
{
    Stock fluffy_the_cat;
    fluffy_the_cat.acquire("NanoSmart", 20, 12.50);
    fluffy_the_cat.show();
    fluffy_the_cat.buy(15,  18.125);
    fluffy_the_cat.show();
    fluffy_the_cat.sell(400, 20.00);
    fluffy_the_cat.show();
    fluffy_the_cat.buy(300000,40.125);
    fluffy_the_cat.show();
    fluffy_the_cat.sell(300000, 0.125);
    fluffy_the_cat.show();
    return 0;
}


10.4
//10.4
#ifndef STOCK10_H_
#define STOCK10_H_
#include
class Stock
{
    private:
        std::string company;
        long shares;
        double share_val;
        double total_val;
        void set_tot() { total_val = shares * share_val; }
    public:
        Stock();
        Stock(const std::string &co, long n = 0, double pr = 0.0);
        ~Stock();
        void buy(long num, double price);
        void sell(long num, double price);
        void update(double price);
        void show();
};
#endif

10.5
//10.5
#include
#include"stock10.h"
        Stock::Stock()
        {
                std::cout << "Default constructor called
"; company = "no name"; shares = 0; share_val = 0.0; total_val = 0.0; } Stock::Stock(const std::string &co, long n , double pr ) { std::cout << "Constructor using " << co << " called
"; company = co; if(n<0) { std::cout << "Number of shares can't be negative; " << company << " shares set to 0.
"; shares = 0; } else shares = n; share_val = pr; set_tot(); } Stock::~Stock() { std::cout << "Bye, " << company << "!
"; } void Stock::buy(long num, double price) { if(num<0) { std::cout << "Number of shares purchased can't be negative. " << "Transaction is aborted.
"; } 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. " << "Transaction is aborted.
"; } else if(num>shares) { cout << "You can't sell more than you have! " << "Transaction is aborted.
"; } else { shares -= num; share_val = price; set_tot(); } } void Stock::update(double price) { share_val = price; set_tot(); } void Stock::show() { using std::cout; using std::ios_base; ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield); std::streamsize prec = cout.precision(3); cout << "Company: " << company << " Shares: " << shares << "
"; cout << " Share Price: $" << share_val; cout.precision(2); cout << " Total Worth: $" << total_val << "
"; cout.setf(orig, ios_base::floatfield); cout.precision(prec); }

10.6
#include
#include"stock10.cpp"
int main()
{
    {
        using std::cout;
        cout << "Using constructors to create new objects
"; Stock stock1("NanoSmart", 12, 20.0); stock1.show(); Stock stock2 = Stock("Boffo Objects", 2, 2.0); stock2.show(); cout << "Assigning stock1 to stock2:
"; stock2 = stock1; cout << "Listing stock1 and stock:
"; stock1.show(); stock2.show(); cout << "Using a constructor to reset an object
"; stock1 = Stock("Nifty Foods", 10, 50.0); cout << "Revised stock1:
"; stock1.show(); cout << "Done
"; } return 0; }

10.7
//10.7
#ifndef STOCK20_H_
#define STOCK20_H_
#include
class Stock
{
private:
    std::string company;
    int shares;
    double share_val;
    double total_val;
    void set_tot(){
        total_val = shares * share_val;
    }
public:
    Stock();
    Stock(const std::string &co, long n = 0, double pr = 0.0);
    ~Stock();
    void buy(long num, double price);
    void sell(long num, double price);
    void update(double price);
    void show() const;
    const Stock &topval(const Stock &s) const;
};
#endif

10.8
//10.8
#include
#include"stock20.h"
Stock::Stock()
{
    company = "no name";
    shares = 0;
    share_val = 0.0;
    total_val = 0.0;
}
Stock::Stock(const std::string &co, long n , double pr )
{
    company = co;
    if(n<0)
    {
        std::cout << "Number of shares can't be negative; "
                  << company << " shares set to 0.
"; shares = 0; } else shares = n; share_val = pr; set_tot(); } Stock::~Stock() { } void Stock::buy(long num, double price) { if(num<0) { std::cout << "Number of shares purchased can't be negative. " << "Transaction is abotted.
"; } 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. " << "Transaction is aborted.
"; } else if(num>shares) { cout << "You can't sell more than you have! " << "Transation is aborted.
"; } else { shares -= num; share_val = price; set_tot(); } } void Stock::update(double price) { share_val = price; set_tot(); } void Stock::show() const { using std::cout; using std::ios_base; ios_base::fmtflags orig = cout.setf(ios_base::fixed, ios_base::floatfield); std::streamsize prec = cout.precision(3); cout << "Company: " << company << " Shares: " << shares << "
"; cout << " Share Price: $" << share_val; cout.precision(2); cout << " Total Worth: $" << total_val << "
"; cout.setf(orig, ios_base::floatfield); cout.precision(prec); } const Stock&Stock::topval(const Stock &s) const { if(s.total_val>total_val) return s; else return *this; }

10.9
#include
#include"stock20.cpp"
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 Enterprisee", 60, 6.5)};
    std::cout << "Stock holdings:
"; int st; for (st = 0; st < STKS;st++) stocks[st].show(); const Stock*top=&stocks[0]; for (st = 1; st < STKS;st++) top = &top->topval(stocks[st]); std::cout << "
Most valuable holding:
"; top->show(); return 0; }

10.10
//10.10
#ifndef STACK_H_
#define STACK_H_
typedef unsigned long 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

10.11
//10.11
#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 &item)
{
    if(top0)
    {
        item = items[--top];
        return true;
    }
    else
        return false;
}

10.12
#include
#includess
#include"stack.cpp"
int main()
{
    using namespace std;
    Stack st;
    char ch;
    unsigned long po;
    cout << "Please enter A to add a purchase order,
" << "P to process aPO, or Q to quit.
"; while(cin>>ch&&toupper(ch!='Q')) { while(cin.get()!='
') continue; if(!isalpha(ch)) { cout << '\a'; continue; } switch(ch) { case'A': case'a': cout << "Enter a PO number to add: "; cin >> po; if(st.isfull()) cout << "stack already full
"; else st.push(po); break; case'p': case'P': if(st.isempty()) cout << "stack already empty
"; else { st.pop(po); cout << "PO #" << po << " popped
"; } break; } cout << "Please enter A to add a purchase order,
" << "P to process aPO, or Q to quit.
"; } cout << "Bye
"; return 0; }