Войти

Показать полную графическую версию : [решено] Комплексные числа - перегрузка оператора >>


котвася
31-07-2010, 00:36
написал:

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;

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

котвася
31-07-2010, 00:46
istream &operator>> (istream &in, Complex &complex)
{
while( (in >> complex.REAL) != +0 || -0)
break;
in.ignore(2);
in >> complex.IMAG;
return in;
}

осталось как-то определить знак и учесть его :)

котвася
31-07-2010, 12:16
ПРИМЕР:

//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;
}

РЕШЕНО :)




© OSzone.net 2001-2012