Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.02 KB | None | 0 0
  1. package pl.adam.paradygmaty;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class main
  7. {
  8.  
  9. public static void main(String[] args)
  10. {
  11.  
  12.  
  13. Rownanie_ogolne rownanie = new Rownanie();
  14. rownanie.podaj_dane();
  15. rownanie.formatuj_rownanie();
  16.  
  17.  
  18. Delta delta = new Delta();
  19. delta.oblicz_d_r(rownanie.Get_a(), rownanie.Get_b(), rownanie.Get_c());
  20. delta.oblicz_d_u(rownanie.Get_a(), rownanie.Get_d());
  21.  
  22. Delta pdelta = new PDelat();
  23.  
  24. pdelta.oblicz_p_d_r(delta.getDelta_r(), delta.getDelta_u());
  25. pdelta.oblicz_p_d_u(delta.getDelta_r(), delta.getDelta_u());
  26.  
  27. Licz_rownanie liczRownanie = new Licz_rownanie();
  28. liczRownanie.oblicz_rownanie(rownanie.Get_a(), rownanie.Get_b(), rownanie.Get_c(), rownanie.Get_d());
  29.  
  30.  
  31. Dzialanie dzialanie = new Dzialanie();
  32. dzialanie.dodaj(delta.getDelta_r(),delta.getDelta_u(),liczRownanie.getX1r(),liczRownanie.getX2r(),liczRownanie.getX3r(),liczRownanie.getX4r(),liczRownanie.getX1u(),liczRownanie.getX2u(),liczRownanie.getX3u(),liczRownanie.getX4u());
  33. dzialanie.odejmij(delta.getDelta_r(),delta.getDelta_u(),liczRownanie.getX1r(),liczRownanie.getX2r(),liczRownanie.getX3r(),liczRownanie.getX4r(),liczRownanie.getX1u(),liczRownanie.getX2u(),liczRownanie.getX3u(),liczRownanie.getX4u());
  34.  
  35.  
  36. WyswietlWyniki wyswietlWynik = new WyswietlWyniki();
  37. wyswietlWynik.wyswietl_wynik(rownanie.Get_a(), rownanie.Get_b(), rownanie.Get_c(), rownanie.Get_d(),delta.getDelta_r(), delta.getDelta_u(),liczRownanie.getX1r(),liczRownanie.getX2r(),liczRownanie.getX3r(),liczRownanie.getX4r(),liczRownanie.getX1u(),liczRownanie.getX2u(),liczRownanie.getX3u(),liczRownanie.getX4u(),dzialanie.getSr(),dzialanie.getSu(),dzialanie.getRr(),dzialanie.getRu());
  38.  
  39. }
  40. }
  41.  
  42. abstract class Rownanie_ogolne
  43. {
  44. protected int tab[]= new int[4];
  45. public abstract void podaj_dane();
  46. public abstract void formatuj_rownanie();
  47. public abstract int Get_a();
  48. public abstract int Get_b();
  49. public abstract int Get_c();
  50. public abstract int Get_d();
  51. }
  52.  
  53. class Rownanie extends Rownanie_ogolne
  54. {
  55. public void podaj_dane()
  56. {
  57.  
  58. System.out.print( "Podaj a:");
  59. Scanner a = new Scanner(System.in);
  60. tab[0] = a.nextInt();
  61. System.out.print( "Podaj b:");
  62. Scanner b = new Scanner(System.in);
  63. tab[1] = b.nextInt();
  64. System.out.print( "Podaj c:");
  65. Scanner c = new Scanner(System.in);
  66. tab[2] = c.nextInt();
  67. System.out.print( "Podaj d:");
  68. Scanner d = new Scanner(System.in);
  69. tab[3] = d.nextInt();
  70. System.out.println();
  71. }
  72.  
  73.  
  74. public void formatuj_rownanie()
  75. {
  76. if (!(tab[0] == 0 && tab[1] == 0 && tab[2] == 0 && tab[3] == 0))
  77. {
  78. if (tab[0] == 1) System.out.print("xx");
  79. else if (tab[0] == -1) System.out.print("-xx");
  80. else if (tab[0] != 0) System.out.print(tab[0] + "xx");
  81. if (tab[1] == 1 && tab[0] == 0) System.out.print("x");
  82. else if (tab[1] == 1 && tab[0] != 0) System.out.print("+x");
  83. else if (tab[1] == -1 && tab[0] == 0) System.out.print("-x");
  84. else if (tab[1] == -1 && tab[0] != 0) System.out.print("x");
  85. else if (tab[1] > 0 && tab[0] != 0) System.out.print( "+" + tab[1] + "x");
  86. else if (tab[1] > 0 && tab[0] == 0) System.out.print(tab[1] + "x");
  87. else if (tab[1] < 0) System.out.print( tab[1] + "x");
  88. if (tab[2] > 0 && (tab[0] != 0 || tab[1] != 0))System.out.print( "+" + tab[2]);
  89. else if (tab[2] > 0 && tab[0] == 0 && tab[1] == 0) System.out.print(tab[2] + "");
  90. else if (tab[2] == 0 && tab[0] == 0 && tab[1] == 0) System.out.print( tab[2]);
  91. else if (tab[2] < 0 && (tab[0] != 0 || tab[1] != 0))System.out.print(tab[2]);
  92. else if (tab[2] < 0 && tab[0] == 0 && tab[1] == 0) System.out.print(tab[2]);
  93. if (tab[3] < -1)System.out.print(tab[3] + "i");
  94. else if (tab[3] == -1) System.out.print( "-i");
  95. else if (tab[3] == 1) System.out.print("+i");
  96. else if (tab[3] > 0) System.out.print("+"+tab[3]+"i");
  97. else if (tab[3] == 0) System.out.print("");
  98. System.out.println("=0");
  99.  
  100. }
  101. }
  102.  
  103.  
  104. public int Get_a()
  105. {
  106. return tab[0];
  107. }
  108. public int Get_b()
  109. {
  110. return tab[1];
  111. }
  112. public int Get_c()
  113. {
  114. return tab[2];
  115. }
  116. public int Get_d()
  117. {
  118. return tab[3];
  119. }
  120.  
  121.  
  122. }
  123.  
  124. class Delta extends Rownanie
  125. {
  126.  
  127.  
  128. protected float delta_r, delta_u;
  129.  
  130. public Delta()
  131. {
  132.  
  133. }
  134.  
  135. public float oblicz_d_r(int a, int b, int c)
  136. {
  137. delta_r = ((b*b) - (4 * a*c));
  138. return delta_r;
  139. }
  140. public float oblicz_d_u(int a, int d)
  141. {
  142. delta_u = (-4 * a*d);
  143. return delta_u;
  144. }
  145. public float getDelta_r()
  146. {
  147. return delta_r;
  148. }
  149.  
  150. public float getDelta_u()
  151. {
  152. return delta_u;
  153. }
  154. public float oblicz_p_d_r(float delta_r, float delta_u)
  155. {
  156. return 0;
  157. }
  158.  
  159. public float oblicz_p_d_u(float delta_r, float delta_u)
  160. {
  161. return 0;
  162. }
  163.  
  164. }
  165.  
  166. class PDelat extends Delta
  167. {
  168. public PDelat()
  169. {
  170.  
  171. }
  172.  
  173. public float oblicz_p_d_r(float delta_r, float delta_u)
  174. {
  175. float pdelta_r = (float) java.lang.Math.sqrt((java.lang.Math.sqrt(delta_r*delta_r + delta_u * delta_u) + delta_r) / 2);
  176. return pdelta_r;
  177. }
  178.  
  179. public float oblicz_p_d_u(float delta_r, float delta_u)
  180. {
  181. float pdelta_u = (float) java.lang.Math.sqrt((java.lang.Math.sqrt(delta_r*delta_r + delta_u * delta_u) - delta_r) / 2);
  182. return pdelta_u;
  183. }
  184. }
  185. class Licz_rownanie extends Delta
  186. {
  187.  
  188. protected float x1r=2, x2r, x3r, x4r, x1u, x2u, x3u, x4u;
  189. public void setX1r(float x1r) {
  190. this.x1r = x1r;
  191. }
  192. public void setX2r(float x2r) {
  193. this.x2r = x2r;
  194. }
  195. public void setX3r(float x3r) {
  196. this.x3r = x3r;
  197. }
  198. public void setX4r(float x4r) {
  199. this.x4r = x4r;
  200. }
  201. public void setX1u(float x1u) {
  202. this.x1u = x1u;
  203. }
  204. public void setX2u(float x2u) {
  205. this.x2u = x2u;
  206. }
  207. public void setX3u(float x3u) {
  208. this.x3u = x3u;
  209. }
  210. public void setX4u(float x4u) {
  211. this.x4u = x4u;
  212. }
  213. public float getX1r() {
  214. return x1r;
  215. }
  216. public float getX2r() {
  217. return x2r;
  218. }
  219. public float getX3r() {
  220. return x3r;
  221. }
  222. public float getX4r() {
  223. return x4r;
  224. }
  225. public float getX1u() {
  226. return x1u;
  227. }
  228. public float getX2u() {
  229. return x2u;
  230. }
  231. public float getX3u() {
  232. return x3u;
  233. }
  234. public float getX4u() {
  235. return x4u;
  236. }
  237.  
  238. public Licz_rownanie()
  239. {
  240.  
  241. }
  242. public void oblicz_rownanie(int a, int b, int c, int d)
  243. {
  244.  
  245.  
  246. if (a != 0 && d == 0)//1
  247. {
  248. float delta_r = oblicz_d_r(a, b, c);
  249. if (delta_r > 0)
  250. {
  251. setX1r((float) ((-b - java.lang.Math.sqrt(delta_r)) / (2 * a)));
  252.  
  253. setX2r((float) ((-b + java.lang.Math.sqrt(delta_r)) / (2 * a)));
  254.  
  255. }
  256. if (delta_r == 0) x1r = (float) ((-1.0*b / (2.0 * a)));
  257. if (delta_r < 0)
  258. {
  259. setX1r((float) ((-1.0*b) / (2.0 * a)));
  260. setX1u((float) -(java.lang.Math.sqrt((oblicz_d_r(a, b, c)*oblicz_d_r(a, b, c)) / 2) / (2.0 * a)));
  261.  
  262. setX2r(getX1r());
  263.  
  264. setX2u(-getX1u());
  265.  
  266. }
  267. }
  268. if (a == 0 && b != 0 && d == 0)//2
  269. {
  270. setX1r((float) ((-1.0*c) / (1.0*b)));
  271. }
  272. if (a == 0 && b == 0 && (c != 0 || d!= 0)) ;//3
  273. if (a == 0 && b == 0 && c == 0 && d== 0) ;//4
  274. if (a == 0 && b != 0 && c != 0)//5
  275. {
  276. setX1r( (float) ((-1.0*c) / (1.0*b)));
  277. setX1u( (float) ((-1.0*d) / (1.0*b)));
  278. }
  279. if (a != 0 && d != 0)//6
  280. {
  281. float delta_r = oblicz_d_r(a, b, c);
  282. float delta_u = oblicz_d_u(a, c);
  283.  
  284. float pdelta_r = oblicz_p_d_r(delta_r, delta_u);
  285. float pdelta_u = oblicz_p_d_u(delta_r, delta_u);
  286.  
  287. setX1r((float) ((-1.0*b - pdelta_r) / (2.0 * a)));
  288. setX1u( (float) ((-pdelta_u) / (2.0 * a)));
  289. setX2r( (float) ((-1.0*b - pdelta_r) / (2.0 * a)));
  290. setX2u( (float) ((pdelta_u) / (2.0 * a)));
  291. setX3r( (float) ((-1.0*b + pdelta_r) / (2.0 * a)));
  292. setX3u( (float) ((pdelta_u) / (2.0 * a)));
  293. setX4r( (float) ((-1.0*b + pdelta_r) / (2.0 * a)));
  294. setX4u( (float) ((-pdelta_u) / (2.0 * a)));
  295. }
  296. }
  297. }
  298.  
  299.  
  300.  
  301. class Dzialanie extends Licz_rownanie
  302. {
  303. protected float su, sr,rr,ru;
  304. public void setSu(float su) {
  305. this.su = su;
  306. }
  307. public void setSr(float sr) {
  308. this.sr = sr;
  309. }
  310. public void setRr(float rr) {
  311. this.rr = rr;
  312. }
  313. public void setRu(float ru) {
  314. this.ru = ru;
  315. }
  316. public float getSu() {
  317. return su;
  318. }
  319. public float getSr() {
  320. return sr;
  321. }
  322. public float getRr() {
  323. return rr;
  324. }
  325. public float getRu() {
  326. return ru;
  327. }
  328. public Dzialanie()
  329. {
  330.  
  331. }
  332. public void dodaj(float delta_r,float delta_u, float x1r, float x2r, float x3r, float x4r, float x1u, float x2u, float x3u, float x4u)
  333. {
  334.  
  335. if (delta_r>0 && delta_u == 0)
  336. {
  337. setSr((float)((x1r) + (x2r)));
  338. }
  339. else if (delta_r<0 || delta_u != 0)
  340. {
  341. setSr ((float)((x1r) + (x2r) + (x3r) + (x4r)));
  342. setSu((float)( (x1u) + (x2u) + (x3u) + (x4u)));
  343. }
  344. }
  345. void odejmij(float delta_r,float delta_u,float x1r, float x2r, float x3r, float x4r, float x1u, float x2u, float x3u, float x4u)
  346. {
  347. if (delta_r>0 && delta_u == 0)
  348. {
  349. setRr((float)((x1r) - (x2r)));
  350. }
  351. else if (delta_r<0 || delta_u != 0)
  352. {
  353. setRr((float)(x1r - x2r - x3r - x4r));
  354. setRu((float)(x1u - x2u - x3u - x4u));
  355. }
  356.  
  357. }
  358.  
  359. }
  360. class WyswietlWyniki extends Dzialanie
  361. {
  362. public WyswietlWyniki()
  363. {
  364.  
  365. }
  366. public void wyswietl_wynik(int a, int b, int c, int d, float deltaR, float deltaU, float x1r, float x2r, float x3r, float x4r, float x1u, float x2u, float x3u, float x4u, float sr, float su, float rr, float ru)
  367. {
  368.  
  369. if (a != 0 && d == 0) //1
  370. {
  371. if (deltaR > 0)
  372. {
  373. System.out.printf("x1r = %.3f\nx2r = %.3f\n", x1r, x2r);
  374. System.out.printf("sr = %.3f\nrr = %.3f\n", sr, rr);
  375. }
  376. else if (deltaR != 0)
  377. {
  378. System.out.printf("x1r = %.3f\n", x1r);
  379. }
  380. else if (deltaR < 0)
  381. {
  382. System.out.printf("x1r = %.3f\nx2r = %.3f\nx1u = %.3f\n x2u = %.3f\n", x1r, x2r, x1u, x2u);
  383. System.out.printf("sr = %.3f\nsu = %.3f\n", sr, su);
  384. System.out.printf("rr = %.3f\nru = %.3f\n", rr, ru);
  385. }
  386. }
  387. else if (a == 0 && b != 0 && d == 0)//2
  388. {
  389.  
  390. if (deltaR < 0)
  391. {
  392. System.out.printf("sr = %.3f\nsu = %.3f\n", sr, su);
  393. System.out.printf("rr = %.3f\nru = %.3f\n", rr, ru);
  394. }
  395. System.out.printf("x1r = %.3f\n", x1r);
  396. }
  397. else if (a == 0 && b == 0 && (c != 0 || d != 0))//3
  398. {
  399. System.out.print("Rownanie sprzeczne\n");
  400. }
  401. else if (a == 0 && b == 0 && c == 0 && d == 0)//4
  402. {
  403. System.out.print("Rownanie tozsamosciowe");
  404. }
  405. else if (a == 0 && b != 0 && c != 0)//5
  406. {
  407. System.out.printf("x1r = %.3f\nx1u = %.3f\n", x1r, x1u);
  408. }
  409.  
  410. else if (a!= 0 && d != 0)//6
  411. {
  412. if (deltaU >= 0) System.out.printf("delta = %.3f + %.3fi", deltaR, deltaU);
  413. else System.out.printf("delta = %.3f %.3fi\n", deltaR, deltaU);
  414.  
  415. System.out.printf("\nPierwiastek z delty\n", oblicz_p_d_r(oblicz_d_r(a, b, c), Math.abs(oblicz_d_u(a, d))));
  416. System.out.printf("x1r = "+ x1r+"\n");
  417. System.out.printf("x1u = %.3f\n", x1u);
  418. System.out.printf("x2r = %.3f\n", x2r);
  419. System.out.printf("x2u = %.3f\n", x2u);
  420. System.out.printf("x3r = %.3f\n", x3r);
  421. System.out.printf("x3u = %.3f\n", x3u);
  422. System.out.printf("x4r = %.3f\n", x4r);
  423. System.out.printf("x4u = %.3f\n", x4u);
  424. System.out.printf("sr = %.3f\nsu = %.3f\n", sr, su);
  425. System.out.printf("rr = %.3f\nru = %.3f\n", ru, rr);
  426. }
  427. }
  428. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement