forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
104 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"editor.formatOnSave": true, | ||
"files.associations": { | ||
"vector": "cpp", | ||
"iosfwd": "cpp", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,5 +10,4 @@ int main() | |
int i = 42; | ||
reset(i); | ||
std::cout << i << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.