From a470c304199866aa1f3d39ff22ec30734f03d617 Mon Sep 17 00:00:00 2001 From: justanothercatgirl Date: Fri, 6 Dec 2024 15:08:31 +0300 Subject: сделал 8 задание)))) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..01e5ef7 --- /dev/null +++ b/main.c @@ -0,0 +1,89 @@ +#include +#include +#include + +#include +#include +#include + +#define neqs 4 +eqf_t eqfs[] = {f1, f2, f3, f4}; + +void help(const char *program, int exit_code) { + printf("Usage:\n%s --method euler|rk4 [--equations ] [--files ] [--dt ] [--interval ] [--init ]\n", program); + puts( "\t--method, -m:\twhich method to use, euler's method or 4th order Runge-Kutta method. Default: euler\n" + "\t--equations, -e:\tAmount of equations. Optional, default 4\n" + "\t--files, -f:\tSpecifies output files for approximations. Default: f1.data, f2.data, ...\n" + "\t--dt, -t:\tApproximation step. default 0.1\n" + "\t--range, -r:\tInterval of an approximation. defaults to '0 40'\n" + "\t--init, -i:\tInitial conditions for problem at the start of an interval. Default: '0.1 -0.1 0.1 -0.1' ...\n" + "\t--help, -h:\tThis message\n\n" + "Note: -e MUST come before -f and -i. If not done so, amount of files will be determined incorrectly" + ); + if (exit_code < 0) return; + else exit(exit_code); +} + +int main(int argc, char *argv[]) { + i32 i, j, numeq = 4; + f64 dt = 0.1, start = 0, end = 40; + f64 * init = NULL; + const char **files = NULL; + void (*solver)(eqf_t*, f64*, const char**, usz, f64, f64, f64) = difeq_solve_euler; + + for (i = 1; i < argc; ++i) { + if (!strcmp("-h", argv[i]) || !strcmp("--help", argv[i])) + help(*argv, 0); + if (!strcmp("-e", argv[i]) || !strcmp("--equations", argv[i])) + numeq = atoi(argv[++i]); + if (!strcmp("-t", argv[i]) || !strcmp("--dt", argv[i])) + dt = atof(argv[++i]); + if (!strcmp("-r", argv[i]) || !strcmp("--range", argv[i])) { + start = atof(argv[++i]); + end = atof(argv[++i]); + } + if (!strcmp("-i", argv[i]) || !strcmp("--init", argv[i])) { + init = calloc(numeq, sizeof(f64)); + for (j = 0; j < numeq; ++j) init[j] = atof(argv[++i]); + } + if (!strcmp("-f", argv[i]) || !strcmp("--files", argv[i])) { + files = calloc(numeq, sizeof(char*)); + for (j = 0; j < numeq; ++j) files[j] = argv[++i]; + } + if (!strcmp("-m", argv[i]) || !strcmp("--method", argv[i])) { + ++i; + if (!strcmp(argv[i], "euler")) { + solver = difeq_solve_euler; + continue; + } + if (!strcmp(argv[i], "rk4")) { + solver = difeq_solve_RK; + continue; + } + printf("\x1b[91mError:\x1b[0mNo such method \"%s\". Available methods: \"euler\"\"rk4\". see --help for details.\n", argv[i]); + return EXIT_FAILURE; + } + } + + if (numeq != neqs) { + printf("\x1b[91mError:\x1b[0mCan not solve more than 4 equations without changing source code of main."); + return EXIT_FAILURE; + } + if (files == NULL) { + files = calloc(4, sizeof(char*)); + files[0] = "f1.data", files[1] = "f2.data", + files[2] = "f3.data", files[3] = "f4.data"; + } + if (init == NULL) { + init = calloc(4, sizeof (double)); + init[0] = init[2] = 0.1, + init[1] = init[3] = -0.1; + } + + solver(eqfs, init, files, neqs, start, end, dt); + puts("Done"); + + free(init); + free(files); + return EXIT_SUCCESS; +} -- cgit v1.2.3-70-g09d2