Advertisement
Guest User

Untitled

a guest
May 27th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.56 KB | None | 0 0
  1. //============================================================================
  2. // Name : lab3.cpp
  3. // Author : tymej
  4. // Version :
  5. // Copyright : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. #include <cmath>
  11. using namespace std;
  12.  
  13. struct l_zesp{
  14. float cz_rz;
  15. float cz_ur;
  16. };
  17.  
  18. void formatuj_zesp(struct l_zesp * lz)
  19. {
  20. if(lz->cz_ur>0) cout << lz->cz_rz << " + " << lz->cz_ur;
  21. if(lz->cz_ur<0) cout << lz->cz_rz << " " << lz->cz_ur;
  22. }
  23.  
  24. class Dane;
  25. class Rownanie{
  26. public:
  27. virtual void wyswietl(Dane &) = 0;
  28. virtual void setDane(Dane&) = 0;
  29. virtual ~Rownanie();
  30. };
  31. Rownanie::~Rownanie(){}
  32.  
  33. class Dane: virtual public Rownanie
  34. {
  35. private:
  36. int tab[4];
  37. public:
  38. Dane(){}
  39. inline int getA() const{return tab[0];}
  40. inline int getB() const{return tab[1];}
  41. inline int getC() const{return tab[2];}
  42. inline int getD() const{return tab[3];}
  43.  
  44. void setDane(Dane & dane){cin >> tab[0] >> tab[1] >> tab[2] >> tab[3];}
  45. void wyswietl(Dane & dane){
  46. if(dane.getA()!=0)cout << dane.getA() << "xx ";
  47.  
  48. if((dane.getA() == 0 && dane.getB()!=0) || (dane.getB()!=0 && dane.getB()<0))cout << dane.getB() << "x ";
  49. else if(dane.getB()!=0) cout <<" + " <<dane.getB()<< "x";
  50.  
  51. if(dane.getC()!=0 && dane.getC()<0)cout <<" "<< dane.getC() << " ";
  52. else if(dane.getC()!=0) cout <<" + "<<dane.getC();
  53.  
  54. if(dane.getD() != 0 && dane.getD()<0)cout << dane.getD() <<"i";
  55. if(dane.getD()!=0 && dane.getD() > 0)cout <<" + " <<dane.getD() << "i";
  56.  
  57. cout << endl;
  58. }
  59. ~Dane();
  60. };
  61. Dane::~Dane(){}
  62.  
  63. class Delta: protected Dane, virtual public Rownanie
  64. {
  65. private:
  66. float deltar;
  67. float deltau;
  68. public:
  69. Delta(): deltar(0), deltau(0){}
  70.  
  71. ~Delta();
  72. void wyswietl(Dane&dane){
  73. if(dane.getA()!=0 && dane.getD() ==0)
  74. {
  75. cout << "Delta = " << deltar << endl;
  76. }
  77. if(dane.getA() != 0 && dane.getD() != 0)
  78. {
  79. if(deltau>0)cout << "DELTA = "<<deltar<<" + "<<deltau <<"i"<<endl;
  80. if(deltau<0)cout <<"DELTA = "<< deltar<<" "<<deltau << "i\n" << endl;
  81. }
  82. }
  83.  
  84. inline float getDeltar() const{return deltar;}
  85. inline float getDeltau() const{return deltau;}
  86. void setDane(l_zesp *ptr){
  87. ptr->cz_rz = sqrt((sqrt((deltar*deltar) + (deltau*deltau)) + deltar)/2);
  88. ptr->cz_ur = sqrt((sqrt((deltar*deltar) + (deltau*deltau)) - deltar)/2);
  89. }
  90.  
  91. void setDane(Dane & dane){
  92. this->deltar = dane.getB()*dane.getB() - 4 *dane.getA()*dane.getC();
  93. this->deltau = -4 *dane.getA()*dane.getD();
  94. }
  95. };
  96. Delta::~Delta(){}
  97.  
  98.  
  99. class Oblicz_rownanie: protected Delta, virtual public Rownanie
  100. {
  101. protected:
  102. float x1r,x2r,x3r,x4r;
  103. float x1u,x2u,x3u,x4u;
  104.  
  105.  
  106. Delta & delta;
  107. l_zesp * zesp;
  108. public:
  109. 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){}
  110. ~Oblicz_rownanie();
  111.  
  112. void setDane(Dane & dane){
  113. if(dane.getA()!=0 && dane.getD() ==0)
  114. {
  115. if(delta.getDeltar() > 0)
  116. {
  117. this->x1r = (-static_cast<float>(dane.getB()) - sqrt(delta.getDeltar()))/(2*static_cast<float>(dane.getA()));
  118. this->x2r = (-static_cast<float>(dane.getB()) + sqrt(delta.getDeltar()))/(2*static_cast<float>(dane.getA()));
  119. }
  120. /*1.2*/
  121. if(delta.getDeltar() == 0)
  122. {
  123. this->x1r = (-static_cast<float>(dane.getB())) / (2*static_cast<float>(dane.getA()));
  124. }
  125. /*1.3*/
  126. if(delta.getDeltar() < 0)
  127. {
  128. this->x1r = (-static_cast<float>(dane.getB()))/(2*static_cast<float>(dane.getA()));
  129. this->x1u = (- sqrt(fabs(delta.getDeltar() )))/ (2*(dane.getA()));
  130. this->x2r = this->x1r;
  131. this->x2u = -(this->x1u);
  132. }
  133. }
  134. /*2*/
  135. if(dane.getA() == 0 && dane.getB()!= 0 && dane.getD()==0)
  136. {
  137. this->x1r = - (static_cast<float>(dane.getC())/static_cast<float>(dane.getB()));
  138. }
  139. /*3*/
  140. if(dane.getA() == 0 && dane.getB() == 0 && (dane.getC() != 0 || dane.getC() != 0))
  141. {
  142. return;
  143. }
  144. /*4*/
  145. if(dane.getA()== 0 && dane.getB()== 0 && dane.getC()== 0 && dane.getD() == 0)
  146. {
  147. return;
  148. }
  149. /*5*/
  150. if(dane.getA() == 0 && dane.getB()!= 0 && dane.getD()!= 0)
  151. {
  152. this->x1r = -(static_cast<float>(dane.getC())/static_cast<float>(dane.getB()));
  153. this->x1u = -(static_cast<float>(dane.getD())/static_cast<float>(dane.getB()));
  154. }
  155. /*6*/
  156. if(dane.getA() != 0 && dane.getD()!= 0)
  157. {
  158. this->x1r = (- static_cast<float>(dane.getB()) - zesp->cz_rz)/(2*static_cast<float>(dane.getA()));
  159. this->x2r = (- static_cast<float>(dane.getB()) + zesp->cz_rz)/(2*static_cast<float>(dane.getA()));
  160. this->x3r = ( static_cast<float>(dane.getB()) + zesp->cz_rz)/(2*static_cast<float>(dane.getA()));
  161. this->x4r = ( static_cast<float>(dane.getB()) - zesp->cz_rz)/(2*static_cast<float>(dane.getA()));
  162.  
  163. this->x1u = ((-static_cast<float>(dane.getB())) - zesp->cz_ur)/(2*static_cast<float>(dane.getA()));
  164. this->x2u = ((-static_cast<float>(dane.getB())) + zesp->cz_ur)/(2*static_cast<float>(dane.getA()));
  165. this->x3u = (static_cast<float>(dane.getB()) + zesp->cz_ur)/(2*static_cast<float>(dane.getA()));
  166. this->x4u = (static_cast<float>(dane.getB()) - zesp->cz_ur)/(2*static_cast<float>(dane.getA()));
  167. }
  168. }
  169.  
  170. void wyswietl(Dane & dane){
  171. /*1*/
  172.  
  173. if(dane.getA()!=0 && dane.getD() ==0)
  174. {
  175. if(delta.getDeltar() > 0)
  176. {
  177. cout << endl <<"x1r = " << x1r << endl;
  178. cout << "x2r = " << x2r<<endl;
  179. }
  180. /*1.2*/
  181. if(delta.getDeltar() ==0)
  182. {
  183. cout << endl << x1r;
  184. }
  185. /*1.3*/
  186. if(delta.getDeltar()< 0)
  187. {
  188.  
  189. if(x1u>0)cout << "x1 =" << x1r << " + " << x1u << "i";
  190. if(x1u<0)cout << "x1 =" << x1r << " " << x1u << "i";
  191. cout << " " << endl;
  192. if(x2u>0)cout << "x2 = "<< x2r << " + " << x2u << "i";
  193. if(x2u<0)cout << "x2 = "<<x2r<<" "<<x2u<<"i";
  194. }
  195. }
  196. /*2*/
  197. if(dane.getA() == 0 && dane.getB() != 0 && dane.getD() ==0)
  198. {
  199. cout << endl << "x1r = "<< x1r <<" ";
  200. }
  201. /*3*/
  202. if(dane.getA() == 0 && dane.getB() == 0 && (dane.getC() != 0 || dane.getD() != 0))
  203. {
  204. cout << "Rownanie sprzeczne";
  205. }
  206. /*4*/
  207. if(dane.getA() == 0 && dane.getB() == 0 && dane.getC() == 0 && dane.getD() == 0)
  208. {
  209. cout << "Rownanie tozsamosciowe";
  210. }
  211. /*5*/
  212. if(dane.getA() == 0 && dane.getB() != 0 && dane.getD() != 0)
  213. {
  214. cout << "x1r = " << x1r<<endl;
  215. cout << "x1u = "<< x1u << endl;
  216. }
  217. /*6*/
  218. if(dane.getA() != 0 && dane.getD() != 0)
  219. {
  220.  
  221. if(x1u>0)cout << "x1 = "<< x1r << " + "<<x1u<<"i";
  222. if(x1u<0)cout << "x1 = "<<x1r <<" "<<x1u<<"i";
  223. cout << " ";
  224. if(x2u>0)cout << "x2 = "<<x2r<<" + "<< x2u << "i";
  225. if(x2u<0)cout << "x2 = "<<x2r <<" "<< x2u<<"i";
  226. cout << endl;
  227. if(x3u>0)cout << "x3 = "<<x3r<<" + "<< x3u << "i";
  228. if(x3u<0)cout << "x3 = "<<x3r <<" "<< x3u<<"i";
  229. cout << " ";
  230. if(x4u>0)cout << "x4 = "<<x4r<<" + "<< x4u << "i";
  231. if(x4u<0)cout << "x4 = "<<x4r <<" "<< x4u<<"i";
  232. cout << " " << endl;
  233. }
  234. }
  235. };
  236. Oblicz_rownanie::~Oblicz_rownanie(){}
  237. class Sri: virtual public Rownanie, protected Oblicz_rownanie
  238. {
  239. private:
  240. float sr;
  241. float su;
  242. float ru;
  243. float rr;
  244. float ir;
  245. float iu;
  246.  
  247.  
  248. public:
  249.  
  250. Sri(Delta&del, l_zesp *ptr):Oblicz_rownanie(del,ptr), sr(0),su(0),rr(0),ru(0),ir(0),iu(0){}
  251.  
  252. void obliczRsi(){
  253. if(delta.getDeltau()!=0){
  254. sr = x1r + x2r + x3r + x4r;
  255. su = x1u + x2u + x3u + x4u;
  256.  
  257. rr = (x1r) - (x2r) - (x3r) - (x4r);
  258. ru = (x1u) - (x2u) - (x3u) - (x4u);
  259.  
  260. ir = ((((x1r)*(x2r)) - ((x1u)*(x2u))) * (((x3r)*(x4r)) - ((x3u)*(x4u))))
  261. - ((((x1r)*(x1u)) + ((x2r)*(x2u))) * (((x3r)*(x4u)) + ((x4r)*(x3u))));
  262.  
  263. iu = ((((x1r)*(x2r)) - ((x1u)*(x2u))) * (((x3r)*(x4r)) - ((x3u)*(x4u))))
  264. + ((((x1r)*(x1u)) + ((x2r)*(x2u))) * (((x3r)*(x4u)) + ((x4r)*(x3u))));
  265. }
  266. if(delta.getDeltau() == 0 && delta.getDeltar()<0)
  267. {
  268. sr = (x1r) + (x2r);
  269. su = (x1u) + (x2u);
  270. rr = (x1r) - (x2r);
  271. ru = (x1u) - (x2u);
  272.  
  273. ir = ((x1r)*(x2r)) - ((x1u)*(x2u));
  274. iu = ((x1r)*(x2u)) + ((x2r)*(x1u));
  275. }
  276. if(delta.getDeltau() == 0 && delta.getDeltar()> 0)
  277. {
  278. sr = (x1r) + (x2r);
  279. rr = (x1r) - (x2r);
  280. ir = (x1r) * (x2r);
  281. }
  282. }
  283.  
  284. void wyswietlRsi(Dane & dane){
  285. if(dane.getA()!=0 && dane.getD() ==0)
  286. {
  287.  
  288. if(delta.getDeltar()> 0)
  289. {
  290. cout << "sr = " << sr <<" rr = "<<rr<<" ir = "<< ir;
  291. }
  292. if(delta.getDeltar() < 0){
  293. cout << endl << "sr = " << sr <<" su = "<<su << endl;
  294. cout << endl << "rr = " << rr <<" ru = "<<ru << endl;
  295. cout << endl << "ir = " << ir <<" iu = "<<iu << endl;
  296. }
  297. }
  298. if(dane.getA() != 0 && dane.getD() != 0)
  299. {
  300. cout << endl << "sr = " << sr <<" su = "<<su;
  301. cout << endl << "rr = " << rr <<" ru = "<<ru;
  302. cout << endl << "ir = " << ir <<" iu = "<<iu;
  303. }
  304. cout << endl;
  305. }
  306. ~Sri();
  307. };
  308. Sri::~Sri(){}
  309.  
  310.  
  311.  
  312. int main(void) {
  313. Rownanie * rownanie;
  314. l_zesp * mn = new l_zesp();
  315. Dane *dane = new Dane();
  316.  
  317. rownanie = dane;
  318. rownanie->setDane(*dane);
  319. rownanie->wyswietl(*dane);
  320. Delta *delta = new Delta();
  321.  
  322. rownanie = delta;
  323. rownanie->setDane(*dane);
  324. rownanie->wyswietl(*dane);
  325. delta->setDane(mn);
  326.  
  327. Sri * wyniki = new Sri(*delta,mn);
  328. rownanie = wyniki;
  329. rownanie->setDane(*dane);
  330. rownanie->wyswietl(*dane);
  331.  
  332. wyniki->obliczRsi();
  333. wyniki->wyswietlRsi(*dane);
  334.  
  335. delete mn;
  336. delete dane;
  337. delete rownanie;
  338. delete wyniki;
  339. return 0;
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement