Skip to content

Commit

Permalink
add ch06
Browse files Browse the repository at this point in the history
  • Loading branch information
applenob committed Jan 20, 2019
1 parent b439645 commit 43840f2
Show file tree
Hide file tree
Showing 15 changed files with 1,353 additions and 69 deletions.
10 changes: 6 additions & 4 deletions cpp_source/ch6.cpp → cpp_source/ch05/ch5.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//
// Created by cer on 17-9-19.
// chapter 06
// Created by cer on 17-9-18.
// chapter 5
// 语句
#include <iostream>
#include <stdexcept>

#include <iostream>
#include <vector>
using namespace std;

int divide(int a, int b){
Expand All @@ -27,3 +27,5 @@ int main(){
}
return 0;
}


17 changes: 17 additions & 0 deletions cpp_source/ch06/Chapter6.h
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
47 changes: 47 additions & 0 deletions cpp_source/ch06/ch6.cpp
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;
}
21 changes: 21 additions & 0 deletions cpp_source/ch06/fact.cpp
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;
}
14 changes: 14 additions & 0 deletions cpp_source/ch06/factMain.cpp
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;
}
22 changes: 0 additions & 22 deletions cpp_source/ch5.cpp

This file was deleted.

Loading

0 comments on commit 43840f2

Please sign in to comment.