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
| #include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
#define MAXPOINTS 2000 #define LINEGRAPH
float *x, *y;
int Xe0, Ye0, Xe1, Ye1;
int n; float a, b, ax, ay; char * teststr = "+000000000.000";
void grinit(void)
{
int gd = DETECT,
gm,
errcode;
initgraph(&gd, &gm, "");
errcode = graphresult(); if(errcode!=grOk) {
putsexit(-1); } }
void setlimits(void)
{
Xe0 = textwidth(teststr)+10; Ye0 = 10; Xe1 = getmaxx() - 40
Ye1 = getmaxy() - 2*textheight(teststr)-10;
}
int allocatememory(void)
{
int ret=0; if(coreleft() >= 2*(n+1)*sizeof(float))
{
x=(float*)calloc(n+1,sizeof(float)); y=(float*)calloc(n+1,sizeof(float));
ret = 1; } return ret; }
void freememory(void)
{
free(x);
free(y);
}
float myfunc(float x)
{
float y;
y = sin(x/2)*x;
return y; }
void fillarray(void)
dt = (b-a)/(n-1); for(t=a,i=0;i<n;i++,t+=dt,xp++,yp++)
{
float *xp = x, *yp = y;
float t, dt;
MaxY = -1E20; MinY = -MaxY;
int i;
{
*xp = t;
*yp = myfunc(t);
if(*yp>MaxY)
MaxY = *yp; if(*yp<MinY)
MinY = *yp; } MinX = a;
MaxX = b; }
float xscreen(float x)
{
return Xe0 + (int)((x-MinX)*ax);
}
float yscreen(float y)
{
return Ye1 - (int)((y-MinY)*ay);
}
void drawbg(char * xwrite, char * ywrite, char * gwrite)
{
int i, temp,
xn, yn; float xo[30], yo[30], dt; char s[40];
setcolor(14); setlinestyle(SOLID_LINE, 0, 1); rectangle(Xe0, Ye0, Xe1, Ye1);
setcolor(15);
temp = yscreen(0); if(temp>Ye0 && temp <Ye1) line(Xe0,temp,Xe1,temp); temp = xscreen(0); if(temp>Xe0 && temp<Xe1) line(temp,Ye0,temp,Ye1);
xn = 1 + (int)((Xe1-Xe0)/textwidth(teststr));
if(xn > 9) xn = 9;
yn = 1 + (int)((Ye1-Ye0)/textheight(teststr)/3);
if(yn>11) yn = 11;
dt = (MaxX - MinX) / xn ; xo[0] = MinX; for(i=1;i<xn+1;i++) xo[i]=xo[i-1]+dt;
dt = (MaxY - MinY) / yn; /* создаем оцифровку по оси Y */
yo7[0] = MinY; for(i=1;i<yn+1;i++) yo[i]=yo[i-1]+dt;
settextjustify(CENTER_TEXT,TOP_TEXT);
setcolor(7); /* серый цвет */
for(i=0;i<=xn;i++) {
temp = xscreen(xo[i]); if(i>0 && i<xn) { setlinestyle(DOTTED_LINE, 0, 1);
sprintf(s,"%6.2f",xo[i]);
outtextxy(temp, Ye1 + 3, s); }
settextjustify(RIGHT_TEXT,CENTER_TEXT);
for(i=0;i<=yn;i++) {
temp = yscreen(yo[i]); if(i>0 && i<yn) { setlinestyle(DOTTED_LINE, 0, 1);
line(Xe0, temp, Xe1, temp);
} setlinestyle(SOLID_LINE, 0, 1);
sprintf(s,"%6.2f",yo[i]); outtextxy(Xe0 - 3, temp, s); setlinestyle(SOLID_LINE, 0, 1);
settextjustify(RIGHT_TEXT,TOP_TEXT); outtextxy(Xe1,Ye1+6+textheight(xwrite),xwrite);
settextjustify(LEFT_TEXT,BOTTOM_TEXT);
outtextxy(Xe0, Ye0, ywrite);
settextjustify(CENTER_TEXT,BOTTOM_TEXT);
outtextxy(getmaxx()/2,getmaxy()-3,gwrite);
}
void drawgraph(void)
{
int xs1, ys1, xs2, ys2, i;
setcolor(15); #ifdef LINEGRAPH setlinestyle(SOLID_LINE, 0, 3); xs1 = xscreen(x[0]); ys1 = yscreen(y[0]); for(i=1;i<n;i++) { xs2 = xscreen(x[i]); ys2 = yscreen(y[i]);
line(xs1,ys1,xs2,ys2);
xs1 = xs2; ys1 = ys2;
}
#else
for(i=0;i<n;i++) { putpixel(xscreen(x[i]), yscreen(y[i]), 15);
} #endif }
void do_F1_proc(void)
{
sound(220); delay(300); sound(440) delay(300); sound(660); delay(300); sound(888); delay(300); nosound(); }
void do_else_proc(void)
{
sound(350); delay(500); nosound(); }
void main() {
char gwrite[40], xwrite[40], ywrite[40]; int key, lMore; clrscr();
puts
grinit();
setlimits();
#ifdef LINEGRAPH n=(Xe1 - Xe0)/8;
#else /* иначе */
n=(Xe1 - Xe0)/2; #endif
if(!allocatememory())
{
puts("Недостаточно памяти");
exit(-2);
} a = -3; b = 2;
fillarray();
ax = (Xe1 - Xe0) / (MaxX - MinX);
ay = (Ye1 - Ye0) / (MaxY - MinY);
strcpy(xwrite,"Ось X");
strcpy(ywrite,"Ось Y");
strcpy(gwrite,"Пример вывода графика");
drawbg(xwrite, ywrite, gwrite);
drawgraph();
lMore = 1; do {
while(kbhit()) getch(); key=getch(); if(key==0) key=getch(); switch(key) { case 27 :
lMore = 0; break; case 59 :
do_F1_proc(); break; deafult:
do_else_proc(); break; } } while(lMore);
freememory();
closegraph();
puts("Программа окончила работу");
} |