Bladeren bron

add chap2.2 2.3

hanojiang 1 jaar geleden
bovenliggende
commit
977594a64a

+ 1 - 1
EssentialCpp/EssentialCpp.vcxproj

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

+ 1 - 1
EssentialCpp/EssentialCpp.vcxproj.filters

@@ -15,7 +15,7 @@
     </Filter>
   </ItemGroup>
   <ItemGroup>
-    <ClCompile Include="main.cpp">
+    <ClCompile Include="test_reference.cpp">
       <Filter>源文件</Filter>
     </ClCompile>
   </ItemGroup>

+ 6 - 6
EssentialCpp/main.cpp

@@ -3,14 +3,14 @@
 using namespace std;
 
 bool Fibon_Elem(int pos, int& elem) {
-	bool ret = false;
-	elem = 1;
-	int first = 1, second = 1;
-	
-	if (pos <= 0) {
-		return ret;
+
+	if (pos >= 1024 || pos <= 0) {
+		elem = 0;
+		return false;
 	}
 
+	elem = 1;
+	int first = 1, second = 1;
 	for (int ix = 3; ix <= pos; ++ix) {
 		elem = first + second;
 		first = second;

+ 4 - 0
EssentialCpp/test.txt

@@ -0,0 +1,4 @@
+1 0
+5 4
+4 3
+5 4

+ 55 - 0
EssentialCpp/test_reference.cpp

@@ -0,0 +1,55 @@
+#include <iostream>
+#include <vector>
+
+#include <fstream>
+
+using namespace std;
+void display(const vector<int>& v, ostream& os = cout);
+
+void display(const vector<int>& v, ostream& os) {
+	for (vector<int>::const_iterator iter = v.begin(); iter != v.end(); ++iter) {
+		os << *iter << " ";
+	}
+	os << endl;
+}
+
+void swap(int& value_a, int& value_b) {
+	int temp = value_a;
+	value_a = value_b;
+	value_b = temp;
+}
+
+void buble_sort(vector<int>& v, ostream* ofil=0) {
+	vector<int>::iterator ix, iy;
+
+	for (ix = v.begin(); ix != v.end(); ++ix) {
+		for (iy = ix + 1; iy != v.end(); ++iy) {
+			if (*ix > *iy) {
+				if (ofil) {//need include #include <fstream>
+					(*ofil) << *ix << " " << *iy << endl;
+					
+				}
+				swap(*ix, *iy);
+			}
+		}
+	}
+}
+
+int main() {
+	vector<int> v = { 0, 1, 0,5,4,3 };
+
+	display(v);
+
+	//swap(v[0], v[1]);
+
+	//display(v);
+
+	ofstream fout("test.txt");
+	buble_sort(v, &fout);
+	display(v);
+
+
+	return 0;
+}
+
+