From 71a946ecf0f7738e5bc927b6a846e4b4a657ddf9 Mon Sep 17 00:00:00 2001 From: Kaehvaman Date: Mon, 2 Dec 2024 23:19:52 +0400 Subject: [PATCH] clock and progressbar tests --- clock/clock.vcxproj | 272 +++++++++++++++++---------------- clock/clock.vcxproj.filters | 50 +++--- clock/main.c | 64 ++++++-- lab22/main.c | 1 + progressbar test/progressbar.c | 131 ++++++++-------- progressbar test/progressbar.h | 40 ++--- 6 files changed, 302 insertions(+), 256 deletions(-) diff --git a/clock/clock.vcxproj b/clock/clock.vcxproj index 2fe3d14..f466895 100644 --- a/clock/clock.vcxproj +++ b/clock/clock.vcxproj @@ -1,135 +1,139 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 17.0 - Win32Proj - {268761f2-af30-432d-91df-77542792a4ab} - clock - 10.0 - - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - - - - - - - - - - - - - - - - - - - - Level3 - true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - true - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - Level3 - true - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - true - true - true - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {268761f2-af30-432d-91df-77542792a4ab} + clock + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/clock/clock.vcxproj.filters b/clock/clock.vcxproj.filters index 669bc4e..32807eb 100644 --- a/clock/clock.vcxproj.filters +++ b/clock/clock.vcxproj.filters @@ -1,22 +1,30 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Исходные файлы - - + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + Исходные файлы + + + + + Файлы заголовков + + \ No newline at end of file diff --git a/clock/main.c b/clock/main.c index 168956b..7b440b9 100644 --- a/clock/main.c +++ b/clock/main.c @@ -1,16 +1,50 @@ -#include -#include -#include -#include - - -int main() { - int n = 10, sl = 10, last_clock = 0; - clock_t start = clock(); - for (int i = 0; i < n; i++) { - printf_s("%d\n", i); - Sleep(sl); - } - printf_s("time = %.3lf seconds\nexpected %d seconds %lf", (double)(clock() - start) / (double)CLOCKS_PER_SEC, n * sl / 1000, 0.3300000000000000000000); - return 0; +#include +#include +#include +#include "../progressbar test/progressbar.h" + +// \033[ = CSI, CSI ?25l hides the cursor, CSI ?25h shows the cursor +// see ANSI escape code wiki for more info +#define HIDE_CURSOR "\033[?25l" +#define SHOW_CURSOR "\033[?25h" + +void wait(int seconds) { + clock_t t0 = clock(); + clock_t t1; + while ((int)(((t1 = clock()) - t0) / CLOCKS_PER_SEC) < seconds) { + Sleep(50); + } + printf(" wait for %d s is over, clock=%d", seconds, (int)t1); +} + +int main() { + + #ifdef WIN32 + printf("ITS WINDOWS!\n"); + #endif + + struct progressbar pb; + int n = 5, sl = 1; + printf(HIDE_CURSOR); + progressbar_start(&pb, n * sl); + + clock_t start = clock(); + + for (int i = 0; i < n; i++) { + wait(sl); + //printf("%d\n", i); + progressbar_add(&pb, sl); + } + //progressbar_clear(); + printf(SHOW_CURSOR); + printf("\n"); + printf_s("time = %.3lf seconds\nexpected %d seconds\n", (double)(clock() - start) / (double)CLOCKS_PER_SEC, n * sl); + + progressbar_start(&pb, n * sl); + for (int i = 0; i < n; i++) { + wait(sl); + //printf("%d\n", i); + progressbar_add(&pb, sl); + } + return 0; } \ No newline at end of file diff --git a/lab22/main.c b/lab22/main.c index c467745..7643866 100644 --- a/lab22/main.c +++ b/lab22/main.c @@ -44,6 +44,7 @@ int main() { printf("strchr_my_arr_res 's' = {%p} = '%c', index = %d\n", strchr_my_arr_res, *strchr_my_arr_res, (int)(strchr_my_arr_res - test3)); char* strchr_my_arr_ptr = strchr_my_ptr(test3, 's'); printf("strchr_my_arr_ptr 's' = {%p} = '%c', index = %d\n", strchr_my_arr_ptr, *strchr_my_arr_ptr, (int)(strchr_my_arr_ptr - test3)); + char dest[10]; return 0; } \ No newline at end of file diff --git a/progressbar test/progressbar.c b/progressbar test/progressbar.c index 6109909..4248adc 100644 --- a/progressbar test/progressbar.c +++ b/progressbar test/progressbar.c @@ -1,66 +1,65 @@ -#include -#include "progressbar.h" - -// see progressbar.h - -static const unsigned progressbar_width = 70; - -static unsigned get_to_print(unsigned current, unsigned max) { - return progressbar_width * current / max; -} - -static unsigned get_percentage(unsigned current, unsigned max) { - return 100 * current / max; -} - -static void progressbar_show(struct progressbar *pb) { - unsigned to_print = get_to_print(pb->current, pb->max); - unsigned percentage = get_percentage(pb->current, pb->max); - - printf("\r["); - for(unsigned i = 0; i < to_print; i++) { - printf("#"); - } - for(unsigned i = 0; i < progressbar_width - to_print; i++) { - printf(" "); - } - printf("] %3u%%", percentage); - fflush(stdout); -} - -void progressbar_start(struct progressbar *pb, unsigned max) { - pb->max = max; - pb->current = 0; - progressbar_show(pb); -} - -void progressbar_set(struct progressbar *pb, unsigned current) { - unsigned old_to_print = get_to_print(pb->current, pb->max); - unsigned old_percentage = get_percentage(pb->current, pb->max); - unsigned to_print = get_to_print(current, pb->max); - unsigned percentage = get_percentage(current, pb->max); - pb->current = current; - if(old_to_print == to_print && old_percentage == percentage) { - return; - } - progressbar_show(pb); -} - -void progressbar_add(struct progressbar *pb, unsigned n) { - progressbar_set(pb, pb->current + n); -} - -void progressbar_inc(struct progressbar *pb) { - progressbar_add(pb, 1); -} - -void progressbar_clear(struct progressbar *pb) { - (void)pb; - printf("\r"); - for(unsigned i = 0; i < progressbar_width + 7; i++) { - printf(" "); - } - fflush(stdout); - printf("\r"); - fflush(stdout); -} +#include +#include "progressbar.h" + +// see progressbar.h + +static const unsigned progressbar_width = 80 - 7; + +static unsigned get_to_print(unsigned current, unsigned max) { + return progressbar_width * current / max; +} + +static unsigned get_percentage(unsigned current, unsigned max) { + return 100 * current / max; +} + +static void progressbar_show(struct progressbar *pb) { + unsigned to_print = get_to_print(pb->current, pb->max); + unsigned percentage = get_percentage(pb->current, pb->max); + + printf("\r["); + for(unsigned i = 0; i < to_print; i++) { + printf("#"); + } + for(unsigned i = 0; i < progressbar_width - to_print; i++) { + printf(" "); + } + printf("] %3u%%", percentage); + fflush(stdout); +} + +void progressbar_start(struct progressbar *pb, unsigned max) { + pb->max = max; + pb->current = 0; + progressbar_show(pb); +} + +void progressbar_set(struct progressbar *pb, unsigned current) { + unsigned old_to_print = get_to_print(pb->current, pb->max); + unsigned old_percentage = get_percentage(pb->current, pb->max); + unsigned to_print = get_to_print(current, pb->max); + unsigned percentage = get_percentage(current, pb->max); + pb->current = current; + if(old_to_print == to_print && old_percentage == percentage) { + return; + } + progressbar_show(pb); +} + +void progressbar_add(struct progressbar *pb, unsigned n) { + progressbar_set(pb, pb->current + n); +} + +void progressbar_inc(struct progressbar *pb) { + progressbar_add(pb, 1); +} + +void progressbar_clear() { + printf("\r"); + for(unsigned i = 0; i < progressbar_width + 7; i++) { + printf(" "); + } + fflush(stdout); + printf("\r"); + fflush(stdout); +} diff --git a/progressbar test/progressbar.h b/progressbar test/progressbar.h index d65c2f7..2b82e85 100644 --- a/progressbar test/progressbar.h +++ b/progressbar test/progressbar.h @@ -1,20 +1,20 @@ -#ifndef JPEG2PNG_PROGRESSBAR_H -#define JPEG2PNG_PROGRESSBAR_H - -struct progressbar { - unsigned current; - unsigned max; -}; - -// initialize progressbar with maximum value -void progressbar_start(struct progressbar *pb, unsigned max); -// set current value -void progressbar_set(struct progressbar *pb, unsigned current); -// add to current value -void progressbar_add(struct progressbar *pb, unsigned n); -// add one to current value -void progressbar_inc(struct progressbar *pb); -// clear line of progress bar -void progressbar_clear(struct progressbar *pb); - -#endif +#ifndef JPEG2PNG_PROGRESSBAR_H +#define JPEG2PNG_PROGRESSBAR_H + +struct progressbar { + unsigned current; + unsigned max; +}; + +// initialize progressbar with maximum value +void progressbar_start(struct progressbar *pb, unsigned max); +// set current value +void progressbar_set(struct progressbar *pb, unsigned current); +// add to current value +void progressbar_add(struct progressbar *pb, unsigned n); +// add one to current value +void progressbar_inc(struct progressbar *pb); +// clear line of progress bar +void progressbar_clear(); + +#endif