Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.00 KB | None | 0 0
  1. // JiPP.lab3.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #define _CRT_SECURE_NO_WARNINGS
  6. #include <iostream>
  7. #include <conio.h>
  8. #include <math.h>
  9. using namespace std;
  10. /*public void podaj_dane(int *a, int *b, int *c, int *d)
  11. {
  12. printf("Podaj a: ");
  13. scanf("%d", a);
  14. printf("Podaj b: ");
  15. scanf("%d", b);
  16. printf("Podaj c ");
  17. scanf("%d", c);
  18. printf("Podaj d: ");
  19. scanf("%d", d);
  20.  
  21. }*/
  22.  
  23.  
  24. struct l_zespolone {
  25. float cz_r;
  26. float cz_u;
  27. };
  28.  
  29. class Rownanie
  30. {
  31.  
  32. protected:
  33. float suma, roznica, iloraz;
  34. float p_delta, delta;
  35. public:
  36. // Rownanie();
  37. // ~Rownanie();
  38. int *dane_wejsciowe;
  39. virtual void podaj_dane()=0;
  40. virtual void formatuj_dane()=0;
  41. virtual void dodaj()=0;
  42. virtual void odejmij()=0;
  43. virtual void pomnoz()=0;
  44. };
  45.  
  46. class Rzeczywista :public Rownanie
  47. {
  48. protected:
  49. Rownanie *r;
  50. l_zespolone *l1;
  51. l_zespolone *l2;
  52. l_zespolone *l3;
  53. l_zespolone *l4;
  54. void podaj_dane();
  55. void formatuj_dane();
  56. void dodaj();
  57. void odejmij();
  58. void pomnoz();
  59. Rzeczywista();
  60. ~Rzeczywista();
  61. public:
  62.  
  63. float oblicz_d();
  64. float oblicz_p_d(float delta);
  65. };
  66.  
  67. Rzeczywista::Rzeczywista()
  68. {
  69. r = new Rzeczywista;
  70. l1 = new l_zespolone;
  71. l2 = new l_zespolone;
  72. l3 = new l_zespolone;
  73. l4 = new l_zespolone;
  74. r->dane_wejsciowe = new int[4];
  75. l1->cz_r = 0; l1->cz_u = 0;
  76. l2->cz_r = 0; l2->cz_u = 0;
  77. l3->cz_r = 0; l3->cz_u = 0;
  78. l4->cz_r = 0; l4->cz_u = 0;
  79. suma = 0; roznica = 0; iloraz = 0;
  80. p_delta = 0; delta = 0;
  81.  
  82.  
  83. }
  84. class Urojona:Rzeczywista
  85. {
  86. Urojona();
  87. ~Urojona();
  88. public:
  89. float oblicz_d();
  90. float oblicz_p_d(float delta);
  91. void oblicz_rne(Rownanie *r, l_zespolone *a, l_zespolone *b, l_zespolone *c, l_zespolone *d);
  92. };
  93. Urojona::Urojona()
  94. {
  95. r = new Urojona;
  96. l1 = new l_zespolone;
  97. l2 = new l_zespolone;
  98. l3 = new l_zespolone;
  99. l4 = new l_zespolone;
  100. r->dane_wejsciowe = new int[4];
  101. l1->cz_r = 0; l1->cz_u = 0;
  102. l2->cz_r = 0; l2->cz_u = 0;
  103. l3->cz_r = 0; l3->cz_u = 0;
  104. l4->cz_r = 0; l4->cz_u = 0;
  105. suma = 0; roznica = 0; iloraz = 0;
  106. p_delta = 0; delta = 0;
  107.  
  108.  
  109. }
  110. void Rzeczywista::podaj_dane()
  111. {
  112. for (int i = 0; i < 4; i++)
  113. {
  114. cin >> dane_wejsciowe[i];
  115. }
  116. }
  117. void Rownanie::formatuj_dane()
  118. {
  119. if (dane_wejsciowe[0] != 0)
  120. {
  121. if (dane_wejsciowe[0] == 1)
  122. printf("xx");
  123. else if (dane_wejsciowe[0] == -1)
  124. printf("-xx");
  125. else
  126. printf("%dxx", dane_wejsciowe[0]);
  127. }
  128. if (dane_wejsciowe[1] != 0)
  129. {
  130. if (dane_wejsciowe[1] == 1 && dane_wejsciowe[0] == 0)
  131. printf("x");
  132. else if (dane_wejsciowe[1] == 1 && dane_wejsciowe[0] != 0)
  133. printf("+x");
  134. else if (dane_wejsciowe[1] == -1)
  135. printf("-x");
  136. else if (dane_wejsciowe[1] > 1 && dane_wejsciowe[0] == 0)
  137. printf("%dx", dane_wejsciowe[1]);
  138. else if (dane_wejsciowe[1] > 1 && dane_wejsciowe[0] != 0)
  139. printf("+%dx", dane_wejsciowe[1]);
  140. else
  141. printf("%dx", dane_wejsciowe[1]);
  142. }
  143. if (dane_wejsciowe[2] != 0)
  144. {
  145. if (dane_wejsciowe[2] == 1 && dane_wejsciowe[0] == 0 && dane_wejsciowe[1] == 0)
  146. printf("1");
  147. else if (dane_wejsciowe[2] == 1)
  148. printf("+1");
  149. else if (dane_wejsciowe[2] == -1)
  150. printf("-1");
  151. else if (dane_wejsciowe[2] > 1 && dane_wejsciowe[0] == 0 && dane_wejsciowe[1] == 0)
  152. printf("%d", dane_wejsciowe[2]);
  153. else if (dane_wejsciowe[2] > 1)
  154. printf("+%d", dane_wejsciowe[2]);
  155. else
  156. printf("%d", dane_wejsciowe[2]);
  157. }
  158. if (dane_wejsciowe[3] != 0)
  159. {
  160. if (dane_wejsciowe[3] == 1 && dane_wejsciowe[0] == 0 && dane_wejsciowe[1] == 0 && dane_wejsciowe[2] == 0)
  161. printf("i");
  162. else if (dane_wejsciowe[3] == 1)
  163. printf("+i");
  164. else if (dane_wejsciowe[3] == -1)
  165. printf("-i");
  166. else if (dane_wejsciowe[3] > 1 && dane_wejsciowe[0] == 0 && dane_wejsciowe[1] == 0 && dane_wejsciowe[2] == 0)
  167. printf("%di", dane_wejsciowe[3]);
  168. else if (dane_wejsciowe[3] > 1)
  169. printf("+%di", dane_wejsciowe[3]);
  170. else
  171. printf("%di", dane_wejsciowe[3]);
  172. }
  173. printf("\n");
  174. }
  175.  
  176. float Rzeczywista::oblicz_d()
  177. {
  178. this->delta = (r->dane_wejsciowe[1])*(r->dane_wejsciowe[1]) - (4.0*(r->dane_wejsciowe[0])*(r->dane_wejsciowe[2]));
  179. return delta;
  180. }
  181. float Urojona::oblicz_d()
  182. {
  183. this->delta= (-4.0 * (r->dane_wejsciowe[0] * r->dane_wejsciowe[3]));
  184. return delta;
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191. float Rzeczywista::oblicz_p_d(float delta)
  192. {
  193. return (sqrt((sqrt(oblicz_d()*oblicz_d() + (-4.0 * (r->dane_wejsciowe[0] * r->dane_wejsciowe[3]))*(-4.0 * (r->dane_wejsciowe[0] * r->dane_wejsciowe[3]))) + oblicz_d()) / 2));
  194. }
  195.  
  196. float Urojona::oblicz_p_d(float delta)
  197. {
  198. return (sqrt((sqrt(Rzeczywista::oblicz_d()*Rzeczywista::oblicz_d() + delta*delta) - Rzeczywista::oblicz_d()) / 2));
  199. }
  200.  
  201. void Urojona::oblicz_rne(Rownanie *r, l_zespolone *x1, l_zespolone *x2, l_zespolone *x3, l_zespolone *x4)
  202. {
  203. if (r->dane_wejsciowe[0] != 0 && r->dane_wejsciowe[3] == 0)
  204. {
  205. //CASE 1.1
  206. if (Rzeczywista::oblicz_d() > 0)
  207. {
  208. x1->cz_r = (-1.0*r->dane_wejsciowe[1] - sqrt(Rzeczywista::oblicz_d())) / 2;
  209. x2->cz_r = (-1.0*r->dane_wejsciowe[1] + sqrt(Rzeczywista::oblicz_d())) / 2;
  210. }
  211.  
  212. //CASE 1.2
  213. else if (Rzeczywista::oblicz_d() == 0)
  214. {
  215. x1->cz_r = (-1.0*r->dane_wejsciowe[1]) / (2.0 * a);
  216. }
  217. //CASE 1.3
  218. else if (Rzeczywista::oblicz_d() < 0)
  219. {
  220. x1->cz_r = (-1.0*r->dane_wejsciowe[1]) / (2.0 * r->dane_wejsciowe[0]);
  221. x1->cz_u = -(sqrt((Rzeczywista::oblicz_d()*Rzeczywista::oblicz_d()) / 2) / (2.0 * r->dane_wejsciowe[0]));
  222. x2->cz_r = x1->cz_r;
  223. x2->cz_u = -x1->cz_u;
  224. }
  225. }
  226. //CASE 2
  227. else if (r->dane_wejsciowe[0] == 0 && r->dane_wejsciowe[1] != 0 && r->dane_wejsciowe[3] == 0)
  228. {
  229. x1->cz_r = (-1.0*r->dane_wejsciowe[2]) / (1.0*r->dane_wejsciowe[1]);
  230. }
  231. //CASE 3
  232. else if (r->dane_wejsciowe[0] == 0 && b == 0 && (c != 0 || d != 0))
  233. {
  234. printf("rownanie sprzeczne \n");
  235. }
  236. //CASE 4
  237. else if (a == 0, b == 0, c == 0, d == 0)
  238. {
  239. printf("rownanie tozsamosciowe \n");
  240. }
  241. //CASE 5
  242. else if (a == 0 && b != 0 && d != 0)
  243. {
  244. *x1r = (-1.0*c) / (1.0*b);
  245. *x1u = (-1.0*d) / (1.0*b);
  246. }
  247. else if (a != 0 && d != 0)
  248. {
  249.  
  250. *x1r = ((-1.0*b - oblicz_p_dr(oblicz_d_r(a, b, c), oblicz_d_u(a, d))) / (2.0 * a));
  251. *x2r = ((-1.0*b - oblicz_p_dr(oblicz_d_r(a, b, c), oblicz_d_u(a, d))) / (2.0 * a));
  252. *x3r = ((-1.0*b + oblicz_p_dr(oblicz_d_r(a, b, c), oblicz_d_u(a, d))) / (2.0 * a));
  253. *x4r = ((-1.0*b + oblicz_p_dr(oblicz_d_r(a, b, c), oblicz_d_u(a, d))) / (2.0 * a));
  254. *x1u = (oblicz_p_du(oblicz_d_r(a, b, c), oblicz_d_u(a, d)) / (2 * a));
  255. *x2u = (-oblicz_p_du(oblicz_d_r(a, b, c), oblicz_d_u(a, d)) / (2 * a));
  256. *x3u = (oblicz_p_du(oblicz_d_r(a, b, c), oblicz_d_u(a, d)) / (2 * a));
  257. *x4u = (-oblicz_p_du(oblicz_d_r(a, b, c), oblicz_d_u(a, d)) / (2 * a));
  258. }
  259. }
  260. void dodaj(float deltar, float deltau, float *x1r, float *x2r, float *x3r, float *x4r, float *x1u, float *x2u, float *x3u, float *x4u, float *sr, float *su)
  261. {
  262. if (deltar > 0 && deltau == 0)
  263. {
  264. *sr = *x1r + *x2r;
  265. }
  266. else if (deltar < 0 && deltau == 0)
  267. {
  268. *sr = *x1r + *x2r;
  269. *su = *x1u + *x2u;
  270. }
  271. else if (deltau != 0)
  272. {
  273. *sr = *x1r + *x2r + *x3r + *x4r;
  274. *su = *x1u + *x2u + *x3u + *x4u;
  275. }
  276. }
  277. void odejmij(float deltar, float deltau, float *x1r, float *x2r, float *x3r, float *x4r, float *x1u, float *x2u, float *x3u, float *x4u, float *rr, float *ru)
  278. {
  279. if (deltar > 0 && deltau == 0)
  280. {
  281. *rr = *x1r - *x2r;
  282. }
  283. else if (deltar < 0 && deltau == 0)
  284. {
  285. *rr = *x1r - *x2r;
  286. *ru = *x1u - *x2u;
  287. }
  288. else if (deltau != 0)
  289. {
  290. *rr = *x1r - *x2r - *x3r - *x4r;
  291. *ru = *x1u - *x2u - *x3u - *x4u;
  292. }
  293. }
  294. void pomnoz(float deltar, float deltau, float *x1r, float *x2r, float *x3r, float *x4r, float *x1u, float *x2u, float *x3u, float *x4u, float *ilr, float *ilu)
  295. {
  296. if (deltar > 0 && deltau == 0)
  297. {
  298. *ilr = *x1r * *x2r;
  299. }
  300. else if (deltar < 0 && deltau == 0)
  301. {
  302. *ilr = (*x1r * *x2r) - (*x1u * *x2u);
  303. *ilu = (*x1r * *x2u) - (*x1u * *x2r);
  304. }
  305. else if (deltau != 0)
  306. {
  307. *ilr = ((*x1r * *x2r) - (*x1u * *x2u))*((*x3r * *x4r) - (*x3u * *x4u)) -
  308. ((*x1u * *x2u) + (*x2r * *x1r))* (((*x3r * *x4u) + (*x4r * *x3u)));
  309. *ilu = ((*x1r * *x2r) - (*x1u * *x2u))*((*x3r * *x4u) - (*x3u * *x4r)) +
  310. ((*x1r * *x2u) + (*x2r * *x1u))* (((*x3r * *x4r) + (*x4u * *x3u)));
  311. }
  312. }
  313. void wyswietl_wynik(int a, int b, int c, int d, float x1r, float x2r, float x3r, float x4r, float x1u, float x2u, float x3u, float x4u, float sr, float su, float rr, float ru, float ilr, float ilu)
  314. {
  315. printf("a=%d b=%d c=%d d=%d \n", a, b, c, d);
  316. if (a != 0 && d == 0)
  317. {
  318. //CASE 1.1
  319. if (oblicz_d_r(a, b, c) > 0)
  320. {
  321. printf("x1r=%f, x2r=%f, \n", x1r, x2r);
  322. }
  323. else if (oblicz_d_r(a, b, c) == 0)
  324. {
  325. printf("x1r=%f, \n", x1r);
  326. }
  327. else if (oblicz_d_r(a, b, c) < 0)
  328. {
  329. printf("x1r=%f, x2r=%f, x1u=%fi, x2u=%fi \n", x1r, x2r, x1u, x2u);
  330. }
  331. }
  332. else if (a == 0 && b != 0 && d == 0)
  333. {
  334. printf("x1r=%f, \n", x1r);
  335. }
  336. else if (a == 0 && b == 0 && (c != 0 || d != 0))
  337. {
  338. printf("rownanie sprzeczne \n");
  339. }
  340. else if (a == 0, b == 0, c == 0, d == 0)
  341. {
  342. printf("rownanie tozsamosciowe \n");
  343. }
  344. else if (a == 0 && b != 0 && d != 0)
  345. {
  346. printf("x1r=%f, x1u=%fi \n", x1r, x1u);
  347. }
  348. else
  349. printf("x1r=%f, x2r=%f, x3r=%f, x4r=%f, x1u=%fi, x2u=%fi, x3u=%fi, x4u=%fi \n", x1r, x2r, x3r, x4r, x1u, x2u, x3u, x4u);
  350. if (oblicz_d_r(a, b, c) > 0 && oblicz_d_u(a, d) == 0)
  351. {
  352. printf("sr=%f, rr=%f, ilr=%f, \n", sr, rr, ilr);
  353. }
  354. else
  355. printf("sr=%f, su=%f, rr=%f, ru=%f, ilr=%f, ilu=%f \n", sr, su, rr, ru, ilr, ilu);
  356.  
  357. }
  358.  
  359.  
  360. int main()
  361. {
  362. int a = 0, b = 0, c = 0, d = 0;
  363. float x1r = 0, x2r = 0, x3r = 0, x4r = 0, x1u = 0, x2u = 0, x3u = 0, x4u = 0;
  364. float sr = 0, su = 0, rr = 0, ru = 0, ilr = 0, ilu = 0;
  365. float deltar = oblicz_d_r(a, b, c);
  366. float deltau = oblicz_d_u(a, d);
  367. oblicz_rne(a, b, c, d, &x1r, &x2r, &x3r, &x4r, &x1u, &x2u, &x3u, &x4u);
  368. dodaj(deltar, deltau, &x1r, &x2r, &x3r, &x4r, &x1u, &x2u, &x3u, &x4u, &sr, &su);
  369. odejmij(deltar, deltau, &x1r, &x2r, &x3r, &x4r, &x1u, &x2u, &x3u, &x4u, &rr, &ru);
  370. pomnoz(deltar, deltau, &x1r, &x2r, &x3r, &x4r, &x1u, &x2u, &x3u, &x4u, &ilr, &ilu);
  371. wyswietl_wynik(a, b, c, d, x1r, x2r, x3r, x4r, x1u, x2u, x3u, x4u, sr, su, rr, ru, ilr, ilu);
  372.  
  373. getchar();
  374. return 0;
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement