#include <graphics.h>
#include <dos.h>
#include <math.h>
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>

int gdriver = DETECT, gmode, errorcode;
int midx, midy, i;
int presskey, position, action;

double x, y;

class screen
{
float mx, my, //Размеры экрана
msx, msy;     //Масштабы по x и y
public:
screen()
    {
	int d=DETECT, m;
	initgraph(&gdriver, &gmode, ".\\");
	mx=getmaxx(); my=getmaxy();
	msx=0.6*mx/2.0; msy=(0.8*my)/10.0;
	setbkcolor(8);
    }

~screen()
    {
	setbkcolor(0);
	closegraph();
	printf("\nBuy!!! ;-)\n\n");
    }

void axis();
void GraphXY();
void title();
};

void screen::axis()
	{
	setcolor(8);
	line(0.5*mx,    0.1*my, 0.5*mx, 0.9*my);     //Ось y
	line(0.1*mx-40, 0.5*my, 0.9*mx+40, 0.5*my);   //Ось x

	//line(0.1*mx-40, 0.5*my, 0.1*mx-30, 0.5*my-3);
	//line(0.1*mx-40, 0.5*my, 0.1*mx-30, 0.5*my+3);
	line(0.9*mx+40, 0.5*my, 0.9*mx+30, 0.5*my-3);
	line(0.9*mx+40, 0.5*my, 0.9*mx+30, 0.5*my+3);

	line(0.5*mx,    0.1*my, 0.5*mx-3, 0.1*my+10);
	line(0.5*mx,    0.1*my, 0.5*mx+3, 0.1*my+10);
	//line(0.5*mx,    0.9*my, 0.5*mx-3, 0.9*my-10);
	//line(0.5*mx,    0.9*my, 0.5*mx+3, 0.9*my-10);

	settextstyle(0, 0, 1);
	setcolor(4);
	outtextxy(mx-32, 0.5*my+10, "X");
	outtextxy(0.5*mx+15, 0.1*my,    "Y");

	setcolor(4);
	char st[4];
	int a;
	x=-1.4;

	while(x<=1.41);
		{
		sprintf(st, "%f", x);
		a=0.5*mx+msx*x;
		line(a, 0.5*my+3, a, 0.5*my-3);
		outtextxy(a-20, 0.5*my+10, st);
		x+=0.1;
		}


	}
void screen::GraphXY()
	{
	}

void screen::title()
	{
	}


class button
{
public:
	void enables(void);
	button(int, int, char *);
	void hide(void)
	{
	setcolor(getbkcolor());
	bar3d(x, y, x+100, y+50, 3, 3);
	setcolor(getbkcolor());
	outtextxy(x+5, y+23, str);
	}
	void disables(void);
private:
	char str[50];
	int x, y;
};

button::button(int x, int y, char *str)
{
strcpy(button::str, str);
button::x=x;
button::y=y;
enables();
}

void button::enables(void)
{
setcolor(7);
setfillstyle(SOLID_FILL, 7);
bar3d(x, y, x+100, y+50, 3, 3);
setcolor(4);
outtextxy((x+(50))-(textwidth(str)/2), y+25-(textheight(str)/2), str);
}

void button::disables(void)
{
setcolor(getbkcolor());
setfillstyle(EMPTY_FILL, 7);
bar3d(x, y, x+100, y+50, 3, 3);
setcolor(7);
outtextxy((x+(50))-(textwidth(str)/2), y+25-(textheight(str)/2), str);
}


class AboutME
{
public:
	/*
	AboutME()
	{
	AboutME::str="<EMPRY>";
	}
	~AbojutME()
	{
	delete AboutME::str;
	}
	*/

	void show(char *str, int z=0, int col=7, int size=0, int style=0)
		{
		AboutME::z=z;
		AboutME::size=size;
		AboutME::style=style;
		strcpy(AboutME::str, str);
		setcolor(col);
		settextstyle(style, 0, size);
		PrnText();
		}

	void hide()
		{
		setcolor(getbkcolor());
		settextstyle(style, 0, size);
		PrnText();
		}
private:
void PrnText()
{
outtextxy(midx-(textwidth(str)/2), z, str);
settextstyle(DEFAULT_FONT, 0, 0);
}
int size, style, z;
char str[100];
};


int main(void)
{
screen n;
AboutME HEADER;


	  /* read result of initialization */

   int errorcode = graphresult();
   if (errorcode != grOk)  /* an error
       occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with error code */
   }
   midx = getmaxx() / 2;
   midy = getmaxy() / 2;

   MainMenu:
   cleardevice();
   setbkcolor(8);
   presskey=0;
   action=0;
   position=1;
   setcolor(7);
   outtextxy(midx/2, 0, "Для выхода нажмите клавишу Esc");
   button T1(100+20, 160, "1) Обо мне");
   button T2(220+20, 160, "2) SIN(x)");
   T2.disables();
   button T3(340+20, 160, "3) COS(x)");
   T3.disables();

   TheButtons:
   presskey=0;
   presskey=getch();
   if(!presskey)
   {
   presskey=getch();
   switch(presskey)
	{
	case 75:
	if(position==1)
	{
	T1.disables();
	T3.enables();
	position=3;
	}
	else
	if(position==2)
	{
	T2.disables();
	T1.enables();
	position=1;
	}
	else
	if(position==3)
	{
	T3.disables();
	T2.enables();
	position=2;
	}
	goto TheButtons;
	case 77:
	if(position==1)
	{
	T1.disables();
	T2.enables();
	position=2;
	}
	else
	if(position==2)
	{
	T2.disables();
	T3.enables();
	position=3;
	}
	else
	if(position==3)
	{
	T3.disables();
	T1.enables();
	position=1;
	}
	goto TheButtons;
	default:
	goto TheButtons;
	}
   }
   else
   switch(presskey)
	{
	case 13:
	case 32:
	action=position;
	goto sw_action;
	case '1':
	action=1;
	goto sw_action;
	case '2':
	action=2;
	goto sw_action;
	case '3':
	action=3;
	goto sw_action;
	case 27:
	goto Exit;
	sw_action:
	default:
	switch(action)
		{
		case 1:
		for(i=0; i<3; i++)
		{
		T1.enables();
		delay(70);
		T1.disables();
		delay(70);
		}
		break;
		case 2:
		for(i=0; i<3; i++)
		{
		T2.enables();
		delay(70);
		T2.disables();
		delay(70);
		}
		break;
		case 3:
		for(i=0; i<3; i++)
		{
		T3.enables();
		delay(70);
		T3.disables();
		delay(70);
		}

		}
	 T1.hide();
	 T2.hide();
	 T3.hide();
	 delay(1000);
	}


   switch(action)
   {
   case 1:
	cleardevice();setbkcolor(9);
	//(char *str, int z=0 int col=7, int size=0, int style=0)
	HEADER.show("About of me :-)", 50, YELLOW, 8, 4);
	HEADER.show("Виталий Дмитриевич Новичков", 150, LIGHTGRAY, 0, 0);
	HEADER.show("Студент МГТУ ГА", 160, LIGHTGRAY, 0, 0);
	HEADER.show("Родился 19 июня 1992 года", 170, BROWN, 0, 0);
	HEADER.show("Живу в городе Москва", 190, GREEN, 0, 0);
	HEADER.show("В районе Ховрино", 200, BROWN, 0, 0);
	if(!getch())getch();
	goto MainMenu;

   case 2:
	cleardevice();setbkcolor(15);
	n.axis();
	if(!getch())getch();
	goto MainMenu;

   case 3:
	cleardevice();setbkcolor(15);
	if(!getch())getch();
	goto MainMenu;

   default:;
   }
   Exit:
   T1.hide();
   T2.hide();
   T3.hide();
   cleardevice();
   /* clean up */
   return 0;
}