1. Керниган Б., Ритчи Д. Язык программирования Си. \ Пер. с англ., 3-е изд., испр. - СПб.: Невский Диалект, 2001 – 352 с.
2. Подбельский В.В., Фомин С.Программирование на языке Си. – М:Финансы и статистика, 2003
3. Трофимов С.П. Методические указания по курсовому проектированию по дисциплине «Программирование на языке высокого уровня». Екатеринбург: изд-во УГТУ-УПИ, 2006. 21 с.
Приложение: листинг
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <graphics.h>
#include <float.h>
#include <stdlib.h>
#include <string.h>
#define SDVIG 50
#define DEL 10
#define TWO 2
#define ESC 27
#define Left 75
#define Right 77
#define Up 72
#define Down 80
#define keyw 119
#define keys 115
#define keya 97
#define keyd 100
#define percent 0.1
#define ENTER 13
#define TURN 48
#define minus 45
int ODZflag=0,o=0,counter=0;
int Vvod(float *a,float *b)
{
int i=0,k=0,j=0;
char c,buf[2], *str=(char*)malloc(1);
float tmp;
printf("Welcome to our programm\n");
printf("Please enter the beginning of piece [a,b]\n");
str[0]='\0';
buf[1]='\0';
while(1)
{
c=getch();
if ((c==ENTER)&&((k==0)||((k==1)&&(i==1))))
continue;
if ((c==ENTER)&&(k>0))
break;
if (((c<'0')||(c>'9'))&&(c!='.')&&(c!=minus))
continue;
if ((c==minus)&&(k!=0))
continue;
else if ((c==minus)&&(k==0))
i++;
if ((c=='.')&&((k==0)||((i==1)&&(k==1))||(j==1)))
continue;
else if ((c=='.')&&(j==0))
j++;
k++;
buf[0]=c;
printf("%c",buf[0]);
str=(char*)realloc(str, strlen(str)+TWO);
str[strlen(str)+1]='\0';
str[strlen(str)]=c;
}
sscanf(str,"%f",&(*a));
printf("\n");
free(str);
k=0;
i=0;
j=0;
*str=(char)malloc(1);
printf("Please enter the end of piece [a,b]\n");
str[0]='\0';
buf[1]='\0';
while(1)
{
c=getch();
if ((c==ENTER)&&((k==0)||((k==1)&&(i==1))))
continue;
if ((c==ENTER)&&(k>0))
break;
if (((c<'0')||(c>'9'))&&(c!='.')&&(c!=minus))
continue;
if ((c==minus)&&(k!=0))
continue;
else if ((c==minus)&&(k==0))
i++;
if ((c=='.')&&((k==0)||((i==1)&&(k==1))||(j==1)))
continue;
else if ((c=='.')&&(j==0))
j++;
k++;
buf[0]=c;
printf("%c",buf[0]);
str=(char*)realloc(str, strlen(str)+TWO);
str[strlen(str)+1]='\0';
str[strlen(str)]=c;
}
sscanf(str,"%f",&(*b));
printf("\n");
free(str);
if((*a)>(*b))
{
tmp=(*a);
(*a)=(*b);
(*b)=tmp;
}
printf("Do you like to plot second graph?[y/n]\n");
while(1)
{
c=getch();
if ((c=='y')||(c=='n'))
break;
}
if (c=='y')
return 1;
else
return 0;
}
void Marker(int V,int *fg,int *sg,int *c1,int *c2)
{
char c;
if (V==0)
{
printf("Please choose the marker for the first graph:\n\
1 - points, 2 - strokes, 3 - squares, 4 - stars...\n");
while (1)
{
c=getch();
if (c=='1')
(*fg)=1;
if (c=='2')
(*fg)=TWO;
if (c=='3')
(*fg)=1+TWO;
if (c=='4')
(*fg)=TWO+TWO;
if ((*fg)!=0)
break;
}
printf("Please choose a colour from 1 to 9 for 1st graph...\n");
while(1)
{
c=getch();
if ((c<'1')||(c>'9'))
continue;
c-=TURN;
(*c1)=(int)c;
break;
}
}
else
{
printf("Please choose the marker for the first graph:\n\
1 - points, 2 - strokes, 3 - squares, 4 - stars...\n");
while (1)
{
c=getch();
if (c=='1')
(*fg)=1;
if (c=='2')
(*fg)=TWO;
if (c=='3')
(*fg)=1+TWO;
if (c=='4')
(*fg)=TWO+TWO;
if ((*fg)!=0)
break;
}
printf("Please choose a colour from 1 to 9 for 1st graph...\n");
while(1)
{
c=getch();
if ((c<'1')||(c>'9'))
continue;
c-=TURN;
(*c1)=(int)c;
break;
}
printf("Please choose the marker for the second graph:\n\
1 - points, 2 - strokes, 3 - squares, 4 - stars...\n");
while (1)
{
c=getch();
if (c=='1')
(*sg)=1;
if (c=='2')
(*sg)=TWO;
if (c=='3')
(*sg)=1+TWO;
if (c=='4')
(*sg)=TWO+TWO;
if ((*sg)!=0)
break;
}
printf("Please choose a colour from 1 to 9 for 2nd graph...\n");
while(1)
{
c=getch();
if ((c<'1')||(c>'9'))
continue;
c-=TURN;
(*c2)=(int)c;
break;
}
}
}
float otobr(float a,float b,float x)
{
return getmaxx()/(b-a)*x-a*getmaxx()/(b-a);
}
void Points(float fun,float a,float b,float x,float shift,float k,int c,float A,float B)
{
putpixel(otobr(a,b,x),getmaxy()/TWO-fun*getmaxx()/(B-A)*k+shift,c);
}
void Strokes(float fun,float a,float b,float x,float shift,float k,int c,float A,float B)
{
setcolor(c);
line(otobr(a,b,x)-TWO-1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift,
otobr(a,b,x)+TWO+1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift);
line(otobr(a,b,x),getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift-TWO-1,
otobr(a,b,x),getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift+TWO+1);
// (*x)+=TWO*(b-a)/getmaxx();
}
void Stars(float fun,float a,float b,float x,float shift,float k,int c,float A,float B)
{
setcolor(c);
line(otobr(a,b,x)-TWO-1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift,
otobr(a,b,x)+TWO+1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift);
line(otobr(a,b,x),getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift-TWO-1,
otobr(a,b,x),getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift+TWO+1);
line(otobr(a,b,x)-TWO-1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift-TWO-1,
otobr(a,b,x)+TWO+1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift+TWO+1);
line(otobr(a,b,x)-TWO-1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift+TWO+1,
otobr(a,b,x)+TWO+1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift-TWO-1);
// (*x)+=TWO*TWO*TWO*(b-a)/getmaxx();
}
void Squares(float fun,float a,float b,float x,float shift,float k,int c,float A,float B)
{
setcolor(c);
line(otobr(a,b,x)-TWO-1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift-TWO-1,
otobr(a,b,x)+TWO+1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift-TWO-1);
line(otobr(a,b,x)+TWO+1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift-TWO-1,
otobr(a,b,x)+TWO+1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift+TWO+1);
line(otobr(a,b,x)+TWO+1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift+TWO+1,
otobr(a,b,x)-TWO-1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift+TWO+1);
line(otobr(a,b,x)-TWO-1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift+TWO+1,
otobr(a,b,x)-TWO-1,getmaxy()/TWO-fun*k*getmaxx()/(B-A)+shift-TWO-1);
//(*x)+=TWO*TWO*TWO*(b-a)/getmaxx();
}
float Kswitcher(int j)
{
float tmp;
if(j==0)
tmp=1.0;
else if (j<0)
tmp=1.0+1.0/(float)DEL*j;//(float)((DEL-1))/DEL);
else if (j>0)
tmp=(float)(DEL)/(DEL-1)*j;
return tmp;
}
void Mashtab(float *a,float *b,int *i,float *shift,float *A,float *B,int *j)
{
while(1)
{
if(kbhit())
{
char c=getch();
if(c==0)
{
c=getch();
}
if (c==Left)
{
cleardevice();
(*a)--;
(*A)--;
(*B)--;
(*b)--;
break;
}
else if (c==Right)
{
cleardevice();
(*a)++;
(*b)++;
(*B)++;
(*A)++;
break;
}
else if (c==Up)
{
cleardevice();
(*shift)+=(getmaxx()+1)/DEL;
break;
}
else if (c==Down)
{
cleardevice();
(*shift)-=(getmaxx()+1)/DEL;
break;
}
else if ((c==keya)&&(o<=(DEL/TWO)))
{
o++;
cleardevice();
(*a)-=percent*(*A);
(*b)-=percent*(*B);
break;
}
else if ((c==keyd)&&(o>=-(DEL/TWO)))
{
o--;
cleardevice();
(*a)+=percent*(*A);
(*b)+=percent*(*B);
break;
}
else if ((c==keyw)&&(counter<=(DEL/TWO)))
{
counter++;
cleardevice();
(*j)++;
break;
}
else if ((c==keys)&&(counter>=-(DEL/TWO)))
{
counter--;
cleardevice();
(*j)--;
break;
}
else if (c==ESC)
{
(*i)=1;
break;
}
}
}
}
void IniGraph()
{
/*int gd,gm,err;
detectgraph(&gd,&gm);
err=graphresult();
if (err)
printf("\n%s",grapherrormsg(err));
registerbgidriver(EGAVGA_driver);
initgraph(&gd,&gm,"");
if ((err=graphresult())!=grOk)
printf("\n%s",grapherrormsg(err)); */
int gdriver=DETECT,gmode;
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"");
}
float fun1(float x)
{
return cos(x);
}
float fun2(float x)
{
if (x<0)
{
ODZflag=1;
return 0;
}
else
{
ODZflag=0;
return sqrt(x);
}
}
void SisCoord(float a,float b,float shift,float A,float B,float k)
{
char c[DEL];
float anax,bnax,nullnax,edotr,i,easy,tmp;
tmp=1/k;
anax=0;
bnax=getmaxx();
nullnax=otobr(A,B,0);
edotr=bnax/DEL;
setcolor(WHITE);
if((getmaxy()/TWO+shift<=getmaxy())&&(getmaxy()/TWO+shift>=0))
{
line(anax,getmaxy()/TWO+shift,bnax,getmaxy()/TWO+shift);
if ((nullnax<=bnax)&&(nullnax>=anax))
{
for (i=nullnax;i>=anax;i-=edotr) //ot 0 do a
{
line(i,getmaxy()/TWO+TWO+shift,i,getmaxy()/TWO-TWO+shift);
sprintf(c,"%4.2f",(i-nullnax)*(b-a)/bnax);
outtextxy(i-DEL-TWO,getmaxy()/TWO+DEL+shift,c);
}
for (i=nullnax;i<=bnax;i+=edotr) //ot 0 do b
{
line(i,getmaxy()/TWO+TWO+shift,i,getmaxy()/TWO-TWO+shift);
sprintf(c,"%4.2f",(i-nullnax)*(b-a)/bnax);
outtextxy(i-DEL-TWO,getmaxy()/TWO+DEL+shift,c);
}
for (i=getmaxy()/TWO+shift;i>0-shift;i-=edotr) //ot 0 do y
{
easy=(-i+getmaxy()/TWO)*(B-A)/bnax;
line(nullnax+TWO,i,nullnax-TWO,i);
sprintf(c,"%4.2f",tmp*easy);
outtextxy(nullnax-SDVIG,i-TWO+shift,c);
}
for (i=getmaxy()/TWO+shift;i<getmaxy()-shift;i+=edotr) //ot 0 do -y
{
easy=(-i+getmaxy()/TWO)*(B-A)/bnax;
line(nullnax+TWO,i,nullnax-TWO,i);
sprintf(c,"%4.2f",tmp*easy);
outtextxy(nullnax-SDVIG,i-TWO+shift,c);
}
line(nullnax+TWO,getmaxy(),nullnax-TWO,getmaxy());
line(nullnax+TWO,0,nullnax-TWO,0);
line(nullnax,0,nullnax,getmaxy());
}
else if ((nullnax>bnax)||(nullnax<anax))
{
for(i=anax;i<bnax;i+=edotr) //ot a do b
{
sprintf(c,"%4.2f",a+i*(b-a)/bnax);
line(i,getmaxy()/TWO+TWO+shift,i,getmaxy()/TWO-TWO+shift);
outtextxy(i-DEL-TWO,getmaxy()/TWO+DEL+shift,c);
}
}
}
else if((getmaxy()/TWO+shift>getmaxy())||(getmaxy()/TWO+shift<0))
{
if ((nullnax<=bnax)&&(nullnax>=anax))
{
for (i=getmaxy();i>0;i-=edotr) //ot maxy do 0
{
line(nullnax+TWO,i,nullnax-TWO,i);
sprintf(c,"%4.2f",tmp*(float)((B-A)/bnax*(getmaxy()/TWO-i+shift)));
outtextxy(nullnax-SDVIG,i-TWO,c);
}
line(nullnax+TWO,getmaxy(),nullnax-TWO,getmaxy());
line(nullnax+TWO,0,nullnax-TWO,0);
line(nullnax,0,nullnax,getmaxy());
}
}
}
void main()
{
clrscr();
int i=0,j=0,V,fg=0,sg=0,c1,c2;
float a,b,k,A,B,x,shift=0;
V=Vvod(&a,&b);
Marker(V,&fg,&sg,&c1,&c2);
A=a;
B=b;
IniGraph();
while(i==0)
{
k=Kswitcher(j);
SisCoord(a,b,shift,A,B,k);
for (x=a;x<=b;x+=(DEL*(b-a))/getmaxx())
{
if (x==x+(DEL*getmaxx()+(b-a))/getmaxx())
break;
if (V==0)
{
if(((getmaxy()/TWO-fun1(x)*k*getmaxx()/(B-A)+shift)<=getmaxy())||
((getmaxy()/TWO-fun1(x)*k*getmaxx()/(B-A)+shift)>=0))
{
switch (fg)
{
case 1:
Points(fun1(x),a,b,x,shift,k,c1,A,B);
break;
case 2:
Strokes(fun1(x),a,b,x,shift,k,c1,A,B);
break;
case 3:
Squares(fun1(x),a,b,x,shift,k,c1,A,B);
break;
case 4:
Stars(fun1(x),a,b,x,shift,k,c1,A,B);
break;
default:
printf("Bug!");
getch();
exit(1);
}
}
} //break;
else if (V==1)
{
if(((getmaxy()/TWO-fun1(x)*k*getmaxx()/(B-A)+shift)<=getmaxy())||
((getmaxy()/TWO-fun1(x)*k*getmaxx()/(B-A)+shift)>=0))
{
switch (fg)
{
case 1:
Points(fun1(x),a,b,x,shift,k,c1,A,B);
break;
case 2:
Strokes(fun1(x),a,b,x,shift,k,c1,A,B);
break;
case 3:
Squares(fun1(x),a,b,x,shift,k,c1,A,B);
break;
case 4:
Stars(fun1(x),a,b,x,shift,k,c1,A,B);
break;
default:
printf("Bug!");
getch();
exit(1);
}
}
if ((fun2(x)==0)&&(ODZflag==1))
continue;
if(((getmaxy()/TWO-fun2(x)*k*getmaxx()/(B-A)+shift)<=getmaxy())||
((getmaxy()/TWO-fun2(x)*k*getmaxx()/(B-A)+shift)>=0))
{
switch (sg)
{
case 1:
Points(fun2(x),a,b,x,shift,k,c2,A,B);
break;
case 2:
Strokes(fun2(x),a,b,x,shift,k,c2,A,B);
break;
case 3:
Squares(fun2(x),a,b,x,shift,k,c2,A,B);
break;
case 4:
Stars(fun2(x),a,b,x,shift,k,c2,A,B);
break;
default:
printf("Bug!");
getch();
exit(1);
}
}
} // break;
//default:
//printf("Bug!");
//getch();
//exit(1);
}
Mashtab(&a,&b,&i,&shift,&A,&B,&j);
}
closegraph();
}