// Programa que realiza operaciones sobre complejos #include #include #include #include #include using namespace std; class Complejo { private: double i,r; public: Complejo(){ r=i=0; } /////////////////////////////////////////////////// Complejo(double re){ r=re; i=0; } /////////////////////////////////////////////////// Complejo(double re, double im){ r=re; i=im; } /////////////////////////////////////////////////// Complejo(Complejo &obj){ r=obj.r; i=obj.i; } ~Complejo() = default; void read() { cout<<"Ingresa numero complejo: " << endl; cout<<"Parte real: "; cin>>r; cout<<"Parte imaginaria: "; cin>>i; } void display() { if (i == 0) cout << r << endl; else if (r == 0) cout << i << "i" << endl; else if (i < 0) cout << r << " - " << abs(i) << "i" << endl; else cout<< r << " + " << i << "i" << endl; } Complejo& suma(Complejo a1, Complejo a2) { this->r=a1.r+a2.r; this->i=a1.i+a2.i; return (*this); } Complejo& suma(Complejo a2) { this->r=r+a2.r; this->i=i+a2.i; return (*this); } Complejo operator+(Complejo a2) { Complejo a(r + a2.r, i + a2.i); return a; } // Defining function Division double Division(double num, double den) { // Used to check against division by 0 const double kDivThresh { 1e-36 }; if( fabs( den ) < kDivThresh ) // don't divide by small number or 0 (better than: div == 0.0) throw overflow_error( "div by 0 error" ); // if so, then throw simple text exception // Otherwise return the result of division return (num / den); } // end Division Complejo operator/(Complejo a2) { auto div = a2.r * a2.r + a2.i * a2.i; auto re = r * a2.r + i * a2.i; auto im = i * a2.r - r * a2.i; try { re = Division(re,div); im = Division(im,div); } catch (runtime_error& e) { cout << "Exception occurred" << endl << e.what(); } Complejo a(re,im); return a; // Return object by value } double modulo() { return sqrt(r*r+i*i); } Complejo conj() { Complejo a; a.r=r; a.i= -i; return a; } Complejo seno() { Complejo a; a.r = sin(r) * cosh(i); a.i = cos(r) * sinh(i); return a; } // Overloaded streaming operators - always outside the class friend istream & operator >> ( istream & i, Complejo & c ); friend ostream & operator << ( ostream & o, const Complejo & c ); }; // Overload stream insertion operator out << c (friend) ostream & operator<< (ostream & out, const Complejo & c) { out << '(' << c.r << ',' << c.i << ')'; return out; } // Overload stream extraction operator in >> c (friend) istream & operator>> (istream & in, Complejo & c) { double inReal, inImag; char inChar; bool validInput = false; // Input shall be in the format "(real,imag)" in >> inChar; if (inChar == '(') { in >> inReal >> inChar; if (inChar == ',') { in >> inImag >> inChar; if (inChar == ')') { c = Complejo(inReal, inImag); validInput = true; } } } if (!validInput) in.setstate(ios_base::failbit); return in; } int main() { int ch; Complejo a,b,c,d(1.5,5.3); Complejo e(d); Complejo f = d; // cout << "f = " << f << endl; do { cout<<"\nOperaciones con complejos" << endl; cout<<" 1.Binarias 2.Unarias 3. Salir\n"; cout<<"\nSeleccionar la opcion: "; cin>>ch; switch(ch) { case 1: //suma cout<<"\nIngresa primer numero complejo (num,num):" << endl; cin >> a; cout << a; // a.read(); // a.display(); cout<<"\nIngresa segundo numero complejo:" << endl; b.read(); b.display(); // cout << "d: " << d<< endl; // cout << "e(d): " << e<< endl; c = a+b; cout<<"Suma (sob. + << ): " << c << endl; // c.suma(a,b); // cout<<"Suma (suma(a,b) << ): " << c << endl; // cout<<"Suma (a.suma(b)): " << a.suma(b) << endl; c = a/b; cout<<"Division (sobr. /): " << c << endl; break; case 2: //modulo a.read(); a.display(); double mod; mod=a.modulo(); cout<<"Modulo: " << mod << endl; Complejo c; c= a.conj(); cout<<"Conjugado: " << c << endl; c= a.seno(); cout<<"Seno: " << c << endl; break; } } while(ch!=3); return 0; }