Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : lab3.cpp
- // Author : tymej
- // Version :
- // Copyright : Your copyright notice
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <cmath>
- using namespace std;
- struct l_zesp{
- float cz_rz;
- float cz_ur;
- };
- void formatuj_zesp(struct l_zesp * lz)
- {
- if(lz->cz_ur>0) cout << lz->cz_rz << " + " << lz->cz_ur;
- if(lz->cz_ur<0) cout << lz->cz_rz << " " << lz->cz_ur;
- }
- class Dane;
- class Rownanie{
- public:
- virtual void wyswietl(Dane &) = 0;
- virtual void setDane(Dane&) = 0;
- virtual ~Rownanie();
- };
- Rownanie::~Rownanie(){}
- class Dane: virtual public Rownanie
- {
- private:
- int tab[4];
- public:
- Dane(){}
- inline int getA() const{return tab[0];}
- inline int getB() const{return tab[1];}
- inline int getC() const{return tab[2];}
- inline int getD() const{return tab[3];}
- void setDane(Dane & dane){cin >> tab[0] >> tab[1] >> tab[2] >> tab[3];}
- void wyswietl(Dane & dane){
- if(dane.getA()!=0)cout << dane.getA() << "xx ";
- if((dane.getA() == 0 && dane.getB()!=0) || (dane.getB()!=0 && dane.getB()<0))cout << dane.getB() << "x ";
- else if(dane.getB()!=0) cout <<" + " <<dane.getB()<< "x";
- if(dane.getC()!=0 && dane.getC()<0)cout <<" "<< dane.getC() << " ";
- else if(dane.getC()!=0) cout <<" + "<<dane.getC();
- if(dane.getD() != 0 && dane.getD()<0)cout << dane.getD() <<"i";
- if(dane.getD()!=0 && dane.getD() > 0)cout <<" + " <<dane.getD() << "i";
- cout << endl;
- }
- ~Dane();
- };
- Dane::~Dane(){}
- class Delta: protected Dane, virtual public Rownanie
- {
- private:
- float deltar;
- float deltau;
- public:
- Delta(): deltar(0), deltau(0){}
- ~Delta();
- void wyswietl(Dane&dane){
- if(dane.getA()!=0 && dane.getD() ==0)
- {
- cout << "Delta = " << deltar << endl;
- }
- if(dane.getA() != 0 && dane.getD() != 0)
- {
- if(deltau>0)cout << "DELTA = "<<deltar<<" + "<<deltau <<"i"<<endl;
- if(deltau<0)cout <<"DELTA = "<< deltar<<" "<<deltau << "i\n" << endl;
- }
- }
- inline float getDeltar() const{return deltar;}
- inline float getDeltau() const{return deltau;}
- void setDane(l_zesp *ptr){
- ptr->cz_rz = sqrt((sqrt((deltar*deltar) + (deltau*deltau)) + deltar)/2);
- ptr->cz_ur = sqrt((sqrt((deltar*deltar) + (deltau*deltau)) - deltar)/2);
- }
- void setDane(Dane & dane){
- this->deltar = dane.getB()*dane.getB() - 4 *dane.getA()*dane.getC();
- this->deltau = -4 *dane.getA()*dane.getD();
- }
- };
- Delta::~Delta(){}
- class Oblicz_rownanie: protected Delta, virtual public Rownanie
- {
- protected:
- float x1r,x2r,x3r,x4r;
- float x1u,x2u,x3u,x4u;
- Delta & delta;
- l_zesp * zesp;
- public:
- Oblicz_rownanie(Delta & del, l_zesp * ptr): x1r(0),x2r(0),x3r(0),x4r(0),x1u(0),x2u(0),x3u(0),x4u(0), delta(del), zesp(ptr){}
- ~Oblicz_rownanie();
- void setDane(Dane & dane){
- if(dane.getA()!=0 && dane.getD() ==0)
- {
- if(delta.getDeltar() > 0)
- {
- this->x1r = (-static_cast<float>(dane.getB()) - sqrt(delta.getDeltar()))/(2*static_cast<float>(dane.getA()));
- this->x2r = (-static_cast<float>(dane.getB()) + sqrt(delta.getDeltar()))/(2*static_cast<float>(dane.getA()));
- }
- /*1.2*/
- if(delta.getDeltar() == 0)
- {
- this->x1r = (-static_cast<float>(dane.getB())) / (2*static_cast<float>(dane.getA()));
- }
- /*1.3*/
- if(delta.getDeltar() < 0)
- {
- this->x1r = (-static_cast<float>(dane.getB()))/(2*static_cast<float>(dane.getA()));
- this->x1u = (- sqrt(fabs(delta.getDeltar() )))/ (2*(dane.getA()));
- this->x2r = this->x1r;
- this->x2u = -(this->x1u);
- }
- }
- /*2*/
- if(dane.getA() == 0 && dane.getB()!= 0 && dane.getD()==0)
- {
- this->x1r = - (static_cast<float>(dane.getC())/static_cast<float>(dane.getB()));
- }
- /*3*/
- if(dane.getA() == 0 && dane.getB() == 0 && (dane.getC() != 0 || dane.getC() != 0))
- {
- return;
- }
- /*4*/
- if(dane.getA()== 0 && dane.getB()== 0 && dane.getC()== 0 && dane.getD() == 0)
- {
- return;
- }
- /*5*/
- if(dane.getA() == 0 && dane.getB()!= 0 && dane.getD()!= 0)
- {
- this->x1r = -(static_cast<float>(dane.getC())/static_cast<float>(dane.getB()));
- this->x1u = -(static_cast<float>(dane.getD())/static_cast<float>(dane.getB()));
- }
- /*6*/
- if(dane.getA() != 0 && dane.getD()!= 0)
- {
- this->x1r = (- static_cast<float>(dane.getB()) - zesp->cz_rz)/(2*static_cast<float>(dane.getA()));
- this->x2r = (- static_cast<float>(dane.getB()) + zesp->cz_rz)/(2*static_cast<float>(dane.getA()));
- this->x3r = ( static_cast<float>(dane.getB()) + zesp->cz_rz)/(2*static_cast<float>(dane.getA()));
- this->x4r = ( static_cast<float>(dane.getB()) - zesp->cz_rz)/(2*static_cast<float>(dane.getA()));
- this->x1u = ((-static_cast<float>(dane.getB())) - zesp->cz_ur)/(2*static_cast<float>(dane.getA()));
- this->x2u = ((-static_cast<float>(dane.getB())) + zesp->cz_ur)/(2*static_cast<float>(dane.getA()));
- this->x3u = (static_cast<float>(dane.getB()) + zesp->cz_ur)/(2*static_cast<float>(dane.getA()));
- this->x4u = (static_cast<float>(dane.getB()) - zesp->cz_ur)/(2*static_cast<float>(dane.getA()));
- }
- }
- void wyswietl(Dane & dane){
- /*1*/
- if(dane.getA()!=0 && dane.getD() ==0)
- {
- if(delta.getDeltar() > 0)
- {
- cout << endl <<"x1r = " << x1r << endl;
- cout << "x2r = " << x2r<<endl;
- }
- /*1.2*/
- if(delta.getDeltar() ==0)
- {
- cout << endl << x1r;
- }
- /*1.3*/
- if(delta.getDeltar()< 0)
- {
- if(x1u>0)cout << "x1 =" << x1r << " + " << x1u << "i";
- if(x1u<0)cout << "x1 =" << x1r << " " << x1u << "i";
- cout << " " << endl;
- if(x2u>0)cout << "x2 = "<< x2r << " + " << x2u << "i";
- if(x2u<0)cout << "x2 = "<<x2r<<" "<<x2u<<"i";
- }
- }
- /*2*/
- if(dane.getA() == 0 && dane.getB() != 0 && dane.getD() ==0)
- {
- cout << endl << "x1r = "<< x1r <<" ";
- }
- /*3*/
- if(dane.getA() == 0 && dane.getB() == 0 && (dane.getC() != 0 || dane.getD() != 0))
- {
- cout << "Rownanie sprzeczne";
- }
- /*4*/
- if(dane.getA() == 0 && dane.getB() == 0 && dane.getC() == 0 && dane.getD() == 0)
- {
- cout << "Rownanie tozsamosciowe";
- }
- /*5*/
- if(dane.getA() == 0 && dane.getB() != 0 && dane.getD() != 0)
- {
- cout << "x1r = " << x1r<<endl;
- cout << "x1u = "<< x1u << endl;
- }
- /*6*/
- if(dane.getA() != 0 && dane.getD() != 0)
- {
- if(x1u>0)cout << "x1 = "<< x1r << " + "<<x1u<<"i";
- if(x1u<0)cout << "x1 = "<<x1r <<" "<<x1u<<"i";
- cout << " ";
- if(x2u>0)cout << "x2 = "<<x2r<<" + "<< x2u << "i";
- if(x2u<0)cout << "x2 = "<<x2r <<" "<< x2u<<"i";
- cout << endl;
- if(x3u>0)cout << "x3 = "<<x3r<<" + "<< x3u << "i";
- if(x3u<0)cout << "x3 = "<<x3r <<" "<< x3u<<"i";
- cout << " ";
- if(x4u>0)cout << "x4 = "<<x4r<<" + "<< x4u << "i";
- if(x4u<0)cout << "x4 = "<<x4r <<" "<< x4u<<"i";
- cout << " " << endl;
- }
- }
- };
- Oblicz_rownanie::~Oblicz_rownanie(){}
- class Sri: virtual public Rownanie, protected Oblicz_rownanie
- {
- private:
- float sr;
- float su;
- float ru;
- float rr;
- float ir;
- float iu;
- public:
- Sri(Delta&del, l_zesp *ptr):Oblicz_rownanie(del,ptr), sr(0),su(0),rr(0),ru(0),ir(0),iu(0){}
- void obliczRsi(){
- if(delta.getDeltau()!=0){
- sr = x1r + x2r + x3r + x4r;
- su = x1u + x2u + x3u + x4u;
- rr = (x1r) - (x2r) - (x3r) - (x4r);
- ru = (x1u) - (x2u) - (x3u) - (x4u);
- ir = ((((x1r)*(x2r)) - ((x1u)*(x2u))) * (((x3r)*(x4r)) - ((x3u)*(x4u))))
- - ((((x1r)*(x1u)) + ((x2r)*(x2u))) * (((x3r)*(x4u)) + ((x4r)*(x3u))));
- iu = ((((x1r)*(x2r)) - ((x1u)*(x2u))) * (((x3r)*(x4r)) - ((x3u)*(x4u))))
- + ((((x1r)*(x1u)) + ((x2r)*(x2u))) * (((x3r)*(x4u)) + ((x4r)*(x3u))));
- }
- if(delta.getDeltau() == 0 && delta.getDeltar()<0)
- {
- sr = (x1r) + (x2r);
- su = (x1u) + (x2u);
- rr = (x1r) - (x2r);
- ru = (x1u) - (x2u);
- ir = ((x1r)*(x2r)) - ((x1u)*(x2u));
- iu = ((x1r)*(x2u)) + ((x2r)*(x1u));
- }
- if(delta.getDeltau() == 0 && delta.getDeltar()> 0)
- {
- sr = (x1r) + (x2r);
- rr = (x1r) - (x2r);
- ir = (x1r) * (x2r);
- }
- }
- void wyswietlRsi(Dane & dane){
- if(dane.getA()!=0 && dane.getD() ==0)
- {
- if(delta.getDeltar()> 0)
- {
- cout << "sr = " << sr <<" rr = "<<rr<<" ir = "<< ir;
- }
- if(delta.getDeltar() < 0){
- cout << endl << "sr = " << sr <<" su = "<<su << endl;
- cout << endl << "rr = " << rr <<" ru = "<<ru << endl;
- cout << endl << "ir = " << ir <<" iu = "<<iu << endl;
- }
- }
- if(dane.getA() != 0 && dane.getD() != 0)
- {
- cout << endl << "sr = " << sr <<" su = "<<su;
- cout << endl << "rr = " << rr <<" ru = "<<ru;
- cout << endl << "ir = " << ir <<" iu = "<<iu;
- }
- cout << endl;
- }
- ~Sri();
- };
- Sri::~Sri(){}
- int main(void) {
- Rownanie * rownanie;
- l_zesp * mn = new l_zesp();
- Dane *dane = new Dane();
- rownanie = dane;
- rownanie->setDane(*dane);
- rownanie->wyswietl(*dane);
- Delta *delta = new Delta();
- rownanie = delta;
- rownanie->setDane(*dane);
- rownanie->wyswietl(*dane);
- delta->setDane(mn);
- Sri * wyniki = new Sri(*delta,mn);
- rownanie = wyniki;
- rownanie->setDane(*dane);
- rownanie->wyswietl(*dane);
- wyniki->obliczRsi();
- wyniki->wyswietlRsi(*dane);
- delete mn;
- delete dane;
- delete rownanie;
- delete wyniki;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement