Skip to content

Commit

Permalink
merge Mooophy
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Sep 25, 2014
2 parents 054a0a7 + b7f30c5 commit a13a1b6
Show file tree
Hide file tree
Showing 20 changed files with 192 additions and 55 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#C++ Primer (第5版) 课后习题答案
#C++ Primer (第5版) 习题答案
##C++ Primer (5th Edition) exercise answers.

### Something I hope you know before go into the answers.
### Note

- Use `-std=c++11`(optional: `-pedantic -Wall`) flag when compiling.(or you can use Visual Studio 2012+)
- I don't know the standard answer, but I tried my best to keep the correctness, if you found any bug, please [tell me](https://github.com/Mooophy/Cpp-Primer/issues/new), thanks.
- If you found any bug, please [let me know](https://github.com/Mooophy/Cpp-Primer/issues/new), thanks.
- I have downloaded the headers from this book's [web site](http://www.informit.com/store/c-plus-plus-primer-9780321714114) and put them in the `include` folder.
- In order to test the program in an efficient way, I also put the test data file in the `data` folder.
- If you find any mistake of the questions. Please check the [Errata](http://ptgmedia.pearsoncmg.com/images/9780321714114/errata/9780321714114_errata_10-31-12.html) first.

### If you want to contribute this repository.
### How to contribute

- Please **fork**([How?](https://help.github.com/articles/fork-a-repo)) this repository.
- Please **fork**([How?](https://help.github.com/articles/fork-a-repo)) this repository first.
- **commit**([How?](https://help.github.com/articles/create-a-repo#commit-your-first-change)) in your own repository.
- Create a **pull request**([How?](https://help.github.com/articles/using-pull-requests)) for me.
- Give me a **pull request**([How?](https://help.github.com/articles/using-pull-requests)).

### Table of Contents

Expand All @@ -40,7 +39,7 @@
- Chapter 18. Tools for Large Programs
- Chapter 19. Specialized Tools and Techniques

### Read more?
### If you speak Chinese

1. [PO在StackOverflow上的习题](http://book.douban.com/review/6500246/)
2. [C++ Primer issues(豆瓣讨论组)](http://www.douban.com/group/532124/)
Expand Down
28 changes: 28 additions & 0 deletions ch05/ex5_10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>

using namespace std;

int main(void)
{
char c;
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
while (cin >> c)
{
if (c == 'a' || c == 'A')
++aCnt;
else if (c == 'e' || c == 'E')
++eCnt;
else if (c == 'i' || c == 'I')
++iCnt;
else if (c == 'o' || c == 'O')
++oCnt;
else if (c == 'u' || c == 'U')
++uCnt;
}
cout << "Num of vowel a : " << aCnt << endl;
cout << "Num of vowel e : " << eCnt << endl;
cout << "Num of vowel i : " << iCnt << endl;
cout << "Num of vowel o : " << oCnt << endl;
cout << "Num of vowel u : " << uCnt << endl;
return 0;
}
11 changes: 11 additions & 0 deletions ch05/ex5_23.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <iostream>
using namespace std;

int main(void)
{
int a, b;
cin >> a >> b;
cout << static_cast<double>(a) / b << endl;

return 0;
}
2 changes: 1 addition & 1 deletion ch05/ex5_23_24_25.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int main()

while(cin>>n1>>n2)
{
if(n2==1)
if(n2==0)
{
try
{
Expand Down
15 changes: 15 additions & 0 deletions ch05/ex5_24.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <iostream>
using namespace std;

int main(void)
{
int a, b;
cin >> a >> b;

if (b == 0)
throw runtime_error("divisor is 0");

cout << static_cast<double>(a) / b << endl;

return 0;
}
34 changes: 34 additions & 0 deletions ch05/ex5_25.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <stdexcept>

using namespace std;

int main(void)
{
int a, b;
cout << "Enter two numbers :" << endl;

while (cin >> a >> b)
{
try{
if (b == 0)
throw runtime_error("divisor is 0");
cout << static_cast<double>(a) / b << endl;
break;
}
catch (runtime_error err){
cout << err.what()
<< "\nTry again ? Enter y or n" << endl;
char c;
cin >> c;
if (!cin || c == 'n')
break;
else
{
cout << "Enter two numbers :" << endl;
}
}
}

return 0;
}
23 changes: 23 additions & 0 deletions ch06/ex6_27.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
#include <initializer_list>

using namespace std;

int add(initializer_list<const int> il);

int main(void)
{
int result = add({ 1, 2, 3, 4, 5 });

cout << result << endl;

return 0;
}

int add(initializer_list<const int> il)
{
int sum = 0;
for (auto a : il)
sum += a;
return sum;
}
1 change: 1 addition & 0 deletions ch13/ex13.43/strvec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <algorithm>
#include <iostream>

std::allocator<std::string> StrVec::alloc;

//! copy constructor
StrVec::StrVec(const StrVec &s)
Expand Down
2 changes: 1 addition & 1 deletion ch13/ex13.43/strvec.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class StrVec
std::string* first_free; // pointer to the first free element
std::string* cap; // pointer to one past the end

std::allocator<std::string> alloc;
static std::allocator<std::string> alloc;

//! utilities for Big 3/5
void reallocate();
Expand Down
74 changes: 44 additions & 30 deletions ch14/ex14.18.19/StrBlob.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ StrBlob::operator =(const StrBlob &sb)
*/
class StrBlobPtr
{
friend bool eq (const StrBlobPtr&, const StrBlobPtr&);
friend bool operator == (const StrBlobPtr&, const StrBlobPtr&);
friend bool operator==(const StrBlobPtr&, const StrBlobPtr&);
friend bool operator!=(const StrBlobPtr&, const StrBlobPtr&);
friend bool operator<(const StrBlobPtr&, const StrBlobPtr&);
friend bool operator>(const StrBlobPtr&, const StrBlobPtr&);
public:
StrBlobPtr(): curr(0) { }
StrBlobPtr(StrBlob &a, size_t sz = 0) : wptr(a.data), curr(sz) { }
Expand All @@ -177,7 +179,8 @@ class StrBlobPtr
//! check returns a shared_ptr to the vector if the check succeeds
std::shared_ptr<std::vector<std::string>>
check(std::size_t, const std::string&) const;

//! a valid iterator ?
bool valid()const;
//! store a weak_ptr, which means the underlying vector might be destroyed
std::weak_ptr<std::vector<std::string>> wptr;
std::size_t curr; // current position within the array
Expand All @@ -191,20 +194,40 @@ class StrBlobPtr
//! in eq(), if l and r both are null then it returns true wiout caomparing curr
//! in this operator, it returns true only when both pointer and curr are identical.
//!
inline bool
operator==(const StrBlobPtr& lhs, const StrBlobPtr& rhs)
inline
bool operator==(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
//! lock () returns shared_ptr when possible ,or nullptr otherwise.
auto l = lhs.wptr.lock();
auto r = rhs.wptr.lock();
//only valid StrBlobPtrs which point to the same element are compatible
if (lhs.valid() && rhs.valid() && lhs.wptr.lock() == rhs.wptr.lock())
return lhs.curr == rhs.curr;
else
throw std::logic_error("StrBlobPtr incompatible");
}

return (l == r) ? (lhs.curr == lhs.curr) : false;
inline
bool operator!=(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return !(lhs == rhs);
}

inline bool
operator !=(const StrBlobPtr& lhs, const StrBlobPtr& rhs)
inline
bool operator<(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return !(lhs == rhs);
//only valid StrBlobPtrs which point to the same element are compatible
if (lhs.valid() && rhs.valid() && lhs.wptr.lock() == rhs.wptr.lock())
return lhs.curr < rhs.curr;
else
throw std::logic_error("StrBlobPtr incompatible");
}

inline
bool operator>(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
//only valid StrBlobPtrs which point to the same element are compatible
if (lhs.valid() && rhs.valid() && lhs.wptr.lock() == rhs.wptr.lock())
return lhs.curr > rhs.curr;
else
throw std::logic_error("StrBlobPtr incompatible");
}


Expand All @@ -229,6 +252,15 @@ StrBlobPtr::check(std::size_t i, const std::string &msg) const
return ret; // otherwise, return a shared_ptr to the vector
}

inline
bool StrBlobPtr::valid()const
{
auto ret = wptr.lock();
if (ret&&curr <= ret->size())
return true;
return false;
}

//! prefix: return a reference to the incremented object
inline
StrBlobPtr& StrBlobPtr::incr()
Expand Down Expand Up @@ -264,23 +296,5 @@ StrBlob::end()
return ret;
}

//! named equality operators for StrBlobPtr
inline
bool eq(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
auto l = lhs.wptr.lock(), r = rhs.wptr.lock();
// if the underlying vector is the same
if (l == r)
// then they're equal if they're both null or
// if they point to the same element
return (!r || lhs.curr == rhs.curr);
else
return false; // if they point to difference vectors, they're not equal
}

inline
bool neq(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return !eq(lhs, rhs);
}
#endif
6 changes: 3 additions & 3 deletions ch15/ex15.11/bulk_quote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ double Bulk_quote::net_price(std::size_t n) const

void Bulk_quote::debug() const
{
std::cout << "data members of this class:\n"
<< "min_qty= " << this->min_qty << " "
<< "discount= " << this->discount<< " \n";
Quote::debug();
std::cout << "min_qty= " << this->min_qty << " "
<< "discount= " << this->discount<< " ";
}
2 changes: 1 addition & 1 deletion ch15/ex15.11/bulk_quote.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef BULK_QUOTE_H
#define BULK_QUOTE_H
#include <quote.h>
#include"quote.h"

class Bulk_quote : public Quote
{
Expand Down
6 changes: 3 additions & 3 deletions ch15/ex15.11/limit_quote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

void Limit_quote::debug() const
{
std::cout << "data members of this class:\n"
<< "max_qty= " << this->max_qty << " "
<< "discount= " << this->discount<< " \n";
Quote::debug();
std::cout << "max_qty= " << this->max_qty << " "
<< "discount= " << this->discount<< " ";
}
7 changes: 6 additions & 1 deletion ch15/ex15.11/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ int main()
*/
Quote& r = q;
r.debug();
std::cout << "\n";
r = bq;
r.debug();
std::cout << "\n";
r = lq;
r.debug();
std::cout << "\n";


std::cout << "====================\n";
Expand All @@ -54,9 +57,11 @@ int main()
*
*/
print_debug(q);
std::cout << "\n";
print_debug(lq);
std::cout << "\n";
print_debug(bq);

std::cout << "\n";

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion ch15/ex15.11/quote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ void Quote::debug() const
{
std::cout << "data members of this class:\n"
<< "bookNo= " <<this->bookNo << " "
<< "price= " <<this->price<< " \n";
<< "price= " <<this->price<< " ";
}
3 changes: 2 additions & 1 deletion ch15/ex15.15.16.17/bulk_quote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ double Bulk_quote::net_price(std::size_t n) const

void Bulk_quote::debug() const
{
Quote::debug();
std::cout //<< "data members of this class:\n"
<< "min_qty= " << quantity << " "
<< "discount= " << this->discount<< " \n";
<< "discount= " << discount<< " ";
}
5 changes: 3 additions & 2 deletions ch15/ex15.15.16.17/limit_quote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

void Limit_quote::debug() const
{
Quote::debug();
std::cout //<< "data members of this class:\n"
<< "max_qty= " << this->quantity << " "
<< "discount= " << this->discount<< " \n";
<< "max_qty= " << quantity << " "
<< "discount= " << discount<< " ";
}
5 changes: 5 additions & 0 deletions ch15/ex15.15.16.17/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@

int main()
{
/*
error C2259 : 'Disc_quote' : cannot instantiate abstract class
1> due to following members :
1> 'double Disc_quote::net_price(size_t) const' : is abstract
*/
Disc_quote d;

return 0;
Expand Down
2 changes: 1 addition & 1 deletion ch15/ex15.15.16.17/quote.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ void Quote::debug() const
{
std::cout //<< "data members of this class:\n"
<< "bookNo= " <<this->bookNo << " "
<< "price= " <<this->price<< " \n";
<< "price= " <<this->price<< " ";
}
Loading

0 comments on commit a13a1b6

Please sign in to comment.