Jump to content

SUBIECTE NOI
« 1 / 5 »
RSS
Succesiune notar versus instanta ...

Montaj aer conditionat in balcon ...

joc idem Half Life gratis

PC game stream catre Nvidia Shiel...
 Pompa de apa HEPU ?!

Vreau o masina electrica de tocat...

Cum ajunge remorca de tir inapoi ...

Alt "Utilizator nou" pe T...
 ULBS INFORMATICA

Index preturi

Boxa membrana tweeter infundata

Am nevoie de poze cu un curcubeu
 Whisky for Mac

Xiaomi 14 Gpay

Izolare zid exterior de scandura

Dezinstalare drivere W11 23H3
 

Numere complexe in C++

- - - - -
  • Please log in to reply
18 replies to this topic

#19
nightmare392

nightmare392

    Active Member

  • Grup: Members
  • Posts: 1,499
  • Înscris: 20.09.2007
Ti-am rescris clasa ca template asa cum s-a sugerat mai sus. Am modificat putin si unii din operatori ca sa poti inlantui operatiile (de ex z=x+=y sau x=y=z). Am pus toata implementarea in header deoarece la template compilatorul are nevoie de implementarea clasei cand face instantierea (se genereaza o noua clasa in functie de tipul cu care faci instantierea). Am mai modificat operatorul != sa returneze
!(*this==number)
, astfel daca vrei sa modifici implementarea lui == si != e suficient sa modifici ==. La fel e indicat sa faci la operatori relationali (<,>,<=,>=). Am mai pus la template float ca default. Am adaugat
return stream
la operatorii <<,>>, pentru ca lipsea. Am modificat putin pe fuga asa ca va rog sa imi atrageti atentia daca am gresit pe undeva.

LE: Am adaugat si operatorii unari si am eliminat
using namespace std

#ifndef _COMPLEXNUMBERS_H__
#define _COMPLEXNUMBERS_H__
#include <iostream>
template <typename T=float>
class ComplexNumber
{
typedef ComplexNumber<T> _Cplx;
public:
ComplexNumber() :a(0), b(0){};
ComplexNumber(float a, float B) :a(a), b(B){}
virtual ~ComplexNumber(){}
void SetReal(float a)
{
  this->a = a;
}
void SetImaginary(float B)
{
  this->b = b;
}
T GetReal() const
{
  return a;
}
T GetImaginary() const
{
  return b;
}
bool operator==(const _Cplx& number)
{
  return ((a == number.a) && (b == number.B));
}
bool operator!=(const _Cplx& number)
{
  return !(*this == number);
}
_Cplx& operator=(const _Cplx& number)
{
  a = number.a;
  b = number.b;
  return *this;
}
_Cplx& operator+=(const _Cplx& number)
{
  a += number.a;
  b += number.b;
  return *this;
}
_Cplx operator-=(const _Cplx& number)
{
  a -= number.a;
  b -= number.b;
  return *this;
}
_Cplx operator*=(const _Cplx& number)
{
  T copyReal = a;
  a = a*number.a - b*number.b;
  b = b*number.a + copyReal*number.b;
  return *this;
}
_Cplx operator/=(const _Cplx& number)
{
  _Cplx a = number * !number;
  *this = (*this * !number) / a;
  return *this;
}
_Cplx operator+(const _Cplx& number)
{
  return _Cplx(a + number.a, b + number.B);
}
_Cplx operator-(const _Cplx& number)
{
  return _Cplx(a - number.a, b - number.B);
}
_Cplx operator*(const _Cplx& number) const
{
  return _Cplx(a * number.a - b * number.b, a * number.b + b * number.a);
}
_Cplx operator/(const _Cplx& number) const
{
  _Cplx a(*this);
  _Cplx conjugate = !number;
  a *= conjugate;
  _Cplx b = number * conjugate;
  a /= b.GetReal();
  return a;
}
_Cplx& operator++()
{
  ++a;
  return *this;
}
_Cplx& operator--()
{
  --a;
  return *this;
}
_Cplx operator++(int)
{
  _Cplx number(*this);
  ++*this;
  return number;
}
_Cplx operator--(int)
{
  _Cplx number(*this);
  --*this;
  return number;
}
_Cplx& operator+=(T value)
{
  a += value;
  return *this;
}
_Cplx& operator-=(T value)
{
  a -= value;
  return *this;
}
_Cplx& operator/=(T value)
{
  a /= value;
  b /= value;
  return *this;
}
_Cplx& operator*=(T value)
{
  a *= value;
  b *= value;
  return *this;
}
_Cplx operator+(T value)
{
  _Cplx other(a, B);
  other[0] += value;
  return other;
}
_Cplx operator-(T value)
{
  ComplexNumber other(a, B);
  other[0] -= value;
  return other;
}
_Cplx operator*(T value)
{
  _Cplx other(a, B);
  other[0] *= value;
  other[1] *= value;
  return other;
}
_Cplx operator/(T value)
{
  _Cplx other(a, B);
  other[0] /= value;
  other[1] /= value;
  return other;
}
_Cplx operator-()
{
  return _Cplx(-a, -B);
}
_Cplx operator+()
{
}
_Cplx operator!() const
{
  return _Cplx(a, -B);
}
void power(int p)
{
  _Cplx copy(*this);
  for (int i = 1; i < p; i++)
   *this *= copy;
}
T operator[](int position)
{
  if (position == 0)
   return a;
  if (position == 1)
   return b;
  throw invalid_argument("Indice gresit!\n");
}
const T& operator[](int position) const
{
  if (position == 0)
   return a;
  if (position == 1)
   return b;
  throw invalid_argument("Indice gresit!\n");
}
protected:
T a, b;
};
template <typename T>
std::ostream& operator<<(std::ostream& stream, ComplexNumber<T>& number)
{
stream << number.GetReal();
if (number.GetImaginary() >= 0.0)
  stream << "+";
stream << number.GetImaginary() << "i";
return stream;
}
bool checkNumber(std::string& str)
{
int i, length = str.length();
for (i = 0; i < length; i++)
  if (str[i] != '-' && str[i] != '+' && str[i] != '.' && str[i] != 'i' &&
   (str[i] < '0' || str[i] > '9'))
   return false;
for (i = 0; i < length - 1; i++)
  if (str[i] == str[i + 1] && str[i] == '.')
   return false;
int noOfSigns = 0;
for (i = 1; i < length; i++)
  if (str[i] == '-' || str[i] == '+')
   noOfSigns++;
if (noOfSigns > 1)
  return false;
if (noOfSigns == 1 && str[length - 1] != 'i')
  return false;
return true;
}
template <typename T>
std::istream& operator>>(std::istream& stream, ComplexNumber<T>& number)
{
std::string str;
stream >> str;
while (!checkNumber(str)) {
  cout << "Introdu un numar complex valid: ";
  stream >> str;
}
int positive, pos = 1, length = str.length();
if (str[0] == '-')
  positive = 0;
else {
  if (str[0] == '+')
   positive = 1;
  else {
   positive = 1;
   pos = 0;
  }
}
std::string real;
int i = pos;
while (str[i] != '-' && str[i] != '+' && i < length) {
  real.push_back(str[i]);
  i++;
}
int length_real = real.length();
std::string::size_type sz;
if (real[i - 1 - pos] != 'i') {
  if (positive)
   number.SetReal(stof(real, &sz));
  else
   number.SetReal(-stof(real, &sz));
  if (i < length) {
   if (str[i] == '-')
	positive = 0;
   else
	positive = 1;
   i++;
   real = std::string("");
   while (str[i] != 'i' && i < length) {
	real.push_back(str[i]);
	i++;
   }
   if (positive)
	number.SetImaginary(stof(real, &sz));
   else
	number.SetImaginary(-stof(real, &sz));
  }
}
else {
  if (positive)
   number.SetImaginary(stof(real, &sz));
  else
   number.SetImaginary(-stof(real, &sz));
}
}
template <typename T>
ComplexNumber<T> power(ComplexNumber<T>& number, int p);
#endif


Edited by nightmare392, 07 December 2014 - 18:04.


Anunturi

Chirurgia spinală minim invazivă Chirurgia spinală minim invazivă

Chirurgia spinală minim invazivă oferă pacienților oportunitatea unui tratament eficient, permițându-le o recuperare ultra rapidă și nu în ultimul rând minimizând leziunile induse chirurgical.

Echipa noastră utilizează un spectru larg de tehnici minim invazive, din care enumerăm câteva: endoscopia cu variantele ei (transnazală, transtoracică, transmusculară, etc), microscopul operator, abordurile trans tubulare și nu în ultimul rând infiltrațiile la toate nivelurile coloanei vertebrale.

www.neurohope.ro

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Forumul Softpedia foloseste "cookies" pentru a imbunatati experienta utilizatorilor Accept
Pentru detalii si optiuni legate de cookies si datele personale, consultati Politica de utilizare cookies si Politica de confidentialitate