Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. struct node
  5. {
  6. int koef;
  7. int power;
  8. node* next;
  9. };
  10. bool empty(node* top)
  11. {
  12. return top == NULL;
  13. }
  14. node* find_end(node *top)
  15. {
  16. node *p = top;
  17. while (p->next)
  18. p = p->next;
  19. return p;
  20. }
  21. void pop(node* &top)
  22. {
  23. node* p;
  24. if (!empty(top))
  25. {
  26. p = top->next;
  27. delete top;
  28. top = p;
  29. }
  30. }
  31. void del_info(node *&top, int x)
  32. {
  33. node *p = top;
  34. if (top->koef == x)
  35. top = p->next;
  36. else
  37. {
  38. while ((p->next)->koef != x)
  39. p = p->next;
  40. p->next = (p->next)->next;
  41. }
  42. }
  43. void push(node* &top, int k, int po)
  44. {
  45. node* p=new node;
  46. p->koef = k;
  47. p->power = po;
  48. p->next = top;
  49. top = p;
  50. }
  51. node* create(ifstream&f, int n)
  52. {
  53. int i;
  54. node *top = NULL;
  55. int k, p;
  56. for (i = 0; i < n; i++)
  57. {
  58. f >> k;
  59. f >> p;
  60. push(top, k, p);
  61. }
  62. return top;
  63. }
  64. node* previous(node* top, node* p)
  65. {
  66. node* pr, *q;
  67. q = top;
  68. pr = NULL;
  69. while (q != p && q)
  70. {
  71. pr = q;
  72. q = q->next;
  73. }
  74. if (q == p) return pr;
  75. return NULL;
  76. }
  77. node* previous_info(node *top, int x)
  78. {
  79. node *p = top;
  80. if (top->koef == x)
  81. return NULL;
  82. else
  83. {
  84. while ((p->next)->koef != x && p)
  85. p = p->next;
  86. return p;
  87. }
  88. }
  89. void sort(node* top)
  90. {
  91. int koef, power;
  92. node* p = top, *q;
  93. while (p&&p->next)
  94. {
  95. q = p->next;
  96. while (q)
  97. {
  98. if (p->power < q->power)
  99. {
  100. power = p->power;
  101. p->power = q->power;
  102. q->power = power;
  103. koef = p->koef;
  104. p->koef = q->koef;
  105. q->koef = koef;
  106. }
  107. q = q->next;
  108. }
  109. p = p->next;
  110. }
  111. }
  112. node** create_mas(ifstream&f,int n)
  113. {
  114. node **mas = new node*[n];
  115. for (int i = 0; i < n; i++)
  116. {
  117. int N;
  118. f >> N;
  119. mas[i]= create(f, N);
  120. }
  121. return mas;
  122. }
  123. bool prime(int n)
  124. {
  125. int i;
  126. bool k = true;
  127. for (i = 2; i <= int(pow(n, 1.0 / 2)); i++)
  128. if (n%i == 0)
  129. return false;
  130. if (k == true)
  131. return 1;
  132. else
  133. return 0;
  134. }
  135. bool prime_polinom(node *top)
  136. {
  137. bool t = 1;
  138. node *p = top;
  139. while (p && t)
  140. {
  141. if (!prime(p->power))
  142. t = 0;
  143. p = p->next;
  144. }
  145. return t;
  146. }
  147. bool func9(node **mas, int n)
  148. {
  149. bool t = 0;
  150. for (int i = 0; i < n && !t; i++)
  151. if (prime_polinom(mas[i]))
  152. t = 1;
  153. return t;
  154. }
  155. bool otr(node* top)
  156. {
  157. node *p = top;
  158. bool t = 1;
  159. while (p && t)
  160. {
  161. if (p->koef > 0)
  162. t = 0;
  163. p = p->next;
  164. }
  165. return t;
  166. }
  167. bool func11(node **mas, int n)
  168. {
  169. bool t = 0;
  170. for (int i = 0; i < n && !t; i++)
  171. if (otr(mas[i]))
  172. t = 1;
  173. return t;
  174. }
  175. node* copy(node* top)
  176. {
  177. node *top2 = NULL;
  178. while (top)
  179. {
  180. push(top2, top->koef, top->power);
  181. top = top->next;
  182. }
  183. return top2;
  184. }
  185. void collecting_terms(node* &top)
  186. {
  187. sort(top);
  188. node* p, *q, *pr;
  189. p = top;
  190. q = p->next;
  191. while (q)
  192. {
  193. if (p->power == q->power)
  194. {
  195. q->koef = q->koef + p->koef;
  196. if (p == top)
  197. {
  198. pop(top);
  199. p = top;
  200. }
  201. else
  202. {
  203. pr = previous(top, p);
  204. pr->next = q;
  205. delete p;
  206. p = q;
  207. }
  208. q = q->next;
  209. }
  210. else
  211. {
  212. p = q;
  213. q = q->next;
  214. }
  215. }
  216. }
  217. node* mult(node* top1, node* top2)
  218. {
  219. node *top3 = NULL;
  220. while (top1)
  221. {
  222. node* p = top2;
  223. while (p)
  224. {
  225. push(top3, top1->koef*p->koef,
  226. top1->power + p->power);
  227. p = p->next;
  228. }
  229. top1 = top1->next;
  230. }
  231. collecting_terms(top3);
  232. return top3;
  233. }
  234. void mult_k(node* top, int k)
  235. {
  236. node* p;
  237. p = top;
  238. while (p)
  239. {
  240. p->koef = k * p->koef;
  241. p = p->next;
  242. }
  243. }
  244. node* sum(node* top1, node* top2)
  245. {
  246. node *top3,*top4;
  247. top3 = copy(top1);
  248. top4 = copy(top2);
  249. find_end(top3)->next = top4;
  250. collecting_terms(top3);
  251. return top3;
  252. }
  253. node* sub(node* top1, node* top2)
  254. {
  255. node *top3, *top4;
  256. top3 = copy(top1);
  257. top4 = copy(top2);
  258. mult_k(top4, -1);
  259. find_end(top3)->next = top4;
  260. collecting_terms(top3);
  261. return top3;
  262. }
  263. void show(node* top)
  264. {
  265. sort(top);
  266. node* p;
  267. p = top;
  268. while (p->next)
  269. {
  270. cout << p->koef << "*x^" << p->power << " + ";
  271. p = p->next;
  272. }
  273. cout << p->koef << "*x^" << p->power << endl;
  274. }
  275. void show_mas(node **mas,int n)
  276. {
  277. for (int i = 0; i < n; i++)
  278. {
  279. collecting_terms(mas[i]);
  280. show(mas[i]);
  281. }
  282. cout << endl;
  283. }
  284. bool otr_3(node *top)
  285. {
  286. node *p = top;
  287. int k = 0;
  288. bool t = 0;
  289. while (p &&k<4)
  290. {
  291. if (p->koef < 0)
  292. k++;
  293. p = p->next;
  294. }
  295. if (k > 3)
  296. t = 1;
  297. return t;
  298. }
  299. int main()
  300. {
  301. ifstream in("input.txt");
  302. int n;
  303. in >> n;
  304. node **mas;
  305. mas=create_mas(in, n);
  306. node *n1, *n2=NULL;
  307. if (func11(mas, n) || func9(mas, n))
  308. {
  309. int i = 0;
  310. while (!(otr(mas[i]) || prime_polinom(mas[i])))
  311. i++;
  312. n1 = mas[i];
  313. for (i; i < n; i++)
  314. {
  315. if (otr(mas[i]) || prime_polinom(mas[i]))
  316. n1 = mult(n1, mas[i]);
  317. }
  318. }
  319. else
  320. n1 = mult(mas[0], mas[1]);
  321.  
  322. if (func11(mas, n))
  323. {
  324. for (int i = 0; i < n; i++)
  325. {
  326. if (otr_3(mas[i]))
  327. n2 = sum(n1, mas[i]);
  328.  
  329. }
  330. }
  331. else
  332. n2 = sub(mas[0], mas[1]);
  333. show(n1);
  334. show(n2);
  335. system("pause");
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement