Zhilong Li 4 лет назад
Родитель
Сommit
ffb67567e1
7 измененных файлов с 66 добавлено и 13 удалено
  1. 5 1
      .gitignore
  2. 8 1
      CMakeLists.txt
  3. 2 1
      include/char_test.hpp
  4. 16 0
      include/functions.cpp
  5. 13 0
      include/functions.h
  6. 3 4
      include/process_bar.hpp
  7. 19 6
      main.cpp

+ 5 - 1
.gitignore

@@ -1,4 +1,8 @@
 cmake-build-debug/
 main.dSYM/
 .vscode/
-build/
+build/
+.clangd
+*.json
+*.so
+*.a

+ 8 - 1
CMakeLists.txt

@@ -1,7 +1,14 @@
 cmake_minimum_required(VERSION 3.16)
-project(CppYoutube)
+project(CppYoutube CXX)
 
 set(CMAKE_CXX_STANDARD 17)
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
 
 include_directories(include)
+set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ../lib)
+add_library(functions SHARED include/functions.cpp)
+set_target_properties(functions PROPERTIES LINKER_LANGUAGE CXX)
+# link_directories(lib)
+# link_libraries(functions)
 add_executable(CppYoutube main.cpp)
+target_link_libraries(CppYoutube functions)

+ 2 - 1
include/char_test.hpp

@@ -1,3 +1,4 @@
+#include <cstdio>
 #include <iostream>
 #include <string>
 #include <vector>
@@ -25,6 +26,6 @@ int test_char()
         cout << char(enc_letter[i]);
     }
     cout << endl;
-
+    printf("What is this?");
     return 0;
 }

+ 16 - 0
include/functions.cpp

@@ -0,0 +1,16 @@
+#include "functions.h"
+
+void test_func()
+{
+    std::cout << "Hello from function but changed!" << std::endl;
+}
+
+bool isPrimeNum(int num)
+{
+    for (int i = 2; i < num; i++)
+    {
+        if (num % i == 0)
+            return false;
+    }
+    return true;
+}

+ 13 - 0
include/functions.h

@@ -0,0 +1,13 @@
+#include <iostream>
+
+void test_func();
+bool isPrimeNum(int num);
+
+template<typename T>
+void swap(T &a, T &b)
+{
+    T temp = a;
+    a = b;
+    b = temp;
+}
+

+ 3 - 4
include/process_bar.hpp

@@ -1,14 +1,13 @@
 #include<iostream>
-using namespace std;
+#include<unistd.h>
 
 void rotate()
 {
     int i = 0;
-    char rotater[] = {'-', '\\', '|', '/'};
+    char rotater[] = {'|', '|', '|', '|'};
     while (i < 100)
     {
-        cout << rotater[i % 4];
-        system("reset");
+	std::cout << rotater[i % 4];
         i++;
     }
 }

+ 19 - 6
main.cpp

@@ -1,11 +1,24 @@
-#include <char_test.hpp>
-#include <process_bar.hpp>
-
-using namespace std;
+// #include "char_test.hpp"
+// #include "process_bar.hpp"
+#include "functions.h"
 
 int main()
 {
     // test_char();
-    rotate();
+    // rotate();
+    // test_func();
+    int num;
+    std::cout << "Input a num to check if it's prime num:\n> ";
+    std::cin >> num;
+    printf(isPrimeNum(num)?"Yes\n":"No\n");
+
+    int a = 3, b = 87;
+    std::cout << "A is " << a << " and B is " << b << std::endl;
+    swap(a, b);
+    std::cout << "A is " << a << " and B is " << b << std::endl;
+    char c = 'c', d = 'd';
+    std::cout << "C is " << c << " and D is " << d << std::endl;
+    swap(c, d);
+    std::cout << "C is " << c << " and B is " << d << std::endl;
     return 0;
-}
+}