-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsFunc.c
360 lines (334 loc) · 13.6 KB
/
CsFunc.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/* CsFunc.c*/
#include <stdio.h>
#include <stdlib.h> //atoi
#include <string.h>
#include <math.h>
#include <conio.h> //äëÿ getch
#include "CsFunc.h"
#include "CsPlotFunc.h"
#define DATACOUNT 13 //÷èñëî ââîäèìûõ âåëè÷èí
void TxtFromFile(char *filename, char *strcatn) //filename èìÿ èëè ïóòü ê ôàéëó. strcatn áóôåð, êóäà ïîìåùàåòñÿ âåñü ôàéë öåëèêîì
{
int i=0; //ñ÷åò÷èê ñòðîê
char buff[2048]={0}; //áóôåð äëÿ ñ÷èòûâàíèÿ ñòðîêè
FILE *fin=fopen(filename, "rb");
if(fin!=NULL)
{
while (!feof (fin))
{
if ( !feof(fin)) // äëÿ êîððåêöèè èçâåñòíîé îøèáêè ðàáîòû feof
{
i=i+1;
fgets(buff, 127, fin);
strcat(strcatn, buff);
// printf("%s string count=%d\n", buff, i);
}
}
// printf("strcatn=%s", strcatn);
}
else printf("TxtFromFile:Cannot open %s", filename);
fclose(fin);
}
void InputIntFromCons(int *a, char *name) // àíàëîãè÷íî InputDoubleFromCons
{
char buff[128];
while(1)
{
printf("Ââåäèòå %s:", name); //printf("Input %s:", name);
fgets(buff, 127, stdin); //
char *c = strchr(buff, '\n'); /* Èùåì ñèìâîë ‘\n’ â ïðî÷èòàííîé ñòðîêå */
if(c != NULL) *c = '\0'; /* Çàïèñü ñèìâîëà ‘\0’âìåñòî ñèìâîëà ‘\n’ */
int i=strspn(buff, "+-1234567890"); /* äëèíà íà÷àëüíîé ïîäñòðîêè, ñ óêàçàííûìè ñèìâîëàìè strspn substring pointer number */
if(i==strlen(buff))
{
*a=atoi(buff);
return;
}
else printf("Íåäîïóñòèìûå ñèìâîëû! Ââåäèòå %s çàíîâî!\n", name);//printf("Unacceptable symbols! Input %s again!\n", name);
}
}
void InputDoubleFromCons(double *a, char *name)
{
char buff[128];
while(1)
{
printf("Ââåäèòå %s:", name); //printf("Input %s:", name);
fgets(buff, 127, stdin); //
char *c = strchr(buff, '\n'); /* Èùåì ñèìâîë ‘\n’ â ïðî÷èòàííîé ñòðîêå */
if(c != NULL) *c = '\0'; /* Çàïèñü ñèìâîëà ‘\0’âìåñòî ñèìâîëà ‘\n’ */
int i=strspn(buff, "eE+-.1234567890"); /* äëèíà íà÷àëüíîé ïîäñòðîêè, ñ óêàçàííûìè ñèìâîëàìè strspn substring pointer number */
if(i==strlen(buff))
{
*a=atof(buff);
return;
}
else printf("Íåäîïóñòèìûå ñèìâîëû! Ââåäèòå %s çàíîâî!\n", name); //printf("Unacceptable symbols! Input %s again!\n", name);
}
}
/* ïóíêò 1 ìåíþ */
void InputDataFromCons(int *N, double *U, double *tstart, double *t1, double *t2, double *tend, double *a, double *b, double *Uin1, double *Uin2, double *C, double *Eps, int *Nmax)
{
InputIntFromCons(N, "N");
InputDoubleFromCons(U, "U");
InputDoubleFromCons(tstart, "tstart");
InputDoubleFromCons(t1, "t1");
InputDoubleFromCons(t2, "t2");
InputDoubleFromCons(tend, "tend");
InputDoubleFromCons(a, "a");
InputDoubleFromCons(b, "b");
InputDoubleFromCons(Uin1, "Uin1");
InputDoubleFromCons(Uin2, "Uin2");
InputDoubleFromCons(C, "C");
InputDoubleFromCons(Eps, "Eps");
InputIntFromCons(Nmax, "Nmax");
}
/* ïóíêò 2 ìåíþ */
void InputDataFromFile(int *N, double *U, double *tstart, double *t1, double *t2, double *tend, double *a, double *b, double *Uin1, double *Uin2, double *C, double *Eps, int *Nmax, char *filename)
{
int i=0, j=0, k; //i ñ÷åò÷èê ñòðîê j ñ÷åò÷èê ñòðîê áåç êîììåíòàðèåâ
char smallbuff[2048]={0}; //áóôåð äëÿ îäíîé ñòðîêè
char buff[DATACOUNT][2048]={0}; //áóôåð äëÿ âñåõ ñòðîê áåç çíàêà #
FILE *fin=fopen(filename, "rb");
if(fin!=NULL)
{
while (!feof (fin))
{
if ( !feof(fin)) // äëÿ êîððåêöèè èçâåñòíîé îøèáêè ðàáîòû feof
{
i=i+1;
fgets(smallbuff, 127, fin);
char *c=strchr(smallbuff, '\n'); // óáèðàåì '\n' â êîíöå ñòðîêè.
if (c!=NULL) *c='\0';
if(smallbuff[0]!='#')
{
strcpy(buff[j], smallbuff);
j++;
if(j>=DATACOUNT) break; //åñëè ÷èñëî ñòðîê áåç êîììåíòàðèåâ (áîëüøå èëè ðàâíî) ÷èñëó àðãóìåíòîâ.
}
// printf("%s string count=%d\n", buff, i);
}
}
// printf("strcatn=%s", strcatn);
for(k=0; k<j; k++) printf("buff[%d][]=%s\n", k, buff[k]); //ïðîâåðêà òîãî, ÷òî îêàçàëîñü â ìàññèâå ñòðîê
/* ñ÷èòûâàåì èç ìàññèâà ñòðîê */
*N=atoi(buff[0]);
*U=atof(buff[1]);
*tstart=atof(buff[2]);
*t1=atof(buff[3]);
*t2=atof(buff[4]);
*tend=atof(buff[5]);
*a=atof(buff[6]);
*b=atof(buff[7]);
*Uin1=atof(buff[8]);
*Uin2=atof(buff[9]);
*C=atof(buff[10]);
*Eps=atof(buff[11]);
*Nmax=atoi(buff[12]);
}
else printf("TxtFromFile:Cannot open %s", filename);
fclose(fin);
}
/* ïóíêò 3 ìåíþ */
void InputDataFromFileForPlots(int *N, double *tend, double **ArrT, double **ArrUin, double **ArrUout, char *filename, int *FormArrFlag)
{
*FormArrFlag=1;
int i=0, j=0, k; //i ñ÷åò÷èê ñòðîê j ñ÷åò÷èê ñòðîê áåç êîììåíòàðèåâ
char smallbuff[2048]={0}; //áóôåð äëÿ îäíîé ñòðîêè
// char buff[DATACOUNT][2048]={0}; //áóôåð äëÿ âñåõ ñòðîê áåç çíàêà #
FILE *fin=fopen(filename, "rb");
if(fin!=NULL)
{
while (!feof (fin)) //òóò ñ÷èòûâàåì N
{
if ( !feof(fin)) // äëÿ êîððåêöèè èçâåñòíîé îøèáêè ðàáîòû feof
{
i=i+1;
fgets(smallbuff, 127, fin);
char *c=strchr(smallbuff, '\n'); // óáèðàåì '\n' â êîíöå ñòðîêè.
if (c!=NULL) *c='\0';
if(smallbuff[0]!='#')
{
sscanf(smallbuff, "%d", N);
break;
}
}
}
printf("N=%d\n", *N);
free(*ArrT); //Îñâîáîæäàåì ïàìÿòü, åñëè ðàíüøå îíà óæå áûëà âûäåëåíà
free(*ArrUin);
free(*ArrUout);
*ArrT=(double *)malloc(sizeof(double)*(*N+1)); //Âûäåëÿåì ïàìÿòü
*ArrUin=(double *)malloc(sizeof(double)*(*N+1));
*ArrUout=(double *)malloc(sizeof(double)*(*N+1));
while (!feof (fin)) //òóò ÷èòàåì â ìàññèâû ArrT ArrUin ArrUout
{
if ( !feof(fin)) // äëÿ êîððåêöèè èçâåñòíîé îøèáêè ðàáîòû feof
{
i=i+1;
fgets(smallbuff, 127, fin);
char *c=strchr(smallbuff, '\n'); // óáèðàåì '\n' â êîíöå ñòðîêè.
if (c!=NULL) *c='\0';
if(smallbuff[0]!='#')
{
sscanf(smallbuff, "%*d%lf%lf%lf", &(*ArrT)[j], &(*ArrUin)[j], &(*ArrUout)[j]); //ïåðâûé àðãóìåíò ýòî íîìåð, åãî îòáðàñûâàåì
j++;
if(j>=(*N+1)) break; //åñëè ÷èñëî ñòðîê áåç êîììåíòàðèåâ (áîëüøå èëè ðàâíî) ÷èñëó ýëåìåíòîâ.
}
// printf("%s string count=%d\n", buff, i);
}
}
// printf("strcatn=%s", strcatn);
for(k=0; k<j; k++) printf("ArrT[%d]=%lf\tArrUin[%d]=%lf\tArrUout[%d]=%lf\n", k, (*ArrT)[k], k, (*ArrUin)[k], k, (*ArrUout)[k]); //ïðîâåðêà òîãî, ÷òî îêàçàëîñü â ìàññèâå ñòðîê
}
else printf("TxtFromFile:Cannot open %s", filename);
fclose(fin);
/* òóò ïðèñâîèì çíà÷åíèå tend ýòî íóæíî äëÿ ðàáîòû ôóíêèé äëÿ ãðàôèêîâ*/
*tend=(*ArrT)[*N];
printf("tend=%lf\n", *tend);
}
/* ïóíêò 4 ìåíþ */
void FormArrT(int N, double tstart, double tend, double **ArrT, double *dt) // óêàçàòåëü íà óêàçàòåëü íóæåí, ÷òîáû ìû ìîãëè èçìåíÿòü óêàçàòåëü (èíà÷å îí ïðîñòî ñêîïèðóåòñÿ, è íè÷åãî íå ïðîèçîéäåò)
{
int i;
double currT=tstart;
free(*ArrT); //Åñëè ìû âûçûâàåì ôóíêöèþ ïîâòîðíî, òî ïðîøëóþ îáëàñòü ïàìÿòè íóæíî îñâîáîäèòü
*ArrT=(double *)malloc(sizeof(double)*(N+1)); //ðàçûìåíîâûâàåì óêàçàòåëü íà óêàçàòåëü è ñâÿçûâàåì ñ ïàìÿòüþ
*dt=(tend-tstart)/N;
for(i=0;i<(N+1);i++)
{
// *(*ArrT+i)=currT; // òàê ïðèñâîèòü ýëåìåíòó ìàññèâà çíà÷åíèå òîæå ìîæíî. ýòà ñòðîêà è ñòðîêà íèæå ýêâèâàëåíòíû äðóã äðóãó ïî ñìûñëó
(*ArrT)[i]=currT; //ðàçûìåíîâûâàåì
currT=currT+*dt;
}
// for(i=0;i<(N+1);i++) printf("(*ArrT)[%d]=%lf\n", i, (*ArrT)[i]); //ïðîâåðêà
}
void FormArrUin(int N, double U, double tstart, double t1, double t2, double tend, double *ArrT, double **ArrUin)
{
int i;
free(*ArrUin);
*ArrUin=(double *)malloc(sizeof(double)*(N+1));
for(i=0;i<(N+1);i++)
{
if((tstart<=ArrT[i]) && (ArrT[i]<=t1)) (*ArrUin)[i]=0;
if((t1<ArrT[i]) && (ArrT[i]<=t2)) (*ArrUin)[i]=U/(t2-t1)*(ArrT[i])-U*t1/(t2-t1);
if((t2<ArrT[i]) && (ArrT[i]<=tend)) (*ArrUin)[i]=U/(t2-tend)*(ArrT[i])-U*tend/(t2-tend);
}
// for(i=0;i<(N+1);i++) printf("(*ArrUin)[%d]=%lf\n", i, (*ArrUin)[i]); //ïðîâåðêà
}
void FormArrUout(int N, double a, double b, double Uin1, double Uin2, double *ArrUin, double **ArrUout, int *FormArrFlag)
{
*FormArrFlag=1; //óñòàíàâëèâàåì ôëàã
int i;
free(*ArrUout);
*ArrUout=(double *)malloc(sizeof(double)*(N+1));
for(i=0;i<(N+1);i++)
{
if(ArrUin[i]<=Uin1) (*ArrUout)[i]=a*Uin1+b;
if((Uin1<ArrUin[i])&&(ArrUin[i]<=Uin2)) (*ArrUout)[i]=a*ArrUin[i]+b;
if(ArrUin[i]>Uin2) (*ArrUout)[i]=a*Uin2+b;
}
// for(i=0;i<(N+1);i++) printf("(*ArrUout)[%d]=%lf\n", i, (*ArrUout)[i]); //ïðîâåðêà
//for(i=0;i<(N+1);i++) printf("FormArrUout:(*ArrUin)[%d]=%lf\n", i, ArrUin[i]); //ïðîâåðêà
}
/* ïóíêò 5 ìåíþ */
void CalcW(int *N, double U, double tstart, double t1, double t2, double tend, double a, double b, double Uin1, double Uin2, double C, double Eps, int Nmax, double **ArrT, double **ArrUin, double **ArrUout, double *dt, int *FormArrFlag, double *W, double *MeaErW, int DebFlag)
{
int Nstart=*N; // ïîíàäîáèòñÿ äëÿ îïðåäåëåíèÿ, ïîñ÷èòàíà ëè W õîòÿ áû 1 ðàç è õîòÿ áû 2 ðàçà
*MeaErW=Eps+1; // ÷òîáû ïåðâûé ðàç çàéòè â öèêë
double Wold;
double Umax, CUmax; //CUmax ÷èñòî äëÿ íàãëÿäíîñòè
int i; //ñ÷åò÷èê
int K; //âåëè÷èíà äëÿ íàõîæäåíèÿ ïîãðåøíîñòè
while((*N<Nmax) &&(*MeaErW>Eps))
{
FormArrT(*N, tstart, tend, ArrT, dt);
FormArrUin(*N, U, tstart, t1, t2, tend, *ArrT, ArrUin);
FormArrUout(*N, a, b, Uin1, Uin2, *ArrUin, ArrUout, FormArrFlag);
/* Íàõîäèì Umax*/
Umax=(*ArrUout)[0]; //èíèöèàëèçèðóåì Umax
for(i=0;i<(*N+1);i++) if(Umax<(*ArrUout)[i]) Umax=(*ArrUout)[i];
CUmax=C*Umax;
/* Íàõîäèì K*/
K=0;
for(i=0;i<(*N+1);i++) if((*ArrUout)[i]>CUmax) K++;
/* Íàõîäèì W*/
Wold=*W; //Ñîõðàíÿåì ñòàðîå çíà÷åíèå W
*W=K/(double)(*N+1);
/* Íàõîäèì MeaErW*/
if(*N!=Nstart) *MeaErW=fabs(*W-Wold);
/* Âûâîä äëÿ ïðîâåðêè*/
OutputResultToCons(*N, *ArrT, *ArrUin, *ArrUout, *dt);
printf("K=%d\n", K);
printf("Umax=%lf\n", Umax);
printf("CUmax=%lf\n", CUmax);
printf("N=%d\n", *N);
printf("W=%lf\n", *W);
printf("MeaErW=%lf\n", *MeaErW);
if(DebFlag==1) //äëÿ ðåæèìà Îòëàäêè
{
printf("Íàæìèòå ëþáóþ êëàâèøó äëÿ ïðîäîëæåíèÿ\n");
getch();
}
*N=*N*2;
}
if(*N!=Nstart) *N=*N/2; //Êîìïåíñèðóåì ïîñëåäíåå ó÷åëè÷åíèå N
/* Âûâîäèì ðåçóëüòàò */
if(*MeaErW<Eps) printf("Õàðàêòåðèñòèêà \t\t\t\tW=%lf\n"
"Íàéäåíà ñ îòíîñèòåëüíîé ïðîãðåøíîñòüþ \tMeaErW=%lf<Eps=%lf\n"
"Ïðè ÷èñëå ïðîìåæóòêîâ \t\t\tN=%d\n", *W, *MeaErW, Eps, *N);
else printf("Òðåáóåìàÿ òî÷íîñòü íå äîñòèãíóòà!\n");
}
/* ïóíêò 6 ìåíþ */
void OutputResultToCons(int N, double *ArrT, double *ArrUin, double *ArrUout, double dt)
{
int i;
printf("¹ ï/ï\tt\t\tUin\t\tUout\n");
for(i=0;i<(N+1);i++) printf("%d\t%lf\t%lf\t%lf\n", i, ArrT[i], ArrUin[i], ArrUout[i]);
printf("dt=%lf\n", dt);
// printf("W=\n");
}
/* ïóíêò 7 ìåíþ */
void OutputResultToFile(int N, double *ArrT, double *ArrUin, double *ArrUout, double dt, char *filename)
{
int i;
FILE *fout=fopen(filename, "w");
if(fout!=NULL)
{
fputs("#¹ ï/ï\tt\t\tUin\t\tUout\n", fout);
fprintf(fout, "%d\n", N); //Ïèøåì N ýòî íåîáõîäèìî äëÿ ìåíþ ïóíêò 3
for(i=0;i<(N+1); i++)
{
fprintf(fout, "%d\t%lf\t%lf\t%lf\n", i, ArrT[i], ArrUin[i], ArrUout[i]);
}
fprintf(fout,"#dt=%lf\n", dt);
}
else printf("OutputResultToCons:Cannot open %s", filename);
fclose(fout);
}
/* ïóíêò 8 ìåíþ */
void OutputWToFile(int N, double W, double MeaErW, char *filename)
{
FILE *fout=fopen(filename, "a");
if(fout!=NULL) fprintf(fout, "#Õàðàêòåðèñòèêà \t\t\t\tW=%lf\n"
"#Íàéäåíà ñ îòíîñèòåëüíîé ïðîãðåøíîñòüþ \tMeaErW=%lf\n"
"#Ïðè ÷èñëå ïðîìåæóòêîâ \t\t\tN=%d\n", W, MeaErW, N);
else printf("Íå óäàëîñü îòêðûòü %s", filename);
fclose(fout);
}
/* ïóíêò 13 ìåíþ */
void DebOutputCurrentToCons(int N, double U, double tstart, double t1, double t2, double tend, double a, double b, double Uin1, double Uin2, double C, double Eps, int Nmax)
{
printf("N=\t%d\n", N);
printf("U=\t%lf\n", U);
printf("tstart=\t%lf\n", tstart);
printf("t1=\t%lf\n", t1);
printf("t2=\t%lf\n", t2);
printf("tend=\t%lf\n", tend);
printf("a=\t%lf\n", a);
printf("b=\t%lf\n", b);
printf("Uin1=\t%lf\n", Uin1);
printf("Uin2=\t%lf\n", Uin2);
printf("C=\t%lf\n", C);
printf("Eps=\t%lf\n", Eps);
printf("Nmax=\t%d\n", Nmax);
}