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
1 changed file
with
18 additions
and
21 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,39 +1,36 @@ | ||
/* | ||
================================================================================= | ||
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. | ||
If you have questions, try to connect with me: pezy<urbancpz@gmail.com> | ||
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]> | ||
================================================================================= | ||
*/ | ||
|
||
#include <iostream> | ||
#include <string> | ||
|
||
void factorial_with_interacts() | ||
int fact(int val) | ||
{ | ||
int num; | ||
std::cout << "Please input a positive number: "; | ||
while (std::cin >> num && num < 0) | ||
std::cout << "Please input a positive number again: "; | ||
std::cout << num; | ||
|
||
unsigned long long result = 1; | ||
while (num > 1) result *= num--; | ||
int ret = 1; | ||
while (val > 1) | ||
ret *= val--; | ||
return ret; | ||
} | ||
|
||
std::cout << "! is "; | ||
if (result) | ||
std::cout << result << std::endl; | ||
else | ||
std::cout << "too big" << std::endl; | ||
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; | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
factorial_with_interacts(); | ||
} | ||
factorial_with_interacts(); | ||
} |