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

Лабы / 02

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


class Complex
{
	friend void SetComplex(Complex *pComp, float r, float i);
	friend void Input(Complex &a);
	friend void Output(Complex &a);

public:
// Construction/destruction
	Complex(void) {re = 0; im = 0;};
	Complex(float r, float i) {re = r; im = i;};
	Complex(Complex &src) { re = src.re; im = src.im; };
	~Complex(void) {re = 0; im = 0;};

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

// Methods
	void Set(float r, float i);
	void Set(Complex &a);
	float GetRe(void) { return re; }
	float GetIm(void) { return im; }

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

void Complex::Set(float r, float i)
{
	re = r;
	im = i;
}

void Complex::Set(Complex &a)
{
	re = a.re;
	im = a.im;
}

void SetComplex(Complex *pComp, float r, float i)
{
	pComp->re = r;
	pComp->im = i;
}

void Input(Complex &a)
{
	printf("Enter Re Im: ");
	scanf("%f %f", &a.re, &a.im);
}

void Output(Complex &a)
{
	printf("%f %f\n", a.re, a.im);
}


void main(void)
{
	Complex z1 = Complex();
	Complex z2(10,12);
	Output(z1);
	Output(z2);

	Input(z1);
	Output(z1);

	z2 = (z1*2) + (z1+1)*expf(5);
	Output(z2);

	SetComplex(&z1, 1, 2);
	Output(z1);

	printf(" ----\n");
	for (float a = -1; a <= 1; a+= 0.5)
	{
		z1.Set(a, a);
		z2 = (z1*2) + (z1+1)*expf(5);
		Output(z2);
	}

	getch();
}
Соседние файлы в папке Лабы