// Classes pour impédance de circuits R,L et C (sans boucle) // Thomas Grenier, MdC, Insa Lyon Dept GE. // 2008-2009 #include #include #include using namespace std; class Complexe { public: double Reel; double Imag; void Accumulate( Complexe z ) { Reel += z.Reel; Imag += z.Imag; } Complexe Inverse() { Complexe r; double d = Reel*Reel + Imag*Imag; r.Reel = Reel / d; r.Imag = -Imag /d; return r; } void Print() { if(Imag < 0.0) cout<< "= "<< Reel << Imag << ".i" << endl; else cout<< "= "<< Reel << "+" << Imag << ".i" << endl; } }; class Component { private: Component *Previous; Component *Next; public: double Valeur; string Name; Component(string name, double v): Name( name ), Valeur( v ) { Reset(); } void Reset() { Previous = NULL; Next = NULL; } Component* GetPrevious() { return Previous; } Component* GetNext() { return Next; } void SetPrevious( Component *comp) { //unlink if( Previous != NULL ) Previous->Next=comp; // make new link Previous = comp; Previous->Next = this; } virtual Complexe Evaluate(double freq)=0; virtual void Print() { cout < Branch; public: Impedance(string name, double v):Component(name, v) {} Complexe Evaluate(double freq) { //branch Complexe total; total.Reel = 0; total.Imag = 0; if( !Branch.empty() ) { Complexe branchImpedance; for(unsigned int i=0; i [0] do { branchImpedance.Accumulate( comp->Evaluate( freq ) ); comp = comp->GetNext(); }while( comp != NULL ); total.Accumulate( branchImpedance.Inverse() ); } } return total.Inverse(); } void AddBranch( Component *comp ) { Branch.push_back( comp ); } void Print() { cout< [i] do { comp->Print(); comp = comp->GetNext(); if( comp != NULL ) cout<<" + "; }while( comp != NULL ); if( (Branch.size() > 1) && (i < Branch.size()-1)) cout<< " // "; } cout<<"]"; } }; int main(void) { // //Impedance Z1("R series",0); //R2.SetPrevious(&R1); //R3.SetPrevious(&R2); //Z1.AddBranch(&R1); //Z1.Print(); //Z1.Evaluate(10).Print(); //cin.get(); //R1.Reset(); R2.Reset(); R3.Reset(); Resistor R1("R1",50), R2("R2",100) , R3("R3",100); Impedance Z1("",0); Impedance Z2("",0); Z2.AddBranch(&R2); Z2.AddBranch(&R3); Z2.SetPrevious(&R1); Z1.AddBranch(&R1); Z1.Print(); Z1.Evaluate(0).Print(); cin.get(); R1.Reset(); R2.Reset(); R3.Reset(); Capacitor C1("C1", 0.001); Inductor L1("L1", 0.002); Impedance RLC("RLC",0); RLC.AddBranch(&R1); RLC.AddBranch(&C1); RLC.AddBranch(&L1); RLC.Print(); cout<< endl; for( double f=0.01;f<100;f+=10) { cout<< "Z("<< f << ") "; RLC.Evaluate(f).Print(); } cin.get(); return 0; }