clock and progressbar tests

This commit is contained in:
Kaehvaman 2024-12-02 23:19:52 +04:00
parent 92f7e73b20
commit 71a946ecf0
6 changed files with 302 additions and 256 deletions

View File

@ -127,8 +127,12 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\progressbar test\progressbar.c" />
<ClCompile Include="main.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\progressbar test\progressbar.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>

View File

@ -18,5 +18,13 @@
<ClCompile Include="main.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="..\progressbar test\progressbar.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\progressbar test\progressbar.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,16 +1,50 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <Windows.h>
#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() {
int n = 10, sl = 10, last_clock = 0;
#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++) {
printf_s("%d\n", i);
Sleep(sl);
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);
}
printf_s("time = %.3lf seconds\nexpected %d seconds %lf", (double)(clock() - start) / (double)CLOCKS_PER_SEC, n * sl / 1000, 0.3300000000000000000000);
return 0;
}

View File

@ -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;
}

View File

@ -3,64 +3,63 @@
// see progressbar.h
static const unsigned progressbar_width = 70;
static const unsigned progressbar_width = 80 - 7;
static unsigned get_to_print(unsigned current, unsigned max) {
return progressbar_width * current / max;
return progressbar_width * current / max;
}
static unsigned get_percentage(unsigned current, unsigned max) {
return 100 * current / 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);
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);
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);
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);
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);
progressbar_set(pb, pb->current + n);
}
void progressbar_inc(struct progressbar *pb) {
progressbar_add(pb, 1);
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);
void progressbar_clear() {
printf("\r");
for(unsigned i = 0; i < progressbar_width + 7; i++) {
printf(" ");
}
fflush(stdout);
printf("\r");
fflush(stdout);
}

View File

@ -2,8 +2,8 @@
#define JPEG2PNG_PROGRESSBAR_H
struct progressbar {
unsigned current;
unsigned max;
unsigned current;
unsigned max;
};
// initialize progressbar with maximum value
@ -15,6 +15,6 @@ 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);
void progressbar_clear();
#endif