diff --git a/.vscode/settings.json b/.vscode/settings.json index 230b7cf9..73603a7f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { + "editor.formatOnSave": true, "files.associations": { "vector": "cpp", "iosfwd": "cpp", diff --git a/ch06/README.md b/ch06/README.md index 5ad4727c..2919c007 100644 --- a/ch06/README.md +++ b/ch06/README.md @@ -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 diff --git a/ch06/ex6_04.cpp b/ch06/ex6_04.cpp index 0582ff6f..36c5066e 100644 --- a/ch06/ex6_04.cpp +++ b/ch06/ex6_04.cpp @@ -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 -================================================================================= -*/ +// About the magic number(13): https://github.com/Mooophy/Cpp-Primer/pull/172 #include #include +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(); } \ No newline at end of file diff --git a/ch06/ex6_10.cpp b/ch06/ex6_10.cpp index 251fa407..4a92680d 100644 --- a/ch06/ex6_10.cpp +++ b/ch06/ex6_10.cpp @@ -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 -#include #include +#include -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; } diff --git a/ch06/ex6_11.cpp b/ch06/ex6_11.cpp index 0e20bd9d..2694d0fa 100644 --- a/ch06/ex6_11.cpp +++ b/ch06/ex6_11.cpp @@ -10,5 +10,4 @@ int main() int i = 42; reset(i); std::cout << i << std::endl; - return 0; } diff --git a/ch06/ex6_12.cpp b/ch06/ex6_12.cpp index d442f86f..1484ba2e 100644 --- a/ch06/ex6_12.cpp +++ b/ch06/ex6_12.cpp @@ -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 #include +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; } diff --git a/ch06/ex6_21.cpp b/ch06/ex6_21.cpp index 5be53b5e..883d98fb 100644 --- a/ch06/ex6_21.cpp +++ b/ch06/ex6_21.cpp @@ -1,15 +1,10 @@ -//! @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 -using std::cout; -int LargerOne(const int i, const int* ip) +int LargerOne(int i, const int* const ip) { return (i > *ip) ? i : *ip; } @@ -17,7 +12,5 @@ int LargerOne(const int i, const int* ip) int main() { int c = 6; - cout << LargerOne(7, &c); - - return 0; + std::cout << LargerOne(7, &c) << std::endl; } diff --git a/ch06/ex6_22.cpp b/ch06/ex6_22.cpp index 91bfdd9e..9779c9df 100644 --- a/ch06/ex6_22.cpp +++ b/ch06/ex6_22.cpp @@ -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 -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; } diff --git a/ch06/ex6_23.cpp b/ch06/ex6_23.cpp index bf2c6b83..04205616 100644 --- a/ch06/ex6_23.cpp +++ b/ch06/ex6_23.cpp @@ -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 -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; } @@ -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() diff --git a/ch06/ex6_25_26.cpp b/ch06/ex6_25_26.cpp index 77917f27..dc8ae6eb 100644 --- a/ch06/ex6_25_26.cpp +++ b/ch06/ex6_25_26.cpp @@ -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 #include @@ -19,5 +16,4 @@ int main(int argc, char** argv) } std::cout << str << std::endl; - return 0; } diff --git a/ch06/ex6_27.cpp b/ch06/ex6_27.cpp index 4823eb6a..7041a9b0 100644 --- a/ch06/ex6_27.cpp +++ b/ch06/ex6_27.cpp @@ -1,7 +1,7 @@ -#include #include +#include -int sum(std::initializer_list il) +int sum(const std::initializer_list& il) { int sum = 0; for (auto i : il) sum += i; @@ -11,5 +11,4 @@ int sum(std::initializer_list il) int main(void) { std::cout << sum({1, 2, 3, 4, 5}) << std::endl; - return 0; } diff --git a/ch06/ex6_33.cpp b/ch06/ex6_33.cpp index 60b4a8c7..230d854a 100644 --- a/ch06/ex6_33.cpp +++ b/ch06/ex6_33.cpp @@ -1,8 +1,10 @@ +// Write a recursive function to print the contents of a vector + #include #include -using std::vector; using std::cout; +using std::vector; using Iter = vector::iterator; void print(Iter beg, Iter end) @@ -17,5 +19,4 @@ int main() { vector vec{1, 2, 3, 4, 5, 6, 7, 8, 9}; print(vec.begin(), vec.end()); - return 0; } diff --git a/ch06/ex6_42.cpp b/ch06/ex6_42.cpp index 50f8c6ba..bbd917f0 100644 --- a/ch06/ex6_42.cpp +++ b/ch06/ex6_42.cpp @@ -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 #include -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") { @@ -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; } diff --git a/ch06/ex6_44.cpp b/ch06/ex6_44.cpp index 8dfec775..06e21c17 100644 --- a/ch06/ex6_44.cpp +++ b/ch06/ex6_44.cpp @@ -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 #include diff --git a/ch06/ex6_47.cpp b/ch06/ex6_47.cpp index 97f25a21..e7627c58 100644 --- a/ch06/ex6_47.cpp +++ b/ch06/ex6_47.cpp @@ -1,16 +1,14 @@ -// -// main.cpp -// Test -// -// Created by pezy on 14/10/30. -// Copyright (c) 2014 pezy. All rights reserved. -// +// Revise the program you wrote in the exercises in 6.3.2 (p. +// 228) that used recursion to print the contents of a vector to conditionally +// print information about its execution. For example, you might print the size +// of the vector on each call. Compile and run the program with debugging turned +// on and again with it turned off. #include #include -using std::vector; using std::cout; using std::endl; +using std::vector; //#define NDEBUG @@ -32,6 +30,4 @@ int main() vector vec{1, 2, 3, 4, 5, 6, 7, 8, 9}; printVec(vec); cout << endl; - - return 0; } diff --git a/ch06/ex6_51.cpp b/ch06/ex6_51.cpp index f238c6c1..efb14da1 100644 --- a/ch06/ex6_51.cpp +++ b/ch06/ex6_51.cpp @@ -1,3 +1,8 @@ +// Write all four versions of f. Each function should print a +// distinguishing message. Check your answers for the previous exercise. If your +// answers were incorrect, study this section until you understand why your +// answers were wrong. + #include using std::cout; using std::endl; @@ -24,10 +29,8 @@ void f(double, double) int main() { - f(2.56, 42); // error: 'f' is ambiguous. + // f(2.56, 42); // error: 'f' is ambiguous. f(42); f(42, 0); f(2.56, 3.14); - - return 0; } diff --git a/ch06/ex6_54_55_56.cpp b/ch06/ex6_54_55_56.cpp index 0cc022e2..39293e17 100644 --- a/ch06/ex6_54_55_56.cpp +++ b/ch06/ex6_54_55_56.cpp @@ -1,5 +1,3 @@ -//! @Yue Wang -//! //! Exercise 6.54: //! Write a declaration for a function that takes two int //! parameters and returns an int, and declare a vector whose @@ -17,8 +15,8 @@ #include #include #include -using std::vector; using std::cout; +using std::vector; //! //! @brief Exercise 6.54