Расчетно-графическая работа по программированию

Реферат - Компьютеры, программирование

Другие рефераты по предмету Компьютеры, программирование

?очего в классе используется перегрузка операторов сложения, вычитания, умножения и т.д.

class matrix

{

protected:

int x,y;

drob det;

public:

drob **matr;

matrix (int x1, int y1);

int Getx () { return x; };

int Gety () { return y; };

void Read (TStringGrid *sg, Analisation &obj);

matrix operator* (drob number);

matrix operator* (int num);

drob Determinant ();

};

//-------------------------------------

matrix::matrix (int x1, int y1)

{

x=x1;

y=y1;

det.chisl=det.znamen=det.cel=0;

matr=new drob *[x];

for (int i=0; i<x; i++)

matr[i]=new drob [y];

}

//-------------------------------------

void matrix::Read (TStringGrid *sg, Analisation &obj)

{

char ***temp;

temp=new char **[x];

for (int i=0; i<x; i++)

temp[i]=new char *[y];

for (int i=0; i<x; i++)

for (int j=0; j<y; j++)

temp[i][j]=sg->Cells[j][i].c_str();

for (int i=0; i<x; i++)

for (int j=0; j<y; j++)

{

matr[i][j]=obj.MAIN(temp[i][j]);

}

for (int i=0; i<x; i++)

delete temp[i];

delete [] temp;

}

//-------------------------------------

matrix matrix::operator* (drob number)

{

matrix res (x,y);

for (int i=0; i<x; i++)

for (int j=0; j<y; j++)

{

res.matr[i][j].chisl=number.chisl*matr[i][j].chisl;

res.matr[i][j].znamen=number.znamen*matr[i][j].znamen;

}

return res;

}

//-------------------------------------

matrix matrix::operator* (int num)

{

matrix res (x,y);

for (int i=0; i<x; i++)

for (int j=0; j<y; j++)

{

res.matr[i][j].chisl=num*matr[i][j].chisl;

res.matr[i][j].znamen=matr[i][j].znamen;

}

return res;

}

//-------------------------------------

 

drob matrix::Determinant ()

{

drob det1;

drob det2;

if (x!=y) MessageBox(NULL,"Ну кто так делает :)",

"ЧАЙНИК!!",MB_ICONEXCLAMATION);

if(x==2 && y==2)

{

det1=matr[0][0]*matr[1][1];

det2=matr[0][1]*matr[1][0];

det=det1-det2;

det.videlen();

}

if (x==1 && y==1)

{

det=matr[0][0];

det.videlen();

}

return det; }

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

#include

#include

#pragma hdrstop

#include "matrix.h"

#include "Resultunit.h"

#include "Numberunit.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma link "CSPIN"

#pragma resource "*.dfm"

TMainForm *MainForm;

//---------------------------------------------------------------------------

///КЛАСС ДРОБЕЙ///

class drob

{

public:

int chisl;

int znamen;

int cel;

drob ()

{

chisl=znamen=cel=0;

}

int videlen ()

{

if (!cel && chisl && znamen && abs(chisl)>=abs(znamen))

{

cel=(int)chisl/znamen;

chisl=(int)chisl%(int)znamen;

}

else

if (cel && chisl && znamen && abs(chisl)>=abs(znamen))

{

cel=cel+((int)chisl/znamen);

chisl=(int)chisl%(int)znamen;

}

return 0;

}

};

drob operator + (drob &m1, drob &m2)

{

drob res;

if (m1.znamen==m2.znamen && m1.znamen!=0)

{

res.chisl=m1.chisl+m2.chisl;

res.znamen=m1.znamen;

}

else

{

res.znamen=m1.znamen*m2.znamen;

res.chisl=(m1.chisl*m2.znamen)+

(m2.chisl*m1.znamen);

}

return res;

}

drob operator - (drob &m1, drob &m2)

{

drob res;

if (m1.znamen==m2.znamen && m1.znamen!=0)

{

res.chisl=m1.chisl-m2.chisl;

res.znamen=m1.znamen;

}

else

{

res.znamen=m1.znamen*m2.znamen;

res.chisl=(m1.chisl*m2.znamen)-

(m2.chisl*m1.znamen);

}

return res;

}

drob operator* (drob &g, drob &h)

{

drob res;

res.chisl=g.chisl*h.chisl;

res.znamen=g.znamen*h.znamen;

return res;

}

drob operator/ (drob &g, drob &h)

{

drob res;

res.chisl=g.chisl*h.znamen;

res.znamen=g.znamen*h.chisl;

return res;

}

//---------------------------------------------------------------------------

///АНАЛИЗАТОР///

enum types

{

DELIM=1,NUMB

};

class Analisation

{

char *ptr;

char token [80];

char ttype;

drob vrem;

//-------------------------------------

void Analiz(double &result)

{

register char op;

double temp;

Convert(result);

while((op=*token)==/ )

{

Get();

Convert(temp);

switch (op)

{

case /:

vrem.chisl=result;

vrem.znamen=temp;

break;

}

}

}

//-------------------------------------

void Convert(double &result)

{

result=(double)atof(token);

Get();

return;

}

//-------------------------------------

void Get()

{

register char *temp;

ttype=0;

temp=token;

*temp=\0;

if(!*ptr)return;

while(isspace(*ptr))++ptr;

if(strchr("/*",*ptr))

{

ttype=DELIM;

*temp++=*ptr++;

}

else if(isdigit(*ptr))

{

while(!isdelim(*ptr))*temp++=*ptr++;

ttype=NUMB;

}

*temp=\0;

}

//-------------------------------------

int isdelim(char c)

{

if (strchr("/*",c))return 1;

else return 0;

}

//-------------------------------------

public:

Analisation()

{

ptr=NULL;

}

drob MAIN(char*exp)

{

double result;

ptr=exp;

Get();

Analiz(result);

return vrem;

}

};

//---------------------------------------------------------------------------

class matrix

{

protected:

int x,y;

drob det;

public:

drob **matr;

matrix (int x1, int y1);

int Getx () { return x; };

int Gety () { return y; };

void Read (TStringGrid *sg, Analisation &obj);

matrix operator* (drob number);

matrix operator* (int num);

drob Determinant ();

};

//-------------------------------------

matrix::matrix (int x1, int y1)

{

x=x1;

y=y1;

det.chisl=det.znamen=det.cel=0;

matr=new drob *[x];

for (int i=0; i<x; i++)

matr[i]=new drob [y];

}

//-------------------------------------

void matrix::Read (TStringGrid *sg, Analisation &obj)

{

char ***temp;

temp=new char **[x];

for (int i=0; i<x; i++)

temp[i]=new char *[y];

for (int i=0; i<x; i++)

for (int j=0; j<y; j++)

temp[i][j]=sg->Cells[j][i].c_str();

for (int i=0; i<x; i++)

for (int j=0; j<y; j++)

{

matr[i][j]=obj.MAIN(temp[i][j]);

}

for (int i=0; i<x; i++)

delete temp[i];

delete [] temp;

}

//-------------------------------------

matrix matrix::operator* (drob number)

{

matrix res (x,y);