Skip to content

Commit

Permalink
Merge pull request #10 from XuhuaHuang/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
XuhuaHuang authored Nov 27, 2022
2 parents 264600e + 7bf3c3b commit 61254b8
Show file tree
Hide file tree
Showing 50 changed files with 404 additions and 148 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
mono_crash.*

# Build results
# Qt build results
build*Qt*-Debug/
build*Qt*-Profile/
build*Qt*-Release/
*.qmake.*
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
Expand Down
4 changes: 2 additions & 2 deletions BitwiseOperator/BitwiseOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

void print(const int& value)
{
std::cout << value << std::endl;
std::cout << value << "\n";
}

int main(void)
Expand All @@ -24,4 +24,4 @@ int main(void)
print(a >> 2);
print((a >> 2) & 1);
return 0;
}
}
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cmake_minimum_required(VERSION 3.20)

project(HelloWorld LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(HelloWorld "Miscellaneous/hello_world.cpp" "LICENSE.md" "README.md")
4 changes: 2 additions & 2 deletions Demonstration/__func__.cpp → CompilerMacro/__func__.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ int main(void)
{
/* Test __func__ predefined macro */
std::cout << hello() << "\n"
<< world() << std::endl;
<< world() << "\n";
LOG("Function call hello() returns: %s", hello());
LOG("Function call world() returns: %s", world());

Expand All @@ -75,4 +75,4 @@ int main(void)

system("pause");
return 0;
}
}
23 changes: 23 additions & 0 deletions CompilerMacro/macro_expansion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*****************************************************************//**
* \file macro_expansion.cpp
* \brief Macro expansion in C++
*
* \author Xuhua Huang
* \date November 2022
*********************************************************************/

#include <iostream>

#define M(x, y, z) x*y+z

int main(void)
{
int a = 1, b = 2, c = 3;
int sum = M(a+b, b+c, a+c);

// M(a, b, a) = M(1, 2, 1) = 3
// M(b, c, c) = M(2, 3, 3) = 9
std::cout << "sum = " << sum << "\n";

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ void demo_constexpr(const int arg) {
return;
}

int main(void) {
auto main(void) -> int {
int temp_const = get_const();
std::cout << "Value of temp_const: " << temp_const << std::endl;
std::cout << "Value of temp_const: " << temp_const << "\n";

demo_constexpr(1);
demo_constexpr(2);

system("pause");
return 0;
}
return EXIT_SUCCESS;
}
2 changes: 1 addition & 1 deletion Demonstration/demo_is_multiple_of.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "../Util/log.h"

#ifndef DEBUG
#define DEBUG(str) std::cout << str << std::endl;
#define DEBUG(str) std::cout << str << "\n";
#endif

/**
Expand Down
4 changes: 2 additions & 2 deletions Demonstration/winAPI_GetCommandLineA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

int main(void)
{
std::cout << GetCommandLineA() << std::endl;
std::cout << GetCommandLineA() << "\n";

char* const pBuf = GetCommandLineA();
if (pBuf != nullptr)
Expand Down Expand Up @@ -57,4 +57,4 @@ int main(void)
}

return 0;
}
}
4 changes: 2 additions & 2 deletions FunctionPointer/function_pointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void printValue(const int value)
*
* \param value: constant copy of an int
*/
std::cout << "[fn][void printValue(int)]Content: " << value << std::endl;
std::cout << "[fn][void printValue(int)]Content: " << value << "\n";
}

int main(void)
Expand Down Expand Up @@ -74,7 +74,7 @@ int main(void)
ForEach(values, [](int value) { // start lambda
// capture all integers in scope by copying
// no trailing return
std::cout << "[lambda][&](int value)Value: " << value << std::endl;
std::cout << "[lambda][&](int value)Value: " << value << "\n";
} // end lambda
);

Expand Down
6 changes: 3 additions & 3 deletions HackerRank/VirtualFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Professor : public Person

