Polynominal


SUBMITTED BY: vysmiles

DATE: April 24, 2017, 1:39 a.m.

UPDATED: April 24, 2017, 1:40 a.m.

FORMAT: Text only

SIZE: 11.1 kB

HITS: 8998

  1. #include "Definition.h"
  2. int menu1(int count)
  3. {
  4. if (0 == count)
  5. {
  6. int n = 0;
  7. cout << "---Polynomial Solver.----" << endl;
  8. cout << "1. Input Two Polynomial." << endl;
  9. cout << "0. Exit." << endl;
  10. do {
  11. cout << "Choose: ";
  12. cin >> n;
  13. } while (0 > n || 1 < n);
  14. return n;
  15. }
  16. else
  17. {
  18. int n = 0;
  19. cout << "---Polynomial Solver.----" << endl;
  20. cout << "1. Input New Two Polynomial." << endl;
  21. cout << "2. Continue." << endl;
  22. cout << "0. Exit." << endl;
  23. do {
  24. cout << "Choose: ";
  25. cin >> n;
  26. } while (0 > n || 2 < n);
  27. return n;
  28. }
  29. }
  30. int menu2()
  31. {
  32. int n = 0;
  33. cout << "1. Value of Polynomial At x."<<endl;
  34. cout << "2. Sum 2 Polynomial." << endl;
  35. cout << "3. Substract 2 Polynomial." << endl;
  36. cout << "4. Product 2 Polynomial." << endl;
  37. cout << "5. Quotient 2 Polynomial." << endl;
  38. cout << "6. Display 2 Poly." << endl;
  39. cout << "0. Exit." << endl;
  40. do {
  41. cout << "Choose: ";
  42. cin >> n;
  43. } while (0 > n || 7 < n);
  44. return n;
  45. }
  46. double Polynomial::Value_At(double n)
  47. {
  48. queue<Term> xx = x;
  49. double valueAt = 0;
  50. while (!xx.empty())
  51. {
  52. Term b = xx.front();
  53. valueAt = valueAt + (b.coefficient * pow(n, b.degree));
  54. xx.pop();
  55. }
  56. return valueAt;
  57. }
  58. unsigned int Polynomial::degree() //Find Max degree in Poly
  59. {
  60. unsigned int max = 0;
  61. queue<Term>xx = x;
  62. Term a = xx.front();
  63. xx.pop();
  64. max = a.degree;
  65. while (!xx.empty())
  66. {
  67. a = xx.front();
  68. if (max < a.degree)
  69. {
  70. max = a.degree;
  71. }
  72. xx.pop();
  73. }
  74. return max;
  75. }
  76. Polynomial Polynomial::ShortPolynomial() //Short member same degree and sort decrease
  77. {
  78. Polynomial q;
  79. queue<Term> xx = x;
  80. queue<Term> xy;
  81. int maxDegree = degree();
  82. while (0 <= maxDegree && 0 < xx.size())
  83. {
  84. q.a.coefficient = 0;
  85. while (!xx.empty())
  86. {
  87. Term a = xx.front();
  88. if (a.degree == maxDegree && 0 != a.coefficient)
  89. {
  90. q.a.coefficient = q.a.coefficient +a.coefficient;
  91. q.a.degree = a.degree;
  92. xx.pop();
  93. }
  94. else
  95. {
  96. xy.push(a);
  97. xx.pop();
  98. }
  99. }
  100. xx = xy;
  101. while (!xy.empty())
  102. {
  103. xy.pop();
  104. }
  105. if (0 != q.a.coefficient)
  106. {
  107. q.x.push(q.a);
  108. }
  109. maxDegree--;
  110. }
  111. return q;
  112. }
  113. bool Polynomial::Quotient(Polynomial p)
  114. {
  115. //cout << "Quotient: a / b = ";
  116. Polynomial q;
  117. Polynomial w;
  118. Polynomial overbalance;
  119. queue<Term> xx = x;
  120. queue<Term> xy = p.x;
  121. int maxDegree_1 = degree();
  122. int maxDegree_2 = p.degree();
  123. if (maxDegree_1 >= maxDegree_2)
  124. {
  125. while (maxDegree_1 >= maxDegree_2)
  126. {
  127. Term a = xx.front();
  128. Term b = xy.front();
  129. q.a.coefficient = a.coefficient / b.coefficient;
  130. q.a.degree = a.degree - b.degree;
  131. q.x.push(q.a);
  132. overbalance = q.Product(p);
  133. overbalance = Difference(overbalance);
  134. xx = overbalance.x;
  135. w.x = xx;
  136. if (!xx.empty())
  137. {
  138. maxDegree_1 = w.degree();
  139. }
  140. else
  141. {
  142. maxDegree_1 = -1;
  143. }
  144. }
  145. q = q.ShortPolynomial();
  146. q.print();
  147. cout << "Over Balance: ";
  148. w.print();
  149. return true;
  150. }
  151. else
  152. {
  153. return false;
  154. }
  155. }
  156. Polynomial Polynomial::Product(Polynomial p)
  157. {
  158. //cout << "Product: a * b = ";
  159. Polynomial q;
  160. queue<Term> xx = x;
  161. while (!xx.empty())
  162. {
  163. queue<Term> xy = p.x;
  164. Term a = xx.front();
  165. while (!xy.empty())
  166. {
  167. Term b = xy.front();
  168. q.a.coefficient = a.coefficient * b.coefficient;
  169. q.a.degree = a.degree + b.degree;
  170. xy.pop();
  171. q.x.push(q.a);
  172. }
  173. xx.pop();
  174. }
  175. q = q.ShortPolynomial();
  176. return q;
  177. }
  178. Polynomial Polynomial::Difference(Polynomial p)
  179. {
  180. //cout << "Substract: a - b = ";
  181. Polynomial q;
  182. Polynomial w;
  183. queue<Term> xx = x;
  184. queue<Term> xy = p.x;
  185. queue<Term> xz;
  186. Term a;
  187. Term b;
  188. int maxDegree_1;
  189. int maxDegree_2;
  190. while (0 != xx.size() || 0 != xy.size())
  191. {
  192. if (0 == xx.size())
  193. {
  194. maxDegree_1 = -1;
  195. }
  196. else
  197. {
  198. w.x = xx;
  199. maxDegree_1 = w.degree();
  200. }
  201. if (0 == xy.size())
  202. {
  203. maxDegree_2 = -1;
  204. }
  205. else
  206. {
  207. w.x = xy;
  208. maxDegree_2 = w.degree();
  209. }
  210. if (maxDegree_1 > maxDegree_2)
  211. {
  212. while (0 != xx.size())
  213. {
  214. a = xx.front();
  215. if (maxDegree_1 == a.degree)
  216. {
  217. q.a.coefficient = a.coefficient;
  218. q.a.degree = a.degree;
  219. xx.pop();
  220. continue;
  221. }
  222. xz.push(a);
  223. xx.pop();
  224. }
  225. xx = xz;
  226. }
  227. else if (maxDegree_1 < maxDegree_2)
  228. {
  229. while (0 != xy.size())
  230. {
  231. b = xy.front();
  232. if (maxDegree_2 == b.degree)
  233. {
  234. q.a.coefficient = 0 - b.coefficient;
  235. q.a.degree = b.degree;
  236. xy.pop();
  237. continue;
  238. }
  239. xz.push(b);
  240. xy.pop();
  241. }
  242. xy = xz;
  243. }
  244. else if(maxDegree_1 == maxDegree_2)
  245. {
  246. a = xx.front();
  247. b = xy.front();
  248. q.a.coefficient = a.coefficient - b.coefficient;
  249. q.a.degree = a.degree;
  250. xx.pop();
  251. xy.pop();
  252. }
  253. while (!xz.empty())
  254. {
  255. xz.pop();
  256. }
  257. q.x.push(q.a);
  258. }
  259. q = q.ShortPolynomial();
  260. return q;
  261. }
  262. Polynomial Polynomial::Sum(Polynomial p)
  263. {
  264. Polynomial q;
  265. queue<Term> xx = x;
  266. queue<Term> xy = p.x;
  267. Term a;
  268. Term b;
  269. while (0 != xx.size() || 0 != xy.size())
  270. {
  271. if (0 != xx.size())
  272. {
  273. a = xx.front();
  274. }
  275. else
  276. {
  277. b = xy.front();
  278. q.x.push(b);
  279. xy.pop();
  280. continue;
  281. }
  282. if (0 != xy.size())
  283. {
  284. b = xy.front();
  285. }
  286. else
  287. {
  288. a = xx.front();
  289. q.x.push(a);
  290. xx.pop();
  291. continue;
  292. }
  293. if (a.degree > b.degree)
  294. {
  295. q.x.push(a);
  296. xx.pop();
  297. }
  298. else if (a.degree < b.degree)
  299. {
  300. q.x.push(b);
  301. xy.pop();
  302. }
  303. else
  304. {
  305. q.a.coefficient = a.coefficient + b.coefficient;
  306. q.a.degree = a.degree;
  307. q.x.push(q.a);
  308. xx.pop();
  309. xy.pop();
  310. }
  311. }
  312. q = q.ShortPolynomial();
  313. return q;
  314. }
  315. void Polynomial::read()
  316. {
  317. if (!x.empty())
  318. {
  319. while (!x.empty())
  320. {
  321. x.pop();
  322. }
  323. }
  324. int n = 0;
  325. cout << "Enter member of Poly: ";
  326. cin >> n;
  327. cout << "====================" << endl;
  328. for (int i = 0; i < n; i++)
  329. {
  330. cout << "Enter coefficient: ";
  331. cin >> a.coefficient;
  332. cout << "Enter degree: ";
  333. cin >> a.degree;
  334. x.push(a);
  335. cout << "---------" << endl;
  336. }
  337. cout << endl << "--------------------" << endl;
  338. }
  339. void Polynomial::print()
  340. {
  341. queue<Term> xx = x;
  342. //cout << "P(x) = ";
  343. if (xx.empty())
  344. {
  345. cout << "0";
  346. }
  347. else
  348. {
  349. while (!xx.empty())
  350. {
  351. Term b = xx.front();
  352. int c = 0;
  353. if (xx.size() == x.size())
  354. {
  355. if (0 > b.coefficient)
  356. {
  357. c = 45;
  358. cout << (char)c << fabs(b.coefficient) << "x^" << b.degree << " ";
  359. }
  360. else
  361. {
  362. cout << b.coefficient << "x^" << b.degree << " ";
  363. }
  364. xx.pop();
  365. continue;
  366. }
  367. else
  368. {
  369. if (0 > b.coefficient)
  370. {
  371. c = 45;
  372. cout << (char)c << " " << fabs(b.coefficient) << "x^" << b.degree << " ";
  373. }
  374. else
  375. {
  376. c = 43;
  377. cout << (char)c << " " << b.coefficient << "x^" << b.degree << " ";
  378. }
  379. xx.pop();
  380. continue;
  381. }
  382. }
  383. }
  384. //cout << "= 0" << endl;
  385. cout << endl;
  386. }
  387. void Run()
  388. {
  389. int n = 0;
  390. int m = 0;
  391. int count = 0;
  392. Polynomial a;
  393. Polynomial b;
  394. Polynomial c;
  395. do
  396. {
  397. n = menu1(count);
  398. switch (n)
  399. {
  400. case 1:
  401. system("cls");
  402. a.read();
  403. a = a.ShortPolynomial();
  404. b.read();
  405. b = b.ShortPolynomial();
  406. system("cls");
  407. do
  408. {
  409. system("cls");
  410. m = menu2();
  411. switch (m)
  412. {
  413. case 1:
  414. system("cls");
  415. cout << "Poly A: P(x) = ";
  416. a.print();
  417. cout << "Poly B: P(x) = ";
  418. b.print();
  419. int x;
  420. cout << "Enter value at x0: ";
  421. cin >> x;
  422. cout << "Value of Poly A at x0 = " << x << " is: " << a.Value_At(x) << endl;
  423. cout << "Value of Poly B at x0 = " << x << " is: " << b.Value_At(x) << endl;
  424. system("pause");
  425. break;
  426. case 2:
  427. system("cls");
  428. cout << "Poly A: P(x) = ";
  429. a.print();
  430. cout << "Poly B: P(x) = ";
  431. b.print();
  432. cout << "Sum: A + B = ";
  433. c = a.Sum(b);
  434. c.print();
  435. system("pause");
  436. break;
  437. case 3:
  438. system("cls");
  439. cout << "Poly A: P(x) = ";
  440. a.print();
  441. cout << "Poly B: P(x) = ";
  442. b.print();
  443. cout << "Substract: A - B = ";
  444. c = a.Difference(b);
  445. c.print();
  446. system("pause");
  447. break;
  448. case 4:
  449. system("cls");
  450. cout << "Poly A: P(x) = ";
  451. a.print();
  452. cout << "Poly B: P(x) = ";
  453. b.print();
  454. cout << "Product: A * B = ";
  455. c = a.Product(b);
  456. c.print();
  457. system("pause");
  458. break;
  459. case 5:
  460. system("cls");
  461. cout << "Poly A: P(x) = ";
  462. a.print();
  463. cout << "Poly B: P(x) = ";
  464. b.print();
  465. cout << "Quotient: A / B = ";
  466. if (true != a.Quotient(b))
  467. {
  468. cout << "False. Degree Of A must be higher than B." << endl;
  469. }
  470. system("pause");
  471. break;
  472. case 6:
  473. system("cls");
  474. cout << "Display 2 poly." << endl;
  475. cout << "Poly A: P(x) = ";
  476. a.print();
  477. cout << "Poly B: P(x) = ";
  478. b.print();
  479. system("pause");
  480. break;
  481. default:
  482. break;
  483. }
  484. } while (m != 0);
  485. break;
  486. case 2:
  487. system("cls");
  488. do
  489. {
  490. system("cls");
  491. m = menu2();
  492. switch (m)
  493. {
  494. case 1:
  495. system("cls");
  496. cout << "Poly A: P(x) = ";
  497. a.print();
  498. cout << "Poly B: P(x) = ";
  499. b.print();
  500. int x;
  501. cout << "Enter value at x0: ";
  502. cin >> x;
  503. cout << "Value of Poly A at x0 = " << x << " is: " << a.Value_At(x) << endl;
  504. cout << "Value of Poly B at x0 = " << x << " is: " << b.Value_At(x) << endl;
  505. system("pause");
  506. break;
  507. case 2:
  508. system("cls");
  509. cout << "Poly A: P(x) = ";
  510. a.print();
  511. cout << "Poly B: P(x) = ";
  512. b.print();
  513. cout << "Sum: A + B = ";
  514. c = a.Sum(b);
  515. c.print();
  516. system("pause");
  517. break;
  518. case 3:
  519. system("cls");
  520. cout << "Poly A: P(x) = ";
  521. a.print();
  522. cout << "Poly B: P(x) = ";
  523. b.print();
  524. cout << "Substract: A - B = ";
  525. c = a.Difference(b);
  526. c.print();
  527. system("pause");
  528. break;
  529. case 4:
  530. system("cls");
  531. cout << "Poly A: P(x) = ";
  532. a.print();
  533. cout << "Poly B: P(x) = ";
  534. b.print();
  535. cout << "Product: A * B = ";
  536. c = a.Product(b);
  537. c.print();
  538. system("pause");
  539. break;
  540. case 5:
  541. system("cls");
  542. cout << "Poly A: P(x) = ";
  543. a.print();
  544. cout << "Poly B: P(x) = ";
  545. b.print();
  546. cout << "Quotient: A / B = ";
  547. if (true != a.Quotient(b))
  548. {
  549. cout << "False. Degree Of A must be higher than B." << endl;
  550. }
  551. system("pause");
  552. break;
  553. case 6:
  554. system("cls");
  555. cout << "Display 2 poly." << endl;
  556. cout << "Poly A: P(x) = ";
  557. a.print();
  558. cout << "Poly B: P(x) = ";
  559. b.print();
  560. system("pause");
  561. break;
  562. default:
  563. break;
  564. }
  565. } while (m != 0);
  566. default:
  567. break;
  568. }
  569. system("cls");
  570. count++;
  571. } while (n != 0);
  572. }

comments powered by Disqus