第十章 编程练习答案
1.
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
class BankAccount
{
private :
std::string fullname;
std::string account;
double money;
public :
BankAccount (const std::string & client, const std::string & acc, double cash = 0.0 );
void save(double cash);
void withdraw(double cash);
void show() const ;
};
#endif
#include <iostream>
#include <string>
#include "account.h"
BankAccount::BankAccount(const std ::string & client, const std ::string & ps, double cash)
{
fullname = client;
account = ps;
money = cash;
}
void BankAccount::save(double cash)
{
if (cash < 0 )
std ::cout << "Number of cash can't be negative.\n" ;
else
money += cash;
}
void BankAccount::withdraw(double cash)
{
if (cash < 0 )
std ::cout << "Number of cash can't be negative.\n" ;
else if (cash > money)
std ::cout << "You can't get more than you have!\n" ;
else
money -= cash;
}
void BankAccount::show() const
{
using std ::cout ;
cout << "Fullname: " << fullname << " Account: " << account << '\n' ;
cout << " money: $" << money << '\n' ;
}
#include <iostream>
#include <string>
#include "account.h"
//使用类,主程序变得简单
int main()
{
{
using std ::cout
cout << "Bank Account information:\n"
BankAccount b1("Nano Smart" , "ASD" , 12334.34 )
b1.show ()
BankAccount b2("Boffo Objects" , "ZXC" , 5834.983 )
b2.show ()
cout << "\nAfter a month:\n"
b1.save (1231.23 )
b1.show ()
b1.withdraw (5353.34 )
b1.show ()
b2.withdraw (555.2322 )
b2.show ()
b2.save (2354.4 )
b2.show ()
}
return 0
}
2.
#ifndef PERSON_H_
#define PERSON_H_
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
#include <iostream>
#include <string>
#include <cstring>
#include "person.h"
Person::Person(const std ::string & ln, const char * fn)
{
lname = ln;
strcpy (fname, fn);
}
void Person::Show() const
{
std ::cout << "Full Name:" << std ::endl;
std ::cout << fname << " " << lname << std ::endl;
}
void Person::FormalShow() const
{
std ::cout << "Formal Name:" << std ::endl;
std ::cout << lname << " " << fname << std ::endl;
}
#include <iostream>
#include <string>
#include <cstring>
#include "person.h"
int main()
{
{
using std ::cout ;
using std ::endl;
Person one;
one.Show();
cout << endl;
one.FormalShow();
cout << endl;
Person two("Smythecraft" );
two.Show();
cout << endl;
two.FormalShow();
cout << endl;
Person three("Dimwdiddy" , "Sam" );
three.Show();
cout << endl;
three.FormalShow();
cout << endl;
}
return 0 ;
}
3.
#ifndef GOLF10_H_
#define GOLF10_H_
#include <string>
class Golf
{
private :
std::string fullname;
int handicap;
public :
Golf (const std::string name, int hc = 0 );
int setgolf();
void sethandicap(const int hc);
void showgolf()const ;
};
#endif
#include <iostream>
#include <string>
#include "U10p3golf.h"
Golf::Golf(const std ::string name, int hc)
{
fullname = name;
handicap = hc;
}
int Golf::setgolf()
{
using namespace std ;
int handicap = 0 ;
int temp = 1 ;
cout << "The fullname is: " ;
getline(cin , fullname);
cout << "The handicap is: " ;
cin >> handicap;
Golf g(fullname, handicap);
*this = g;
return fullname == "" ? 0 : 1 ;
}
void Golf::sethandicap(const int hc)
{
handicap = hc;
}
void Golf::showgolf() const
{
using std ::cout ;
using std ::endl;
cout << "The fullname is: " << fullname << endl;
cout << "The handicap is: " << handicap << endl;
}
#include <iostream>
#include "U10p3golf.h"
int main()
{
Golf g("Alex Tony" , 10 );
g.showgolf();
std ::cout << std ::endl;
g.setgolf();
std ::cout << std ::endl;
g.showgolf();
std ::cout << std ::endl;
g.sethandicap(99 );
g.showgolf();
return 0 ;
}
4.
#ifndef SALES_H_
#define SALES_H_
namespace SALES
{
class Sales
{
private :
static const int QUARTERS = 4 ;
double sales[QUARTERS];
double average;
double max;
double min;
public :
Sales ();
Sales(const double *ar, const int n);
void setsales();
void showSales() const ;
};
}
#endif
#include <iostream>
#include "U10p4sales.h"
SALES::Sales::Sales()
{
for (int i = 0 ; i < QUARTERS; i++)
sales[i] = 0 ;
average = 0 ;
max = 0 ;
min = 0 ;
}
SALES::Sales::Sales(const double * ar,const int n)
{
double a, b, c;
a = b = ar[0 ];
c = 0.0 ;
for (int i = 0 ; i < n; i++)
{
sales[i] = ar[i];
c += ar[i];
if (ar[i] < a)
a = ar[i];
if (ar[i] > b)
b = ar[i];
}
average = c / n;
max = b;
min = a;
}
void SALES::Sales::setsales()
{
double ar[QUARTERS]={0 ,0 ,0 ,0 };
std ::cout << "Please enter 4 number:" << std ::endl;
int n = 0 ;
do {
std ::cin >> ar[n];
n++;
}while ((n < QUARTERS) );
double a, b, c;
a = b = ar[0 ];
c = 0.0 ;
for (int i = 0 ; i < n; i++)
{
sales[i] = ar[i];
c += ar[i];
if (ar[i] < a)
a = ar[i];
if (ar[i] > b)
b = ar[i];
}
average = c / n;
max = b;
min = a;
}
void SALES::Sales::showSales() const
{
for (int i = 0 ; i < QUARTERS; i++)
std ::cout << sales[i] << " " ;
std ::cout << "\nThe average is: " << average << " The max is: "
<< max << " The min is: " << min << std ::endl;
}
#include <iostream>
#include "U10p4sales.h"
int main()
{
int n;
std ::cout << "Please enter n items:" ;
std ::cin >> n;
double a[n];
std ::cout << "Enter numbers: " << std ::endl;
for (int i = 0 ; i < n; i++)
std ::cin >> a[i];
SALES::Sales s(a, n);
s.showSales();
s.setsales();
s.showSales();
return 0 ;
}
5.
#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);
};
#endif
#include "U10p5stack.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 (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 ;
}
#include <iostream>
#include <cctype>
#include "U10p5stack.h"
int main()
{
using namespace std ;
double payment;
Stack st;
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 fullname: " ;
cin .get(po.fullname, 35 );
cout << "Enter a PO number to add: " ;
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 already empty\n" ;
else {
st.pop(po);
cout << "PO: " << po.fullname << " ayment: " << po.payment << " popped\n" ;
payment += po.payment;
cout << "The total of payment: " << payment << 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.
#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
#include <iostream>
#include "U10p6move.h"
Move::Move (double a, double b)
{
x = a;
y = b;
}
void Move::showmove() const
{
std ::cout << "X: " << x << " Y: " << y << std ::endl;
std ::cout << std ::endl;
}
Move Move::add(const Move & m) const
{
return Move(x + m.x, y + m.y);
}
void Move::reset(double a, double b)
{
x = a;
y = b;
}
#include <iostream>
#include "U10p6move.h"
//主程序
int main()
{
std ::cout << "The first place:\n"
Move m1(1.1 , 3.3 )
Move m2(4.2 , 2.0 )
Move m3(-2.2 , -4.0 )
m1.showmove ()
std ::cout << "Move m2:\n"
m1.add (m2).showmove ()
std ::cout << "Then move m3:\n"
m1.add (m2).add (m3).showmove ()
std ::cout << "Place reset:\n"
m1.reset ()
m1.showmove ()
return 0
}
7.
#ifndef PLORG_H
#define PLORG_H
class Plorg
{
private :
char name[20];
int CI;
public :
Plorg (char name[20] = "Plorga" , int n = 50 );
void setPlorg(int n);
void showPlorg();
};
#endif
#include <iostream>
#include <cstring>
#include "Plorg.h"
Plorg::Plorg (char * str, int n)
{
strcpy (name, str);
CI = n;
}
void Plorg::setPlorg(int n)
{
CI = n;
}
void Plorg::showPlorg()
{
std ::cout << "The plorg name: " << name << std ::endl;
std ::cout << "The plorg CI: " << CI << std ::endl;
std ::cout << std ::endl;
}
#include <iostream>
#include <cstring>
#include "Plorg.h"
int main()
{
using namespace std ;
Plorg p1;
p1.showPlorg();
char s[20 ];
int n;
cout << "Please enter the plorg name: " ;
cin .getline(s, 20 );
cout << "Please enter the CI: " ;
cin >> n;
Plorg p2(s, n);
p2.showPlorg();
int count;
cout << "Please enter the CI: " ;
cin >> count;
p1.setPlorg(count);
p1.showPlorg();
return 0 ;
}
8.
#ifndef LIST_H
#define LIST_H
typedef int Item;
class List
{
private :
static const int MAX = 100 ;
Item items[MAX];
int top;
public :
List (){ top = 0 ; }
bool add(const Item item);
bool isempty() const ;
bool isfull() const ;
void visit(void (*pf)(Item & item));
};
#endif
#include <iostream>
#include "List.h"
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 ;
}
void List::visit(void (*pf)(Item & item))
{
std ::cout << "\nDisplay the item: \n" ;
for (int i = 0 ; i < top; ++i)
(*pf)(items[i]);
}
#include <iostream>
#include <string>
#include <cctype>
#include "List.h"
void func(Item & item);
int main()
{
using namespace std ;
List st;
string str;
cout << "At first: " << endl;
if (st.isempty() == 1 )
str = "Yes!" ;
if (st.isempty() == 0 )
str = "No!" ;
cout << "isEmpty? " << str << endl;
if (st.isfull() == 1 )
str = "Yes!" ;
if (st.isfull() == 0 )
str = "No!" ;
cout << "isFull? " << str << endl;
st.add(1 );
st.add(2 );
st.add(3 );
st.add(4 );
cout << "\nNow: " << endl;
if (st.isempty() == 1 )
str = "Yes!" ;
if (st.isempty() == 0 )
str = "No!" ;
cout << "isEmpty? " << str << endl;
if (st.isfull() == 1 )
str = "Yes!" ;
if (st.isfull() == 0 )
str = "No!" ;
cout << "isFull? " << str << endl;
void (*pf)(Item & item);
pf = func;
st.visit(pf);
system("PAUSE" );
return 0 ;
}
void func(Item & item)
{
std ::cout << item << std ::endl;
}