第10章编程题:
(1)
Account.h:
cpp
#ifndef ACCOUNT_H_
#define ACCOUNT_H_
#include <string>
class Account
{
private:
std::string name ;
std::string code ;
double money ;
public:
Account() ;
Account(std::string Name, std::string Code, double Money) ;
~Account() ;
void ShowAccout() ;
void InputMoney(double Money) ;
void OutputMoney(double Money) ;
} ;
#endif
Account.cpp:
cpp
#include "Account.h"
#include <iostream>
#include <string>
using namespace std ;
Account::Account()
{}
Account::Account(string Name, string Code, double Money)
{
name = Name ;
code = Code ;
money = Money ;
}
Account::~Account()
{}
void Account::ShowAccout()
{
using std::ios_base ;
ios_base::fmtflags orig =
cout.setf(ios_base::fixed, ios_base::floatfield) ;
std::streamsize prec = cout.precision(0) ;
cout << "This customer, who is called " << name
<< ", and has $" << money
<< " in " << code << " Account.\n";
cout.setf(orig, ios_base::floatfield) ;
cout.precision(prec) ;
}
void Account::InputMoney(double Money)
{
money += Money ;
}
void Account::OutputMoney(double Money)
{
money -= Money ;
}
main.cpp:
cpp
#include "Account.h"
#include <iostream>
const int Num = 8 ;
int main()
{
Account A[Num] = {
Account("Jackie Chen", "098425345", 56943),
Account("John Rebort", "*&%$#4586", 12366),
Account("Bluse Amy", "3425834", 9032455),
Account("Killy Joe", "@#%^&*890", 234568690),
Account("Beyonce Jim", "*&^%$#678", 999912366),
Account("Alan London", "5555555", 222222222222),
Account("Jackie Chen", "098425345", 56943),
Account("Floyd Dork", "4324049", 247)
} ;
std::cout << "Let's see everyone: \n" ;
for (int i=0; i<Num; i++)
{
A[i].ShowAccout() ;
}
std::cout << "\nNow everyone increases $100: \n" ;
for (int i=0; i<Num; i++)
{
A[i].InputMoney(100) ;
}
for (int i=0; i<Num; i++)
{
A[i].ShowAccout() ;
}
std::cout << "\nNow everyone decreases $100: \n" ;
for (int i=0; i<Num; i++)
{
A[i].OutputMoney(100) ;
}
for (int i=0; i<Num; i++)
{
A[i].ShowAccout() ;
}
}
(2)
Person.h:
cpp
#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' ;} ;
Person(const string & ln, const char * fn = "Heyyou") ;
void Show() const ;
void FormalShow() const ;
} ;
#endif
Person.cpp:
cpp
#include "Person.h"
#include <iostream>
#include <cstring>
using namespace std ;
Person::Person(const string & ln, const char * fn)
{
lname = ln ;
strcpy(fname, fn) ;
}
void Person::Show() const
{
cout << fname << " " << lname ;
}
void Person::FormalShow() const
{
cout << lname << ", " << fname ;
}
main.cpp:
cpp
#include "Person.h"
#include <iostream>
int main()
{
Person one ;
Person two("Smythecraft") ;
Person three("Dimwiddy", "Sam") ;
std::cout << "The name of one:\n" ;
one.Show() ;
std::cout << std::endl ;
one.FormalShow() ;
std::cout << "\nThe name of two:\n" ;
two.Show() ;
std::cout << std::endl ;
two.FormalShow() ;
std::cout << "\nThe name of three:\n" ;
three.Show() ;
std::cout << std::endl ;
three.FormalShow() ;
std::cout << std::endl ;
}
(3)
golf.h:
cpp
#ifndef GOLF_H_
#define GOLF_H_
class golf
{
private:
static const int Len = 40 ;
char fullname[Len] ;
int handicap ;
public:
golf(const char * name = "", int hc = 0) ;
~golf() ;
int setgolf() ;
void Handicap() ;
void showgolf() const ;
} ;
#endif
golf.cpp:
cpp
#include "golf.h"
#include <cstring>
#include <iostream>
golf::golf(const char * name, int hc)
{
strncpy(fullname, name, Len) ;
handicap = hc ;
}
golf::~golf()
{
std::cout << "delete!\n" ;
}
int golf::setgolf()
{
using namespace std ;
char name[Len] = {0} ;
int hc ;
cout << "Fullname: " ;
if (cin.get(name, Len).get() == -1)
{
cin.clear() ; // 务必对输入队列进行重置
cout << "Enter Fail!\n" ;
return 0 ;
}
cout << "Handicap: " ;
while(!(cin >> hc))
{
cin.clear() ;
while(cin.get() != '\n') ;
cout << "Please enter a number: " ;
}
cin.get() ;
golf G(name, hc) ;
*this = G ;
return 1 ;
}
void golf::Handicap()
{
int hc ;
std::cin >> hc ;
(*this).handicap = hc ;
}
void golf::showgolf() const
{
std::cout << "The " << handicap << " player is "
<< fullname << std::endl ;
}
main.cpp:
cpp
#include "golf.h"
#include <iostream>
const int Num = 5 ;
int main()
{
using namespace std ;
golf G[Num] ;
int num = 0 ;
cout << "Please input the information of all of players:\n" ;
for (int i=0; i<Num; i++)
{
cout << "#" << i+1 << ": \n" ;
if(!G[i].setgolf()) break ;
else num ++ ;
}
cout << "\nNow change everyone's handicap:\n" ;
for (int i=0; i<num; i++)
{
cout << "#" << i+1 << ": " ;
G[i].Handicap() ;
}
cout << "\nAnd then display all players:\n" ;
for (int i=0; i<num; i++)
{
G[i].showgolf() ;
}
}
(4)
Sales.h:
cpp
#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[], int n) ;
~Sales() ;
void setSales() ;
void ShowSales() const ;
} ;
}
#endif
Sales.cpp:
cpp
#include "Sales.h"
#include <iostream>
using namespace SALES ;
Sales::Sales()
{
min = max = average = 0 ;
for (int i=0; i<QUARTERS; i++)
sales[i] = 0 ;
}
Sales::Sales(const double ar[], int n)
{
int i ;
for (i=0; i<n && i<QUARTERS; i++)
sales[i] = ar[i] ;
while (i<QUARTERS)
{
sales[i] = 0 ;
i ++ ;
}
min = max = sales[0] ;
double total = 0 ;
for (i=0; i<QUARTERS; i++)
{
total += sales[i] ;
if (min > sales[i]) min = sales[i] ;
if (max < sales[i]) max = sales[i] ;
}
average = total / QUARTERS ;
}
Sales::~Sales()
{
std::cout << "delete!\n" ;
}
void Sales::setSales()
{
using namespace std ;
int n ;
cout << "Enter the number of double you want to input: " ;
cin >> n ;
double *ar = new double[n] ;
cout << "Enter these numbers you want to input:\n" ;
for (int i=0; i<n; i++)
{
cin >> ar[i] ;
}
Sales S(ar, n) ;
*this = S ;
delete [] ar ;
}
void Sales::ShowSales() const
{
std::cout << "The members of Sales:\n" ;
int i ;
for (i=0; i<QUARTERS; i++)
{
std::cout << sales[i] << " " ;
}
std::cout << std::endl ;
std::cout << "max = " << max << std::endl ;
std::cout << "min = " << min << std::endl ;
std::cout << "average = " << average << std::endl ;
}
main.cpp:
cpp
#include "Sales.h"
#include <iostream>
using namespace SALES ;
int main()
{
using namespace std ;
double ar[7] = {7, 6, 5, 4, 3, 2, 1} ;
Sales S1(ar, 7) ;
cout << "The content of S1:\n" ;
S1.ShowSales() ;
Sales S2 ;
S2.setSales() ;
cout << "The content of S2:\n" ;
S2.ShowSales() ;
}
(5)
Stack.h:
cpp
#ifndef STACK_H_
#define STACK_H_
struct customer {
char fullname[35] ;
double payment ;
} ;
typedef customer Item ;
class Stack
{
private:
static const int MAX = 10 ;
Item items[MAX] ;
int top ;
double total_payment ;
public:
Stack() ;
~Stack() ;
bool isempty() const ;
bool isfull() const ;
bool push(const Item & e) ;
bool pop(Item & e) ;
} ;
#endif
Stack.cpp:
cpp
#include "Stack.h"
#include <cstring>
#include <iostream>
Stack::Stack()
{
top = -1 ;
total_payment = 0 ;
}
Stack::~Stack()
{
std::cout << "Delete!\n" ;
}
bool Stack::isempty() const
{
if (top == -1) return true ;
else return false ;
}
bool Stack::isfull() const
{
if (top == (MAX-1)) return true ;
else return false ;
}
bool Stack::push(const Item & e)
{
if (top == (MAX-1)) return false ;
top++ ;
strcpy(items[top].fullname, e.fullname) ;
items[top].payment = e.payment ;
return true ;
}
bool Stack::pop(Item & e)
{
if (top == -1) return true ;
strcpy(e.fullname, items[top].fullname) ;
e.payment = items[top].payment ;
top -- ;
total_payment += e.payment ;
std::cout << "total_payment = " << total_payment << std::endl ;
return true ;
}
main.cpp:
cpp
#include "Stack.h"
#include <iostream>
#include <cctype>
int main()
{
using namespace std ;
Stack St ;
char ch ;
Item item ;
cout << "Please enter A to add a customer,\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 customer:\n" ;
cout << "Fullname: " ;
cin.get(item.fullname, 35).get() ;
cout << "Payment: " ;
cin >> item.payment ;
if (!St.push(item))
cout << "The Stack is full!\n" ;
break ;
case 'P':
case 'p': cout << "Delete a customer:\n" ;
if (!St.pop(item))
cout << "The Stack is empty!\n" ;
cout << "Fullname: " ;
cout << item.fullname << endl ;
cout << "Payment: " ;
cout << item.payment << endl ;
break ;
}
cout << "Please enter A to add a customer,\n"
<< "P to process a PO, or Q to quit.\n" ;
}
cout << "Bye!\n" ;
return 0 ;
}
(6)
Move.h:
cpp
#ifndef MOVE_H_
#define MOVE_H_
class Move
{
private:
double x ;
double y ;
public:
Move(double a = 0, double b = 0) ;
~Move() ;
void showmove() const ;
Move add(const Move & m) const ;
void reset(double a = 0, double b = 0) ;
} ;
#endif
Move.cpp:
cpp
#include "Move.h"
#include <iostream>
Move::Move(double a, double b)
{
x = a ;
y = b ;
}
Move::~Move()
{
std::cout << "Delete!\n" ;
}
void Move::showmove() const
{
std::cout << "In this object, x = " << x << ", "
<< "y = " << y << std::endl ;
}
Move Move::add(const Move & m) const
{
Move M(m.x, m.y);
return M ;
}
void Move::reset(double a, double b)
{
x = a ;
y = b ;
}
main.cpp:
cpp
#include "Move.h"
int main()
{
Move Begining ;
Move Next(9, 8) ;
Begining.showmove() ;
Next.showmove() ;
Move Destination = Begining.add(Next) ;
Destination.showmove() ;
}
(7)
Plorg.h:
cpp
#ifndef PLORG_H_
#define PLORG_H_
class Plorg
{
private:
static const int MAX = 19 ;
char fullname[MAX] ;
int CI ;
public:
Plorg(const char * name = "Plorga") ;
~Plorg() ;
void ResetCI(int ci) ;
void ShowPlorg() const ;
} ;
#endif
Plorg.cpp:
cpp
#include "Plorg.h"
#include <iostream>
#include <cstring>
Plorg::Plorg(const char * name){
strncpy(fullname, name, MAX) ;
CI = 50 ;
}
Plorg::~Plorg(){
std::cout << "Delete!\n" ;
}
void Plorg::ResetCI(int ci){
CI = ci ;
}
void Plorg::ShowPlorg() const
{
std::cout << "The fullname: " << fullname
<< ", CI = " << CI << std::endl ;
}
main.cpp:
cpp
#include "Plorg.h"
int main()
{
Plorg P1("Jim Killy") ;
Plorg P2("Jackie London") ;
Plorg P3("Trump Jim") ;
P1.ShowPlorg() ;
P2.ShowPlorg() ;
P3.ShowPlorg() ;
P1.ResetCI(90) ;
P2.ResetCI(80) ;
P3.ResetCI(70) ;
P1.ShowPlorg() ;
P2.ShowPlorg() ;
P3.ShowPlorg() ;
}
(8)
List.h:
cpp
#ifndef LIST_H_
#define LIST_H_
typedef int Item ;
struct LinkList {
Item ElemType ;
LinkList * next ;
} ;
class List
{
private:
static const int MAX = 10 ;
LinkList * head ;
LinkList * rail ;
LinkList * p ;
int num ;
public:
List() ;
~List() ;
bool Add(const Item & i) ;
bool isEmpty() ;
bool isFull() ;
void visit(void (*pf)(Item & i)) ;
} ;
#endif
List.cpp:
cpp
#include "List.h"
#include <iostream>
List::List() {
head = new LinkList ;
head->next = NULL ;
rail = p = head ;
num = 0 ;
}
List::~List() {
while (head != rail) {
p = head ;
head = head -> next ;
delete p ;
}
delete head ;
head = rail = p = NULL ;
num = 0 ;
std::cout << "This List has been destroyed!\n" ;
}
bool List::Add(const Item & i) {
if (num == MAX) return false ;
p = rail ;
p->next = new LinkList ;
p->next->ElemType = i ;
p->next->next = NULL ;
rail = p->next ;
num++ ;
return true ;
}
bool List::isEmpty() {
if (num == 0) return true ;
else return false ;
}
bool List::isFull() {
if (num == MAX) return true ;
else return false ;
}
void List::visit(void (*pf)(Item & i)) {
for (p = head->next; p; p=p->next)
(*pf)(p->ElemType) ;
}
main.cpp:
cpp
#include "List.h"
#include <iostream>
void Watch(Item & i) ;
void AddOne(Item & i) ;
int main()
{
using namespace std ;
List L ;
cout << "Let's input some numbers:\n" ;
Item i ;
while (cin>>i) {
if (L.isFull()) {
cout << "This List is filled!\n" ;
break ;
}
else L.Add(i) ;
}
cout << "Let's see all of members from this List:\n" ;
L.visit(Watch) ;
cout << endl ;
cout << "Let's add one to every member from this List:\n" ;
L.visit(AddOne) ;
L.visit(Watch) ;
cout << endl ;
}
void Watch(Item & i) {
std::cout << i << " " ;
}
void AddOne(Item & i) {
i += 1 ;
}