Моделювання життя за допомогою ресурсів мови С++

Страницы работы

6 страниц (Word-файл)

Содержание работы

МІНИСТЕРСТВО ОСВІТИ І НАУКИ УКРАЇНИ

НАЦІОНАЛЬНИЙ ТЕХНІЧНИЙ УНЕВЕРСИТЕТ

«ХАРКІВСЬКИЙ ПОЛІТЕХНІЧНИЙ ІНСТИТУТ»

РГР

з курсу Объектно-ориентированное программирование

Виконав:

Студент гр. КІТ-14в

Богачов О. С.

Харків – 2006

Тема:                                                                       Взаємодія об’єктів

Індивідуальне завдання:

  1. Промоделювати життя за допомогою ресурсів мови С++.
  2. За допомогою програми UML побудувати схеми класів, які присутні у  програмі

Текст програми:

#include <graphics.h>

#include <conio.h>

#include <stdlib.h>

#include <dos.h>

#include <iostream.h>

#include <math.h>

const

                    All=20;

class CPoint

{

                    public:

                                       int X, Y, R, color, f[2], preg, inf, death, k, b;

                                       long int time;

                                       CPoint (int IX, int IY, int IR, int col);

                                       CPoint *Child;

                                       virtual void Show();

                                       virtual void Hide();

                                       void PregnantSM ();

                                       void Zoom (void);

                                       void Move (int DX, int DY);

                                       void Birth (void);

};

class Volk:public CPoint

{

    public:

                                       Volk (int IX, int IY, int IR, int col) : CPoint (IX, IY, IR, col){};

                                       virtual void Show(void);

                                       virtual void Hide(void);

};

class CCircleSM : public CPoint

{

                    public:

                                       CCircleSM (int IX, int IY, int IR, int col) : CPoint (IX, IY, IR, col){};

                                       virtual void Show(void);

                                       virtual void Hide(void);

                                       friend void TouchCirScr (CCircleSM *Cir);

                                       friend void TpuchCirs (CCircleSM *Cir1, CCircleSM *Cir2);

};

CPoint :: CPoint(int IX, int IY, int IR, int col)

{

                    X=IX;Y=IY;R=IR;color = col;preg = 0;inf = 0;f[0] = random (2);

                    f[1] = random (2);death = 0;time = 0;

                    if ((R == 0) && (color == 14))

                    {

                                       k = 2;

                                       b = 2;

                    }

                    else

                    {

                                       k = 1;

                                       b = 1;

                    }

};

struct CNode

{

                    CNode *PNext;

                    CPoint *PObj;

};

class CList

{

                    public:

                                       CNode *PHead,*PTemp;

                                       CList(void);

                                       void AddNode(CPoint *PNObj);

                                       void ShowAll(void);

                                       void MoveAll(void);

                                       void DelEl (CNode *PCur);

                                       ~CList(void);

};

void CPoint :: Show (void)

{

                    putpixel(X, Y, 1);

};

void CPoint :: Hide (void)

{

                    putpixel(X, Y, 0);

};

void CPoint :: Zoom (void)

{

                    R += 1;

                    if ((preg == 1) && (time % 600 == 0))

                                       Child->R += 1;

};

void CPoint :: Move (int DX, int DY)

{

                    Hide();

                    if (time == 100) time = 0;

                    if (preg == 1)

                                       Child->Hide ();

                    if (f[0]==0) X += k * DX;

                    else X -= k * DX;

                    if (f[1]==0) Y += b * DY;

                    else Y -= b * DY;

                    if ((time % 100 == 0) && (R != 0))

                                       Zoom ();

                    //krygek malenkii na meste

                    if (preg == 1)

                    {

                                       Child->Show ();

                    }

                    Show();

                    if (R == 28)

                                       death = 1;

                    k = 1;

                    b = 1;

};

void Volk :: Show(void)

{

                    setcolor(15);

                    circle(X, Y, R+3);

                    setcolor(4);

                    line(X,Y,X,Y-2);

                    line(X,Y,X,Y+2);

                    line(X,Y,X-2,Y);

                    line(X,Y,X+2,Y);

}

void Volk :: Hide (void)

{

                    setcolor (0);

                    circle (X, Y, R+3);

                    setcolor (0);

                    line(X,Y,X,Y-2);

                    line(X,Y,X,Y+2);

                    line(X,Y,X-2,Y);

                    line(X,Y,X+2,Y);

}

void CCircleSM :: Show (void)

{

                    setcolor (color);

                    circle (X, Y, R);

                    arc(X,Y,360,0,2);

}

void CCircleSM :: Hide (void)

{

                    setcolor (0);

                    circle (X, Y, R);

                    arc(X,Y,360,0,2);

}

void CPoint :: PregnantSM (void)

{

                    Child = new CCircleSM (X, Y, 2, color);

                    preg=1;

                    Child->Show ();

}

void CPoint :: Birth (void)

{

                                       Child->Hide ();

                                       Child->Show ();

                                       preg = 0;

}

void TouchCirs (CPoint *Cir1, CPoint *Cir2)

{

                    int r=sqrt(pow((Cir1->X-Cir2->X),2)+pow((Cir1->Y-Cir2->Y),2)),

                                       R=Cir1->R+Cir2->R+1;

                    if ((r <= R) && (Cir1->R != 0) && (Cir2->R != 0))

                    {

                                       int tmp = Cir1->f[0];

                                       Cir1->f[0] = Cir2->f[0];

                                       Cir2->f[0] = tmp;

                                       tmp = Cir1->f[1];

Похожие материалы

Информация о работе