ADDED .vscode/settings.json Index: .vscode/settings.json ================================================================== --- .vscode/settings.json +++ .vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.exclude": { + "build": true + } +} ADDED CMakeLists.txt Index: CMakeLists.txt ================================================================== --- CMakeLists.txt +++ CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.1 FATAL_ERROR) + +project(Kursova LANGUAGES CXX) + +add_executable(main src/main) +target_compile_features(main PRIVATE cxx_std_17) ADDED src/main.cxx Index: src/main.cxx ================================================================== --- src/main.cxx +++ src/main.cxx @@ -0,0 +1,86 @@ +#include +#include +#include +#include + +// Неободимо для поддержки Unicode +#ifdef _WIN32 +#include // SetConsoleOutputCP +#else +#include +#endif + +using std::cin, std::cout, std::endl, std::flush, std::setw; +using std::stack, std::tuple; + +void f_rec(const size_t fun_count, const size_t max_recursion_depth, const size_t n = 1, const size_t depth = 0) +{ + cout << setw(depth) << "" << n << endl; + if (depth >= max_recursion_depth) + return; + + for (size_t i = 0; i < fun_count - 1; i++) + f_rec(fun_count, max_recursion_depth, (n + i) % fun_count + 1, depth + 1); +} + +void f_iter(const size_t fun_count, const size_t max_recursion_depth) +{ + using stack_item = tuple; + + stack return_stack; + return_stack.push({1, 0}); + + while (!return_stack.empty()) + { + auto [n, depth] = return_stack.top(); + return_stack.pop(); + + stack queue; + + // BEGIN unmodified code + cout + << setw(depth) << "" << n << endl; + if (depth >= max_recursion_depth) + continue; + + for (size_t i = 0; i < fun_count - 1; i++) + { + queue.push({(n + i) % fun_count + 1, depth + 1}); + } + // END unmodified code + + while (!queue.empty()) + { + return_stack.push(queue.top()); + queue.pop(); + } + } +} + +int main() +{ + + // Включаем поддержку Unicode +#ifdef _WIN32 + SetConsoleOutputCP(65001); +#else + std::locale::global(std::locale("")); +#endif + + size_t fun_count; + size_t max_recursion_depth; + + cout << u8"Кол-во функций: " << flush; + cin >> fun_count; + + cout << u8"Макс. глубина рекурсии: " << flush; + cin >> max_recursion_depth; + + cout << u8"Вывод рекурсивной функции:" << endl; + f_rec(fun_count, max_recursion_depth); + + cout << u8"Вывод нерекурсивной функции:" << endl; + f_iter(fun_count, max_recursion_depth); + + return 0; +}