Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / 7_complex

.cpp
Скачиваний:
23
Добавлен:
30.04.2013
Размер:
3.08 Кб
Скачать
#include <stdio.h>
#include <conio.h>
#include <math.h>


//-----------------------------------------------------------------------------
// !!! Ya ne znayu, KAK skladyvat'/umnojat' complex. chisla, tak chto ya tut
// mog konkretno navrat'.
//-----------------------------------------------------------------------------
class CComplex
{
	friend void WriteData(CComplex *pComp, float r, float i);// ???

public:
	// Construction/destruction
	inline CComplex(void) {re = 0; im = 0;};// default
	inline CComplex(float r, float i) {re = r; im = i;};// overloaded

	inline ~CComplex(void) {re = 0; im = 0;};

	// Operators
	inline CComplex operator-(void) const				{ return CComplex(-re, -im); }
	inline int operator==(const CComplex &z) const		{ return (z.re == re && z.im == im); }
	inline int operator!=(const CComplex &z) const		{ return !(*this == z); }
	inline CComplex operator+(const CComplex &z) const	{ return CComplex(re+z.re, im+z.im); }
	inline CComplex operator-(const CComplex &z) const	{ return CComplex(re-z.re, im-z.im); }
	inline CComplex operator*(const CComplex &z) const	{ return CComplex(re*z.re, im*z.im); }
	inline CComplex operator/(const CComplex &z) const	{ return CComplex(re/z.re, im/z.im); }
	inline CComplex operator*(float fl) const			{ return CComplex(re*fl, im*fl); }
	inline CComplex operator/(float fl) const			{ return CComplex(re/fl, im/fl); }
	inline CComplex operator^(float p) const			{ return CComplex(powf(re,p), powf(im,p)); }

	// Functions
	void Calculate(float real, float imaginary);
	void Calculate(CComplex &z);

	// Methods
	// for accessing private members from non-friendly code
	float GetRe(void) { return re; }
	float GetIm(void) { return im; }

private:
	// Members
	float re;
	float im;
};

void CComplex::Calculate(float real, float imaginary)
{
	Calculate(CComplex(real, imaginary));
}

// do it with yourself! :)
void CComplex::Calculate(CComplex &z)
{
	float i = 1.0;// ?!!
	*this = (z^3) + (z^2)*(1+2*i) + z^(-5*i)*(1-2*i);
}


void WriteData(CComplex *pComp, float r, float i)// do i need this ???
{
	pComp->re = r;
	pComp->im = i;
}



void datainput(CComplex **a, int c)
{
	float re, im;
	for (int i=0; i<c; i++)
	{
		printf("Element %d, input 'real imaginary': ", i);
		if (scanf("%f %f", &re, &im) == 2)
		{
			a[i] = new CComplex(re, im);
			printf("OK\n");
		}
	}
}

void dataprint(CComplex **a, int c)
{
	for (int i=0; i<c; i++)
		printf("Element %d: %f, %f\n", i, a[i]->GetRe(), a[i]->GetIm());
}

void dataprocess(CComplex **a, int c)
{
	for (int i=0; i<c; i++)
		a[i]->Calculate(*a[i]);
}

#define CNT 4

void main(void)
{
	CComplex *a[CNT];
	datainput(a, CNT);
	printf("\n----\n");
	dataprint(a, CNT);
	printf("\n----\nProcessing data...\n");
	dataprocess(a, CNT);
	printf("\n----\n");
	dataprint(a, CNT);

	// save the world - save the memory!
	for (int i=0; i<CNT; i++)
	{
		printf("Deleting element %d...\n", i);
		delete a[i];
	}
	printf("Done.\nNow hit a key to quit.");
	getch();
}
Соседние файлы в папке Лабы