C/C++ - [решено] Комплексные числа - перегрузка оператора >>

Ответить
Аватара пользователя
котвася

C/C++ - [решено] Комплексные числа - перегрузка оператора >>

Сообщение котвася »

написал:



Код:

Код: Выделить всё

istream &operator>> (istream &in, Complex &complex)
{
	while( (in >> complex.REAL) != +0 || -0); \\ Вводится REAL пока не встретится знак(+-) -> работает правольно
	in.ignore(1); // пропускаем j -> работает не правильно
	in >> complex.IMAG; // Вводим IMAG -> работает не правильно
	return in;
}

комплексное число вводится в форма REAl + jIMAG, выводится в том же.

результат всего это, например:

123123+j0

реальную часть выводит правильно, а мнимую нет(точнее правильно из-за по умолчанию =0)

она не изменяется кодом:



Код:

Код: Выделить всё

in >> complex.IMAG;

как быть?

еще бы хотелось узнать как определить знак мнимой части?
Аватара пользователя
котвася

Re: C/C++ - [решено] Комплексные числа - перегрузка оператора >>

Сообщение котвася »

Код:

Код: Выделить всё

istream &operator>> (istream &in, Complex &complex)
{
	while( (in >> complex.REAL) != +0 || -0)
		break;
	in.ignore(2);
	in >> complex.IMAG;
	return in;
}

осталось как-то определить знак и учесть его Изображение
Аватара пользователя
котвася

Re: C/C++ - [решено] Комплексные числа - перегрузка оператора >>

Сообщение котвася »

ПРИМЕР:



Код:

Код: Выделить всё

//COPMLEX.H
//Определение класса Complex
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using std::ostream;
using std::istream;
class Complex
{
	friend ostream &operator<< (ostream &, const Complex &);
	friend istream &operator>> (istream &, Complex &);
public:
	Complex(double = 0.0, double = 0.0);
	Complex operator+ (const Complex &) const;
	Complex operator- (const Complex &) const;
	Complex &operator= (const Complex &);
	void print() const;
private:
	double REAL;
	double IMAG;
};
#endif;



Код:

Код: Выделить всё

//COPMLEX.CPP
//Определение функций=элементов класса Complex
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ostream;
using std::istream;
#include <iomanip>
using std::setw;
#include "complex.h"
ostream &operator<< (ostream &out, const Complex &complex)
{
	out << complex.REAL << ((complex.IMAG < 0) ? "-j" : "+j") << ((complex.IMAG < 0) ? complex.IMAG*(-1) : complex.IMAG);
	return out;
}
istream &operator>> (istream &in, Complex &complex)
{
	while( (in >> complex.REAL) != +0 || -0)
		break;
	int marker = 0;
	marker = in.get();
	in.ignore(1);
	in >> complex.IMAG;
	if(marker == 45)
		complex.IMAG = -complex.IMAG;
	return in;
}
Complex::Complex(double real, double imag)
{
	REAL = real;
	IMAG = imag;
}
Complex Complex::operator+ (const Complex &operand) const
{
	Complex sum;
	sum.REAL = REAL + operand.REAL;
	sum.IMAG = IMAG + operand.IMAG;
	return sum;
}
Complex Complex::operator-(const Complex &operand) const
{
	Complex diff;
	diff.REAL = REAL - operand.REAL;
	diff.IMAG = IMAG - operand.IMAG;
	return diff;
}
Complex& Complex::operator= (const Complex &right)
{
	REAL = right.REAL;
	IMAG = right.IMAG;
	return *this;
}



Код:

Код: Выделить всё

//MAIN.CPP
//Драйвер проверка класса Complex
#include "complex.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
	setlocale(LC_ALL, ".1251");
	cout << "Драйвер проверки класса Complex" << endl;
	cout << "Введите комплексное число в формате Re+jIm" << endl;
	Complex complex;
	cin >> complex;
	cout << complex << endl;
	system("pause");
	return 0;
}

РЕШЕНО Изображение
Ответить

Вернуться в «Программирование и базы данных»