#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "modules.h"

int main() {
	FILE* fp;
	FILE* fg;
	double a, b;
	int n;
	char cheb[2][6] = { "","cheb_" };
	char funcc[2][3] = { "fs","fg" };
	char path_grid[100];
	char path_poly[100];
	double** h;
	double* xh;
	double* yh;
	double* sd;
	for (int l = 0;l < -1;l++) {
		for (int t = 0;t < 2;t++) {
			
			if (t == 0) {
				a = -2;
				b = 4;
			}
			else {
				a = 0;
				b = 5;
			}
			for (int n = 5;n <= 100;n += 1) {
				if (t == 0 && l == 0) {
					h = generate_chebgrid(f_smooth, n, a, b);
				}
				else if (t == 0 && l == 1) {
					h = generate_ugrid(f_smooth, n, a, b);
				}
				else if (t == 1 && l == 0) {
					h = generate_chebgrid(f_gap, n, a, b);
				}
				else {
					h = generate_ugrid(f_gap, n, a, b);
				}
				xh = malloc(sizeof(double) * n);
				yh = malloc(sizeof(double) * n);



				snprintf(path_poly, 100, "lab8_interpol1/%spoly_%s_%d.txt", cheb[l], funcc[t], n);
				snprintf(path_grid, 100, "lab8_interpol1/%sgrid_%s_%d.txt", cheb[l], funcc[t], n);



				printf("%s\n", path_poly);
				fg = fopen(path_grid, "w+");
				if (fg == NULL) {
					exit(10);
				}
				fp = fopen(path_poly, "w+");
				if (fp == NULL) {
					exit(10);
				}

				for (int i = 0;i < n;i++) {
					fprintf(fg, "%.25lf %.25lf", h[i][0], h[i][1]);
					if (i != n - 1) {
						fprintf(fg, "\n");
					}
				}
				fclose(fg);

				for (int i = 0; i < n;i++) {
					xh[i] = h[i][0];
					yh[i] = h[i][1];
				}
				sd = sep_difference(xh, yh, n);
				for (long double x = a;x < b - (b - a) / 10000;x += (b - a) / 10000) {
					fprintf(fp, "%lf %Lf", x, poly_newton(x, xh, yh, n, sd));
					if (x <= b - (b - a) / 5000) {
						fprintf(fp, "\n");
					}
				}
				fclose(fp);
			}
		}
	}
	for (int m_e = 1;m_e <= 0;m_e++) {
		for (int l = 0;l < 2;l++) {
			for (int t = 0;t < 2;t++) {

				if (t == 0) {
					a = -2;
					b = 4;
				}
				else {
					a = 0;
					b = 5;
				}
				n = 10;

				if (t == 0 && l == 0) {
					h = generate_chebgrid(f_smooth, n, a, b);
				}
				else if (t == 0 && l == 1) {
					h = generate_ugrid(f_smooth, n, a, b);
				}
				else if (t == 1 && l == 0) {
					h = generate_chebgrid(f_gap, n, a, b);
				}
				else {
					h = generate_ugrid(f_gap, n, a, b);
				}
				h = grid_change(h, n, (double)m_e / 100);
				xh = malloc(sizeof(double) * n);
				yh = malloc(sizeof(double) * n);

				snprintf(path_poly, 100, "lab8_interpol_a/%spoly_%s_%d.txt", cheb[l], funcc[t], m_e);
				snprintf(path_grid, 100, "lab8_interpol_a/%sgrid_%s_%d.txt", cheb[l], funcc[t], m_e);

				printf("%s\n", path_poly);
				fg = fopen(path_grid, "w+");
				if (fg == NULL) {
					exit(10);
				}
				fp = fopen(path_poly, "w+");
				if (fp == NULL) {
					exit(10);
				}

				for (int i = 0;i < n;i++) {
					fprintf(fg, "%.25lf %.25lf", h[i][0], h[i][1]);
					if (i != n - 1) {
						fprintf(fg, "\n");
					}
				}
				fclose(fg);

				for (int i = 0; i < n;i++) {
					xh[i] = h[i][0];
					yh[i] = h[i][1];
				}
				sd = sep_difference(xh, yh, n);
				for (long double x = a;x < b - (b - a) / 10000;x += (b - a) / 10000) {
					fprintf(fp, "%lf %Lf", x, poly_newton(x, xh, yh, n, sd));
					if (x <= b - (b - a) / 5000) {
						fprintf(fp, "\n");
					}
				}
				fclose(fp);

			}
		}
	}
	free(h);
	free(xh);
	free(yh);
	free(sd);

	return 0;
}

