Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. /**
  2. * This program will...
  3. * 1) Recieve information from the user on the degree of a polynomail, followed by the coefficients of each term
  4. * 2) Interpret the information and display the polynomial in standard form
  5. * 3) Evaluate the polynomial at a user-given value
  6. * 4) Evaluate the first and then second derivative of the polynomial at user-given value
  7. * 5) Receive a second polynomial from the user and then compare the second polynomial to the first derivative of the first polynomial
  8. * Course: csc1253 Section 001
  9. * Programming Project #: 5
  10. * Instructor: Dr. Duncan
  11. * @author BLAKE LALONDE
  12. * @since 11 / 21 / 2019
  13. * @version 2
  14. */
  15.  
  16.  
  17.  
  18. #include <iostream>
  19. #include <iomanip>
  20. #include <string>
  21. #include <cmath>
  22. #include <sstream>
  23.  
  24. using namespace std;
  25.  
  26. class Polynomial
  27. {
  28.  
  29. private:
  30. double* poly;
  31.  
  32. public:
  33. // Creates the zero polynomial //
  34. Polynomial()
  35. {
  36. poly = new double[2];
  37. poly[0] = 0;
  38. poly[1] = 0;
  39. }
  40.  
  41. // Creates a polynomial using the contents of the degCoeffs array //
  42. Polynomial(double degCoeffs[])
  43. {
  44. int i;
  45. double size = degCoeffs[0] + 2;
  46. poly = new double[size];
  47. for (i = 0; i <= size; i++)
  48. {
  49. poly[i] = degCoeffs[i];
  50. }
  51. }
  52.  
  53. // Used to deallocate the memory associated with this polynomial //
  54. ~Polynomial()
  55. {
  56.  
  57. }
  58.  
  59. // Evaluates the polynomial at the specified value //
  60. double eval(double x) const
  61. {
  62. int i;
  63. int power = poly[0];
  64. double sum = poly[1];
  65. int size = power + 2;
  66. for (i = 2; i < size; i++)
  67. {
  68. sum = sum * x + poly[i];
  69. }
  70. if (sum == -0)
  71. return 0;
  72. return sum;
  73. }
  74.  
  75. // Gives a string representation of this polynomial in standard form where zero terms, coefficeints 1 or -1, and exponents 1 are not displayed //
  76. string str() const
  77. {
  78. stringstream sout;
  79. int power = poly[0];
  80. int i;
  81. int powerChanger = power - 1;
  82. int size = power + 2;
  83.  
  84. if (size < 2)
  85. {
  86. sout << "0";
  87. string exception = sout.str();
  88. return exception;
  89. }
  90. else if (poly[1] == 1)
  91. sout << "x^" << power;
  92. else if (poly[1] == -1)
  93. sout << "-x^" << power;
  94. else if (poly[1] == 0)
  95. sout << "";
  96. else
  97. {
  98. if (power == 1)
  99. {
  100. if (poly[1] == 1)
  101. sout << "x";
  102. else if (poly[1] == -1)
  103. sout << " - x";
  104. else
  105. sout << poly[1] << "x";
  106. }
  107. else if (power > 1)
  108. sout << poly[1] << "x^" << power;
  109. }
  110.  
  111. if (poly[0] == 0)
  112. sout << poly[1];
  113.  
  114. for (i = 2; i <= power + 2 && powerChanger >= 0; i++, powerChanger--)
  115. {
  116. if (powerChanger == 0)
  117. {
  118. if (poly[power + 1] > 0)
  119. sout << " + " << poly[power + 1];
  120. else if (poly[power + 1] < 0)
  121. sout << " - " << abs(poly[power + 1]);
  122. }
  123. else if (powerChanger == 1)
  124. {
  125. if (poly[i] < -1)
  126. sout << " - " << abs(poly[i]) << "x";
  127. else if (poly[i] == -1)
  128. sout << " - x";
  129. else if (poly[i] == 0)
  130. sout << "";
  131. else if (poly[i] == 1)
  132. sout << " + x";
  133. else
  134. sout << " + " << poly[i] << "x";
  135. }
  136. else
  137. {
  138. if (poly[i] == 1)
  139. sout << " + x^" << powerChanger;
  140. else if (poly[i] == -1)
  141. sout << " - x^" << powerChanger;
  142. else if (poly[i] == 0)
  143. sout << "";
  144. else
  145. {
  146. if (poly[i] > 0)
  147. sout << " + " << poly[i] << "x^" << powerChanger;
  148. else if (poly[i] < 0)
  149. sout << " - " << abs(poly[i]) << "x^" << powerChanger;
  150. }
  151. }
  152. }
  153. string polynomial = sout.str();
  154. return polynomial;
  155. }
  156.  
  157. // Determines whether this polynomial is equivalent to the specified polynomial //
  158. bool equals(const Polynomial& other) const
  159. {
  160. int i;
  161. double size1 = poly[0] + 2;
  162. double size2 = other.poly[0] + 2;
  163. if (size1 != size2)
  164. return false;
  165. for (i = 0; i < size1; i++)
  166. {
  167. if (poly[i] != other.poly[i])
  168. return false;
  169. }
  170. return true;
  171. }
  172.  
  173. // Generates a polynomial that represents the derivative of the given polynomial //
  174. Polynomial differentiate() const
  175. {
  176. double size = poly[0] + 2;
  177. double degree = poly[0];
  178. poly[0] = degree - 1;
  179. if (size <= 1)
  180. {
  181. double polyException[] = { 0, 0 };
  182. return Polynomial();
  183. }
  184. else
  185. for (int i = 1; i <= size; i++)
  186. {
  187. poly[i] = degree * poly[i];
  188. degree--;
  189. }
  190. return Polynomial(poly);
  191. }
  192. };
  193.  
  194.  
  195.  
  196. int main()
  197. {
  198. int i;
  199. double polyDegree;
  200. double polySize;
  201. cout << "Enter the degree of the polynomial -> ";
  202. cin >> polyDegree;
  203. if (polyDegree < 0)
  204. {
  205. cout << "Please enter a positive degree." << endl;
  206. return 0;
  207. }
  208. double* polyList = new double[polyDegree + 2];
  209. polyList[0] = polyDegree;
  210. polySize = polyDegree + 2;
  211. cout << "Enter the coefficients -> ";
  212. for (i = 1; i < polySize; i++)
  213. cin >> polyList[i];
  214.  
  215. Polynomial original = Polynomial(polyList);
  216. cout << endl << "f(x) = " << original.str() << endl;
  217. cout << "Enter a value at which to evaluate this polynomial -> ";
  218. double x;
  219. cin >> x;
  220. cout << "f(" << x << ") = " << original.eval(x) << endl << endl;
  221.  
  222. Polynomial firstDeriv = Polynomial(polyList);
  223. cout << "f'(x) = " << firstDeriv.differentiate().str() << endl;
  224. cout << "Enter a value at which to evaluate the first derivative -> ";
  225. cin >> x;
  226. cout << "f'(" << x << ") = " << firstDeriv.eval(x) << endl << endl;
  227.  
  228. Polynomial secondDeriv = Polynomial(polyList);
  229. cout << "f'(x) = " << secondDeriv.differentiate().differentiate().str() << endl;
  230. cout << "Enter a value at which to evaluate the second derivative -> ";
  231. cin >> x;
  232. cout << "f\"(" << x << ") = " << secondDeriv.differentiate().eval(x) << endl << endl;
  233.  
  234. double newPolyDegree;
  235. double newPolySize;
  236. cout << "Enter the degree of the second polynomial -> ";
  237. cin >> newPolyDegree;
  238. double* newPolyList = new double[newPolyDegree + 2];
  239. newPolyList[0] = newPolyDegree;
  240. newPolySize = newPolyDegree + 2;
  241. cout << "Enter the coefficents -> ";
  242. for (i = 1; i < newPolySize; i++)
  243. cin >> newPolyList[i];
  244. Polynomial newPolynomial = Polynomial(newPolyList);
  245. cout << endl << "g(x) = " << newPolynomial.str() << endl;
  246. if (firstDeriv.equals(newPolynomial))
  247. cout << "g(x) = f'(x)";
  248. else
  249. cout << "g(x) <> f'(x)";
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement