// bankaccount.h -- class BankAccount declaration
#ifndef __BANKACCOUNT_H__
#define __BANKACCOUNT_H__
// class definition
class BankAccount
{
private:
char name[40]; // or std::string name;
char acctnum[25]; // or std::string acctnum;
double balance;
public:
BankAccount(const char * client, const char * num, double bal = 0.0);
// or BankAccount(const std::string & client,
// const std::string & num, double bal = 0.0);
void show(void) const;
void deposit(double cash);
void withdraw(double cash);
};
#endif /* __BANKACCOUNT_H__ */
// bankaccount.cpp -- class BankAccount function definition
#include <iostream>
#include <cstring> // strncpy
#include "bankaccount.h"
BankAccount::BankAccount(const char * client, const char * num, double bal)
{
strncpy(name, client, 39);
name[39] = '\0';
strncpy(acctnum, num, 24);
acctnum[24] = '\0';
balance = bal;
}
/* // 或者:
BankAccount::BankAccount(const std::string & client, const std::string & num, double bal)
{
name = client;
acctnum = num;
balance = bal;
}
*/
void BankAccount::show(void) const
{
std::cout << "name: " << name
<< ", account: " << acctnum
<< ", balance: " << balance << std::endl;
}
void BankAccount::deposit(double cash)
{
using std::cout;
if (cash < 0) {
cout << "Deposits cannot be negative!\n";
} else
balance += cash;
}
void BankAccount::withdraw(double cash)
{
using std::cout;
if (cash < 0) {
cout << "Withdraws cannot be negative!\n";
} else
balance -= cash;
}
/***********************************
2017年11月6日10:59:45
Athor:xiyuan255
Course:C++
Contain:depositwithdraw.cpp
Reference: C++ Primer plus
说明:C++ Primer plus第十章的第一题练习题
【 P378 】
*************************************/
// depositwithdraw.cpp -- bank deposit and withdraw apply
#include <iostream>
#include "bankaccount.h"
int main()
{
using namespace std;
BankAccount ba("zhang san", "xiao xing", 1000.0); // 隐式调用构造函数
// or BankAccount ba = BankAccount("zhang san", "xiao xing", 1000.0); // 显式调用构造函数
cout << "orignal account:\n";
ba.show();
ba.deposit(3000.0);
cout << "deposit 3000.0 after account:\n";
ba.show();
ba.withdraw(2000.0);
cout << "withdraw 3000.0 after account:\n";
ba.show();
return 0;
}
/**
输出结果:
orignal account:
name: zhang san, account: xiao xing, balance: 1000
deposit 3000.0 after account:
name: zhang san, account: xiao xing, balance: 4000
withdraw 3000.0 after account:
name: zhang san, account: xiao xing, balance: 2000
*/
// person.h -- class Person declaration
#ifndef __PERSON_H__
#define __PERSON_H__
#include <iostream>
using std::string;
// class Person declaration
class Person
{
private:
static const int LIMIT = 25;
string lname; // Person's last name
char fname[LIMIT]; // Person's first name
public:
Person() { lname = ""; fname[0] = '\0'; } // #1
Person(const string & ln, const char * fn = "Heyyou"); // #2
// the following methods display lname and fname
void show() const; // firstname lastname format
void formalShow() const; // lastname, firstname format
};
#endif /* __PERSON_H__ */
// person.cpp -- class Person function definition
#include <string> // string类
#include "person.h"
Person::Person(const string & ln, const char * fn)
{
lname = ln;
strncpy(fname, fn, LIMIT-1);
fname[LIMIT-1] = '\0';
}
void Person::show() const
{
std::cout << "name: " << fname
<< " " << lname << "\n";
}
void Person::formalShow() const
{
std::cout << "name: " << lname
<< " " << fname << "\n";
}
/***********************************
2017年11月6日13:40:26
Athor:xiyuan255
Course:C++
Contain:personer.cpp
person.h
person.cpp
Reference: C++ Primer plus
说明:C++ Primer plus第十章的第二题练习题
【 P378 】
/***********************************
2017年11月6日16:45:29
Athor:xiyuan255
Course:C++
Contain:salesmain2.cpp
namesp2.h
namesp2.cpp
Reference: C++ Primer plus
说明:C++ Primer plus第十章的第四题练习题
【 P378 】
*************************************/
// salesmain2.cpp -- main function
#include <iostream>
#include "namesp2.h"
int main()
{
using SALES::Sales;
double slarr[3] = { 1280.65, 1820.5, 1560.45 };
Sales slobj(slarr, 3);
slobj.showSales();
slobj.setSales();
slobj.showSales();
return 0;
}
/**
输出结果:
sales[0] = 1280.65
sales[1] = 1820.5
sales[2] = 1560.45
sales[3] = 0
average = 1553.87, max = 1820.5, min = 1280.65
Enter the #1 quarter sales: 1800.45
Enter the #2 quarter sales: 1900.35
Enter the #3 quarter sales: 2100
Enter the #4 quarter sales: 2000.85
sales[0] = 1800.45
sales[1] = 1900.35
sales[2] = 2100
sales[3] = 2000.85
average = 1950.41, max = 2100, min = 1800.45
*/
// stack2.h -- class definition for the stack ADT
#ifndef __STACK2_H__
#define __STACK2_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
// push() returns false if stack already is empty, true otherwise
bool pop(Item & item); // pop to into item
};
#endif /* __STACK2_H__ */
// stack2.cpp -- Stack member functions
#include "stack2.h"
Stack::Stack() // create an empty stack
{
top = 0;
}
bool Stack::isEmpty() const
{
return (0 == top);
}
bool Stack::isFull() const
{
return (MAX == top);
}
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;
}
/***********************************
2017年11月7日09:11:42
Athor:xiyuan255
Course:C++
Contain:stacker2.cpp
stack2.cpp
stack2.h
Reference: C++ Primer plus
说明:C++ Primer plus第十章的第五题练习题
【 P378 】
*************************************/
// stacker2.cpp -- testing the Stack calss
#include <iostream>
#include <cctype> // or ctype.h // toupper()
#include "stack2.h"
int main()
{
using namespace std;
Stack st; // create an empty stack
char ch;
Item po;
double total_payment = 0.0;
cout << "Please enter A to add to stack,\n"
<< "P to into item, 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: \n";
cout << "fullname: ";
cin.get(po.fullname, 35);
cout << "payment: ";
cin >> po.payment;
cin.get();
if (st.isFull())
cout << "stack already full\n";
else
st.push(po);
break;
case 'P':
case 'p': if (st.isEmpty())
cout << "stack alresdy empty\n";
else {
st.pop(po);
cout << "PO fullname:" << po.fullname << " popped\n";
cout << "PO fullname:" << po.payment << " popped\n";
total_payment += po.payment;
cout << "total_payment:" << total_payment << endl;
}
break;
}
cout << "Please enter A to add to stack,\n"
<< "P to into item, or Q to quit.\n";
}
cout << "Bye.\n";
return 0;
}
/**
输出结果:
Please enter A to add to stack,
P to into item, or Q to quit.
A
Enter a PO number to add:
fullname: ZHANG SAN
payment: 35.35
Please enter A to add to stack,
P to into item, or Q to quit.
P
PO fullname:ZHANG SAN popped
PO fullname:35.35 popped
total_payment:35.35
Please enter A to add to stack,
P to into item, or Q to quit.
A
Enter a PO number to add:
fullname: LI SHI
payment: 25.8
Please enter A to add to stack,
P to into item, or Q to quit.
A
Enter a PO number to add:
fullname: WANG WU
payment: 45.6
Please enter A to add to stack,
P to into item, or Q to quit.
P
PO fullname:WANG WU popped
PO fullname:45.6 popped
total_payment:80.95
Please enter A to add to stack,
P to into item, or Q to quit.
P
PO fullname:LI SHI popped
PO fullname:25.8 popped
total_payment:106.75
Please enter A to add to stack,
P to into item, or Q to quit.
P
stack alresdy empty
Please enter A to add to stack,
P to into item, or Q to quit.
Q
Bye.
*/
//move.h -- class Move declaration
#ifndef __MOVE_H__
#define __MOVE_H__
class Move
{
private:
double x;
double y;
public:
Move(double a = 0, double b = 0); // sets x, y to a, b
void showMove() const; // shows current x, y values
// this function adds x of m to x of invoking object to get new x,
// adds y of m to y of invoking object to get new y, creates a new
// move object initialized to new x, y values and returns it
Move add(const Move & m) const;
void reset(double a = 0, double b = 0); // resets x, y to a, b
};
#endif /* __MOVE_H__ */
// move.cpp -- class Move function definition
#include <iostream>
#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
<< ", y = " << y << endl;
}
Move Move::add(const Move & m) const
{
return Move(m.x + x, m.y + y);
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
/***********************************
2017年11月7日10:40:36
Athor:xiyuan255
Course:C++
Contain:movemian.cpp
move.h
move.cpp
Reference: C++ Primer plus
说明:C++ Primer plus第十章的第六题练习题
【 P378 】
*************************************/
// movemain.cpp -- debug move.cpp function
#include <iostream>
#include "move.h"
int main()
{
using std::cout;
using std::endl;
Move moves = Move(1.0, 2.0);
cout << "orignal moves: \n";
moves.showMove();
moves = moves.add(Move(3.0, 4.0));
cout << "moves add Move(3.0, 4.0) after: \n";
moves.showMove();
moves.reset(1.0, 1.0);
cout << "moves reset(1.0, 1.0) after: \n";
moves.showMove();
return 0;
}
/**
输出结果:
orignal moves:
x = 1, y = 2
moves add Move(3.0, 4.0) after:
x = 4, y = 6
moves reset(1.0, 1.0) after:
x = 1, y = 1
*/
// plorg.h -- class Plorg declarator
#ifndef __PLORG_H__
#define __PLORG_H__
// class Betelgeusean plorg declarator
class Plorg
{
private:
enum { MAX = 20 };
char name[MAX];
int CI;
public:
Plorg(char * pName = "Plorga", int ci = 50);
void modifyCI(const int ci);
void showPlorg() const;
};
#endif /* __PLORG_H__ */
// plorg.cpp -- class Plorg function definition
#include <iostream>
#include "plorg.h"
Plorg::Plorg(char * pName, int ci)
{
strncpy(name, pName, MAX-1);
name[MAX-1] = '\0';
CI = ci;
}
void Plorg::modifyCI(const int ci)
{
CI = ci;
}
void Plorg::showPlorg() const
{
using std::cout;
using std::endl;
cout << "\"" << name << "\": " << CI << endl;
}
/***********************************
2017年11月7日11:25:18
Athor:xiyuan255
Course:C++
Contain:plorgmain.cpp
plorg.h
plorg.cpp
Reference: C++ Primer plus
说明:C++ Primer plus第十章的第七题练习题
【 P378 】
*************************************/
#include "plorg.h"
int main()
{
Plorg plorgs("xiao ming", 25); // 隐式调用构造函数
plorgs.showPlorg();
plorgs.modifyCI(60);
plorgs.showPlorg();
Plorg plo = Plorg(); // 显式调用构造函数
plo.showPlorg();
return 0;
}
/**
输出结果:
"xiao ming": 25
"xiao ming": 60
"Plorga": 50
*/
// list.h -- class List declarator
#ifndef __LIST_H__
#define __LIST_H__
typedef long int Item;
class List
{
private:
enum { MAX = 6 };
Item items[MAX];
int index;
public:
List() { index = 0; }
void addItem(const Item & data);
bool isEmpty() const;
bool isFull() const;
void visit(void (*pf)(Item & data));
};
#endif /* __LIST_H__ */
// list.cpp -- class List function definition
#include <iostream>
#include "list.h"
bool List::isEmpty() const
{
return (0 == index);
}
bool List::isFull() const
{
return (MAX == index);
}
void List::addItem(const Item & data)
{
using std::cout;
if (isFull())
cout << "List already full!\n";
else {
items[index++] = data;
}
}
void List::visit(void (*pf)(Item & data))
{
int i = 0;
while (i < index) {
(*pf)(items[i]);
i++;
}
std::cout << std::endl;
}
/***********************************
2017年11月7日13:22:19
Athor:xiyuan255
Course:C++
Contain:lister.cpp
list.h
list.cpp
Reference: C++ Primer plus
说明:C++ Primer plus第十章的第八题练习题
【 P378 】
*************************************/
// lister.cpp -- debug class list.cpp
#include <iostream>
#include "list.h"
void print(Item & data)
{
std::cout << data << " ";
}
int main()
{
List object; // 会调用默认的构造函数,即 List() { index = 0; }
std::cout << "isEmpty? " << object.isEmpty() << std::endl;
object.addItem(1);
object.addItem(2);
object.addItem(3);
object.addItem(4);
object.addItem(5);
std::cout << "isFull? " << object.isFull() << std::endl;
object.visit(print);
object.addItem(6);
std::cout << "isFull? " << object.isFull() << std::endl;
object.addItem(7);
object.visit(print);
return 0;
}
/**
输出结果:
isEmpty? 1
isFull? 0
1 2 3 4 5
isFull? 1
List already full!
1 2 3 4 5 6
*/
*************************************/// personer.cpp -- class Person apply#include "person.h"int main(){Person one; // use default constructorPerson two("Smythecraft"); // use #2 with one default argumentPerson three = Person("Dimwiddy", "Sam"); // use #s, no defaultone.show();one.formalShow();std::cout << std::endl;two.show();two.formalShow();std::cout << std::endl;three.show();three.formalShow();std::cout << std::endl;return 0;}/**输出结果:name:name:name: Heyyou Smythecraftname: Smythecraft Heyyouname: Sam Dimwiddyname: Dimwiddy Sam*/
// golf2.h -- for pe9-2.cpp
#ifndef __GOLF2_H__
#define __GOLF2_H__
const int Len = 40;
struct golf {
char fullname[Len];
int handicap;
};
class Golf
{
private:
golf contents;
public:
// non-interactive version:
// constructor function sets golf structure to provided name,
// handicap using values passed as arguments to the function
Golf(const char * name = "null", int hc = 0);
// interactive version:
// constructor function solicits name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setGolf();
// function resets handicap to new value
void Golf::handicap(int hc);
// function displays contents of golf structure
void showGolf() const;
char * showFullname() { return this->contents.fullname; }
};
#endif /* __GOLF2_H__ */
#include <iostream>
#include "golf2.h"
using namespace std;
// constructor function #1
Golf::Golf(const char * name, int hc)
{
if (strlen(name) < Len) {
int length = strlen(name);
memcpy(contents.fullname, name, length);
while (length < Len)
contents.fullname[length++] = '\0';
} else {
memcpy(contents.fullname, name, Len-2);
contents.fullname[Len-1] = '\0';
}
contents.handicap = hc;
}
int Golf::setGolf()
{
using std::cout;
using std::cin;
char fullname[Len];
int handicap;
cout << "Enter name: ";
if ( !cin.getline(fullname, Len) )
return 0;
cout << "Enter grade: ";
cin >> handicap;
cin.get(); // 将cin >> g.handicap语句没有讲输入队列中的换行符取走
(*this) = Golf(fullname, handicap);
return 1;
}
void Golf::handicap(int hc)
{
contents.handicap = hc;
}
void Golf::showGolf() const
{
using std::cout;
using std::endl;
cout << "fullname = " << contents.fullname
<< ", handicap = " << contents.handicap << endl;
}
/***********************************
2017年11月6日16:15:27
Athor:xiyuan255
Course:C++
Contain:pe9-2.cpp
golf2.h
golf2.cpp
Reference: C++ Primer plus
说明:C++ Primer plus第十章的第三题练习题
【 P338 】
*************************************/
// pe9-2.cpp -- operation main
#include <iostream>
#include "golf2.h"
int main()
{
using std::cout;
using std::cin;
using std::endl;
Golf ann("Ann Birdfree", 24);
ann.showGolf();
Golf andy[6] = {0, };
for (int i = 0; i < 6; i++) {
cout << "The #" << i+1 << endl;
if ( !andy[i].setGolf() )
break;
}
for (int i = 0; i < 6; i++) {
andy[i].showGolf();
}
cout << "Modify " << andy[2].showFullname() << " of value: ";
int number;
cin >> number;
andy[2].handicap(number);
for (int i = 0; i < 6; i++) {
andy[i].showGolf();
}
return 0;
}
/**
输出结果:
fullname = Ann Birdfree, handicap = 24
The #1
Enter name: zhang san
Enter grade: 35
The #2
Enter name: li shi
Enter grade: 29
The #3
Enter name: wang wu
Enter grade: 36
The #4
Enter name: zhao liu
Enter grade: 40
The #5
Enter name: xiao ming
Enter grade: 35
The #6
Enter name: xiao hua
Enter grade: 42
fullname = zhang san, handicap = 35
fullname = li shi, handicap = 29
fullname = wang wu, handicap = 36
fullname = zhao liu, handicap = 40
fullname = xiao ming, handicap = 35
fullname = xiao hua, handicap = 42
Modify wang wu of value: 46
fullname = zhang san, handicap = 35
fullname = li shi, handicap = 29
fullname = wang wu, handicap = 46
fullname = zhao liu, handicap = 40
fullname = xiao ming, handicap = 35
fullname = xiao hua, handicap = 42
*/
// namesp2.h -- namespace definition
#ifndef __NAMESP2_H__
#define __NAMESP2_H__
namespace SALES
{
class Sales
{
private:
static const int QUARTERS = 4;
double sales[QUARTERS];
double average;
double max;
double min;
public:
// copies the lesser of 4 or n items from the array ar
// to the sales member of s and computes and stores the
// average, maximum, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0
Sales(const double ar[], int n);
// gathers sales for 4 quarters interactively, stores them
// in the sales member of s and computes and stores the
// average, maximum, and minimum values
void setSales();
// display all information in structure s
void showSales() const;
};
}
#endif /* __NAMESP2_H__ */
// namesp2.cpp -- function definition of namespace
#include <iostream>
#include "namesp2.h"
namespace SALES
{
Sales::Sales(const double ar[], int n)
{
int i;
double minimum = ar[0];
double maximum = ar[0];
long double sum = 0.0;
for (i = 0; i < (n > 4? QUARTERS : n); i++) {
sales[i] = ar[i];
sum += sales[i];
if (minimum > ar[i])
minimum = ar[i];
if (maximum < ar[i])
maximum = ar[i];
}
average = sum / i;
max = maximum;
min = minimum;
while (i < 4) {
sales[i] = 0.0;
i++;
}
}
// gathers sales for 4 quarters interactively, stores them
// in the sales member of s and computes and stores the
// average, maximum, and minimum values
void Sales::setSales()
{
double minimum = 0.0;
double maximum = 0.0;
long double sum = 0.0;
for (int i = 0; i < QUARTERS; i++) {
std::cout << "Enter the #" << i + 1 << " quarter sales: ";
std::cin >> sales[i];
}
minimum = sales[0];
minimum = sales[0];
for (int i = 0; i < QUARTERS; i++) {
sum += sales[i];
if (minimum > sales[i])
minimum = sales[i];
if (maximum < sales[i])
maximum = sales[i];
}
average = sum / QUARTERS;
max = maximum;
min = minimum;
}
// display all information in structure s
void Sales::showSales() const
{
using std::cout;
using std::endl;
for (int i = 0; i < QUARTERS; i++) {
cout << "sales[" << i << "] = "
<< sales[i] << endl;
}
cout << "average = " << average
<< ", max = " << max
<< ", min = " << min << endl;
}
}