Add Example
This commit is contained in:
parent
07e56839c7
commit
768df4b636
7 changed files with 199 additions and 0 deletions
42
2025.04.18/Example/Makefile
Normal file
42
2025.04.18/Example/Makefile
Normal file
|
@ -0,0 +1,42 @@
|
|||
WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \
|
||||
-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \
|
||||
-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \
|
||||
-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \
|
||||
-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \
|
||||
-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \
|
||||
-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \
|
||||
-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \
|
||||
-Wmissing-field-initializers -Wpointer-sign
|
||||
|
||||
CFLAGS = -std=gnu99 -mfpmath=sse -O3
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
EXE = exe
|
||||
CLEAN = del
|
||||
LFLAGS = -lssp -lm
|
||||
else
|
||||
EXE = out
|
||||
CLEAN = rm -f
|
||||
LFLAGS = -lm
|
||||
endif
|
||||
|
||||
TARGET = a01.$(EXE)
|
||||
OBJ = main.o solve.o array_io.o
|
||||
|
||||
%.o: %.c
|
||||
gcc $(CFLAGS) $(WFLAGS) -c $< -o $@
|
||||
|
||||
$(TARGET): $(OBJ)
|
||||
gcc $^ -o $@ $(LFLAGS)
|
||||
|
||||
# Отладочная сборка (gdb)
|
||||
gdb: CFLAGS = -mfpmath=sse -std=gnu99 -g -O0
|
||||
gdb: clean $(TARGET)
|
||||
|
||||
# Профилировочная сборка (gprof)
|
||||
prof: CFLAGS = -std=gnu99 -mfpmath=sse -pg -O3
|
||||
prof: LFLAGS = -lm -pg
|
||||
prof: clean $(TARGET)
|
||||
|
||||
clean:
|
||||
$(CLEAN) *.o *$(EXE)
|
49
2025.04.18/Example/array_io.c
Normal file
49
2025.04.18/Example/array_io.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "array_io.h"
|
||||
|
||||
io_status read_values (double * restrict X, double * restrict Y, const int n, const char * restrict name)
|
||||
{
|
||||
FILE *fp;
|
||||
if (!(fp = fopen(name, "r")))
|
||||
return ERROR_OPEN;
|
||||
|
||||
for (int i = 0; i < n; ++i)
|
||||
if (fscanf(fp, "%lf %lf", X + i, Y + i) != 1)
|
||||
{ fclose(fp); return ERROR_READ; }
|
||||
|
||||
fclose(fp);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
void print_values (const double * restrict X, const double * restrict Y, const int n, const int p)
|
||||
{
|
||||
int np = (n > p ? p : n);
|
||||
|
||||
for (int i = 0; i < np; i++)
|
||||
printf("f(%lf) = %lf\n");
|
||||
}
|
||||
|
||||
int read_or_init_matrix(double *a, char *name, int k, int n)
|
||||
{
|
||||
if (name)
|
||||
{ /* из файла */
|
||||
io_status ret;
|
||||
ret = read_matrix(a, n, name);
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
printf("Cannot open %s\n", name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
printf("Cannot read %s\n", name);
|
||||
}
|
||||
return 3;
|
||||
} while (0);
|
||||
} else init_matrix(a, n, k);
|
||||
|
||||
return 0;
|
||||
}
|
7
2025.04.18/Example/array_io.h
Normal file
7
2025.04.18/Example/array_io.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#ifndef ARRAY_IO_H
|
||||
#define ARRAY_IO_H
|
||||
|
||||
io_status read_values (double * restrict X, double * restrict Y, const int n, const char * restrict name);
|
||||
void print_values (const double * restrict X, const double * restrict Y, const int n, const int p);
|
||||
|
||||
#endif
|
15
2025.04.18/Example/io_status.h
Normal file
15
2025.04.18/Example/io_status.h
Normal file
|
@ -0,0 +1,15 @@
|
|||
#ifndef IO_STATUS_H
|
||||
#define IO_STATUS_H
|
||||
|
||||
#define ERR_MEM "Error: Not enough memory!"
|
||||
#define ERR_OPEN "Error: Cannot open file"
|
||||
#define ERR_READ "Error: Cannot read file"
|
||||
|
||||
typedef enum _io_status
|
||||
{
|
||||
SUCCESS,
|
||||
ERROR_OPEN,
|
||||
ERROR_READ
|
||||
} io_status;
|
||||
|
||||
#endif
|
74
2025.04.18/Example/main.c
Normal file
74
2025.04.18/Example/main.c
Normal file
|
@ -0,0 +1,74 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "array_io.h"
|
||||
#include "io_status.h"
|
||||
#include "matrix.h"
|
||||
#include "solve.h"
|
||||
|
||||
/* ./a.out x_0 n filename */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
double x_0, t, *X = 0, *Y = 0;
|
||||
int n, task = 1;
|
||||
char *name = 0;
|
||||
io_status ret;
|
||||
|
||||
if (
|
||||
!((argc == 4) &&
|
||||
sscanf(argv[1], "%e", &x_0) == 1 &&
|
||||
sscanf(argv[2], "%d", &n) == 1) &&
|
||||
(name = argv[3] && !name)
|
||||
) {
|
||||
fprintf(stderr, "Usage: %s x_0 n filename\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO: Удалить вывод в stderr
|
||||
X = (double *)malloc(n * sizeof(double));
|
||||
if (!X)
|
||||
{
|
||||
fprintf(stderr, "%s\n", ERR_MEM);
|
||||
return 2;
|
||||
}
|
||||
|
||||
Y = (double *)malloc(n * sizeof(double));
|
||||
if (!Y)
|
||||
{
|
||||
free(X);
|
||||
fprintf(stderr, "%s\n", ERR_MEM);
|
||||
return 2;
|
||||
}
|
||||
|
||||
ret = read_values(X, Y, n);
|
||||
do {
|
||||
switch (ret)
|
||||
{
|
||||
case SUCCESS:
|
||||
continue;
|
||||
case ERROR_OPEN:
|
||||
fprintf(stderr, "%s '%s'!\n", ERR_OPEN, name);
|
||||
break;
|
||||
case ERROR_READ:
|
||||
fprintf(stderr, "%s '%s'!\n", ERR_READ, name);
|
||||
break;
|
||||
}
|
||||
|
||||
free(X);
|
||||
free(Y);
|
||||
|
||||
return 3;
|
||||
} while (0);
|
||||
|
||||
t = clock();
|
||||
r = t1_solve(x_0, n, X, Y);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
printf("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, r, t);
|
||||
|
||||
free(X);
|
||||
free(Y);
|
||||
|
||||
return 0;
|
||||
}
|
6
2025.04.18/Example/solve.c
Normal file
6
2025.04.18/Example/solve.c
Normal file
|
@ -0,0 +1,6 @@
|
|||
#include "solve.c"
|
||||
|
||||
double t1_solve (const int x_0, const int n, const double * restrict X, const double * restrict Y)
|
||||
{
|
||||
return 0;
|
||||
}
|
6
2025.04.18/Example/solve.h
Normal file
6
2025.04.18/Example/solve.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
double t1_solve (const int x_0, const int n, const double * restrict X, const double * restrict Y);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue