From a4624b477fb4e164391131088bbcbec116a85efd Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 19 May 2025 21:26:47 +0800 Subject: [PATCH] feat: add quiz 2 counter. --- CMakeLists.txt | 1 + counter/CMakeLists.txt | 11 +++++++++++ counter/include/counter.h | 15 +++++++++++++++ counter/main.cpp | 21 +++++++++++++++++++++ counter/src/counter.cpp | 34 ++++++++++++++++++++++++++++++++++ counter/tests/CMakeLists.txt | 10 ++++++++++ counter/tests/smoke-tests.cpp | 0 7 files changed, 92 insertions(+) create mode 100644 counter/CMakeLists.txt create mode 100644 counter/include/counter.h create mode 100644 counter/main.cpp create mode 100644 counter/src/counter.cpp create mode 100644 counter/tests/CMakeLists.txt create mode 100644 counter/tests/smoke-tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2748b20..a6e49c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,3 +6,4 @@ set(CMAKE_CXX_STANDARD 11) add_subdirectory(third-party/googletest-1.17.0) add_subdirectory(mixplus) +add_subdirectory(counter) \ No newline at end of file diff --git a/counter/CMakeLists.txt b/counter/CMakeLists.txt new file mode 100644 index 0000000..555166e --- /dev/null +++ b/counter/CMakeLists.txt @@ -0,0 +1,11 @@ +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) + +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src SRC) + +add_library(libcount STATIC ${SRC}) + +add_executable(counter main.cpp) + +target_link_libraries(counter libcount) + +add_subdirectory(tests) diff --git a/counter/include/counter.h b/counter/include/counter.h new file mode 100644 index 0000000..6bac9b9 --- /dev/null +++ b/counter/include/counter.h @@ -0,0 +1,15 @@ +#include +#include + +namespace counter +{ + struct Counter + { + static std::unordered_map count(const std::string& input); + + static void countPair(std::unordered_map& map, const std::string& input); + + + static void countTriple(std::unordered_map& map, const std::string& input); + }; +} \ No newline at end of file diff --git a/counter/main.cpp b/counter/main.cpp new file mode 100644 index 0000000..559dc93 --- /dev/null +++ b/counter/main.cpp @@ -0,0 +1,21 @@ +#include +#include + +int main(const int argc, char** argv) +{ + if (argc != 2) + { + std::cout << "ERROR: Need a string." << std::endl; + } + + const std::string input(argv[1]); + + const auto map = counter::Counter::count(input); + + for (const auto& pair : map) + { + std::cout << pair.first << ' ' << pair.second << std::endl; + } + + return 0; +} diff --git a/counter/src/counter.cpp b/counter/src/counter.cpp new file mode 100644 index 0000000..e1fc65f --- /dev/null +++ b/counter/src/counter.cpp @@ -0,0 +1,34 @@ +#include "counter.h" + + +void counter::Counter::countPair(std::unordered_map& map, const std::string& input) +{ + for (int i = 1; i < input.size(); i++) + { + const auto pair = input.substr(i - 1, 2); + + map[pair] += 1; + } +} + +void counter::Counter::countTriple(std::unordered_map& map, const std::string& input) +{ + for (int i = 2; i < input.size(); i++) + { + const auto pair = input.substr(i - 2, 3); + + map[pair] += 1; + } +} + +std::unordered_map counter::Counter::count(const std::string& input) +{ + std::unordered_map map; + + countPair(map, input); + countTriple(map, input); + + return map; +} + + diff --git a/counter/tests/CMakeLists.txt b/counter/tests/CMakeLists.txt new file mode 100644 index 0000000..f70795d --- /dev/null +++ b/counter/tests/CMakeLists.txt @@ -0,0 +1,10 @@ +enable_testing() + +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} TEST_SRC) + +add_executable( + counter_tests + ${TEST_SRC} +) + +target_link_libraries(counter_tests GTest::gtest_main libcount) diff --git a/counter/tests/smoke-tests.cpp b/counter/tests/smoke-tests.cpp new file mode 100644 index 0000000..e69de29