Esempio n. 1
0
int main (int argc, char **argv)
{
    int opt;
    char *inp = NULL;
    char *out = NULL;
    char *gpt = NULL;
    double fromX = 0;
    double toX = 0;
    int n = 100;
    char *progname= argv[0];

    points_t pts;
    coefficients_t cof;

    pts.n = 0;
    cof.base = 0;

    /* process options, save user choices */
    while ((opt = getopt (argc, argv, "p:s:g:f:t:n:")) != -1)
    {
        switch (opt)
        {
        case 'p':
            inp = optarg;
            break;
        case 's':
            out = optarg;
            break;
        case 'g':
            gpt = optarg;
            break;
        case 'f':
            fromX = atof (optarg);
            break;
        case 't':
            toX = atof (optarg);
            break;
        case 'n':
            n = atoi (optarg);
            break;
        default:                   /* '?' */
            fprintf (stderr, usage, progname);
            exit (EXIT_FAILURE);
        }
    }
    if( optind < argc )
    {
        fprintf( stderr, "\nBad parameters!\n" );
        for( ; optind < argc; optind++ )
            fprintf( stderr, "\t\"%s\"\n", argv[optind] );
        fprintf( stderr, "\n" );
        fprintf( stderr, usage, progname );
        exit( EXIT_FAILURE );
    }

    /* if points-file was given, then read points, generate spline, save it to file */
    if (inp != NULL)
    {
        FILE *ouf = NULL; /* we shall open it later, when we shall get points */

        FILE *inf = fopen (inp, "r");
        if (inf == NULL)
        {
            fprintf (stderr, "%s: can not read points file: %s\n\n", argv[0], inp);
            exit (EXIT_FAILURE);
        }

        if (read_pts_failed (inf, &pts))
        {
            fprintf (stderr, "%s: bad contents of points file: %s\n\n", argv[0], inp);
            exit (EXIT_FAILURE);
        }
        else
            fclose (inf);

        ouf = fopen (out, "w");
        if (ouf == NULL)
        {
            fprintf (stderr, "%s: can not write coefficients file: %s\n\n", argv[0], out);
            exit (EXIT_FAILURE);
        }

        make_cof (&pts, &cof);

        if( cof.base > 0 )
            write_cof (&cof, ouf);

        fclose (ouf);
    }
    else if (out != NULL)   /* if point-file was NOT given, try to read coefficients from a file */
    {
        FILE *coff = fopen (out, "r");
        if (coff == NULL)
        {
            fprintf (stderr, "%s: can not read coefficients file: %s\n\n", argv[0], inp);
            exit (EXIT_FAILURE);
        }
        if (read_cof (coff, &cof))
        {
            fprintf (stderr, "%s: bad contents of coefficients file: %s\n\n", argv[0], inp);
            exit (EXIT_FAILURE);
        }
    }
    else     /* ponts were not given nor coefficients was given -> it is an error */
    {
        fprintf (stderr, usage, argv[0]);
        exit (EXIT_FAILURE);
    }

    if (cof.base < 1)   /* check if there are a valid coefficients */
    {
        fprintf (stderr, "%s: bad coefficients: base=%d\n\n", argv[0], cof.base - 1);
        exit (EXIT_FAILURE);
    }

    /* check if plot was requested and generate it if yes */
    if (gpt != NULL && n > 1)
    {
        FILE *gpf = fopen (gpt, "w");
        int i;
        double dx;
        if( fromX == 0 && toX == 0 )   /* calculate plot range if it was not specified */
        {
            if( pts.n > 1 )
            {
                fromX= pts.x[0];
                toX=   pts.x[pts.n-1];
            }
            else
            {
                fromX= 0;
                toX= 1;
            }
        }
        dx = (toX - fromX) / (n - 1);

        if (gpf == NULL)
        {
            fprintf (stderr, "%s: can not write gnuplot file: %s\n\n", argv[0], gpt);
            exit (EXIT_FAILURE);
        }

        for (i = 0; i < n; i++)
            fprintf (gpf, "%g %g\n", fromX + i * dx, value_cof (&cof, fromX + i * dx));

        fclose (gpf);
    }

    return 0;
}
Esempio n. 2
0
int main (int argc, char **argv) {
	int opt;								
	char *in = NULL;
	char *gnuplot = NULL;
	double odX = 0;
	double doX = 0;
	int n = 100; 	/* domyślne ustawienie potrzebne do działania programu */
	double *dane_a = NULL;
	double *dane_b = NULL;

	points_t pts;
	pts.n = 0;

	if (argv[1] == NULL) {
		fprintf (stderr, uzycie, argv[0]);
	}

	while ((opt = getopt (argc, argv, "p:g:o:d:n:")) != -1) { 		/* opcje działanie + zapisywanie wyborów użytkownika */
		switch (opt) {
		case 'p':
			in = optarg;
			break;
		case 'g':
			gnuplot = optarg;
			break;
		case 'o':
			odX = atof (optarg);
			break;
		case 'd':
			doX = atof (optarg);
			break;
		case 'n':
			n = atoi (optarg);
			break;
		default:
			fprintf (stderr, uzycie, argv[0]);
			return EXIT_FAILURE;
		}
	}


	if( optind < argc ) {							
		fprintf( stderr, "\nPodano złe parametry!\n" );
		for( ; optind < argc; optind++ )
			fprintf( stderr, "\t\"%s\"\n", argv[optind] );
		fprintf( stderr, "\n" );
		fprintf( stderr, uzycie, argv[0] );
		return EXIT_FAILURE;
	}


	if (in != NULL) {
		FILE *inf = fopen (in, "r");
		if (inf == NULL) {
			fprintf (stderr, "%s: brak możliwości czytania pliku z danymi: %s\n\n", argv[0], in);
			return EXIT_FAILURE;
		}

		if (read_pts_failed (inf, &pts)) {
			fprintf (stderr, "%s: nieprawidłowa zawartość pliku z danymi: %s\n\n", argv[0], in);
			return EXIT_FAILURE;
		}
		else
			fclose (inf);		


										/*od tego miejsca program "na nowo" */

		if (n > 1 && gnuplot != NULL) {					/* Sprawdzanie czy było żądanie wykresu + ew. rysowanie*/

			FILE *plik_gnuplota = fopen (gnuplot, "w");

			double przetworzony_zakres;					/*wzięte z oryginalnej wersji, delikatnie zmienione; dobrze działa z nową wersją */
			int i = 0;

			przetworzony_zakres = (doX-odX)/(n-1);			
			
			if( doX == 0 && odX == 0 ) { 				/* obliczanie wykresu */
				if( pts.n > 1 ) {
					odX= pts.x[0];
					doX= pts.x[pts.n-1];
				} else {
					odX= 1;
					doX= 2;
				}
			}	
			
			if (plik_gnuplota == NULL) {
				printf ("Nie można stworzyć gnuplot'owego pliku: %s\n\n", gnuplot);
				return EXIT_FAILURE;
			}
	
			dane_a = licz_wartosci_wspolczynnikow_ai(pts);
			dane_b = licz_wartosci_wspolczynnikow_bi(pts);

			for (i = 0; i < n; i++) {
				fprintf (plik_gnuplota, "%f\t%f\n", (odX+i*przetworzony_zakres), licz_wartosci_y(pts, odX + i*przetworzony_zakres, dane_a, dane_b)); /*pisanie do wyznaczonego pliku*/
			}
			
			fclose (plik_gnuplota);
		}
	}

	free (pts.x);
	free (pts.y);
		
	return EXIT_SUCCESS;
}