Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1. public static int zerozero=0;
  2. public static SingleLinkedList A=new SingleLinkedList();
  3. public static SingleLinkedList B=new SingleLinkedList();
  4. public static SingleLinkedList C=new SingleLinkedList();
  5. public static SingleLinkedList R=new SingleLinkedList();
  6. public void setPolynomial(char poly, int[][] terms)
  7. {
  8. int found=0;
  9. int [] temp=new int[2];
  10. for (int i=1;i<terms.length;i++)
  11. {
  12. for (int j=0;j<terms.length;j++)
  13. {
  14. if (terms[i][1]>terms[j][1])
  15. {
  16. temp=terms[i];
  17. terms[i]=terms[j];
  18. terms[j]=temp;
  19. }
  20. }
  21. }
  22. SingleLinkedList polyL=new SingleLinkedList();
  23. if (poly=='A')
  24. {
  25. found=1;
  26. }
  27. else if (poly=='B')
  28. {
  29. found=2;
  30. }
  31. else if (poly=='C')
  32. {
  33. found=3;
  34. }
  35. else if (poly=='R')
  36. {
  37. found=4;
  38. }
  39. if (terms.length==1&&terms[0][0]==0&&terms[0][1]==0) {
  40. polyL.add(terms[0]);
  41. zerozero=1;
  42. }
  43. for (int i=0;i<terms.length;i++)
  44. {
  45. if (terms [i][0]!=0)
  46. {
  47. polyL.add(terms[i]);
  48. }
  49. }
  50. if (found==1)
  51. {
  52. A=polyL;
  53. }
  54. else if (found==2)
  55. {
  56. B=polyL;
  57. }
  58. else if(found==3)
  59. {
  60. C=polyL;
  61. }
  62. else if(found==4)
  63. {
  64. R=polyL;
  65. }
  66. }
  67. public String print(char poly)
  68. {
  69. int found=0;
  70. SingleLinkedList help=new SingleLinkedList();
  71. if((poly=='A')||(poly=='B')||(poly=='C')||(poly=='R'))
  72. {
  73. if(poly=='A')
  74. {
  75. help=A;
  76. }
  77. else if(poly=='B')
  78. {
  79. help=B;
  80. }
  81. else if(poly=='C')
  82. {
  83. help=C;
  84. }
  85. else
  86. {
  87. help=R;
  88. }
  89. }
  90. String equation="";
  91. if(!help.isEmpty())
  92. {
  93. for(int i=0;i<help.size();i++)
  94. {
  95. int[] temp=(int[]) help.get(i);
  96. if(temp[1]!=0)
  97. {
  98. String be=Integer.toString(temp[0])+"X^"+Integer.toString(temp[1]);
  99. equation=equation+be+'+';
  100. }
  101. else
  102. {
  103. String be=Integer.toString(temp[0]);
  104. equation=equation+be;
  105. found=1;
  106. }
  107.  
  108. }
  109. if (found==0) {
  110. equation = equation.substring(0, equation.length() - 1);
  111. }
  112. }
  113.  
  114. return equation;
  115. }
  116. public void clearPolynomial(char poly)
  117. {
  118. SingleLinkedList polyL=new SingleLinkedList();
  119. int found=0;
  120. if (poly=='A')
  121. {
  122. polyL=A;
  123. found=1;
  124.  
  125. }
  126. else if (poly=='B')
  127. {
  128. polyL=B;
  129. found=2;
  130. }
  131. else if (poly=='C')
  132. {
  133. polyL=C;
  134. found=3;
  135. }
  136. polyL.clear();
  137. if (found==1)
  138. {
  139. A=polyL;
  140. }
  141. else if (found==2)
  142. {
  143. B=polyL;
  144. }
  145. else if(found==3)
  146. {
  147. C=polyL;
  148. }
  149. }
  150. public float evaluatePolynomial(char poly, float value)
  151. {
  152. SingleLinkedList help=new SingleLinkedList();
  153. if((poly=='A')||(poly=='B')||(poly=='C')||(poly=='R'))
  154. {
  155. if(poly=='A')
  156. {
  157. help=A;
  158. }
  159. else if(poly=='B')
  160. {
  161. help=B;
  162. }
  163. else if(poly=='C')
  164. {
  165. help=C;
  166. }
  167. else
  168. {
  169. help=R;
  170. }
  171. }
  172. float result=0;
  173. if(!help.isEmpty())
  174. {
  175. for(int i=0;i<help.size();i++)
  176. {
  177. int[]temp=(int[]) help.get(i);
  178. result=(float) (result+(temp[0]*(Math.pow(value, temp[1]))));
  179. }
  180. }
  181. else
  182. {
  183. System.out.println("Variable not set");
  184. }
  185. return result;
  186. }
  187. public int[][] subtract(char poly1, char poly2)
  188. {
  189. SingleLinkedList help1=new SingleLinkedList();
  190. SingleLinkedList help2=new SingleLinkedList();
  191. if((poly1=='A')||(poly1=='B')||(poly1=='C'))
  192. {
  193. if(poly1=='A')
  194. {
  195. help1=A;
  196. }
  197. else if(poly1=='B')
  198. {
  199. help1=B;
  200. }
  201. else
  202. {
  203. help1=C;
  204. }
  205. }
  206. if((poly2=='A')||(poly2=='B')||(poly2=='C'))
  207. {
  208. if(poly2=='A')
  209. {
  210. help2=A;
  211. }
  212. else if(poly2=='B')
  213. {
  214. help2=B;
  215. }
  216. else
  217. {
  218. help2=C;
  219. }
  220. }
  221. SingleLinkedList exponents=new SingleLinkedList();
  222. SingleLinkedList coefficients=new SingleLinkedList();
  223. SingleLinkedList final_terms=new SingleLinkedList();
  224. if((help1.size()!=0)&&((help2.size()!=0)||(help2.size()==0)))
  225. {
  226. for(int i=0;i<help1.size();i++)
  227. {
  228. int found=0;
  229. int []temp=(int[]) help1.get(i);
  230. for(int j=0;j<help2.size();j++)
  231. {
  232. int []help_temp=(int[]) help2.get(j);
  233. if(temp[1]==help_temp[1])
  234. {
  235. found=1;
  236. int sub=temp[0]-help_temp[0];
  237. if(sub!=0)
  238. {
  239. int [] help_to_sub={sub,temp[1]};
  240. coefficients.add(help_to_sub);
  241. }
  242. exponents.add(help2.get(j));
  243. break;
  244. }
  245. }
  246. if(found==0)
  247. {
  248. coefficients.add(temp);
  249. }
  250. }
  251. for(int z=0;z<help2.size();z++)
  252. {
  253. int count=0;
  254. for(int r=0;r<exponents.size();r++)
  255. {
  256. int []please_help=(int[]) help2.get(z);
  257. int []please_help1=(int[])exponents.get(r);
  258. if((please_help[0]!=please_help1[0])&&(please_help[1]!=please_help1[1]))
  259. {
  260. count++;
  261. }
  262. }
  263. if(count==exponents.size())
  264. {
  265. int []please_help=(int[]) help2.get(z);
  266. if(please_help[0]!=0)
  267. {
  268. final_terms.add(help2.get(z));
  269. }
  270. }
  271. }
  272. }
  273. int [][] sub_result1=new int [help2.size()][2];
  274. if((help1.size()==0))
  275. {
  276. for(int u=0;u<help2.size();u++)
  277. {
  278. int[] s=(int[]) help2.get(u);
  279. sub_result1[u][0]=-s[0];
  280. sub_result1[u][1]=s[1];
  281. }
  282. return sub_result1;
  283. }
  284. if(coefficients.size()==0)
  285. {
  286. int [][] sub_result2= {{0,0}};
  287. return sub_result2 ;
  288. }
  289. int [][] sub_result=new int [final_terms.size()+coefficients.size()][2];
  290. for(int t=0;t<coefficients.size();t++)
  291. {
  292. int[] s=(int[]) coefficients.get(t);
  293. sub_result[t][0]=s[0];
  294. sub_result[t][1]=s[1];
  295. }
  296. for(int t=0;t<Math.abs(final_terms.size());t++)
  297. {
  298. int[] s=(int[]) final_terms.get(t);
  299. sub_result[coefficients.size()+t][0]=-s[0];
  300. sub_result[coefficients.size()+t][1]=s[1];
  301.  
  302. }
  303. int [] temp=new int[2];
  304. for (int i=1;i<sub_result.length;i++)
  305. {
  306. for (int j=0;j<sub_result.length;j++)
  307. {
  308. if (sub_result[i][1]>=sub_result[j][1])
  309. {
  310. temp=sub_result[i];
  311. sub_result[i]=sub_result[j];
  312. sub_result[j]=temp;
  313. }
  314. }
  315. }
  316. return sub_result;
  317.  
  318. }
  319. public int[][] add(char poly1, char poly2){
  320. int found =0;
  321. SingleLinkedList help=new SingleLinkedList();
  322. SingleLinkedList help1=new SingleLinkedList();
  323. SingleLinkedList help2=new SingleLinkedList();
  324. if (poly1=='A') {
  325. help1=A;
  326. }if (poly1=='B') {
  327. help1=B;
  328. }if (poly1=='C') {
  329. help1=C;
  330. }
  331. if (poly2=='A') {
  332. help2=A;
  333. }if (poly2=='B') {
  334. help2=B;
  335. }if (poly2=='C') {
  336. help2=C;
  337. }
  338.  
  339. int size=help1.size()+help2.size();
  340. int count=0;
  341. int k=0;
  342. int p1 [][]=new int [help1.size()][2];
  343. int p2 [][]=new int[help2.size()][2];
  344.  
  345. for (int i=0;i<help1.size();i++) {
  346. p1[i]=(int[]) help1.get(i);
  347. }
  348. for (int i=0;i<help2.size();i++) {
  349. p2[i]=(int[]) help2.get(i);
  350. }
  351. int sum [][]=new int[size][2];
  352. for (int i=0,j=0;i<size&&j<size;i++,j++) {
  353. if (i<help1.size()&&j<help2.size()){
  354. if (p1[i][1]==p2[j][1]) {
  355. sum [k][0]=p1[i][0]+p2[j][0];
  356. sum [k][1]=p1 [i][1];
  357. k++;
  358. }else {
  359. if (p1[i][1]>=p2[j][1]) {
  360. sum [k][0]=p1[i][0];
  361. sum [k][1]=p1 [i][1];
  362. j--;
  363. k++;
  364. }
  365. else if (p1[i][1]<p2[j][1]) {
  366. sum [k][0]=p2[j][0];
  367. sum [k][1]=p2 [j][1];
  368. i--;
  369. k++;
  370. }
  371. }}
  372. else if (i>=help1.size()&&j<help2.size()) {
  373. sum [k][0]=p2[j][0];
  374. sum [k][1]=p2[j][1];
  375. k++;
  376. }else if (i<help1.size()&&j>=help2.size()) {
  377. sum [k][0]=p1[j][0];
  378. sum [k][1]=p1[j][1];
  379. k++;
  380. }else {
  381. break;
  382. }
  383. }
  384. for (int i=0;i<sum.length;i++) {
  385. if (sum[i][0]!=0) {
  386. help.add(sum[i]);}
  387. }
  388. int sumf [][]=new int[help.size()][2];
  389. for (int i=0;i<help.size();i++) {
  390. sumf[i]=(int[])help.get(i);
  391. }
  392. return sumf;
  393. }
  394.  
  395. public static int max (int Parr[][]) {
  396. int m=-1000000000;
  397. for (int i=0;i<Parr.length;i++) {
  398. if (Parr[i][1]>m) {
  399. m=Parr[i][1];
  400. }
  401. }
  402. return m;
  403. }
  404. public int[][] multiply(char poly1, char poly2){
  405.  
  406. SingleLinkedList help1=new SingleLinkedList();
  407. SingleLinkedList help2=new SingleLinkedList();
  408. if (poly1=='A') {
  409. help1=A;
  410. }if (poly1=='B') {
  411. help1=B;
  412. }if (poly1=='C') {
  413. help1=C;
  414. }
  415. if (poly2=='A') {
  416. help2=A;
  417. }if (poly2=='B') {
  418. help2=B;
  419. }if (poly2=='C') {
  420. help2=C;
  421. }
  422. int zero[][]=new int[1][2];
  423. if (zerozero==1) {
  424. return zero;
  425. }
  426. int p1 [][]=new int [help1.size()][2];
  427. int p2 [][]=new int[help2.size()][2];
  428. for (int i=0;i<help1.size();i++) {
  429. p1[i]=(int[]) help1.get(i);
  430. }
  431.  
  432. for (int i=0;i<help2.size();i++) {
  433. p2[i]=(int[]) help2.get(i);
  434. }
  435. int max=max(p1)+max(p2);
  436. int []power=new int [max+1];
  437. int index;
  438. for (int i=0;i<help1.size();i++) {
  439. for (int j=0;j<help2.size();j++) {
  440. index= p1[i][1]+p2[j][1];
  441. power[index]=power[index]+p1[i][0]*p2[j][0];
  442. }
  443. }
  444. int [][]product =new int [max+1][2];
  445. for (int i=0,j=max;i<max+1&&j>-1;i++,j--) {
  446. if (power[j]!=0) {
  447. product[i][0]=power[j];
  448. product[i][1]=j;
  449. }
  450. }
  451. SingleLinkedList help=new SingleLinkedList();
  452. for (int i=0;i<product.length;i++) {
  453. if (product[i][0]!=0) {
  454. help.add(product[i]);
  455. }
  456. }
  457. int [][]productf =new int [help.size()][2];
  458. for (int i=0;i<help.size();i++) {
  459. productf[i]=(int[])help.get(i);
  460. }
  461. return productf;
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement