1、
7、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/19
From:C++ primer plus 第十章编程练习 第1题
*********************************************************************************************/
/***************************** BankAccount.h**********************************************************/
#ifndef BANKACCOUNT_H_
#define BANKACCOUNT_H_
class BankAccount
{
private:
char name[30];
char account_number[30];
double balance;
public:
BankAccount(const char * client,const char * num,double bal);
void show() const;
void deposit(double cash);
void withdraw(double cash);
};
#endif
/***************************** BankAccount.cpp**********************************************************/
#include"stdafx.h"
#include"BankAccount.h"
#include<string>
#include<iostream>
using namespace std;
BankAccount::BankAccount(const char * client, const char * num, double bal)
{
strcpy_s(name, client);
strcpy_s(account_number, num);
balance = bal;
}
void BankAccount::show() const
{
cout << "name:" << name << endl;
cout << "account number:" << account_number << endl;
cout << "balance:" << balance<<endl;
}
void BankAccount::deposit(double cash)
{
balance += cash;
}
void BankAccount::withdraw(double cash)
{
balance -= cash;
}
/***************************** main.cpp**********************************************************/
#include"stdafx.h"
#include"BankAccount.h"
#include<iostream>
using namespace std;
int main()
{
BankAccount july("july","12345",50000);
july.show();
double deposit_money;
double withdraw_money;
cout << "Please enter the money to deposit:";
cin >> deposit_money;
july.deposit(deposit_money);
july.show();
cout << "Please enter the money to withdraw:";
cin >> withdraw_money;
july.withdraw(withdraw_money);
july.show();
return 0;
}
2、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/19
From:C++ primer plus 第十章编程练习 第2题
*********************************************************************************************/
/***************************** person.h******************************************************/
#ifndef PERSON_H_
#define PERSON_H_
#include<string>
using namespace std;
class person
{
private:
static const int LIMIT = 25;
string lname;
char fname[LIMIT];
public:
person(){ lname = "", fname[0] = '\0'; }//#1
person(const string & ln, const char * fn = "Heyyou");//#2
void show()const;//firstname lastname format
void FormalShow() const;//lastname firstname format
};
#endif
/***************************** person.cpp******************************************************/
#include"stdafx.h"
#include"person.h"
#include<iostream>
#include<string>
using namespace std;
person::person(const string & ln, const char * fn )
{
lname = ln;
strcpy_s(fname,fn);
}
void person::show()const
{
cout << fname<<" ";
cout << lname << endl;
}
void person::FormalShow()const
{
cout << lname << " ";
cout << fname << endl;
}
/***************************** main.cpp******************************************************/
#include"stdafx.h"
#include"person.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
person one;
person two("Smythecraft");
person three("Dimwiddy", "Sam");
one.show();
one.FormalShow();
two.show();
two.FormalShow();
three.show();
three.FormalShow();
return 0;
}
3、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/20
From:C++ primer plus 第十章编程练习 第3题
*********************************************************************************************/
/***********************************golf.h***************************************************/
#ifndef GOLF_H_
#define GOLF_H_
const int Len = 40;
class golf
{
private:
static const int Len = 40;
char fullname[Len];
int handicap;
public:
golf();
golf(const char * name, int hc);
const golf & setgolf(golf &);
void showgolf()const;
};
#endif
/*******************golf.cpp********************************/
#include "stdafx.h"
#include<iostream>
#include<string>
#include"golf.h"
using namespace std;
golf::golf()
{
strcpy_s(fullname, "no name");
handicap = 0;
}
golf::golf(const char * name, int hc)
{
strcpy_s(fullname, name);
handicap = hc;
}
const golf & golf::setgolf(golf & g)
{
cout << "Please enter the name:";
cin.getline(g.fullname, Len);
cout << "Please enter the handicap:";
cin >> g.handicap;
cin.get();
return *this;
}
void golf::showgolf()const
{
cout << "fullname:" << fullname << endl;
cout << "handicap:" << handicap << endl;
}
/*******************main.cpp********************************/
#include "stdafx.h"
#include<iostream>
#include"golf.h"
using namespace std;
int main()
{
golf a;
golf b("Ann Birdfree", 24);
a.showgolf();
b.showgolf();
a.setgolf(a);
a.showgolf();
return 0;
}
4、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/20
From:C++ primer plus 第十章编程练习 第4题
*********************************************************************************************/
/***********************************Sales.h***************************************************/
namespace SALES
{
class sales
{
private:
static const int QUARTERS = 4;
double salesArr[QUARTERS];
double average;
double max;
double min;
public:
sales();
sales(const double ar[], int n);
void sales::setSales();
void showSales();
};
};
/******************Sales.cpp****************************/
#include"stdafx.h"
#include"Sales.h"
#include<iostream>
namespace SALES
{
using std::cout;
using std::cin;
using std::endl;
static double malaverage(double arr[], int ArrSize)
{
double s = 0;
for (int i = 0; i < ArrSize; i++)
s += arr[i];
double average = s / ArrSize;
return average;
}
static double malmax(double arr[], int ArrSize)
{
double temp = arr[0];
for (int i = 1; i < ArrSize; i++)
{
if (arr[i] > temp)
temp = arr[i];
}
return temp;
}
static double malmin(double arr[], int ArrSize)
{
double temp = arr[0];
for (int i = 1; i < ArrSize; i++)
{
if (arr[i] < temp)
temp = arr[i];
}
return temp;
}
sales::sales()
{
for (int i = 0; i < QUARTERS; i++)
salesArr[i] = 0;
average = 0;
max = 0;
min = 0;
}
sales::sales(const double ar[], int n)
{
int m = QUARTERS < n ? QUARTERS : n;
for (int i = 0; i < m; i++)
{
salesArr[i] = ar[i];
}
for (int i = m; i < QUARTERS; i++)
salesArr[i] = 0;
average = malaverage(salesArr, m);
max = malmax(salesArr, m);
min = malmin(salesArr, m);
}
void sales::setSales()
{
cout << "Enter 4 sales:\n";
for (int i = 0; i < QUARTERS; i++)
{
cout << "sales " << i + 1 << " :";
cin >> salesArr[i];
}
average = malaverage(salesArr, QUARTERS);
max = malmax(salesArr, QUARTERS);
min =malmin(salesArr, QUARTERS);
*this = sales(salesArr, QUARTERS);
}
void sales::showSales()
{
cout << "sales:\n";
for (int i = 0; i < QUARTERS; i++)
cout << salesArr[i] << endl;
cout << "average:" << average << endl;
cout << "max:" << max << endl;
cout << "min:" << min << endl;
}
};
/******************main.cpp****************************/
#include"stdafx.h"
#include"Sales.h"
#include<iostream>
using namespace std;
using namespace SALES;
int main()
{
sales s1;
s1.showSales();
double ar[3] = { 1.2, 2.0, 3.1 };
sales s2(ar, 3);
s2.showSales();
s1.setSales();
s1.showSales();
return 0;
}
5、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/20
From:C++ primer plus 第十章编程练习 第5题
*********************************************************************************************/
/***********************************stack.h***************************************************/
#ifndef STACK_H_
#define STACK_H_
struct customer
{
char fullname[35];
double payment;
};
typedef customer 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);
};
typedef customer Item;
#endif
/******************stack.cpp****************************/
#include"stdafx.h"
#include"stack.h"
#include<iostream>
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 (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;
}
/******************main.cpp****************************/
#include"stdafx.h"
#include"stack.h"
#include<iostream>
using namespace std;
int main()
{
Stack st;
char ch;
double s = 0.0;
customer 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.fullname to add:";
cin >> po.fullname;
cout << "Enter a PO.payment to add:";
cin >> po.payment;
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
{
s += po.payment;
st.pop(po);
cout << "PO# " << po.fullname << " popped\n";
cout << "sum of payment:" << s << 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;
}
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/20
From:C++ primer plus 第十章编程练习 第6题
*********************************************************************************************/
/***********************************stack.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.cpp****************************/
#include"stdafx.h"
#include"move.h"
#include<iostream>
using namespace std;
Move::Move(double a, double b)
{
x = a;
y = b;
}
void Move::showmove()const
{
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;
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
/******************main.cpp****************************/
#include"stdafx.h"
#include"move.h"
#include<iostream>
using namespace std;
int main()
{
Move m1;
cout << "m1:\n";
m1.showmove();
Move m2(1, 2);
cout << "m2:\n";
m2.showmove();
m2.reset(3, 4);
cout << "reset m2:\n";
m2.showmove();
Move m3(1, 1);
Move m4 = m2.add(m3);
cout << "m4:\n";
m4.showmove();
return 0;
}
7、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/20
From:C++ primer plus 第十章编程练习 第7题
*********************************************************************************************/
/***********************************stack.h***************************************************/
#ifndef PLORG_H_
#define PLORG_H_
class Plorg
{
private:
static const int MAX = 20;
char name[MAX];
int CI;
public:
Plorg();
Plorg(const char * n, int c=50);
void resetCI(int c);
void show()const;
};
#endif
/***********************************plorg.cpp***************************************************/
#include"stdafx.h"
#include"plorg.h"
#include<iostream>
using namespace std;
Plorg::Plorg()
{
strcpy_s(name, "Plorga");
CI = 0;
}
Plorg::Plorg(const char * n, int c)
{
strcpy_s(name,n);
CI = c;
}
void Plorg::resetCI(int c)
{
CI = c;
}
void Plorg::show()const
{
cout << "name: " << name << endl;
cout << "CI: " << CI << endl;
}
/***********************************main.cpp***************************************************/
#include"stdafx.h"
#include"plorg.h"
#include<iostream>
using namespace std;
int main()
{
Plorg p1;
cout << "p1:\n";
p1.show();
Plorg p2("New Plorga");
cout << "p2:\n";
p2.show();
p2.resetCI(25);
cout << "reset CI p2:\n";
p2.show();
return 0;
}
8、
/********************************************************************************************
Author:Tang Qingyun
Time:2017/03/20
From:C++ primer plus 第十章编程练习 第8题
*********************************************************************************************/
/***********************************list.h***************************************************/
#ifndef LIST_H_
#define LIST_H_
typedef int Item;
class List
{
private:
static const int MAX = 10;
Item items[MAX];
int top;
public:
List();
bool isempty() const;
bool isfull() const;
bool add(const Item & item);
int count();
void visit(void(*pf)(Item &));
};
#endif
/***********************************list.cpp***************************************************/
#include"stdafx.h"
#include"list.h"
#include<iostream>
using namespace std;
List::List()
{
top = 0;
}
bool List::isempty() const
{
return top == 0;
}
bool List::isfull() const
{
return top == MAX;
}
bool List::add(const Item & item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
int List::count()
{
return top;
}
void List::visit(void(*pf)(Item &))
{
for (int i = 0; i < top; i++)
(*pf)(items[i]);
}
/***********************************main.cpp***************************************************/
#include"stdafx.h"
#include"list.h"
#include<iostream>
using namespace std;
void show(Item & item);
int main()
{
List L1;
Item po;
if (L1.isempty())
cout << "The List is empty.\n";
if (L1.isfull())
cout << "The List is full.\n";
else
cout << "Please Enter the data item(q to quit):\n";
while (cin >> po)
{
if (L1.add(po)==false)
{
cout << "The List is full.\n";
exit(1);
}
cout << "Enter next data item(q to quit):\n";
}
L1.visit(show);;
return 0;
}
void show(Item & item)
{
cout << item << endl;
}