-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
163 lines (144 loc) · 4.31 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "punkty.h"
#include "funkcje_trygonometryczne.h"
#include "aproksymator_f_trygonometrycznymi.h"
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
char *usage =
"Usage: %s -s spline-file [-p points-file] [ -g gnuplot-file [-f from_x -t to_x -n n_points ] ]\n"
" if points-file is given then\n"
" reads discrete 2D points from points-file\n"
" writes spline approximation to spline-file\n"
" - number of points should be >= 4\n"
" else (points-file not given)\n"
" reads spline from spline-file\n"
" endfi\n"
" if gnuplot-file is given then\n"
" makes table of n_points within <from_x,to_x> range\n"
" - from_x defaults to x-coordinate of the first point in points-file,\n"
" - to_x defaults to x-coordinate of the last point\n"
" - n_points defaults to 100\n"
" - n_points must be > 1\n"
" endif\n";
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];
punkty_t pts;
parametry_funkcji_t par;
pts.n = 0;
par.n = 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 (czytaj_bledne_punkty (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 spline file: %s\n\n", argv[0], out);
exit (EXIT_FAILURE);
}
oblicz_a_b (&pts, &par);
if( par.n > 0 )
wypisz_parametry (&par, ouf);
fclose (ouf);
} else if (out != NULL) { /* if point-file was NOT given, try to read splines from a file */
FILE *parf = fopen (out, "r");
if (parf == NULL) {
fprintf (stderr, "%s: can not read spline file: %s\n\n", argv[0], inp);
exit (EXIT_FAILURE);
}
if (czytaj_parametry (&par, parf)) {
fprintf (stderr, "%s: bad contents of spline file: %s\n\n", argv[0],
inp);
exit (EXIT_FAILURE);
}
} else { /* ponts were not given nor spline was given -> it is an error */
fprintf (stderr, usage, argv[0]);
exit (EXIT_FAILURE);
}
if (par.n < 1) { /* check if there is a valid spline */
fprintf (stderr, "%s: bad spline: n=%d\n\n", argv[0], par.n);
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 if( par.n > 1 ) {
fromX= par.x[0];
toX= par.x[par.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, wartosc_wielomianu (&par, fromX + i * dx));
}
fclose (gpf);
}
return 0;
}