10-1
Bank.h
#ifndef Bank_h_
#define Bank_h_
#include<cstring>
class BankAccount
{
private:
char name[40];
char acctnum[25];
double balance;
public:
BankAccount(const char* client, const char* num, double bal = 0.0);
void show(void) const;
void deposit(double cash);
void withdraw(double cash);
};
#endif
bank.cpp
#include <iostream>
#include"Bank.h"
#include<cstring>
using namespace std;
BankAccount::BankAccount(const char* client, const char* num, double bal)
{
strncpy_s(name, client, 39);
name[39] = '\0';
strncpy_s(acctnum, num, 24);
acctnum[24] = '\0';
balance = bal;
}
void BankAccount::show(void) const
{
cout << "名称:" << name;
cout << "\n编号:" << acctnum;
cout << "\n存款:" << balance << endl;
}
void BankAccount::deposit(double cash)
{
balance += cash;
}
void BankAccount::withdraw(double cash)
{
balance -= cash;
}
main.cpp
#include <iostream>
#include "Bank.h"
int main()
{
char guest_name[40];
char guest_acctnum[25];
double guest_balance;
using namespace std;
cout << "请输入客户名称:" << endl;
cin >> guest_name;
cout << "请输入客户编号:" << endl;
cin >> guest_acctnum;
cout << "请输入客户存款:" << endl;
cin >> guest_balance;
BankAccount ba_1(guest_name, guest_acctnum, guest_balance);
ba_1.show();
ba_1.deposit(100);
ba_1.withdraw(200);
ba_1.show();
return 0;
}
10-2
书上static const int LIMIT = 25少了个int,好像是写掉了,还是我买的书不是正版?
Person.h
#ifndef Bank_h_
#define Bank_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
Person.cpp
#include <iostream>
#include"Person.h"
using namespace std;
Person::Person(const string& ln, const char* fn)
{
lname = ln;
strcpy_s(fname, fn);
}
void Person::show() const
{
cout << "The person is " << fname << " " << lname << endl;
}
void Person::FormalShow() const
{
cout << "The person is " << lname << " " << fname << endl;
}
main.cpp
#include <iostream>
#include "Person.h"
int main()
{
using namespace std;
Person one;
Person two("Smythecraft");
Person three("Dimwiddy", "Sam");
one.show();
cout << endl;
one.FormalShow();
two.show();
two.FormalShow();
three.show();
three.FormalShow();
return 0;
}
10-3
Golf.h
#ifndef Golf_h_
#define Golf_h_
const int Len = 40;
class golf
{
private:
char fullname[Len];
int handicap;
public:
golf(const char* name, int hc);
int setgolf();
void sethandicap(int hc);
void showgolf() const;
};
#endif
Golf.cpp
#include <string.h>
#include <iostream>
#include"Golf.h"
using namespace std;
golf::golf(const char* name, int hc)
{
strcpy_s(fullname, name);
handicap = hc;
}
int golf::setgolf()
{
cout << "Please enter fullname: \n";
cin.getline(fullname, Len);
if (!strcmp(fullname, ""))
return 0;
cout << "Please enter handicap: \n";
cin >> handicap;
cin.get();
golf g(fullname, handicap);
*this = g;
return fullname == "" ? 0 : 1;
}
void golf::sethandicap(int hc)
{
handicap = hc;
}
void golf::showgolf() const
{
cout << "fullname: " << fullname << "\n";
cout << "handicap: " << handicap << "\n";
}
main.cpp
#include <string.h>
#include <iostream>
#include"Golf.h"
const int GolfSize = 3;
int main()
{
golf ann("Ann Birdfree", 24);
ann.sethandicap(12);
ann.showgolf();
ann.setgolf();
ann.showgolf();
return 0;
}
10-4
同样可以使用重载,另外发现了一个问题,在setSales()中,考虑max和min时,因为是临时输入的新数组,所以可以在输入数组之后再判断大小,如果直接令max=min=sale[0],会将max和min设置为初始的默认值0,是错误的
Sales.h
#ifndef SALES_h_
#define SALES_h_
namespace SALES
{
const int QUARTERS = 8;
class Sales
{
private:
double sales[QUARTERS];
double average;
double max;
double min;
public:
Sales();
Sales(const double* ar, int n);
void setSales();
void setSales(const double ar[], int n);
void showSales() const;
};
}
#endif
Sales.cpp
#include <iostream>
#include"Sales.h"
using namespace std;
using namespace SALES;
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, int n)
{
double sum = 0;
max = ar[0];
min = ar[0];
if (n < QUARTERS)
{
for (int i = 0; i < n; i++)
{
sales[i] = ar[i];
sum += sales[i];
if (max < sales[i])
max = sales[i];
else if (min > sales[i])
min = sales[i];
}
average = sum / n;
for (int i = n; i < QUARTERS; i++)
sales[i] = 0;
}
else
cout << "too long\n";
}
void SALES::Sales::setSales()
{
const double* ar[QUARTERS];
double sum = 0;
int n = 0;
cout << "Please enter num of sales:" << endl;
cin >> n;
cout << "Please enter sales:" << endl;
if (n < QUARTERS)
{
for (int i = 0; i < n; i++)
{
cin >> sales[i];
sum += sales[i];
}
max = sales[0];
min = sales[0];
for (int i = 0; i < n; i++)
{
if (max < sales[i])
max = sales[i];
else if (min > sales[i])
min = sales[i];
}
average = sum / n;
for (int i = n; i < QUARTERS; i++)
sales[i] = 0;
}
else
cout << "too long\n";
}
void SALES::Sales::setSales(const double ar[], int n)
{
double sum = 0;
max = ar[0];
min = ar[0];
if (n < QUARTERS)
{
for (int i = 0; i < n; i++)
{
sales[i] = ar[i];
sum += ar[i];
if (max < ar[i])
max = ar[i];
else if (min > ar[i])
min = ar[i];
}
average = sum / n;
max = max;
min = min;
for (int i = n; i < QUARTERS; i++)
sales[i] = 0;
}
else
cout << "too long\n";
}
void SALES::Sales::showSales() const
{
cout << "Sales" << "\n";
cout << "Average: " << average << "\n";
cout << "max: " << max << "\n";
cout << "min: " << min << "\n";
}
main.cpp
#include <iostream>
#include"Sales.h"
using namespace SALES;
int main()
{
Sales a, b;
double arr[3] = { 1.1, 22, 4.4 };
a.setSales(arr, 3);
a.showSales();
b.setSales();
b.showSales();
return 0;
}
10-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 isfull() const;
bool isempty() const;
bool push(const Item& item);
bool pop(Item& item);
};
#endif
Stack.cpp
#include <iostream>
#include"Stack.h"
using namespace std;
Stack::Stack()
{
top = 0;
}
bool Stack::isfull() const
{
return top == MAX;
}
bool Stack::isempty() const
{
return top == 0;
}
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 <iostream>
#include"Stack.h"
int main()
{
using namespace std;
double payment = 0;
Stack st;
char ch;
Item it;
cout << "Please enter A to add,\n" << "P to process a it, or Q to quit.\n";
while (cin >> ch && ch != 'Q')
{
while (cin.get() != '\n')
continue;
switch (ch)
{
case'A':
case'a':cout << "Enter a item fullname: ";
cin.get(it.fullname, 35);
cout << "Enter a item number to add: ";
cin >> it.payment;
cin.get();
if (st.isfull())
cout << "stack already full\n";
else
st.push(it);
break;
case'P':
case'p':if (st.isempty())
cout << "stack already empty\n";
else {
st.pop(it);
cout << "Item: " << it.fullname << " payment: " << it.payment << " popped\n";
payment += it.payment;
cout << "The total of payment: " << payment << endl;
}
break;
}
}
return 0;
}
10-6
Move.h
#ifndef MOVE_h_
#define MOVE_h_
class Move
{
private:
double x;
double y;
public:
Move(double a = 10, double b = 0);
void showmove() const;
Move add(const Move& m) const;
void reset(double a = 0, double b = 0);
};
#endif
Move.cpp
#include <iostream>
#include"Move.h"
using namespace std;
Move::Move(double a, double b)
{
x = a;
y = b;
}
void Move::showmove() const
{
cout << "x = " << x << ", ";
cout << "y = " << y << 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;
}
main.cpp
#include <iostream>
#include"Move.h"
int main()
{
using namespace std;
Move mo(1, 1), mo2(2, 2);
mo.showmove();
mo = mo.add(mo2);
mo.showmove();
mo.reset();
mo.showmove();
return 0;
}
10-7
Plorg.h
#ifndef PLORG_h_
#define PLORG_h_
const int LEN = 20;
class Plorg
{
private:
char name[LEN];
int CI;
public:
Plorg(const char name[LEN] = "Plorga", const int CI = 50);
void setCI(int CI);
void showplorg();
};
#endif
Plorg.cpp
#include <iostream>
#include"Plorg.h"
using namespace std;
Plorg::Plorg(const char* n, const int c)
{
strcpy_s(name, n);
CI = c;
}
void Plorg::setCI(int c)
{
CI = c;
}
void Plorg::showplorg()
{
cout << "Plorg name: " << name << endl;
cout << "CI: " << CI << endl;
}
main.cpp
#include <iostream>
#include"Plorg.h"
int main()
{
using namespace std;
Plorg pl;
pl.showplorg();
pl.setCI(20);
pl.showplorg();
return 0;
}
10-8
List.h
#ifndef LIST_h_
#define LIST_h_
typedef int Item;
class List
{
private:
enum {MAX = 50};
Item items[MAX];
int top;
public:
List();
bool add(const Item& items);
bool isempty() const;
bool isfull() const;
void visit(void(*pf)(Item& ));
};
#endif
List.cpp
#include <iostream>
#include"List.h"
using namespace std;
List::List()
{
top = 0;
}
bool List::add(const Item& item)
{
if (top < MAX)
{
items[top++] = item;
return true;
}
else
return false;
}
bool List::isempty() const
{
return top == 0;
}
bool List::isfull() const
{
return top == MAX;
}
void List::visit(void(*pf)(Item& ))
{
for (int i = 0; i < top; ++i)
{
(*pf)(items[i]);
}
}
main.cpp
#include <iostream>
#include"List.h"
using namespace std;
void f(Item& item);
int main()
{
List li;
cout << li.isfull() << endl;
cout << li.isempty() << endl;
li.add(9);
li.add(4);
li.add(8);
cout << li.isfull() << endl;
cout << li.isempty() << endl;
void(*pf)(Item & item);
pf = f;
li.visit(pf);
return 0;
}
void f(Item& item)
{
cout << item << " ";
}