1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include<iomanip>
#include<cmath>
#include<iostream>
#include<fstream>
using namespace std;
#define PI 3.14159265358979323
double z,z2,x,x2;
class Complex
{
protected:
double real1,img1,real2,img2;
char sign1,sign2,j1,j2;
public :
void getValue();
void seriesValue(double &z,double &x);
void parallelValue(double &z,double &x);
void showSeries(double &z,double &x);
void showParallel(double &z,double &x);
};
void Complex::getValue()
{
number1:
cout<<"Enter the first complex number Z1 in form of (x+jy):";
cin>>real1>>sign1>>j1>>img1;
if(sign1!='+')
{
img1=-img1;
if(sign1!='-')
{
cout << "\n\tOnly + and - are allowed.\n" << endl;
goto number1;
}
}
if(j1!='j')
{
cout << "\nOnly Letter 'j' is allowed.\n" << endl;
goto number1;
}
number2:
cout<<"\n\nEnter the first complex number Z2 in form of (x+jy):";
cin>>real2>>sign2>>j2>>img2;
if(sign2!='+')
{
img2=-img2;
if(sign2!='-')
{
cout << "\tOnly + and - are allowed.\n" << endl;
goto number2;
}
}
if(j2!='j')
{
cout << "\tOnly Letter 'j' is allowed.\n" << endl;
goto number2;
}
}
void Complex::seriesValue(double &z,double &x)
{
z=real1+real2;
x=img1+img2;
}
void Complex::parallelValue(double &z2,double &x2)
{
z2=((real1*real2-img1*img2)*(real1+real2)+(img1*real2+img2*real1)*(img1+img2))/((pow(real1+real2,2))+(pow(img1+img2,2)));
x2=((img1*real2+img2*real1)*(real1+real2)-(real1*real2-img1*img2)*(img1+img2))/((pow(real1+real2,2))+(pow(img1+img2,2)));
}
void Complex::showSeries(double &z,double &x)
{
if ( x>= 0 )
{
cout << "\n\nWhen impedance A & B are in series,the effective Impedance is " <<setprecision(4)<< z << " + j" <<setprecision(3)<< x<<endl;
}
else
cout<< "\n\nWhen impedance A & B are in series,the effective Impedance is " <<setprecision(4)<<z << " - j" <<setprecision(3)<< x*(-1)<<endl;
}
void Complex::showParallel(double &z2,double &x2)
{
if ( x2>= 0 )
cout << "\n\nWhen impedance A & B are in parallel,the effective Impedance is " <<setprecision(4)<< z2 << " + j" <<setprecision(3)<< x2 <<endl;
else
cout << "\n\nWhen impedance A & B are in parallel,the effective Impedance is " <<setprecision(4)<<z2 << " - j" <<setprecision(3)<< x2*(-1) <<endl;
}
class Polar:public Complex //inheritant from class Complex
{
protected:
double r,d,r2,d2;
public:
void PolarValue();
void ShowPolar();
void printdata();
};
void Polar::PolarValue()
{
r=sqrt(pow(real1,2)+pow(img1,2));
d=atan(img1/real1)*(180/PI);
r2=sqrt(pow(real2,2)+pow(img2,2));
d2=atan(img2/real2)*(180/PI);
}
void Polar::ShowPolar()
{
cout<<"\n\n(Polar form Z1= "<<r<<" < "<<d<<" degree)"<<endl;
cout<<"(Polar form Z2= "<<r2<<" < "<<d2<<" degree)"<<endl;
}
void Polar::printdata()
{
ofstream myfile;
myfile.open("C:\\temp\\complex.txt");
myfile<<"Output data of Series & Parallel impedance of Z1 & Z2"<<endl<<endl;
if (sign1=='-')
myfile<<"Enter the first complex number Z1 in form of (x+jy):"<<real1<<sign1<<j1<<img1*-1;
else
myfile<<"Enter the first complex number Z1 in form of (x+jy):"<<real1<<sign1<<j1<<img1;
if (sign2=='-')
myfile<<"\n\nEnter the first complex number Z2 in form of (x+jy):"<<real2<<sign2<<j2<<img2*-1;
else
myfile<<"\n\nEnter the first complex number Z2 in form of (x+jy):"<<real2<<sign2<<j2<<img2;
if ( x>= 0 )
myfile << "\n\nWhen impedance A & B are in series,the effective Impedance is " <<setprecision(4)<< z << " + j" <<setprecision(3)<< x<<endl;
else
myfile<< "\n\nWhen impedance A & B are in series,the effective Impedance is " <<setprecision(4)<<z << " - j" <<setprecision(3)<< x*(-1)<<endl;
if ( x2>= 0 )
myfile << "\n\nWhen impedance A & B are in parallel,the effective Impedance is " <<setprecision(4)<< z2 << " + j" <<setprecision(3)<< x2 <<endl;
else
myfile << "\n\nWhen impedance A & B are in parallel,the effective Impedance is " <<setprecision(4)<<z2 << " - j" <<setprecision(3)<< x2*(-1) <<endl;
myfile<<"\n\n(Polar form Z1= "<<r<<" < "<<d<<" degree)"<<endl;
myfile<<"(Polar form Z2= "<<r2<<" < "<<d2<<" degree)"<<endl;
myfile.close();
}
int main()
{
Polar p;
p.getValue();
p.seriesValue(z,x);
p.showSeries(z,x);
p.parallelValue(z2,x2);
p.showParallel(z2,x2);
p.PolarValue();
p.ShowPolar();
p.printdata();
}
|