void putdata(void) override {
Person::putdata();
std::cout << " " << this->publications << " " << this->cur_id << std::endl;
std::cout << " " << this->publications << " " << this->cur_id << "\n";
}
};

Expand Down Expand Up @@ -85,7 +85,7 @@ class Student : public Person

void putdata(void) override {
Person::putdata();
std::cout << " " << this->sum << " " << this->cur_id << std::endl;
std::cout << " " << this->sum << " " << this->cur_id << "\n";
}
};

Expand Down Expand Up @@ -117,4 +117,4 @@ int main() {
per[i]->putdata(); // Print the required output for each object.

return 0;
}
}
3 changes: 2 additions & 1 deletion Lambda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ cmake_minimum_required(VERSION 3.20)

project(Lambda)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Adding multiple executable to the project
# Right-click on each item to set as start-up project
add_executable(ForEachWithLambda "lambda_functional.cpp")
add_executable(GenericAdd "generic_add.cpp")
add_executable(LambdaBasics "lambda_notes.cpp")
add_executable(LambdaScopeDemo "lambda_scope_demo.cpp")
add_executable(LambdaWithStdSortAlgo "lambda_with_stdlib.cpp")
Expand Down
2 changes: 1 addition & 1 deletion Lambda/FunctorLambda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ project(FunctorLambda)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(FunctorLambda "FunctorLambda/FunctorLambda.cpp")
add_executable(FunctorLambda "FunctorLambda.cpp")
File renamed without changes.
35 changes: 35 additions & 0 deletions Lambda/OperationFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*****************************************************************//**
* \file OperationFactory.hpp
* \brief Operation factory function definition
* Factory and command design pattern
*
* To compile:
* $ g++ .\OperationFactory.hpp
*
* \author Xuhua Huang
* \date November 2022
*********************************************************************/

#ifndef OPERATIONFACTORY_HPP
#define OPERATIONFACTORY_HPP

#ifndef _IOSTREAM_
#include <iostream>
#endif

#ifndef _FUNCTIONAL_
#include <functional>
#endif

enum class Op { ADD, MUL };

std::function<int(int, int)> OperationFactory(Op op) {
switch(op) {
case Op::ADD:
return [](int a, int b) { return a + b; };
case Op::MUL:
return std::multiplies<int>();
}
}

#endif
33 changes: 33 additions & 0 deletions Lambda/generic_add.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*****************************************************************//**
* \file generic_add.cpp
* \brief
*
* \author Xuhua Huang
* \date November 2022
*********************************************************************/

#include <iostream>
#include <stdlib.h>

/**
* Lambda is an anonymous struct with the overloaded operator().
* Consider the following example
*/
struct _anonymous_struct_add {
template<typename T, typename U>
constexpr auto operator() (T a, U b) const { return a + b; }
// use it like this: constexpt const _anonymous_struct_add add = _anonymous_struct_add{};
};

auto main(void) -> int {
constexpr auto add = []<typename T>(T a, T b) -> T {
return a + b;
};
constexpr auto add5 = [add](auto x) -> auto {
return add(x, 5);
};

std::cout << add5(2) << "\n"; // 7

return EXIT_SUCCESS;
}
10 changes: 5 additions & 5 deletions Lambda/lambda_functional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
template<class T>
void printVector(std::vector<T> argVector)
{
std::cout << "\n[fn]The content of this vector is listed: " << std::endl;
std::cout << "\n[fn]The content of this vector is listed: " << "\n";

typename std::vector<T>::iterator iter; // keyword 'typename' is required for iterator
for (iter = argVector.begin(); iter < argVector.end(); iter++)
std::cout << *iter << std::endl; // dereference the iterator to print content
std::cout << *iter << "\n"; // dereference the iterator to print content

std::cout << "[fn]This is the end of the vector." << std::endl;
std::cout << "[fn]This is the end of the vector." << "\n";
return;
}

Expand Down Expand Up @@ -56,7 +56,7 @@ int main(void)

