记录我看C++ Primer Plus(第六版)第十二章源代码
12.1
//12.1
#include
#ifndef STRNGBAD_H_
#define STRNGBAD_H_
class StringBad
{
private:
char *str;
int len;
static int num_strings;
public:
StringBad(const char *s);
StringBad();
~StringBad();
friend std::ostream &operator<<(std::ostream &os, const StringBad &st);
};
#endif
12.2
#include
#include"strngbad.h"
using std::cout;
int StringBad::num_strings = 0;
StringBad::StringBad(const char *s)
{
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
num_strings++;
cout << num_strings << ": \"" << str << "\"object created\n";
}
StringBad::StringBad()
{
len = 4;
str = new char[4];
std::strcpy(str, "C++");
num_strings++;
cout << num_strings << ": \"" << str << "\" default object created\n";
}
StringBad::~StringBad()
{
cout << "\"" << str << "\" objecy deleted, ";
--num_strings;
cout << num_strings << " left\n";
delete[] str;
}
std::ostream & operator<<(std::ostream &os, const StringBad &st)
{
os << st.str;
return os;
}
12.3
#include
using std::cout;
#include"strngbad.cpp"
void callme1(StringBad &);
void callme2(StringBad);
int main()
{
using std::endl;
{
cout << "Starting an inner bllock.\n";
StringBad headline1("Celery Stalks at Midnight");
StringBad headline2("Lettuce Prey");
StringBad sports("Spinach Leaves Bow1 for Dollars");
cout << "headline1: " << headline1 << endl;
cout << "headline2: " << headline2 << endl;
cout << "sports: " << sports << endl;
callme1(headline1);
cout << "headline1: " << headline1 << endl;
callme2(headline2);
cout << "headline2: " << headline2 << endl;
cout << "Initialize one object to another:\n";
StringBad sailor = sports;
cout << "sailor: " << sailor << endl;
cout << "Assign one object to another:\n";
StringBad knot;
knot = headline1;
cout << "Knot: " << knot << endl;
cout << "Exiting the block.\n";
}
cout << "End of main()\n";
return 0;
}
void callme1(StringBad &rsb)
{
cout << "String passed by reference:\n";
cout << " \"" << rsb << "\"\n";
}
void callme2(StringBad sb)
{
cout << "String passed by value:\n";
cout<<" \""<
12.4
//12.4
#ifndef STRING1_H_
#define STRING1_H_
#include
using std::ostream;
using std::istream;
class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIM = 80;
public:
String(const char *s);
String();
String(const String &);
~String();
int length() const { return len; }
String &operator=(const String &);
String &operator=(const char *);
char &operator[](int i);
const char &operator[](int i) const;
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st, const String &st2);
friend ostream &operator<<(ostream &os, const String &st);
friend istream &operator>>(istream &is, String &st);
static int HowMany();
};
#endif
12.5
//12.5
#include
#include"string1.h"
using std::cin;
using std::cout;
int String::num_strings = 0;
String::String(const char *s)
{
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
num_strings++;
}
String::String()
{
len = 4;
str = new char[1];
str[0] = '\0';
num_strings++;
}
String::String(const String &st)
{
num_strings++;
len=st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
}
String::~String()
{
--num_strings;
delete[] str;
}
String &String::operator=(const String &st)
{
if(this==&st)
return *this;
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
String &String::operator=(const char *s)
{
delete[] str;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
return *this;
}
char &String::operator[](int i)
{
return str[i];
}
const char &String::operator[](int i) const
{
return str[i];
}
bool operator<(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2)
{
return st2 > st1;
}
bool operator==(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) == 0);
}
ostream &operator<<(ostream &os, const String &st)
{
os << st.str;
return os;
}
istream &operator>>(istream &is, String &st)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if(is)
st = temp;
while(is&&is.get()!='\n')
continue;
return is;
}
int String::HowMany()
{
return num_strings;
}
12.6
#include
#include"string1.cpp"
const int ArSize = 10;
const int MaxLen = 81;
int main()
{
using std::cin;
using std::cout;
using std::endl;
String name;
cout << "Hi what's your name?\n>>";
cin >> name;
cout <:\n";
String sayings[ArSize];
char temp[MaxLen];
int i;
for (i = 0; i < ArSize;i++)
{
cout << i+1<< ": ";
cin.get(temp, MaxLen);
while(cin&&cin.get()!='\n')
continue;
if(!cin||temp[0]=='\0')
break;
else
sayings[i] = temp;
}
int total = i;
if(total>0)
{
cout << "Here are your sayings:\n";
for (i = 0; i < total;i++)
cout << sayings[i][0] << ": " << sayings[i] << endl;
int shortest = 0;
int first = 0;
for (i = 1; i < total;i++)
{
if(sayings[i].length()
12.7
#include
#include
#include
#include"string1.cpp"
const int ArSize = 10;
const int MaxLen = 81;
int main()
{
using namespace std;
String name;
cout << "Hi, what's your name?\n>>";
cin >> name;
cout << name << ", please enter up to " << ArSize
<< " short sayings :\n";
String sayings[ArSize];
char temp[MaxLen];
int i;
for (i = 0; i < ArSize;i++)
{
cout << i + 1 << ": ";
cin.get(temp, MaxLen);
while(cin&&cin.get()!='\n')
continue;
if(!cin||temp[0]=='\0')
break;
else
sayings[i] = temp;
}
int total = i;
if(total>0)
{
cout << "Here are your sayings:\n";
for (i = 0; i < total;i++)
cout << sayings[i] << "\n";
String *shortest = &sayings[0];
String *first = &sayings[0];
for (i = 1; i < total;i++)
{
if(sayings[i].length()length())
shortest = &sayings[i];
if(sayings[i]<*first)
first = &sayings[i];
cout << "Shortest saying:\n"
<< *shortest << endl;
cout << "First alphabetically:\n"
<< *first << endl;
srand(time(0));
int choice = rand() % total;
String *favorite = new String(sayings[choice]);
cout << "My favorite saying:\n"
<< *favorite << endl;
delete favorite;
}
}
else
cout << "Not much to say,eh?\n";
cout << "Bye.\n";
return 0;
}
12.8
#include
#include
#include
using namespace std;
const int BUF = 512;
class JustTesting
{
private:
string words;
int number;
public:
JustTesting(const string &s="Just Testing",int n=0)
{
words = s;
number = n;
cout << words << " constructed\n";
}
~JustTesting()
{
cout << words << " destroyed\n";
}
void Show() const { cout << words << "," << number << endl; }
};
int main()
{
char *buffer = new char[BUF];
JustTesting *pc1, *pc2;
pc1 = new (buffer) JustTesting;
pc2 = new JustTesting("Heap1", 20);
cout << "Meory block addresses:\n"
<< "Buffer: "
<< (void *)buffer << " heap:" << pc2 << endl;
cout << "Memory contents:\n";
cout << pc1 << ": ";
pc1->Show();
cout << pc2 << ": ";
pc2->Show();
JustTesting *pc3, *pc4;
pc3 = new (buffer) JustTesting("Bad Idea", 6);
pc4 = new JustTesting("Heap2", 10);
cout << "Memory contents:\n";
cout << pc3 << ": ";
pc3->Show();
cout << pc4 << ": ";
pc4->Show();
delete pc2;
delete pc4;
delete[] buffer;
cout << "Done\n";
return 0;
}
12.9
#include
#include
#include
using namespace std;
const int BUF = 512;
class JustTesting
{
private:
string words;
int number;
public:
JustTesting(const string &s="Just Testing",int n=0)
{
words = s;
number = n;
cout << words << " constructed\n";
}
~JustTesting()
{
cout << words << " destroyed\n";
}
void Show() const { cout << words << "," << number << endl; }
};
int main()
{
char *buffer = new char[BUF];
JustTesting *pc1, *pc2;
pc1 = new (buffer) JustTesting;
pc2 = new JustTesting("Heap1", 20);
cout << "Meory block addresses:\n"
<< "Buffer: "
<< (void *)buffer << " heap:" << pc2 << endl;
cout << "Memory contents:\n";
cout << pc1 << ": ";
pc1->Show();
cout << pc2 << ": ";
pc2->Show();
JustTesting *pc3, *pc4;
pc3 = new (buffer+sizeof(JustTesting)) JustTesting("Better Idea", 6);
pc4 = new JustTesting("Heap2", 10);
cout << "Memory contents:\n";
cout << pc3 << ": ";
pc3->Show();
cout << pc4 << ": ";
pc4->Show();
delete pc2;
delete pc4;
pc3->~JustTesting();
pc1->~JustTesting();
delete[] buffer;
cout << "Done\n";
return 0;
}
12.10
//12.10
#ifndef QUEUE_H_
#define QUEUE_H_
class Customer
{
private:
long arrive;
int processtime;
public:
Customer() { arrive = processtime = 0; }
void set(long when);
long when() const { return arrive; }
int ptime() const { return processtime;}
};
typedef Customer Item;
class Queue
{
private:
struct Node{
Item item;
struct Node *next;
};
enum
{
Q_SIZE = 10
};
Node *front;
Node *rear;
int items;
const int qsize;
Queue(const Queue &q):qsize(0){
}
Queue &operator=(const Queue &q) { return *this;}
public:
Queue(int qs = Q_SIZE);
~Queue();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const Item &item);
bool dequeue(Item &item);
};
#endif
12.11
//12.11
#include"queue.h"
#include
Queue::Queue(int qs):qsize(qs)
{
front = rear = NULL;
items = 0;
}
Queue::~Queue()
{
Node * temp;
while(front!=NULL)
{
temp = front;
front = front->next;
delete temp;
}
}
bool Queue::isempty() const
{
return items == 0;
}
bool Queue::isfull() const
{
return items == qsize;
}
int Queue::queuecount() const
{
return items;
}
bool Queue::enqueue(const Item &item)
{
if(isfull())
{
return false;
}
Node *add = new Node;
add->item = item;
add->next = NULL;
items++;
if(front==NULL)
front = add;
else
rear->next = add;
rear = add;
return true;
}
bool Queue::dequeue(Item &item)
{
if(front==NULL)
return false;
item = front->item;
items--;
Node *temp = front;
front = front->next;
delete temp;
if(items==0)
rear = NULL;
return true;
}
void Customer::set(long when)
{
processtime = std::rand() % 3 + 1;
arrive = when;
}
12.12
#include
#include
#include
#include"queue.cpp"
const int MIN_PER_HR = 60;
bool newcustomer(double x);
int main()
{
using std::cin;
using std::cout;
using std::endl;
using std::ios_base;
std::srand(std::time(0));
cout << "Case Study: Bank of Heather Automatic Teller\n";
cout << "Enter maximum size of queue: ";
int qs;
cin >> qs;
Queue line(qs);
cout << "Enter the number of simulation hours: ";
int hours;
cin >> hours;
long cyclelimit = MIN_PER_HR * hours;
cout << "Enter the average number of customers per hour: ";
double perhour;
cin >> perhour;
double min_per_cust;
min_per_cust = MIN_PER_HR;
Item temp;
long turnaways = 0;
long customers = 0;
long served = 0;
long sum_line = 0;
int wait_time = 0;
long line_wait = 0;
for (int cycle = 0; cycle < cyclelimit;cycle++)
{
if(newcustomer(min_per_cust))
{
if(line.isfull())
turnaways++;
else
{
customers++;
temp.set(cycle);
line.enqueue(temp);
}
}
if(wait_time<=0&&!line.isempty())
{
line.dequeue(temp);
wait_time = temp.ptime();
line_wait += cycle - temp.when();
served++;
}
if(wait_time>0)
wait_time--;
sum_line += line.queuecount();
}
if(customers>0)
{
cout << "customers acccepted: " << customers << endl;
cout << " customers served: " << served << endl;
cout << " turnaways: " << turnaways << endl;
cout << "average queue size: ";
cout.precision(2);
cout .setf(ios_base::fixed, ios_base::floatfield);
cout << (double)sum_line / cyclelimit << endl;
cout << " average wait time: "
<< (double)line_wait / served <<
" minutes\n";
}
else
cout << "No customers!\n";
cout << "Done!\n";
return 0;
}
bool newcustomer(double x)
{
return (std::rand() * x / RAND_MAX < 1);
}