Forráskód Böngészése

add chap2-4 - chap2-8

hanojiang 1 éve
szülő
commit
a5301ab8f6

+ 6 - 1
EssentialCpp/EssentialCpp.vcxproj

@@ -127,7 +127,12 @@
     </Link>
   </ItemDefinitionGroup>
   <ItemGroup>
-    <ClCompile Include="test_reference.cpp" />
+    <ClCompile Include="chap2_8.cpp" />
+    <ClCompile Include="utils.cpp" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="chap2_8.h" />
+    <ClInclude Include="utils.h" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">

+ 12 - 1
EssentialCpp/EssentialCpp.vcxproj.filters

@@ -15,8 +15,19 @@
     </Filter>
   </ItemGroup>
   <ItemGroup>
-    <ClCompile Include="test_reference.cpp">
+    <ClCompile Include="utils.cpp">
       <Filter>源文件</Filter>
     </ClCompile>
+    <ClCompile Include="chap2_8.cpp">
+      <Filter>源文件</Filter>
+    </ClCompile>
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="utils.h">
+      <Filter>头文件</Filter>
+    </ClInclude>
+    <ClInclude Include="chap2_8.h">
+      <Filter>源文件</Filter>
+    </ClInclude>
   </ItemGroup>
 </Project>

+ 42 - 0
EssentialCpp/chap2_4.cpp

@@ -0,0 +1,42 @@
+#include "utils.h"
+#include <limits>
+vector<u32>* fibon_seq(int size) {
+	static vector<u32> elems;
+
+	if (size <= 0 || size > 1024) {
+		cout << "size is invalid!" << endl;
+		return 0;
+	}
+		
+
+	int current_size = elems.size();
+
+	if (size > current_size) {
+		for (int ix = current_size; ix < size; ++ix) {
+			if (ix < 2) {
+				elems.push_back(1);
+			}
+			else {
+				int value_ix = elems[ix - 1] + elems[ix - 2];
+				elems.push_back(value_ix);
+			}
+			
+		}
+	}
+
+	return &elems;
+
+}
+
+int main() {
+	vector<u32>* v = fibon_seq(100);
+	if (v) {
+		display(*v);
+	}
+	
+
+	int max_int = numeric_limits<long>::max();
+	cout << max_int << endl;
+
+	return 0;
+}

+ 64 - 0
EssentialCpp/chap2_5.cpp

@@ -0,0 +1,64 @@
+#include "utils.h"
+#include <limits>
+
+bool is_size_ok(int size) {
+	if (size <= 0 || size > 1024) {
+		cout << "size is invalid!" << endl;
+		return false;
+	}
+	else {
+		return true;
+	}
+}
+
+vector<u32>* fibon_seq(int size) {
+	static vector<u32> elems;
+
+	if (!is_size_ok(size)) {
+		return 0;
+	}
+
+
+	int current_size = elems.size();
+
+	if (size > current_size) {
+		for (int ix = current_size; ix < size; ++ix) {
+			if (ix < 2) {
+				elems.push_back(1);
+			}
+			else {
+				int value_ix = elems[ix - 1] + elems[ix - 2];
+				elems.push_back(value_ix);
+			}
+
+		}
+	}
+
+	return &elems;
+
+}
+
+bool fibon_elem(int position, u32& elem) {
+
+	vector<u32>* fibon_vector = fibon_seq(position);
+	if (fibon_vector) {
+		elem = (*fibon_vector)[position-1]; //index overflow
+		return true;
+	}
+	else {
+		elem = 0;
+		return false;
+	}
+
+}
+
+
+int main() {
+	u32 elem = 0;
+	bool ret = fibon_elem(100, elem);
+	if (ret) {
+		cout << "elem is" << elem << endl;
+	}
+
+	return 0;
+}

+ 72 - 0
EssentialCpp/chap2_6.cpp

@@ -0,0 +1,72 @@
+#include "utils.h"
+#include <limits>
+
+bool is_size_ok(int size) {
+	if (size <= 0 || size > 1024) {
+		display_message("size is invalid");
+		return false;
+	}
+	else {
+		return true;
+	}
+}
+
+vector<u32>* fibon_seq(int size) {
+	static vector<u32> elems;
+
+	if (!is_size_ok(size)) {
+		return 0;
+	}
+
+
+	int current_size = elems.size();
+
+	if (size > current_size) {
+		for (int ix = current_size; ix < size; ++ix) {
+			if (ix < 2) {
+				elems.push_back(1);
+			}
+			else {
+				int value_ix = elems[ix - 1] + elems[ix - 2];
+				elems.push_back(value_ix);
+			}
+
+		}
+	}
+
+	return &elems;
+
+}
+
+bool fibon_elem(int position, u32& elem) {
+
+	vector<u32>* fibon_vector = fibon_seq(position);
+	if (fibon_vector) {
+		elem = (*fibon_vector)[position - 1]; //index overflow
+		return true;
+	}
+	else {
+		elem = 0;
+		return false;
+	}
+
+}
+
+
+int main() {
+	u32 elem = 0;
+	bool ret = fibon_elem(0, elem);
+	if (ret) {
+		cout << "elem is" << elem << endl;
+	}
+	display_message('s');
+	display_message("test string message", 1);
+	display_message("test string message", 1, 2);
+
+	string s1("test string message 1");
+	vector<u32>* v1 = fibon_seq(10);
+	display(*v1);
+
+	display_message(s1, *v1);
+	return 0;
+}

+ 107 - 0
EssentialCpp/chap2_8.cpp

@@ -0,0 +1,107 @@
+#include "utils.h"
+#include <limits>
+#include "chap2_8.h"
+
+typedef vector<u32>* (*seq_ptr)(int) ;
+
+seq_ptr seq_array[seq_cnt] = { fibon_seq };
+
+inline bool is_size_ok(int size) {
+	if (size <= 0 || size > 1024) {
+		display_message("size is invalid");
+		return false;
+	}
+	else {
+		return true;
+	}
+}
+
+vector<u32>* fibon_seq(int size) {
+	static vector<u32> elems;
+
+	if (!is_size_ok(size)) {
+		return 0;
+	}
+
+
+	int current_size = elems.size();
+
+	if (size > current_size) {
+		for (int ix = current_size; ix < size; ++ix) {
+			if (ix < 2) {
+				elems.push_back(1);
+			}
+			else {
+				int value_ix = elems[ix - 1] + elems[ix - 2];
+				elems.push_back(value_ix);
+			}
+
+		}
+	}
+
+	return &elems;
+
+}
+/*
+
+*/
+bool fibon_elem(int position, u32& elem) {
+
+	vector<u32>* fibon_vector = fibon_seq(position);
+	if (fibon_vector) {
+		elem = (*fibon_vector)[position - 1]; //index overflow
+		return true;
+	}
+	else {
+		elem = 0;
+		return false;
+	}
+
+}
+
+bool seq_elem(int position, u32& elem,
+				seq_ptr seq_func) {
+
+	if (!seq_func) {
+		display_message("invalid parameter seq_func=NULL_PTR!");
+		return false;
+	}
+
+	vector<u32>* vec = seq_func(position);
+	if (vec) {
+		elem = (*vec)[position - 1]; //index overflow
+		return true;
+	}
+	else {
+		elem = 0;
+		return false;
+	}
+
+}
+
+
+
+int main() {
+	u32 elem = 0;
+	bool ret = fibon_elem(0, elem);
+	if (ret) {
+		cout << "elem is" << elem << endl;
+	}
+	display_message('s');
+	display_message("test string message", 1);
+	display_message("test string message", 1, 2);
+
+	string s1("test string message 1");
+	vector<u32>* v1 = fibon_seq(10);
+	display(*v1);
+
+	display_message(s1, *v1);
+
+	ret = seq_elem(100, elem, fibon_seq);
+
+	if (ret) {
+		cout << "elem is " << elem << endl;
+	}
+
+	return 0;
+}

+ 5 - 0
EssentialCpp/chap2_8.h

@@ -0,0 +1,5 @@
+#pragma once
+#include "utils.h"
+const int seq_cnt = 1;
+
+vector<u32>* fibon_seq(int size);

+ 32 - 0
EssentialCpp/utils.cpp

@@ -0,0 +1,32 @@
+
+#include "utils.h"
+
+
+void display(const vector<u32>& v, ostream& os) {
+	for (vector<u32>::const_iterator iter = v.begin(); iter != v.end(); ++iter) {
+		os << *iter << "\n";
+	}
+	os << endl;
+}
+
+void display_message(char ch)
+{
+	cout << ch << endl;
+}
+
+void display_message(const string& s)
+{
+	cout << s << endl;
+}
+
+void display_message(const string& s, int v1)
+{
+	cout << s << ", num v1 = " << v1 << endl;
+}
+
+void display_message(const string&s, int v1, int v2)
+{
+	cout << s << ", num v1 = " << v1 << ", num v2 = " << v2 << endl;
+}
+
+

+ 32 - 0
EssentialCpp/utils.h

@@ -0,0 +1,32 @@
+#pragma once
+#include <iostream>
+#include <vector>
+using namespace std;
+typedef unsigned int u32;
+
+void display(const vector<u32>& v, ostream& os = cout);
+
+void display_message(char ch);
+void display_message(const string&);
+void display_message(const string&, int);
+void display_message(const string&, int, int);
+
+
+// 模板声明与定义不能分开
+
+template <typename T>
+void display_message(const string& s, const vector<T>& vec);
+
+template <typename T>
+void display_message(const string& s, const vector<T>& vec)
+{
+	
+		cout << s << endl;
+	
+		for (int i = 0; i < vec.size(); ++i) {
+			T elem = vec[i];
+			cout << elem << ' ';
+		}
+		cout << endl;
+}
+