/* create a lambda */
auto print_int_val = [&](int value) {
std::cout << "Printing with lambda: " << value << std::endl;
std::cout << "Printing with lambda: " << value << "\n";
};

/* call ForEach and pass lambda as a function pointer */
Expand All @@ -67,7 +67,7 @@ int main(void)
[](int value) -> int { return value > 3; }
);

std::cout << "\nThe first element that's greater than 3 has a value of: " << *iter << std::endl;
std::cout << "\nThe first element that's greater than 3 has a value of: " << *iter << "\n";

return 0;
}
4 changes: 2 additions & 2 deletions LinkedList/linked_list.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* mingw32-make */
/* ./linkedlist */
/* cmake .\CMakelists.txt */
/* cmake .\CMakeLists.txt */

#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -69,4 +69,4 @@ void print_linked_list(node_t* _head) {
}
printf("\n");
return;
}
}
8 changes: 4 additions & 4 deletions MemberFnAmpersand/MemberFnAmpersand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ struct Arg
int i = 1;
Arg(int _i) : i {_i} {}
int getArg()&& {
std::cout << "In member function int getArg() &&" << std::endl;
std::cout << "In member function int getArg() &&" << "\n";
return i;
}

int& getArg()& {
std::cout << "In member function int& getArg() &" << std::endl;
std::cout << "In member function int& getArg() &" << "\n";
return i;
}
};

int main(void)
{
Arg g1{ 5 }; /* instanciation with innitializer_list */
std::cout << g1.getArg() << std::endl; /* expecting int& getArg() & */
std::cout << std::move(g1).getArg() << std::endl; /* expecting int getArg() && */
std::cout << g1.getArg() << "\n"; /* expecting int& getArg() & */
std::cout << std::move(g1).getArg() << "\n"; /* expecting int getArg() && */
return 0;
}
4 changes: 2 additions & 2 deletions Miscellaneous/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ int* func() {

int main(void) {
int* p = func();
std::cout << *p << std::endl; // OK; reserved by compiler
std::cout << *p << std::endl; // ERROR without exception! void pointer
std::cout << *p << "\n"; // OK; reserved by compiler
std::cout << *p << "\n"; // ERROR without exception! void pointer

system("pause");
return 0;
Expand Down
12 changes: 6 additions & 6 deletions MoveSemantic/move_with_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ int main(void)
// still able to access hello_str, but not world_str

/* Verify element via printing. */
std::cout << "The vector contains: " << std::endl;
std::cout << "The vector contains: " << "\n";
for (const std::string& str : str_vec)
std::cout << str << "\t";
std::cout << "\nEnd of the vector" << std::endl;
std::cout << "\nEnd of the vector" << "\n";

std::cout << "Attempting to access hello_str: " << hello_str << std::endl
<< "Attempting to access world_str: " << world_str << std::endl;
std::cout << "Attempting to access hello_str: " << hello_str << "\n"
<< "Attempting to access world_str: " << world_str << "\n";

/* Test functions in the ../Util headers. */
std::cout << "Testing Util functions: " << std::endl;
std::cout << "Testing Util functions: " << "\n";
if (util::type::is_instance_of<std::vector<std::string>>(str_vec)) {
util::vector::print_vec(str_vec);
}

system("pause");
return 0;
}
}
8 changes: 4 additions & 4 deletions Pointer/smart_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class Entity
{
public:
Entity() {
std::cout << "[DFCONST]Creating Entity..." << std::endl;
std::cout << "[DFCONST]Creating Entity..." << "\n";
}

~Entity() {
std::cout << "[DFDEST]Destroying Entity..." << std::endl;
std::cout << "[DFDEST]Destroying Entity..." << "\n";
}

void printEntity() {
std::cout << "[fn]Printing Entity..." << std::endl;
std::cout << "[fn]Printing Entity..." << "\n";
}
};

Expand All @@ -49,4 +49,4 @@ int main(void)

std::cin.get();
return 0;
}
}
Loading

0 comments on commit 61254b8

Please sign in to comment.