Guest User

Untitled

a guest
Jul 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. //USE POINTER NOTATION
  2. #include <iostream>
  3. #include <cctype>
  4. #include <cstring>
  5. #include <cstdlib>
  6. //#include <string>
  7. using namespace std;
  8.  
  9. class Polynomial
  10. {
  11. private:
  12. int *coefficient;
  13. int counter;
  14. public:
  15. Polynomial()
  16. {
  17. coefficient = new int [1];
  18. *coefficient = '\0';
  19. }
  20. Polynomial(char *);
  21. //copyconstructor
  22. Polynomial(const Polynomial &obj)
  23. {
  24. counter = obj.counter;
  25. coefficient = new int[counter];
  26. for (int i = 0; i < counter; i++)
  27. *(coefficient + i) = obj.coefficient[i];
  28. }
  29. //destructor
  30. ~Polynomial()
  31. {
  32. delete [] coefficient;
  33. }
  34. void setPolynomial(char *s);
  35.  
  36. void getPolynomial() const
  37. {
  38. for (int i = 0; i < counter; i++)
  39. {
  40. cout << coefficient[i] << "x^" << i;
  41. if (i != counter - 1)
  42. cout << " + ";
  43. }
  44. }
  45. Polynomial operator + (const Polynomial &);
  46. Polynomial operator - (const Polynomial &);
  47. Polynomial operator * (const Polynomial &);
  48. const Polynomial operator=(const Polynomial &right)
  49. {;
  50. delete [] coefficient;
  51. counter = right.counter;
  52. coefficient = new int[counter];
  53. for (int i = 0; i < right.counter; i++)
  54. *(coefficient + i) = right.coefficient[i];
  55. return *this;
  56. }
  57. };
  58.  
  59. int main()
  60. {
  61. char input[50];
  62. char input2[50];
  63.  
  64. cout << "Example: -4x^0 + x^1 + 0x^2+ 4x^3 + -3x^4" << endl;
  65. cout << "Enter a polynomial: ";
  66. cin.getline(input, 50);
  67. Polynomial one(input);
  68. cout << "Enter second polynomial: ";
  69. cin.getline(input2, 50);
  70. Polynomial two(input2);
  71.  
  72. int choice;
  73. do
  74. {
  75. cout << "1. Addition\n2. Subtraction\n3. Multiplication";
  76. cout << "\n4. Assignment\n5. Exit\nEnter your choice: ";
  77. cin >> choice;
  78.  
  79. switch (choice)
  80. {
  81. case 1:
  82. {
  83. cout << "Addiction\n";
  84. Polynomial add = one + two;
  85. one.getPolynomial();
  86. cout << " + ";
  87. two.getPolynomial();
  88. cout << " = ";
  89. add.getPolynomial();
  90. cout << endl;
  91. }
  92. break;
  93. case 2:
  94. {
  95. cout << "Subtraction\n";
  96. Polynomial subtract = one - two;
  97. one.getPolynomial();
  98. cout << " - (";
  99. two.getPolynomial();
  100. cout << ") = ";
  101. subtract.getPolynomial();
  102. cout << endl;
  103. }
  104. break;
  105. case 3:
  106. {
  107. cout << "Multiplication\n";
  108. Polynomial multiply = one * two;
  109. one.getPolynomial();
  110. cout << " * (";
  111. two.getPolynomial();
  112. cout << ") = ";
  113. multiply.getPolynomial();
  114. cout << endl;
  115. }
  116. break;
  117. case 4:
  118. cout << "Assignment\n";
  119. cout << "First polynomial: ";
  120. one.getPolynomial();
  121. cout << "\nSecond polynomial: ";
  122. two.getPolynomial();
  123. cout << "\nFirst = Second\nFirst polynomial is now: ";
  124. one = two;
  125. one.getPolynomial();
  126. cout << endl;
  127. break;
  128. case 5:
  129. cout << "Goodbye\n";
  130. break;
  131. default:
  132. cout << "Invalid choice\n";
  133. break;
  134. }
  135. }while (choice != 5);
  136. return 0;
  137. }
  138.  
  139. Polynomial::Polynomial(char *s)
  140. {
  141. char *string;
  142. string = new char [strlen(s) + 1];
  143. int length = strlen(string);
  144. strcpy(string, s);
  145.  
  146. char *copy;
  147. copy = new char [length];
  148. strcpy(copy, string);
  149.  
  150. char *p = strtok(string, " +-");
  151. counter = 0;
  152. while (p)
  153. {
  154. p = strtok(NULL, " +-");
  155. counter++;
  156. }
  157.  
  158. coefficient = new int[counter];
  159.  
  160. p = strtok(copy, " +");
  161. int a = 0;
  162. while (p)
  163. {
  164. long int coeff;
  165. char *endptr;
  166. coeff = strtol(p, &endptr, 10); //stops at first non number
  167. if (*p == 'x')
  168. coeff = 1;
  169.  
  170. coefficient[a] = coeff;
  171. p = strtok(NULL, " +");
  172. a++;
  173. }
  174. }
  175.  
  176. void Polynomial::setPolynomial(char *s)
  177. {
  178. char *string;
  179. string = new char [strlen(s) + 1];
  180. int length = strlen(string);
  181. strcpy(string, s);
  182.  
  183. char *copy;
  184. copy = new char[length];
  185. strcpy(copy, string);
  186.  
  187. char *p = strtok(string, " +-");
  188. counter = 0;
  189. while (p)
  190. {
  191. p = strtok(NULL, " +-");
  192. counter++;
  193. }
  194.  
  195. coefficient = new int[counter];
  196.  
  197. p = strtok(copy, " +");
  198. int a = 0;
  199. while (p)
  200. {
  201. long int coeff;
  202. char *endptr;
  203. coeff = strtol(p, &endptr, 10); //stops at first non number
  204. if (*p == 'x')
  205. coeff = 1;
  206.  
  207. coefficient[a] = coeff;
  208. p = strtok(NULL, " +");
  209. a++;
  210. }
  211. }
  212. Polynomial Polynomial::operator + (const Polynomial &right)
  213. {
  214. Polynomial temp;
  215. //string, length, coefficient, counter
  216. int smallest, largest;
  217. smallest = (counter > right.counter) ? right.counter : counter;
  218. largest = (counter > right.counter) ? counter : right.counter;
  219. if (counter == right.counter)
  220. largest = counter;
  221.  
  222. //make coefficient array
  223. temp.counter = largest;
  224. temp.coefficient = new int [largest];
  225. for (int i = 0; i < smallest; i++)
  226. temp.coefficient[i] = coefficient[i] + right.coefficient[i];
  227. for (int j = smallest; j < largest; j++)
  228. {
  229. if (counter > right.counter)
  230. temp.coefficient[j] = coefficient[j];
  231. else
  232. temp.coefficient[j] = right.coefficient[j];
  233. }
  234. return temp;
  235. }
  236. Polynomial Polynomial::operator - (const Polynomial &right)
  237. {
  238. Polynomial temp;
  239. int smallest, largest;
  240. smallest = (counter > right.counter) ? right.counter : counter;
  241. largest = (counter > right.counter) ? counter : right.counter;
  242. if (counter == right.counter)
  243. largest = counter;
  244.  
  245. //make coefficient array
  246. temp.counter = largest;
  247. temp.coefficient = new int [largest];
  248. for (int i = 0; i < smallest; i++)
  249. temp.coefficient[i] = coefficient[i] - right.coefficient[i];
  250. for (int j = smallest; j < largest; j++)
  251. {
  252. if (counter > right.counter)
  253. temp.coefficient[j] = coefficient[j];
  254. else
  255. temp.coefficient[j] = right.coefficient[j];
  256. }
  257. return temp;
  258. }
  259. Polynomial Polynomial::operator * (const Polynomial &right)
  260. {
  261. Polynomial temp;
  262.  
  263. //make coefficient array
  264. int count = (counter + right.counter) - 1;
  265. temp.counter = count;
  266. temp.coefficient = new int [count];
  267. for (int i = 0; i < counter; i++)
  268. {
  269. for (int j = 0; j < right.counter; j++)
  270. temp.coefficient[i+j] += coefficient[i] * right.coefficient[j];
  271. }
  272. return temp;
  273. }
Add Comment
Please, Sign In to add comment