Created
June 3, 2026 10:23
-
-
Save sunmeat/8427a550570c0d8328bed1eb2a378cbb to your computer and use it in GitHub Desktop.
приклад на статичні поля та методи, клас Math, утілітарний клас ConsoleApp (версія для мак ос)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| #include <cstdlib> | |
| #include <ctime> | |
| #include <unistd.h> | |
| #include <termios.h> | |
| using namespace std; | |
| #define uint unsigned int | |
| class Math { | |
| public: | |
| Math() = delete; | |
| static const int ZERO = 0; | |
| static const double PI; | |
| static const double E; | |
| static unsigned long long int factorial(uint number) { | |
| if (number == 0 || number == 1) return 1; | |
| return number * factorial(number - 1); | |
| } | |
| static unsigned long long int fibonachi(uint index) { | |
| if (index == 0 || index == 1) return 1; | |
| return fibonachi(index - 1) + fibonachi(index - 2); | |
| } | |
| }; | |
| double const Math::PI = 3.141592653589793; | |
| double const Math::E = 2.718281828459045; | |
| class ConsoleApp { | |
| public: | |
| ConsoleApp() = delete; | |
| enum class Color { | |
| BLACK = 30, RED = 31, GREEN = 32, YELLOW = 33, | |
| BLUE = 34, MAGENTA = 35, CYAN = 36, WHITE = 37, | |
| BRIGHT_BLACK = 90, BRIGHT_RED = 91, BRIGHT_GREEN = 92, | |
| BRIGHT_YELLOW = 93, BRIGHT_BLUE = 94, BRIGHT_MAGENTA = 95, | |
| BRIGHT_CYAN = 96, BRIGHT_WHITE = 97 | |
| }; | |
| static void set_title(const string& title) { | |
| cout << "\033]0;" << title << "\007"; | |
| cout.flush(); | |
| } | |
| static void hide_cursor() { | |
| cout << "\033[?25l"; | |
| cout.flush(); | |
| } | |
| static void show_cursor() { | |
| cout << "\033[?25h"; | |
| cout.flush(); | |
| } | |
| static void clear_screen() { | |
| system("clear"); | |
| } | |
| static void start_random_generation() { | |
| srand((unsigned int)time(0)); | |
| rand(); | |
| } | |
| static void set_text_color(Color color) { | |
| cout << "\033[" << (int)color << "m"; | |
| } | |
| static void reset_color() { | |
| cout << "\033[0m"; | |
| } | |
| static int random_number(int min, int max) { | |
| return (rand() * rand()) % (max - min + 1) + min; | |
| } | |
| static void quick_setup(const string& title = "Мій консольний додаток") { | |
| set_title(title); | |
| hide_cursor(); | |
| start_random_generation(); | |
| } | |
| static void wait_for_key() { | |
| struct termios oldt, newt; | |
| tcgetattr(STDIN_FILENO, &oldt); | |
| newt = oldt; | |
| newt.c_lflag &= ~(ICANON | ECHO); | |
| tcsetattr(STDIN_FILENO, TCSANOW, &newt); | |
| getchar(); | |
| tcsetattr(STDIN_FILENO, TCSANOW, &oldt); | |
| show_cursor(); | |
| } | |
| static void exit_app() { | |
| reset_color(); | |
| wait_for_key(); | |
| } | |
| }; | |
| int main() { | |
| ConsoleApp::quick_setup("Приклад статичних методів та констант"); | |
| cout << "Факторіал 5 = " << Math::factorial(5) << "\n"; | |
| cout << "Число Фібоначчі 10 = " << Math::fibonachi(10) << "\n"; | |
| ConsoleApp::exit_app(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment