Skip to content

Commit

Permalink
improving ch6
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 26, 2017
1 parent df4b14e commit 4c5ce5c
Show file tree
Hide file tree
Showing 17 changed files with 104 additions and 140 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"editor.formatOnSave": true,
"files.associations": {
"vector": "cpp",
"iosfwd": "cpp",
Expand Down
2 changes: 1 addition & 1 deletion ch06/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ Error (Clang):
## Exercise 6.31

when you can find the preexited object that the reference refered.
when you can find the preexisted object that the reference refered.

## Exercise 6.32

Expand Down
39 changes: 17 additions & 22 deletions ch06/ex6_04.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
/*
=================================================================================
C++ Primer 5th Exercise Answer Source Code
Copyright (C) 2014-2015 github.com/pezy/CppPrimer
// Write a function that interacts with the user, asking for a number and
// generating the factorial of that number. Call this function from main.

Write a function that interacts with the user, asking for a number and
generating the factorial of that number. Call this function from main.
About the magic number(13): https://github.com/Mooophy/Cpp-Primer/pull/172
If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/
// About the magic number(13): https://github.com/Mooophy/Cpp-Primer/pull/172

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

int fact(int val)
{
int ret = 1;
while (val > 1)
ret *= val--;
return ret;
int ret = 1;
while (val > 1) ret *= val--;
return ret;
}

void factorial_with_interacts() {
for (int val = 0; std::cout << "Enter a number within [0, 13): ", std::cin >> val; ) {
if (val < 0 || val > 12) continue;
std::cout << val << "! =" << fact(val) << std::endl;
}
void factorial_with_interacts()
{
for (int val = 0; cout << "Enter a number within [0, 13): ", cin >> val;) {
if (val < 0 || val > 12) continue;
cout << val << "! =" << fact(val) << endl;
}
}

int main()
{
factorial_with_interacts();
factorial_with_interacts();
}
29 changes: 13 additions & 16 deletions ch06/ex6_10.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
//!@Yue wang
//!
//! Exercise 6.10:
//! Using pointers, write a function to swap the values of two ints.
//! Test the function by calling it and printing the swapped values.
//!
// Using pointers, write a function to swap the values of two ints.
// Test the function by calling it and printing the swapped values.

#include <iostream>
#include <string>
#include <stdexcept>
#include <string>

void swap(int* lhs, int* rhs)
using std::cin;
using std::cout;
using std::endl;

void swap(int* const lhs, int* const rhs)
{
int tmp;
tmp = *lhs;
auto tmp = *lhs;
*lhs = *rhs;
*rhs = tmp;
}

int main()
{
for (int lft, rht;
std::cout << "Please Enter:\n", std::cin >> lft >> rht;) {
swap(&lft, &rht);
std::cout << lft << " " << rht << std::endl;
for (int lht, rht; cout << "Please Enter:\n", cin >> lht >> rht;) {
swap(&lht, &rht);
cout << lht << " " << rht << endl;
}

return 0;
}
1 change: 0 additions & 1 deletion ch06/ex6_11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ int main()
int i = 42;
reset(i);
std::cout << i << std::endl;
return 0;
}
28 changes: 13 additions & 15 deletions ch06/ex6_12.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
//!@Yue Wang
//!
//! Exercise 6.12:
//! Rewrite the program from exercise 6.10 in § 6.2.1 (p. 210) to use
//! references instead of pointers to swap the value of two ints. Which
//! version do you think would be easier to use and why?
// The version using reference is easier.
//!
// Rewrite the program from exercise 6.10 in 6.2.1 (p. 210) to use
// references instead of pointers to swap the value of two ints. Which
// version do you think would be easier to use and why?
// The version using reference is easier.

#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;

void swap(int& lhs, int& rhs)
{
int temp = lhs;
auto tmp = lhs;
lhs = rhs;
rhs = temp;
rhs = tmp;
}

int main()
{
for (int left, right;
std::cout << "Please Enter:\n", std::cin >> left >> right;) {
for (int left, right; cout << "Please Enter:\n", cin >> left >> right;) {
swap(left, right);
std::cout << left << " " << right << std::endl;
cout << left << " " << right << endl;
}

return 0;
}
17 changes: 5 additions & 12 deletions ch06/ex6_21.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
//! @Alan
//!
//! Exercise 6.21:
//! Write a function that takes an int and a pointer to an int and
//! returns the larger of the int value or the value to which the
//! pointer points. What type should you use for the pointer?
//!
// Write a function that takes an int and a pointer to an int and
// returns the larger of the int value or the value to which the
// pointer points. What type should you use for the pointer?

#include <iostream>
using std::cout;

int LargerOne(const int i, const int* ip)
int LargerOne(int i, const int* const ip)
{
return (i > *ip) ? i : *ip;
}

int main()
{
int c = 6;
cout << LargerOne(7, &c);

return 0;
std::cout << LargerOne(7, &c) << std::endl;
}
27 changes: 11 additions & 16 deletions ch06/ex6_22.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
//! @Yue Wang
//!
//! Exercise 6.22:
//! Write a function to swap two int pointers.
//!
// Write a function to swap two int pointers.

#include <iostream>

void swap(int*& lft, int*& rht)
void swap(const int*& lhs, const int*& rhs)
{
auto tmp = lft;
lft = rht;
rht = tmp;
auto temp = lhs;
lhs = rhs;
rhs = temp;
}

int main()
{
int i = 42, j = 99;
auto lft = &i;
auto rht = &j;
swap(lft, rht);
std::cout << *lft << " " << *rht << std::endl;

return 0;
const int i = 42, j = 99;
auto lhs = &i;
auto rhs = &j;
swap(lhs, rhs);
std::cout << *lhs << " " << *rhs << std::endl;
}
24 changes: 15 additions & 9 deletions ch06/ex6_23.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// Write your own versions of each of the print functions
// presented in this section. Call each of these functions to print i and j
// defined as follows:
// int i = 0, j[2] = {0, 1};

#include <iostream>

using std::cout;
using std::endl;
using std::begin;
using std::cout;
using std::end;
using std::endl;

void print(int* pi)
void print(int* const pi)
{
if (pi) cout << *pi << endl;
}
Expand All @@ -19,19 +24,20 @@ void print(const char* p)

void print(const int* beg, const int* end)
{
while (beg != end) cout << *beg++ << endl;
while (beg != end) cout << *beg++ << " ";
cout << endl;
}

void print(const int ia[], size_t size)
{
for (size_t i = 0; i != size; ++i) {
cout << ia[i] << endl;
}
for (size_t i = 0; i != size; ++i) cout << ia[i] << " ";
cout << endl;
}

void print(const int(&arr)[2])
void print(const int (&arr)[2])
{
for (auto i : arr) cout << i << endl;
for (auto i : arr) cout << i << " ";
cout << endl;
}

int main()
Expand Down
14 changes: 5 additions & 9 deletions ch06/ex6_25_26.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//! @Yue Wang
//!
//! Exercise 6.25: Write a main function that takes two arguments.
//! Concatenate the supplied arguments and print the resulting string.
//!
//! Exercise 6.26: Write a program that accepts the options presented
//! in this section. Print the values of the arguments passed to main.
//!
// Write a main function that takes two arguments.
// Concatenate the supplied arguments and print the resulting string.

// Write a program that accepts the options presented
// in this section. Print the values of the arguments passed to main.

#include <iostream>
#include <string>
Expand All @@ -19,5 +16,4 @@ int main(int argc, char** argv)
}

std::cout << str << std::endl;
return 0;
}
5 changes: 2 additions & 3 deletions ch06/ex6_27.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <iostream>
#include <initializer_list>
#include <iostream>

int sum(std::initializer_list<int> il)
int sum(const std::initializer_list<int>& il)
{
int sum = 0;
for (auto i : il) sum += i;
Expand All @@ -11,5 +11,4 @@ int sum(std::initializer_list<int> il)
int main(void)
{
std::cout << sum({1, 2, 3, 4, 5}) << std::endl;
return 0;
}
5 changes: 3 additions & 2 deletions ch06/ex6_33.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Write a recursive function to print the contents of a vector

#include <iostream>
#include <vector>

using std::vector;
using std::cout;
using std::vector;
using Iter = vector<int>::iterator;

void print(Iter beg, Iter end)
Expand All @@ -17,5 +19,4 @@ int main()
{
vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9};
print(vec.begin(), vec.end());
return 0;
}
17 changes: 4 additions & 13 deletions ch06/ex6_42.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
//! @creator by Wang Yue
//! @refactor by pezy
//!
//! @date 27, July. 2015
//!
//! @question
//! Give the second parameter of make_plural (§ 6.3.2, p. 224) a default
//! argument of 's'. Test your program by printing singular and plural versions
//! of the words success and failure.
//!
// Give the second parameter of make_plural (6.3.2, p. 224) a default
// argument of 's'. Test your program by printing singular and plural versions
// of the words success and failure.

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;
using std::string;

string make_plural(size_t ctr, const string& word, const string& ending = "s")
{
Expand All @@ -27,6 +20,4 @@ int main()
<< make_plural(1, "failure") << endl;
cout << "plural : " << make_plural(2, "success", "es") << " "
<< make_plural(2, "failure") << endl;

return 0;
}
6 changes: 1 addition & 5 deletions ch06/ex6_44.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
//! @Alan
//!
//! Exercise 6.44: Rewrite the isShorter function from § 6.2.2 (p. 211) to be
//! inline.
//!
// Rewrite the isShorter function from 6.2.2 (p. 211) to be inline.

#include <iostream>
#include <string>
Expand Down
Loading

0 comments on commit 4c5ce5c

Please sign in to comment.