Advertisement
Dzham

Untitled

Feb 25th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <vector>
  5.  
  6. template <typename T>
  7. class Polynomial {
  8. private:
  9. std::vector<T> coefficients = {};
  10.  
  11. void clear_zeroes() {
  12. for (int i = static_cast<int>(coefficients.size()) - 1; i > -1; --i) {
  13. if (coefficients[i] == static_cast<T>(0)) {
  14. coefficients.pop_back();
  15. } else {
  16. break;
  17. }
  18. }
  19. }
  20.  
  21. void resize(int s) {
  22. coefficients.resize(s);
  23. }
  24.  
  25. public:
  26. Polynomial() = default;
  27.  
  28. Polynomial(const T& t) {
  29. coefficients = {};
  30. coefficients.push_back(t);
  31. this->clear_zeroes();
  32. }
  33.  
  34. Polynomial(const std::vector<T>& v) {
  35. coefficients = {};
  36. coefficients = std::move(v);
  37. this->clear_zeroes();
  38. }
  39.  
  40. template <typename InIter>
  41. Polynomial(InIter first, InIter last) {
  42. coefficients = {};
  43. while (first != last) {
  44. coefficients.push_back(*first++);
  45. }
  46. this->clear_zeroes();
  47. }
  48.  
  49. void clear_z() {
  50. clear_zeroes();
  51. }
  52.  
  53. const std::vector<T>& GetCoeff() const {
  54. return coefficients;
  55. }
  56.  
  57. int Degree() const {
  58. return static_cast<int>(coefficients.size()) - 1;
  59. }
  60.  
  61. T& at(int i) {
  62. if (i > Degree()) {
  63. coefficients.resize(i + 1);
  64. }
  65. return coefficients[i];
  66. }
  67.  
  68. T operator[] (int i) const {
  69. if (i > this->Degree()) {
  70. return static_cast<T>(0);
  71. }
  72. return coefficients[i];
  73. }
  74.  
  75. bool operator == (const Polynomial& p) const {
  76. return coefficients == p.GetCoeff();
  77. }
  78.  
  79. bool operator != (const Polynomial& p) const {
  80. return coefficients != p.GetCoeff();
  81. }
  82.  
  83. Polynomial& operator += (const Polynomial& p) {
  84. if (coefficients.size() < p.GetCoeff().size()) {
  85. coefficients.resize(p.GetCoeff().size());
  86. }
  87. for (size_t i = 0; i < p.GetCoeff().size(); ++i) {
  88. coefficients[i] += p.GetCoeff()[i];
  89. }
  90. this->clear_zeroes();
  91. return *this;
  92. }
  93.  
  94. Polynomial& operator -= (const Polynomial& p) {
  95. if (coefficients.size() < p.GetCoeff().size()) {
  96. coefficients.resize(p.GetCoeff().size());
  97. }
  98. for (size_t i = 0; i < p.GetCoeff().size(); ++i) {
  99. coefficients[i] -= p.GetCoeff()[i];
  100. }
  101. this->clear_zeroes();
  102. return *this;
  103. }
  104.  
  105. Polynomial& minus(const Polynomial& p) {
  106. if (coefficients.size() < p.GetCoeff().size()) {
  107. coefficients.resize(p.GetCoeff().size());
  108. }
  109. for (size_t i = 0; i < p.GetCoeff().size(); ++i) {
  110. coefficients[i] -= p.GetCoeff()[i];
  111. }
  112. return *this;
  113. }
  114.  
  115. Polynomial& operator *= (const Polynomial& p) {
  116. if (p.Degree() == -1) {
  117. coefficients.clear();
  118. } else if (this->Degree() != -1) {
  119. std::vector<T> new_coef(p.Degree() + this->Degree() + 1);
  120. for (int i = 0; i <= p.Degree(); ++i) {
  121. for (int j = 0; j <= this->Degree(); ++j) {
  122. new_coef[i + j] += p.GetCoeff()[i] * coefficients[j];
  123. }
  124. }
  125. coefficients = std::move(new_coef);
  126. this->clear_zeroes();
  127. }
  128. return *this;
  129. }
  130.  
  131. Polynomial operator + () const {
  132. return *this;
  133. }
  134.  
  135. Polynomial operator - () const {
  136. Polynomial p(*this);
  137. for (int i = 0; i < p.GetCoeff().size(); ++i) {
  138. p[i] *= static_cast<T>(-1);
  139. }
  140. p.clear_zeroes();
  141. return p;
  142. }
  143.  
  144. T operator ()(const T& t) const {
  145. T value = static_cast<T>(0);
  146. T curr = t;
  147. T beg = t;
  148. if (this->Degree() == -1) {
  149. return value;
  150. }
  151. value += coefficients[0];
  152. for (size_t i = 1; i < coefficients.size(); ++i) {
  153. value += curr * coefficients[i];
  154. curr *= beg;
  155. }
  156. return value;
  157. }
  158.  
  159. typename std::vector<T>::const_iterator begin() const {
  160. return coefficients.begin();
  161. }
  162.  
  163. typename std::vector<T>::const_iterator end() const {
  164. return coefficients.end();
  165. }
  166.  
  167. Polynomial& to_canonic() {
  168. if (Degree() != -1) {
  169. T coef = coefficients[coefficients.size() - 1];
  170. for (int i = 0; i <= Degree(); ++i) {
  171. coefficients[i] /= coef;
  172. }
  173. }
  174. return *this;
  175. }
  176.  
  177. Polynomial operator /= (const Polynomial& p) {
  178. Polynomial rest;
  179. while (rest.Degree() >= p.Degree()) {
  180. Polynomial result;
  181. result.at(rest.Degree() - p.Degree()) = rest[rest.Degree()] / p[p.Degree()];
  182. rest += result * p;
  183. }
  184. rest.clear_z();
  185. return rest;
  186. }
  187.  
  188. ~Polynomial() {
  189. coefficients.clear();
  190. }
  191. };
  192.  
  193. template <typename T>
  194. Polynomial<T> operator + (const Polynomial<T>& p1, const Polynomial<T>& p2) {
  195. Polynomial<T> result = p1;
  196. return result += p2;
  197. }
  198.  
  199. template <typename T>
  200. Polynomial<T> operator - (const Polynomial<T>& p1, const Polynomial<T>& p2) {
  201. Polynomial<T> result = p1;
  202. return result -= p2;
  203. }
  204.  
  205. template <typename T>
  206. Polynomial<T> minus2(const Polynomial<T>& p1, const Polynomial<T>& p2) {
  207. Polynomial<T> result = p1;
  208. return result.minus(p2);
  209. }
  210.  
  211. template <typename T>
  212. Polynomial<T> operator * (const Polynomial<T>& p1, const Polynomial<T>& p2) {
  213. Polynomial<T> result = p1;
  214. return result *= p2;
  215. }
  216.  
  217. template <typename T>
  218. Polynomial<T> operator & (const Polynomial<T>& p1, const Polynomial<T>& p2) {
  219. Polynomial<T> preres;
  220. if (p1.Degree() != -1) {
  221. preres += p1[0];
  222. for (int i = 1; i <= p1.Degree(); ++i) {
  223. Polynomial<T> pow = p2;
  224. for (int j = 0; j < i - 1; ++j) {
  225. pow *= Polynomial<T>(p2);
  226. }
  227. preres += (Polynomial<T>(pow) * Polynomial<T>(p1[i]));
  228. }
  229. }
  230. return preres;
  231. }
  232.  
  233. template<typename T>
  234. Polynomial<T> operator / (Polynomial<T> p1, const Polynomial<T>& p2) {
  235. Polynomial<T> result = p1;
  236. return result /= p2;
  237. }
  238.  
  239. template<typename T>
  240. Polynomial<T> operator % (Polynomial<T> p1, const Polynomial<T>& p2) {
  241. Polynomial<T> rest = p1;
  242. while (rest.Degree() >= p2.Degree()) {
  243. Polynomial<T> result;
  244. result.at(rest.Degree() - p2.Degree()) =
  245. rest[rest.Degree()] / p2[p2.Degree()];
  246. rest -= result * p2;
  247. }
  248. rest.clear_z();
  249. return rest;
  250. }
  251.  
  252. template <typename T>
  253. Polynomial<T> operator , (const Polynomial<T>& p1, const Polynomial<T>& p2) {
  254. if (p2.Degree() == -1) {
  255. Polynomial<T> r = p1;
  256. r.to_canonic();
  257. return r;
  258. } else {
  259. return (p2, p1 % p2);
  260. }
  261. }
  262.  
  263. template <typename T>
  264. std::ostream& operator << (std::ostream& out, const Polynomial<T>& p) {
  265. for (int i = p.Degree(); i > 1; --i) {
  266. T coef = p.GetCoeff()[i];
  267. if (coef < static_cast<T>(0)) {
  268. if (coef == static_cast<T>(-1)) {
  269. out << "-x^" << i;
  270. } else {
  271. out << coef << "*x^" << i;
  272. }
  273. } else if (coef > static_cast<T>(0)) {
  274. if (p.Degree() != i) {
  275. out << "+";
  276. }
  277. if (coef == static_cast<T>(1)) {
  278. out << "x^" << i;
  279. } else {
  280. out << coef << "*x^" << i;
  281. }
  282. }
  283. }
  284. if (p.GetCoeff().size() >= 2) {
  285. T coef = p.GetCoeff()[1];
  286. if (coef < static_cast<T>(0)) {
  287. if (coef == static_cast<T>(-1)) {
  288. out << "-x";
  289. } else {
  290. out << coef << "*x";
  291. }
  292. } else if (coef > static_cast<T>(0)) {
  293. if (p.Degree() != 1) {
  294. out << "+";
  295. }
  296. if (coef == static_cast<T>(1)) {
  297. out << "x";
  298. } else {
  299. out << coef << "*x";
  300. }
  301. }
  302. }
  303. if (p.GetCoeff().size() >= 1) {
  304. T coef = p.GetCoeff()[0];
  305. if (coef > static_cast<T>(0)) {
  306. if (p.Degree() != 0) {
  307. out << "+";
  308. }
  309. out << coef;
  310. } else if (coef < static_cast<T>(0)) {
  311. out << coef;
  312. }
  313. }
  314. if (p.GetCoeff().size() == 0) {
  315. out << 0;
  316. }
  317. return out;
  318. }
  319.  
  320. int main() {
  321. std::size_t nCoefficientsA;
  322. std::cout << "Number of <double> coefficients in the first vector: " << std::endl;
  323. std::cin >> nCoefficientsA;
  324.  
  325. std::vector<double> coefficientsA;
  326. std::cout << "List of coefficients: " << std::endl;
  327. while (coefficientsA.size() < nCoefficientsA) {
  328. double coefficient;
  329. std::cin >> coefficient;
  330. coefficientsA.push_back(coefficient);
  331. }
  332.  
  333. std::size_t nCoefficientsB;
  334. std::cout << "Number of <double> coefficients in the second vector: " << std::endl;
  335. std::cin >> nCoefficientsB;
  336.  
  337. std::vector<double> coefficientsB;
  338. std::cout << "List of coefficients: " << std::endl;
  339. while (coefficientsB.size() < nCoefficientsB) {
  340. double coefficient;
  341. std::cin >> coefficient;
  342. coefficientsB.push_back(coefficient);
  343. }
  344. std::reverse(coefficientsA.begin(), coefficientsA.end());
  345. std::reverse(coefficientsB.begin(), coefficientsB.end());
  346.  
  347. Polynomial<double> A(coefficientsA);
  348. Polynomial<double> B(coefficientsB);
  349. Polynomial<double> C(0);
  350.  
  351. std::cout << "First vector: " << A << std::endl;
  352. std::cout << "Second vector: " << B << std::endl;
  353. std::cout << "Third vector: " << C << std::endl;
  354.  
  355. if (A == B) {
  356. std::cout << "Equal" << std::endl;
  357. } else if (A != B) {
  358. std::cout << "Not Equal" << std::endl;
  359. }
  360.  
  361.  
  362. std::cout << "+ " << A + B << std::endl;
  363.  
  364. std::cout << "- " << A - B << std::endl;
  365. std::cout << "/ : " << (A / B) << std::endl;
  366. std::cout << "% : " << (A % B) << std::endl;
  367. std::cout << "WTF\n";
  368. std::cout << ", : " << (A , B) << std::endl;
  369. std::cout << A(-1) << '\n';
  370.  
  371. std::cout << "List of the coefficients before variable degrees from 0 to 19: " << std::endl;
  372. for (int i = 0; i < 20; ++i) {
  373. std::cout << A[i] << " ";
  374. }
  375. std::cout << std::endl;
  376. auto i = A.begin();
  377. while (i != A.end()) {
  378. std::cout << *i++ << " ";
  379. }
  380. std::cout << '\n';
  381. std::cout << A.Degree();
  382. std::cout << '\n';
  383. std::cout << *A.begin();
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement