вычисление среднего для знаковых целых и массивов (ну и парсер TeX немного)

--HG--
rename : c/tex_parser/get_tags.sh => cpp/tex_parser/get_tags.sh
rename : c/tex_parser/tables4.tex => cpp/tex_parser/tables4.tex
rename : c/tex_parser/tables5.tex => cpp/tex_parser/tables5.tex
rename : c/tex_parser/tex_table_class.hpp => cpp/tex_parser/tex_table_class.hpp
rename : c/tex_parser/tex_table_class_tags.hpp => cpp/tex_parser/tex_table_class_tags.hpp
This commit is contained in:
Kolan Sh 2011-06-07 19:08:15 +04:00
parent 08b17e2229
commit 7359589f6d
12 changed files with 205 additions and 18 deletions

78
c/avg_task/avg.c Normal file
View File

@ -0,0 +1,78 @@
#include <stdio.h>
#include <stdlib.h>
//~ 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;
}

54
c/avg_task/avg_arr.c Normal file
View File

@ -0,0 +1,54 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

View File

@ -1,13 +0,0 @@
//~ #include <stdio.h>
//~ #include <cstdlib>
//~ #include <iostream>
#include <unistd.h>
using namespace std;
int main(int argc, char *argv[])
{
return 0;//EXIT_SUCCESS;
}

View File

@ -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)

View File

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

View File

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

View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#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;
}