forked from applenob/Cpp_Primer_Practice
-
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
15 changed files
with
1,353 additions
and
69 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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// Created by cer on 19-1-20. | ||
// | ||
|
||
#ifndef CPP_PRIMER_PRACTICE_CHAPTER6_H | ||
#define CPP_PRIMER_PRACTICE_CHAPTER6_H | ||
|
||
int fact(int val); | ||
int func(); | ||
|
||
template <typename T> | ||
T abs(T i) | ||
{ | ||
return i >= 0 ? i : -i; | ||
} | ||
|
||
#endif //CPP_PRIMER_PRACTICE_CHAPTER6_H |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// Created by cer on 17-9-19. | ||
// chapter 06 | ||
// 函数 | ||
|
||
#include <iostream> | ||
#include <string> | ||
|
||
using namespace std | ||
|
||
int fact(int i) | ||
{ | ||
return i > 1 ? i * fact(i - 1) : 1; | ||
} | ||
|
||
void interactive_fact() | ||
{ | ||
string const prompt = "Enter a number within [1, 13) :\n"; | ||
string const out_of_range = "Out of range, please try again.\n"; | ||
for (int i; cout << prompt, cin >> i; ) | ||
{ | ||
if (i < 1 || i > 12) | ||
{ | ||
cout << out_of_range; | ||
continue; | ||
} | ||
cout << fact(i) << endl; | ||
} | ||
} | ||
|
||
bool str_subrange(const string &str1, const string &str2){ | ||
if(str1.size()==str2.size()) | ||
return str1==str2; | ||
string::size_type size={min(str1.size(),str2.size())}; | ||
string::size_type i=0; | ||
while(i!=size){ | ||
if(str1[i]!=str2[i]) | ||
return ; //error! no return value! | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
// interactive_fact(); | ||
str_subrange(); | ||
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// Created by cer on 19-1-20. | ||
// | ||
|
||
#include "Chapter6.h" | ||
#include <iostream> | ||
|
||
int fact(int val) | ||
{ | ||
if (val == 0 || val == 1) return 1; | ||
else return val * fact(val-1); | ||
} | ||
|
||
int func() | ||
{ | ||
int n, ret = 1; | ||
std::cout << "input a number: "; | ||
std::cin >> n; | ||
while (n > 1) ret *= n--; | ||
return ret; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// Created by cer on 19-1-20. | ||
// | ||
|
||
|
||
#include "Chapter6.h" | ||
#include <iostream> | ||
|
||
int main() | ||
{ | ||
std::cout << "5! is " << fact(5) << std::endl; | ||
std::cout << func() << std::endl; | ||
std::cout << abs(-9.78) << std::endl; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.