diff --git a/c/avg_task/avg.c b/c/avg_task/avg.c new file mode 100644 index 0000000..4dc7997 --- /dev/null +++ b/c/avg_task/avg.c @@ -0,0 +1,78 @@ +#include +#include + +//~ int avg(int a, int b) +//~ { + //~ if (abs(a) < abs(b)) + //~ return a + (b - a) / 2; + //~ else + //~ return b + (a - b) / 2; +//~ } + +int avg(int a, int b) +{ + return abs(a) < abs(b) ? a + (b - a) / 2 : b + (a - b) / 2; +} + +//~ int avg(int a, int b) +//~ { + //~ if (sign(a) != sign(b)) + //~ return (a + b) / 2; + //~ else + //~ return a + (b - a) / 2; +//~ } + +//~ int avg(int a, int b) +//~ { + //~ if (((a > 0) && (b > 0)) || ((a < 0) && (b < 0))) + //~ return a + (b - a) / 2; + //~ else + //~ return (a + b) / 2; +//~ } + +int main(int argc, char *argv[]) +{ + int ab[][2] = { + {3, 3}, + {3, 4}, + {4, 4}, + {4, 5}, + {5, 5}, + {5, 4}, + {4, 3}, + {-3, 3}, + {-3, 4}, + {-4, 4}, + {-4, 5}, + {-5, 5}, + {-5, 4}, + {-4, 3}, + {3, -3}, + {3, -4}, + {4, -4}, + {4, -5}, + {5, -5}, + {5, -4}, + {4, -3}, + {-3, -3}, + {-3, -4}, + {-4, -4}, + {-4, -5}, + {-5, -5}, + {-5, -4}, + {-4, -3} + }; + + size_t i; + + for (i = 0; i < sizeof(ab) / 2 / sizeof(int); i++) { + int iavg = avg(ab[i][0],ab[i][1]); + int davg = ((double)ab[i][0] + (double)ab[i][1]) / 2.0; + + if (iavg != davg) + printf("avg(%d, %d) = %d (верное = %d)\n", ab[i][0], ab[i][1], avg(ab[i][0],ab[i][1]), davg); + } + + + return 0; +} diff --git a/c/avg_task/avg_arr.c b/c/avg_task/avg_arr.c new file mode 100644 index 0000000..e5c7346 --- /dev/null +++ b/c/avg_task/avg_arr.c @@ -0,0 +1,54 @@ +#include +#include + +int avg(size_t N, int *a) +{ + size_t i; + + int mean = 0, + rem = 0; + + for (i = 0; i < N; i++) { + int tmp = a[i] - mean; + int ii = (int)i; + mean += tmp / (ii + 1); + rem += tmp % (ii + 1); + + if (abs(rem) > ii) { + if (rem > 0) { + rem -= ii + 1; + mean++; + } + else { + rem += ii + 1; + mean--; + } + } + } + + // округление до ближайшего целого + //~ if (abs(rem) > ii / 2) + //~ if (rem > 0) + //~ mean++; + //~ else + //~ mean--; + + return mean; +} + +int main(int argc, char *argv[]) +{ + const size_t N = 88; + + int *a = (int *)malloc(N * sizeof(int)); + + size_t i; + for (i = 0; i < N; i++) + a[i] = rand() % 1024; + + printf("avg(a[]) = %d\n", avg(N, a)); + + free(a); + + return 0; +} diff --git a/c/tex_parser/tex_table_class_test.cpp b/c/tex_parser/tex_table_class_test.cpp deleted file mode 100644 index c6c0cb6..0000000 --- a/c/tex_parser/tex_table_class_test.cpp +++ /dev/null @@ -1,13 +0,0 @@ -//~ #include -//~ #include -//~ #include -#include - -using namespace std; - -int main(int argc, char *argv[]) -{ - - - return 0;//EXIT_SUCCESS; -} diff --git a/c/tex_parser/Makefile b/cpp/tex_parser/Makefile similarity index 90% rename from c/tex_parser/Makefile rename to cpp/tex_parser/Makefile index ccf73e7..09efd13 100644 --- a/c/tex_parser/Makefile +++ b/cpp/tex_parser/Makefile @@ -10,7 +10,7 @@ ifeq ($(mode),) mode = debug endif ifeq ($(mode),debug) - CFLAGS = -O0 -g -std=gnu99 -pedantic -Wextra -Wconversion + CFLAGS = -O0 -g -std=gnu++98 -pedantic -Wextra -Wconversion LDFLAGS = endif ifeq ($(mode),profile) @@ -49,11 +49,11 @@ endif @echo ".........................." %.o : - $(CC) -c $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ $< + $(CXX) -c $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ $< tex_table_class_test_obj = xmalloc.o xerror.o tex_table_class_test: tex_table_class_test.o $(tex_table_class_test_obj) - $(CXX) $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ tex_table_class_test.o $(tex_table_class_test_obj) $(dataserver_obj) + $(CXX) $(CFLAGS) $(LDFLAGS) $(INCLUDE) -o $@ tex_table_class_test.o $(tex_table_class_test_obj) tex_table_class_test.o: tex_table_class_test.cpp $(tex_table_class_test_obj) diff --git a/c/tex_parser/get_tags.sh b/cpp/tex_parser/get_tags.sh similarity index 100% rename from c/tex_parser/get_tags.sh rename to cpp/tex_parser/get_tags.sh diff --git a/c/tex_parser/tables4.tex b/cpp/tex_parser/tables4.tex similarity index 100% rename from c/tex_parser/tables4.tex rename to cpp/tex_parser/tables4.tex diff --git a/c/tex_parser/tables5.tex b/cpp/tex_parser/tables5.tex similarity index 100% rename from c/tex_parser/tables5.tex rename to cpp/tex_parser/tables5.tex diff --git a/c/tex_parser/tex_table_class.cpp b/cpp/tex_parser/tex_table_class.cpp similarity index 99% rename from c/tex_parser/tex_table_class.cpp rename to cpp/tex_parser/tex_table_class.cpp index 9496e1a..dfa5417 100644 --- a/c/tex_parser/tex_table_class.cpp +++ b/cpp/tex_parser/tex_table_class.cpp @@ -6,8 +6,6 @@ int parse_table(const char *table_source, table_c *table) { size_t table_source_len = strlen(table_source); - - for (size_t i = 0; i < table_source_len; i++) { char c = table_source[i]; diff --git a/cpp/tex_parser/tex_table_class.geany b/cpp/tex_parser/tex_table_class.geany new file mode 100644 index 0000000..383d227 --- /dev/null +++ b/cpp/tex_parser/tex_table_class.geany @@ -0,0 +1,29 @@ + +[indentation] +indent_width=8 +indent_type=1 +indent_hard_tab_width=8 +detect_indent=false +indent_mode=2 + +[project] +name=tex_table_class +base_path=/home/kolan/dev/c/tex_parser/ + +[long line marker] +long_line_behaviour=1 +long_line_column=72 + +[files] +current_page=2 +FILE_NAME_0=1806;LaTeX;0;45;1;1;1;/home/kolan/dev/c/tex_parser/tables4.tex;0 +FILE_NAME_1=0;LaTeX;0;45;1;1;1;/home/kolan/dev/c/tex_parser/tables5.tex;0 +FILE_NAME_2=539;C;0;16;1;1;1;/home/kolan/dev/c/tex_parser/tex_table_class_test.cpp;0 +FILE_NAME_3=530;Make;0;16;1;1;1;/home/kolan/dev/c/tex_parser/Makefile;0 +FILE_NAME_4=271;C;0;61;1;1;1;/home/kolan/projects/include/xmalloc.h;0 +FILE_NAME_5=252;C++;0;61;1;1;1;/home/kolan/dev/c/tex_parser/tex_table_class.cpp;0 +FILE_NAME_6=2406;C++;0;61;1;1;1;/home/kolan/dev/c/tex_parser/tex_table_class.hpp;0 +FILE_NAME_7=704;C++;0;61;1;1;1;/home/kolan/dev/c/tex_parser/tex_table_class_tags.hpp;0 +FILE_NAME_8=0;C;0;61;1;1;1;/home/kolan/projects/include/xerror.c;0 +FILE_NAME_9=17;C;0;61;1;1;1;/home/kolan/projects/include/xerror.h;0 +FILE_NAME_10=273;C;0;16;1;1;1;/home/kolan/tmp.55.c;0 diff --git a/c/tex_parser/tex_table_class.hpp b/cpp/tex_parser/tex_table_class.hpp similarity index 100% rename from c/tex_parser/tex_table_class.hpp rename to cpp/tex_parser/tex_table_class.hpp diff --git a/c/tex_parser/tex_table_class_tags.hpp b/cpp/tex_parser/tex_table_class_tags.hpp similarity index 100% rename from c/tex_parser/tex_table_class_tags.hpp rename to cpp/tex_parser/tex_table_class_tags.hpp diff --git a/cpp/tex_parser/tex_table_class_test.cpp b/cpp/tex_parser/tex_table_class_test.cpp new file mode 100644 index 0000000..2586ab6 --- /dev/null +++ b/cpp/tex_parser/tex_table_class_test.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "xerror.h" +#include "xmalloc.h" + +using namespace std; + +int main(int argc, char *argv[]) +{ + if (argc != 1) { + die("Usage: ./tex_table_class_test /path/to/table.tex", -1); + } + + FILE *tex_file = fopen(argv[1], "rb"); + if (!tex_file) + die("main()", errno); + + struct stat stat_buf; + int result = fstat(fileno(tex_file), &stat_buf); + if (result == -1) + die("main()", errno); + + char *tex_buf = (char *)xmalloc((size_t)stat_buf.st_size + 1); + if (fread(tex_buf, 1, (size_t)stat_buf.st_size, tex_file) != (size_t)stat_buf.st_size) + die("main()", errno); + + tex_buf[stat_buf.st_size] = 0; + + fclose(tex_file); + + + + + return EXIT_SUCCESS; +}