forked from ShujiaHuang/Cpp-Primer-Plus-6th
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ceb3191
commit eaa2f37
Showing
17 changed files
with
1,243 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// stack.h -- class definition for the stack ADT | ||
#ifndef STACK_H_ | ||
#define STACK_H_ | ||
|
||
typedef unsigned long 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// stacker.cpp -- testing the Stack class | ||
#include <iostream> | ||
#include <cctype> // or ctype.h | ||
#include "stack.h" | ||
int main() | ||
{ | ||
using namespace std; | ||
Stack st; // create an empty stack | ||
char ch; | ||
unsigned long 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: "; | ||
cin >> po; | ||
if (st.isfull()) | ||
cout << "stack already full\n"; | ||
else | ||
st.push(po); | ||
break; | ||
case 'P': | ||
case 'p': if (st.isempty()) | ||
cout << "stack already empty\n"; | ||
else { | ||
st.pop(po); | ||
cout << "PO #" << po << " popped\n"; | ||
} | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// stock00.cpp -- implementing the Stock class | ||
// version 00 | ||
#include <iostream> | ||
#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.\n"; | ||
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.\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. " | ||
<< "Transaction is aborted.\n"; | ||
} | ||
else if (num > shares) | ||
{ | ||
cout << "You can't sell more than you have! " | ||
<< "Transaction is aborted.\n"; | ||
} | ||
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 << '\n' | ||
<< " Share Price: $" << share_val | ||
<< " Total Worth: $" << total_val << '\n'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// stock00.h -- Stock class interface | ||
// version 00 | ||
#ifndef STOCK00_H_ | ||
#define STOCK00_H_ | ||
|
||
#include <string> | ||
|
||
class Stock // class declaration | ||
{ | ||
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(); | ||
}; // note semicolon at the end | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// stock01.cpp -- revised show() method | ||
#include <iostream> | ||
#include "stock00.h" | ||
|
||
void Stock::acquire(const std::string & co, long n, double pr) | ||
{ | ||
company = co; | ||
if (n < 0) | ||
{ | ||
std::cerr << "Number of shares can't be negative; " | ||
<< company << " shares set to 0.\n"; | ||
shares = 0; | ||
} | ||
else | ||
shares = n; | ||
share_val = pr; | ||
set_tot(); | ||
} | ||
|
||
void Stock::buy(long num, double price) | ||
{ | ||
if (num < 0) | ||
{ | ||
std::cerr << "Number of shares purchased can't be negative. " | ||
<< "Transaction is aborted.\n"; | ||
} | ||
else | ||
{ | ||
shares += num; | ||
share_val = price; | ||
set_tot(); | ||
} | ||
} | ||
|
||
void Stock::sell(long num, double price) | ||
{ | ||
using std::cerr; | ||
if (num < 0) | ||
{ | ||
cerr << "Number of shares sold can't be negative. " | ||
<< "Transaction is aborted.\n"; | ||
} | ||
else if (num > shares) | ||
{ | ||
cerr << "You can't sell more than you have! " | ||
<< "Transaction is aborted.\n"; | ||
} | ||
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; | ||
// set format to #.### | ||
ios_base::fmtflags orig = cout.setf(ios_base::fixed); | ||
int prec = cout.precision(3); | ||
|
||
cout << "Company: " << company | ||
<< " Shares: " << shares << '\n'; | ||
cout << " Share Price: $" << share_val; | ||
// set format to *.** | ||
cout.precision(2); | ||
cout << " Total Worth: $" << total_val << '\n'; | ||
|
||
// restore original format | ||
cout.setf(orig, ios_base::floatfield); | ||
cout.precision(prec); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// stock1.cpp – Stock class implementation with constructors, destructor added | ||
#include <iostream> | ||
#include "stock10.h" | ||
|
||
// constructors (verbose versions) | ||
Stock::Stock() // default constructor | ||
{ | ||
std::cout << "Default constructor called\n"; | ||
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\n"; | ||
company = co; | ||
|
||
if (n < 0) | ||
{ | ||
std::cout << "Number of shares can't be negative; " | ||
<< company << " shares set to 0.\n"; | ||
shares = 0; | ||
} | ||
else | ||
shares = n; | ||
share_val = pr; | ||
set_tot(); | ||
} | ||
// class destructor | ||
Stock::~Stock() // verbose class destructor | ||
{ | ||
std::cout << "Bye, " << company << "!\n"; | ||
} | ||
|
||
// other methods | ||
void Stock::buy(long num, double price) | ||
{ | ||
if (num < 0) | ||
{ | ||
std::cout << "Number of shares purchased can't be negative. " | ||
<< "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. " | ||
<< "Transaction is aborted.\n"; | ||
} | ||
else if (num > shares) | ||
{ | ||
cout << "You can't sell more than you have! " | ||
<< "Transaction is aborted.\n"; | ||
} | ||
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; | ||
// set format to #.### | ||
ios_base::fmtflags orig = | ||
cout.setf(ios_base::fixed, ios_base::floatfield); | ||
std::streamsize prec = cout.precision(3); | ||
|
||
cout << "Company: " << company | ||
<< " Shares: " << shares << '\n'; | ||
cout << " Share Price: $" << share_val; | ||
// set format to #.## | ||
cout.precision(2); | ||
cout << " Total Worth: $" << total_val << '\n'; | ||
|
||
// restore original format | ||
cout.setf(orig, ios_base::floatfield); | ||
cout.precision(prec); | ||
} |
Oops, something went wrong.