include stdio include stdlib include iostream include math include con

  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
#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
struct coordinates
{
double x,y;
};
class graph
{
private:
double scope; //masshtab
int x, y; //center coordinates
int tpx, tpy; //temp point x,y
int pointWasSet;
public:
int initGraphics()
{
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode,"d:/subject/bc/BGI");
}
int buildAxis()
{
setcolor(WHITE);
//OY-build
line(x, 0, x, 479);
//OX-build
line(0, y, 639, y);
return 0;
}
int convXY(char t, double coord) //type: x or y
{
switch(t)
{
case 'x': return x + ceil(scope * coord);
case 'y': return y - ceil(scope * coord);
}
}
int setPoint(double px, double py, int color)
{
setcolor(color);
if(pointWasSet == 1) line(tpx,tpy,convXY('x', px), convXY('y', py));
else putpixel(convXY('x', px), convXY('y', py), color);
pointWasSet = 1;
tpx = convXY('x', px);
tpy = convXY('y', py);
//printf("%d %d\n",convXY('x', px),convXY('y', py));
}
int setDivider(double divx, double divy)
{
char t_x[2], t_y[2];
double len = 0.1;
if (divx == 0)
{
gcvt(divy,2,t_y);
line(convXY('x', -len/2),convXY('y', divy),convXY('x', len/2), convXY('y',divy));
outtextxy(convXY('x', -1.5*len),convXY('y',divy),t_y);
}
if (divy == 0);
{
gcvt(divy,2,t_x);
}
}
int buildByArray(coordinates* carray, int howmany, int color)
{
for(int i = 0; i < howmany; i++)
{
setPoint(carray[i].x, carray[i].y, color);
delay(100);
gotoxy(1,30);
//printf(" x = %d | y = %d", convXY('x', carray[i].x), convXY('y', carray[i].y));
}
}
int reset()
{
pointWasSet = 0;
}
graph(int cx, int cy, double scope)
{
if(cx >= 1 && cx <= 639) x = cx;
else x = 320;
if(cy >= 1 && cy <= 479) y = cy;
else y = 240;
this->scope = scope;
pointWasSet = 0;
initGraphics();
buildAxis();
//setDivider(0,1);
double len = 0.1;
outtextxy(convXY('x', -0.1),convXY('y',-0.1),"0");
line(convXY('x', -len/2),convXY('y', 1),convXY('x', len/2), convXY('y',1));
outtextxy(convXY('x', -1.5*len),convXY('y',1),"1");
line(convXY('x', -len/2),convXY('y', 2),convXY('x', len/2), convXY('y',2));
outtextxy(convXY('x', -1.5*len),convXY('y',2),"2");
line(convXY('x', -len/2),convXY('y', 3),convXY('x', len/2), convXY('y',3));
outtextxy(convXY('x', -1.5*len),convXY('y',3),"3");
//----------
line(convXY('x', 1),convXY('y', -len/2),convXY('x', 1), convXY('y',len/2));
outtextxy(convXY('x', 1),convXY('y',-len),"1");
line(convXY('x', 2),convXY('y', -len/2),convXY('x', 2), convXY('y',len/2));
outtextxy(convXY('x', 2),convXY('y',-len),"2");
line(convXY('x', 3),convXY('y', -len/2),convXY('x', 3), convXY('y',len/2));
outtextxy(convXY('x', 3),convXY('y',-len),"3");
}
graph()
{
x = 320;
y = 240;
scope = 1;
pointWasSet = 0;
initGraphics();
buildAxis();
}
~graph()
{
getch();
closegraph();
}
};
int fact(int a)
{
if(a > 0)
{
return a*fact(a-1);
}
return 1;
}
double sum(float x, int i, int a)
{
if (a <= 0) return 0;
return pow(x * log(a), i)/fact(i);
}
int line()
{
printf("+---------------------------------------------+\n");
return 0;
}
int main()
{
clrscr();
int a = 3;
double f = 0;
double e = 0.0001;
double t_sum = 1;
double f_toch = 0;
double a_thing = 0;
coordinates carr[20];
coordinates carr_2[20];
coordinates carr_3[20];
line();
printf("| X | f(x) | f(x) toch | pohybka | N |\n");
line();
for (float x = 0.05, array_iterator = 0; x <= 1.05; x += 0.05, array_iterator++)
{
t_sum = 1;
f = 0;
for(int i = 0; fabs(t_sum) >= e; i++)
{
t_sum = sum(x, i, a);
f += t_sum;
}
f_toch = pow(a,x);
if(f != 0)
{
a_thing = 100 * (f - f_toch)/f;
}
else cout<<"Error: f(x) = 0"<<endl;
carr[array_iterator].x = carr_2[array_iterator].x = x;
carr[array_iterator].y = f;
carr_2[array_iterator].y = f_toch;
printf("| %.2lf | %.6lf | %.6lf | %.5lf | %d |\n", x, f, f_toch, a_thing, i);
line();
}
x = 0.5;
for(int i = 0; sum(x, i, a) >= e; i++)
{
carr_3[i].y = sum(x, i, a);
carr_3[i].x = i;
}
cout<<"Hit any key to build graphs."<<endl;
getch();
//for(int j = 0; j < i; j++)
// cout<<endl<<carr_3[j].x<<" "<<carr_3[j].y<<endl;
graph gr(50,450,150);
gr.buildByArray(carr, 18, 5);
outtextxy(400,5," - f(x) = (x * ln(a))^i / i!");
gr.reset();
gr.buildByArray(carr_2, 18, 10);
outtextxy(400,15," - f(x) = 1/cos(x)");
gr.reset();
gr.buildByArray(carr_3, 5, 15);
outtextxy(400,25," - f(x) = Ai(i)");
return 0;
}