Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #define ESP 1e-6
- using namespace std;
- class comp {
- float Re, Im;
- public:
- comp() {}
- comp& operator = (const comp &x) {
- Re = x.Re;
- Im = x.Im;
- return *this;
- }
- comp& operator -= (const comp &x) {
- Re -= x.Re;
- Im -= x.Im;
- return *this;
- }
- friend comp operator + (const comp &a, const comp &b);
- bool operator == (const comp &x) {
- return(this -> Re == x.Re && this -> Im == x.Im);
- }
- void set(char s) {
- cout<<"Enter comp numb "<<s<<":"<<endl;
- cout<<"\tRe: ";
- cin>>Re;
- cout<<"\tIm: ";
- cin>>Im;
- return;
- }
- void get(char s);
- };
- int main () {
- comp a, b, c;
- a.set('a');
- b.set('b');
- cout<<"operator -="<<endl;
- cout<<"before"<<endl;
- a.get('a');
- b.get('b');
- cout<<"after"<<endl;
- a -= b;
- a.get('a');
- b.get('b');
- cout<<"operator +"<<endl;
- a.get('a');
- b.get('b');
- c = a + b;
- cout<<"c = a + b :"<<endl;
- c.get('c');
- cout<<"operator == "<<endl;
- a.get('a');
- b.get('b');
- cout<<"a == b"<<endl;
- if(a == b) {
- cout<<"true";
- } else {
- cout<<"false";
- }
- a = b;
- cout<<" a assigned b :"<<endl;
- cout<<"operator =="<<endl;
- a.get('a');
- b.get('b');
- cout<<"a == b"<<endl;
- if(a == b) {
- cout<<"true";
- } else {
- cout<<"false";
- }
- return 0;
- }
- comp operator + (const comp &a, const comp &b) {
- comp c;
- c.Re = a.Re + b.Re;
- c.Im = a.Im + b.Im;
- return c;
- }
- bool zero(double x) {
- return fabs(x) <= ESP; //було так return fabs(x) > ESP;
- }
- void comp :: get(char s) {
- cout <<"comp numb "<<s<<": ";
- if(!zero(Re)) {
- cout<<Re;
- }
- if(!zero(Im)) {
- if(!zero(Im) && Im > 0) {
- cout<<" + ";
- }
- if(Im < 0) {
- cout<<" - ";
- }
- cout << abs(Im)<<"i";
- }
- cout <<endl;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement