Skip to content

Commit

Permalink
提交第五章
Browse files Browse the repository at this point in the history
代码 + txt笔记
  • Loading branch information
AndyHsu-cn committed Apr 2, 2022
1 parent 3473878 commit 71a2e55
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 0 deletions.
76 changes: 76 additions & 0 deletions chapt05/5_01/LibMat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <iostream>
#include <string>
using namespace std;

class LibMat
{
public:
LibMat()
{
cout << "LibMat::LibMat() default constructor was called" << endl;
}

virtual ~LibMat()
{
cout << "LibMat::~LibMat() default destructor was called" << endl;
}
virtual void print() const
{
cout << "LibMat::print() -- I am a LibMat object" << endl;
}
};

class Book : public LibMat
{
public:
Book(const string &title, const string &author) : _title(title), _author(author)
{
cout << "Book::Book( " << _title << ", " << _author << " ) constructor was called" << endl;
}

virtual ~Book()
{
cout << "Book::~Book() was called" << endl;
}

virtual void print() const
{
cout << "Book::print() -- I am a Book object" << endl;
cout << "My title is: " << _title << endl;
cout << "My author is: " << _author << endl;
}

const string & title() const { return _title;}
const string & author() const { return _author;}

protected:
string _title;
string _author;
};

class AudioBook : public Book
{
public:
AudioBook(const string &title, const string &author, const string &narrator) : Book(title, author), _narrator(narrator)
{
cout << "AudioBook::AudioBook(" << _title << ", " << _author << ", " << _narrator
<< " ) constructor was called" << endl;
}

~AudioBook()
{
cout << "AudioBook::~AudioBook() destructor was called" << endl;
}

virtual void print() const
{
cout << "AudiBook::print() -- I am a AudiBook object" << endl;
cout << "My title is: " << _title << endl;
cout << "My author is: " << _author << endl;
cout << "My narrator is: " << _narrator << endl;
}

const string & narrator() const { return _narrator;}
protected:
string _narrator;
};
27 changes: 27 additions & 0 deletions chapt05/5_01/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "LibMat.hpp"

void print(const LibMat & mat)
{
cout << "In global print(): about to print mat.print()" << endl;
mat.print();
}

int main()
{
// cout << "\nCreating a LibMat object to print()" << endl;
// LibMat libmat;
// print(libmat);

// cout << "\nCreating a Book object to print()" << endl;
// Book b("The Castle", "Franz Kafka");
// print(b);

// cout << "\nCreating a AudiBook object to print()" << endl;
// AudioBook ab("Man Without Qualities", "Robert Musil", "Keneth Meyer");
// print(ab);

LibMat * audio = new AudioBook("Man Without Qualities", "Robert Musil", "Keneth Meyer");
delete audio;

return 0;
}
67 changes: 67 additions & 0 deletions chapt05/5_02/fibonacci.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "num_sequence.hpp"

class Fibonacci : public num_sequence
{
public:
Fibonacci(int len = 1, int beg_pos = 1) : _length(len), _beg_pos(beg_pos) {}
virtual int elem(int pos) const;
virtual const char *what_am_i() const { return "Fibonacci"; }
virtual ostream &print(ostream &os = cout) const;
int length() const { return _length; }
int beg_pos() const { return _beg_pos; }

protected:
virtual void gen_elems(int pos) const;
int _length;
int _beg_pos;
static vector<int> _elems;
};

vector<int> Fibonacci::_elems;

int Fibonacci::elem(int pos) const
{
if (!check_integrity(pos, _elems.size()))
return 0;
if (pos > _elems.size())
Fibonacci::gen_elems(pos);
return _elems[pos - 1];
}

void Fibonacci::gen_elems(int pos) const
{
if (_elems.empty())
{
_elems.push_back(1);
_elems.push_back(1);
}
if (_elems.size() < pos) // 不用等号可以吗
{
int ix = _elems.size();
int n_2 = _elems[ix - 2];
int n_1 = _elems[ix - 1];
for (; ix < pos; ++ix)
{
int elem = n_1 + n_2;
_elems.push_back(elem);
cout << "gen_elmes: " << elem << endl;
n_2 = n_1;
n_1 = elem;
}
}
}

ostream & Fibonacci::print(ostream &os) const
{
int elem_pos = _beg_pos - 1;
int end_pos = elem_pos + _length;
// [elem_pos, end_pos)

if(end_pos > _elems.size())
Fibonacci::gen_elems(end_pos);

os << "( "<< _beg_pos << ", " << _length << " ) ";
while(elem_pos < end_pos)
os << _elems[elem_pos++] << ' ';
return os;
}
19 changes: 19 additions & 0 deletions chapt05/5_02/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "fibonacci.hpp"

ostream & operator<<(ostream &os, const num_sequence &ns)
{
return ns.print(os);
}
int main()
{
Fibonacci fib;

cout << fib << endl;

Fibonacci fib2(16);
cout << fib2 << endl;

Fibonacci fib3(8, 12);
cout << fib3 << endl;
return 0;
}
32 changes: 32 additions & 0 deletions chapt05/5_02/num_sequence.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <vector>
using namespace std;

class num_sequence
{
public:
virtual ~num_sequence() {}
virtual int elem(int pos) const = 0;
virtual const char * what_am_i() const = 0;
virtual ostream & print(ostream & os = cout) const = 0;
static int max_elems() { return _max_elems; };

protected:
virtual void gen_elems(int pos) const = 0;
bool check_integrity(int pos, int size) const;

const static int _max_elems = 1024;
};

bool num_sequence::check_integrity(int pos, int size) const
{
if(pos <= 0 || pos > _max_elems)
{
cerr << "!! invalid position: " << pos << " Cannot honor request\n";
return false;
}
if(pos > size)
gen_elems(pos);
return true;
}

39 changes: 39 additions & 0 deletions chapt05/5_03/fibonacci.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "num_sequence.hpp"

class Fibonacci : public num_sequence
{
public:
Fibonacci(int len = 1, int beg_pos = 1) : num_sequence(len, beg_pos, _elems)
{
}
virtual const char *what_am_i() const { return "Fibonacci"; }

protected:
virtual void gen_elems(int pos) const;
static vector<int> _elems;
};

vector<int> Fibonacci::_elems;

void Fibonacci::gen_elems(int pos) const
{
if (_elems.empty())
{
_elems.push_back(1);
_elems.push_back(1);
}
if (_elems.size() < pos) // 不用等号可以吗
{
int ix = _elems.size();
int n_2 = _elems[ix - 2];
int n_1 = _elems[ix - 1];
for (; ix < pos; ++ix)
{
int elem = n_1 + n_2;
_elems.push_back(elem);
cout << "gen_elmes: " << elem << endl;
n_2 = n_1;
n_1 = elem;
}
}
}
19 changes: 19 additions & 0 deletions chapt05/5_03/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "fibonacci.hpp"

ostream & operator<<(ostream &os, const num_sequence &ns)
{
return ns.print(os);
}
int main()
{
Fibonacci fib;
cout << fib << endl;

Fibonacci fib2(16);
cout << fib2 << endl;

num_sequence * pns = new Fibonacci(8, 12);
cout << *pns << endl;

return 0;
}
61 changes: 61 additions & 0 deletions chapt05/5_03/num_sequence.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <iostream>
#include <vector>
using namespace std;

class num_sequence
{
public:
virtual ~num_sequence() { }
int elem(int pos) const;
virtual const char *what_am_i() const = 0;
ostream &print(ostream &os = cout) const;
static int max_elems() { return _max_elems; };
int length() const { return _length; }
int beg_pos() const { return _beg_pos; }

protected:
num_sequence(int len, int bp, vector<int> &re) : _length(len), _beg_pos(bp), _relems(re)
{
}
virtual void gen_elems(int pos) const = 0;
bool check_integrity(int pos, int size) const;
int _length;
int _beg_pos;
const static int _max_elems = 1024;
vector<int> &_relems;
};

bool num_sequence::check_integrity(int pos, int size) const
{
if (pos <= 0 || pos > _max_elems)
{
cerr << "!! invalid position: " << pos << " Cannot honor request\n";
return false;
}
if (pos > size)
gen_elems(pos);
return true;
}

int num_sequence::elem(int pos) const
{
if (!check_integrity(pos, _relems.size()))
return 0;
if (pos > _relems.size())
gen_elems(pos);
return _relems[pos - 1];
}

ostream &num_sequence::print(ostream &os) const
{
int elem_pos = _beg_pos - 1;
int end_pos = elem_pos + _length;

if (end_pos > _relems.size())
gen_elems(end_pos);

os << "( " << _beg_pos << ", " << _length << " ) ";
while (elem_pos < end_pos)
os << _relems[elem_pos++] << ' ';
return os;
}
10 changes: 10 additions & 0 deletions chapt05/5_04/fibonacci.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "num_sequence.hpp"

class Fibonacci : public num_sequence
{
public:
virtual const char *what_am_i()
{
return "Fibonacci";
}
};
26 changes: 26 additions & 0 deletions chapt05/5_04/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "fibonacci.hpp"
#include <typeinfo>

int main()
{
Fibonacci fib;
Fibonacci fib2;
num_sequence ns;

num_sequence * pns = &fib;

cout << fib.what_am_i() << endl;
cout << pns->what_am_i() << endl;
cout << typeid(fib).name() << endl;
cout << typeid(fib2). name() << endl;
cout << (typeid(fib)==typeid(fib2)) << endl;

cout << typeid(ns).name() << endl;
cout << (typeid(fib)==typeid(ns)) << endl;

cout << typeid(*pns).name() << endl;

return 0;
}

// -Wall == warning all
Loading

0 comments on commit 71a2e55

Please sign in to comment.