You are on page 1of 6

#include

#include
#include
#include

<stdio.h>
<iostream.h>
<stdlib.h>
<math.h>

class complex
{
public:
float a,b,modul,modulul,unghic,unghis;
int semnc,semns;
complex()
{
a=0;
b=0;
semnc=0;
semns=0;
}
complex(float x,float y)
{
a=x;
b=y;
semnc=0;
semns=0;
}
complex(float mo,float unc,float uns,int sem1,int sem2)
{
modulul=mo;
unghic=unc;
unghis=uns;
semnc=sem1;
semns=sem2;
}
void convert();
complex operator + (complex);
complex operator - (complex);
complex operator * (complex);
complex operator / (complex);
void afisare();
void calc_mod()
{
if(semnc==0)
modul=(float)sqrt(a*a+b*b);
else
{
convert();
modul=(float)sqrt(a*a+b*b);
convert();
}
}
friend FILE * operator >> (FILE *fis1,complex &c)
{
float *ex;
if(c.semnc==0)
{
fread(ex,sizeof(c.a),1,fis1);
c.a=*ex;
fread(ex,sizeof(c.b),1,fis1);
c.b=*ex;

}
else
{
c.convert();
fread(ex,sizeof(c.a),1,fis1);
c.a=*ex;
fread(ex,sizeof(c.b),1,fis1);
c.b=*ex;
c.convert();
}
return fis1;
}
friend FILE * operator << (FILE *fis1,complex c)
{
float *ex;
if(c.semnc==0)
{
ex=&c.a;
fwrite(ex,sizeof(c.a),1,fis1);
ex=&c.b;
fwrite(ex,sizeof(c.b),1,fis1);
}
else
{
c.convert();
ex=&c.a;
fwrite(ex,sizeof(c.a),1,fis1);
ex=&c.b;
fwrite(ex,sizeof(c.b),1,fis1);
c.convert();
}
return fis1;
}
complex conjug()
{
complex t;
if(semnc==0)
{
t.a=a;
t.b=-b;
}
else
{
convert();
t.a=a;
t.b=-b;
convert();
}
return t;
}
};
void complex::convert()
{

const float pi=3.1415926535897932384626433832795;


if(semnc==0)
{
calc_mod();
modulul=modul;
unghic=(float)acos(a/modul);
unghis=(float)asin(b/modul);
if(cos(unghic)<0)
{
semnc=-1;
unghic=pi-unghic;
}
else
semnc=1;
if(sin(unghis)<0)
{
semns=-1;
unghis=pi+unghis;
}
else
semns=1;
}
else
{
a=(float)(modulul*semnc*cos(unghic));
b=(float)(modulul*semns*sin(unghis));
semnc=semns=0;
}
}
complex complex::operator + (complex y)
{
complex t;
if((semnc!=0)&&(y.semnc!=0))
{
convert();
y.convert();
t.a=a+y.a;
t.b=b+y.b;
convert();
y.convert();
t.convert();
}
else
if((semnc==0)&&(y.semnc==0))
{
t.a=a+y.a;
t.b=b+y.b;
}
else
{
printf("\nEROARE ! Formele de reprezentare ale celor doi
termeni nu coincid !!!");
t.a=t.b=0;
}
return t;
}

complex complex::operator - (complex y)


{
complex t;
if((semnc!=0)&&(y.semnc!=0))
{
convert();
y.convert();
t.a=a-y.a;
t.b=b-y.b;
convert();
y.convert();
t.convert();
}
else
if((semnc==0)&&(y.semnc==0))
{
t.a=a-y.a;
t.b=b-y.b;
}
else
{
printf("\nEROARE ! Formele de reprezentare ale celor doi
termeni nu coincid !!!");
t.a=t.b=0;
}
return t;
}
complex complex::operator * (complex y)
{
complex t;
if((semnc!=0)&&(y.semnc!=0))
{
convert();
y.convert();
t.a=a*y.a-b*y.b;
t.b=a*y.b+b*y.a;
convert();
y.convert();
t.convert();
}
else
if((semnc==0)&&(y.semnc==0))
{
t.a=a*y.a-b*y.b;
t.b=a*y.b+b*y.a;
}
else
{
printf("\nEROARE ! Formele de reprezentare ale celor doi
termeni nu coincid !!!");
t.a=t.b=0;
}
return t;
}

complex complex::operator / (complex y)


{
complex t;
if((semnc!=0)&&(y.semnc!=0))
{
convert();
y.convert();
t.a=(a*y.a+b*y.b)/(y.a*y.a+y.b*y.b);
t.b=(-a*y.b+b*y.a)/(y.a*y.a+y.b*y.b);
convert();
y.convert();
t.convert();
}
else
if((semnc==0)&&(y.semnc==0))
{
t.a=(a*y.a+b*y.b)/(y.a*y.a+y.b*y.b);
t.b=(-a*y.b+b*y.a)/(y.a*y.a+y.b*y.b);
}
else
{
printf("\nEROARE ! Formele de reprezentare ale celor doi
termeni nu coincid !!!");
t.a=t.b=0;
}
return t;
}
void complex::afisare()
{
char lp='+';
if(b<0) lp=' ';
if(semnc==0)
{
if(a>=0.1) //conversia este cu pierderi si a poate fi 0.0...01 i
n loc de 0
printf("\n%.1f%c",a,lp);
else
printf("\n");
if(b!=0)
printf("%.1fi",b);
}
else
{
char ch1,ch2;
ch1=' ';
ch2='+';
if(semnc==-1)
ch1='-';
if(semns==-1)
ch2='-';
if(modulul>=0.1)
printf("\n%.1f*(%ccos(%.1f)%ci*sin(%.1f))",modulul,ch1,u
nghic,ch2,unghis);
else
printf("\n0");

}
}
void main()
{
complex z(1,1),t(0,2.5),u;
FILE *fisier;
z.afisare();
t.afisare();
z=z+t;
z.afisare();
u=t;
u.afisare();
fisier=fopen("test","wb");
fisier << u << z;
fclose(fisier);
z.afisare();
fisier=fopen("test","rb");
fisier >> z >> u;
fclose(fisier);
z.afisare();
u.afisare();
z=z.conjug();
z.afisare();
z.calc_mod();
printf("\n%.1f",z.modul);
z.convert();
z.afisare();
t.convert();
t.afisare();
z=z+t;
z.afisare();
z.convert();
z.afisare();
}

You might also like