Advertisement
Adytzu04

lab5p1

Nov 12th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. //h
  2.  
  3. #ifndef         LAB5P1_H
  4. #define         LAB5P1_H
  5.  
  6. #include <iostream>
  7. #include <math.h>
  8.  
  9. using namespace std;
  10.  
  11. class Complex
  12. {
  13.     int re,im;
  14. public:
  15.         Complex () {};
  16.         Complex (int,int);
  17.         Complex operator + (Complex);
  18.         void afisare();
  19.         float operator~();
  20.         int operator==(Complex c2);
  21.         Complex operator-(Complex c2);
  22.         Complex operator*(Complex c2);
  23.           void Complex::citire()
  24.         {              
  25.  
  26.                 cout<<"Re=";
  27.                 cin>>this->re;
  28.                 cout<<"\nIm=";
  29.                 cin>>this->im;
  30.         }
  31. };
  32.  
  33. #endif
  34.  
  35. //c
  36.  
  37. #include "lab5p1.h"
  38.  
  39. Complex::Complex (int re, int im)
  40. {
  41.         this->re = re;
  42.         this->im = im;
  43. }
  44.  
  45. Complex Complex::operator+(Complex c2)
  46. {
  47.         Complex temp;
  48.         temp.re = this->re + c2.re;
  49.         temp.im = this->im + c2.im;
  50.         return temp;
  51. }
  52.  
  53. void Complex::afisare()
  54. {
  55. cout << "(" << re << "+i" << im << ")" << endl;
  56. }
  57.  
  58. int Complex::operator ==(Complex c2)
  59. {
  60.     return ((this->re ==c2.re ) && (this->im ==c2.im ));
  61. }
  62.  
  63. float Complex::operator ~()
  64. {   float rim,inm,x;
  65.     rim=this->re*this->re;
  66.     inm=this->im*this->im;
  67.     x=sqrt(rim+inm);
  68.     return x;
  69.     //return ( sqrt((this->re*this->re)+(this->im*this->im)));
  70. }
  71. Complex Complex::operator-(Complex c2)
  72. {
  73.         Complex temp;
  74.         temp.re = this->re - c2.re;
  75.         temp.im = this->im - c2.im;
  76.         return temp;
  77. }
  78.  
  79. Complex Complex::operator *(Complex c2)
  80. {      
  81.         int x,y;
  82.         x=this->re*c2.re-this->im*c2.im;
  83.         y=this->re*c2.im+this->im*c2.re;
  84.         Complex b(x,y);
  85.         return b;
  86. }
  87.  
  88. //main
  89.  
  90. #include "lab5p1.h"
  91.  
  92. int main()
  93. {
  94.    
  95.     int test;
  96.     float v;
  97.     Complex c1,c2,imp;
  98.  
  99.     cout<<"Introduceti primul numar complex:"<<endl;
  100.     c1.citire();
  101.     cout<<"\nIntroduceti al doilea numar complex:"<<endl;
  102.     c2.citire();
  103.  
  104.     cout<<"\nModulul numarului complex c1 este:"<<endl;
  105.     v=~c1;
  106.     cout<<v;
  107.  
  108.     cout<<"\nModulul numarului complex c2 este:"<<endl;
  109.     v=~c2;
  110.     cout<<v;
  111.  
  112.     if(c1==c2)
  113.         cout<<"\nNumerele complexe c1 si c2 sunt egale!"<<endl;
  114.     else
  115.         cout<<"\nNumerele complexe c1 si c2  nu sunt egale!"<<endl;
  116.  
  117.     imp=c1*c2;
  118.     cout<<"Rezultatul inmultirii c1*c2 este: "<<endl;
  119.     imp.afisare();
  120.  
  121.     cout<<"\nPress RETURN to exit!";
  122.     cin>>test;
  123.  
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement