Guest User

Untitled

a guest
Jun 13th, 2016
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. using namespace std;
  6.  
  7. class Node{
  8.  
  9. private:
  10.  
  11. int coefficient;
  12. char variable;
  13. int exponent;
  14. Node * next;
  15. friend class linkedList;
  16.  
  17. public:
  18.  
  19. //Constructor 1
  20. Node (int coeff, int exp)
  21. {
  22. this->coefficient = coeff;
  23. this->exponent = exp;
  24. if (exp == 0)
  25. {
  26. this->variable = NULL;
  27. }
  28. else
  29. {
  30. this->variable = 'x';
  31. }
  32.  
  33. }
  34.  
  35. //Constructor 2
  36. Node (int coeff, int exp, Node* next)
  37. {
  38. this->coefficient = coeff;
  39. this->exponent = exp;
  40. this->next = next;
  41. if (exp == 0)
  42. {
  43. this->variable = NULL;
  44. }
  45. else
  46. {
  47. this->variable = 'x';
  48. }
  49. }
  50.  
  51. ~Node()
  52. {
  53.  
  54. }
  55. };
  56.  
  57. class linkedList
  58. {
  59. private:
  60.  
  61. Node *listHead;
  62.  
  63. public:
  64.  
  65. //Constructor
  66. linkedList()
  67. {
  68. listHead = new Node (-9999,0,NULL);
  69. }
  70.  
  71. //Function to find the spot of where to insert the node into the list.
  72. Node* findSpot (int coefficient, int exponent)
  73. {
  74. Node *spot = listHead;
  75.  
  76. while(spot->next != NULL && spot->next->exponent >= exponent)
  77. {
  78. spot = spot->next;
  79. }
  80.  
  81. return spot;
  82. }
  83.  
  84.  
  85. //Function to insert into the linked list
  86. void listInsert(int coefficient, int exponent)
  87. {
  88. Node *spot = findSpot (coefficient, exponent);
  89.  
  90. if(spot->exponent == exponent )
  91. {
  92. int temp;
  93. temp = spot->coefficient + coefficient;
  94. spot->coefficient = temp;
  95. }
  96. else
  97. {
  98. Node* newNode = new Node(coefficient, exponent);
  99. newNode->next = spot->next;
  100. spot->next = newNode;
  101. }
  102.  
  103. }
  104.  
  105.  
  106. //Function used to insert into the linked list but subtracting like powers.
  107. void listInsertSubtraction(int coefficient, int exponent)
  108. {
  109. Node *spot = findSpot (coefficient, exponent);
  110.  
  111. if(spot->exponent == exponent )
  112. {
  113. int temp;
  114. temp = spot->coefficient - coefficient;
  115. spot->coefficient = temp;
  116. }
  117. else
  118. {
  119. int tempcoeff;
  120. tempcoeff = -coefficient;
  121.  
  122. Node* newNode = new Node(coefficient, exponent);
  123. newNode->next = spot->next;
  124. spot->next = newNode;
  125. }
  126.  
  127. }
  128.  
  129. //Linked List Print function
  130. void printList(ofstream& out)
  131. {
  132. Node* spot = listHead;
  133. out << "Polynomial in canonical form - " << endl;
  134. while(spot != NULL)
  135. {
  136. if (spot->next != NULL && spot->coefficient == 1 && spot->exponent > 0)
  137. {
  138. out << "(" << spot->variable << "^" << spot->exponent << ")" << " + ";
  139.  
  140. }
  141.  
  142. else if(spot->next != NULL && spot->coefficient != 1 && spot-> exponent > 0)
  143. {
  144. out << "(" << spot->coefficient << spot->variable << "^" << spot->exponent << ")" << " + ";
  145. }
  146.  
  147. else if (spot->exponent == 0 && spot->coefficient > -9999)
  148. {
  149. out << "(" << spot->coefficient << ")" << endl << endl;
  150. }
  151.  
  152. /*else if (spot->next != NULL && spot->coefficient == 1 && spot->exponent == 0)
  153. {
  154. out << "(" << spot->variable << "^" << spot->exponent << ")" << " + ";
  155. }*/
  156.  
  157. /*else if (spot->next != NULL && spot->coefficient == 1 && spot->exponent == 0)
  158. {
  159. out << "(" << spot->coefficient << ")";
  160. }*/
  161.  
  162.  
  163. spot = spot->next;
  164. }
  165. }
  166.  
  167. //Function to multiply two polynomials.
  168. linkedList polynomialMultiplication (linkedList& polynomialA, linkedList& polynomialB)
  169. {
  170. linkedList newPolynomial;
  171.  
  172. //Temporary Nodes point to the first element of each linked list.
  173. Node* tempNodeA = polynomialA.listHead->next;
  174. Node* tempNodeB = polynomialB.listHead->next;
  175.  
  176. while (tempNodeA != NULL && tempNodeB != NULL)
  177. {
  178. int coefficientC;
  179. int exponentC;
  180.  
  181. //Foils out the first element of polynomial A with all the nodes in B.
  182. coefficientC = tempNodeA->coefficient * tempNodeB->coefficient;
  183. exponentC = tempNodeA->exponent + tempNodeB->exponent;
  184. newPolynomial.listInsert (coefficientC, exponentC);
  185. tempNodeB = tempNodeB->next;
  186.  
  187. //When B hits the end of the list
  188. if (tempNodeB == NULL)
  189. {
  190. //While loop for A with nodes remaining.
  191. while (tempNodeA->next != NULL)
  192. {
  193.  
  194. //Move the pointer to the next node in A, and reset the pointer of B to the beginning of the list.
  195. tempNodeA = tempNodeA->next;
  196. tempNodeB = polynomialB.listHead->next;
  197.  
  198. //Repeating the foil until it reaches the end of the list.
  199. while (tempNodeB != NULL)
  200. {
  201. coefficientC = tempNodeA->coefficient * tempNodeB->coefficient;
  202. exponentC = tempNodeA->exponent + tempNodeB->exponent;
  203. newPolynomial.listInsert (coefficientC, exponentC);
  204. tempNodeB = tempNodeB->next;
  205.  
  206. }
  207. }
  208. }
  209. }
  210.  
  211. //Returns the whole new linked list.
  212. return newPolynomial;
  213.  
  214. }
  215.  
  216. //Function to add two polynomials.
  217. linkedList polynomialAddition (linkedList polynomialA, linkedList polynomialB)
  218. {
  219.  
  220. linkedList newPolynomial = polynomialA;
  221.  
  222. //Temporary Nodes point to the first element of each linked list.
  223. Node* tempNodeB = polynomialB.listHead->next;
  224.  
  225. while (tempNodeB != NULL)
  226. {
  227. newPolynomial.listInsert (tempNodeB->coefficient, tempNodeB->exponent);
  228. tempNodeB = tempNodeB->next;
  229.  
  230. }
  231.  
  232. return newPolynomial;
  233. }
  234.  
  235. //Function to subtract the 2nd polynomial from the first polynomial.
  236. linkedList polynomialSubtraction (linkedList polynomialA, linkedList polynomialB)
  237. {
  238. linkedList newPolynomial = polynomialA;
  239.  
  240. //Temporary Nodes point to the first element of each linked list.
  241. Node* tempNodeB = polynomialB.listHead->next;
  242.  
  243. while (tempNodeB != NULL)
  244. {
  245. newPolynomial.listInsertSubtraction (tempNodeB->coefficient, tempNodeB->exponent);
  246. tempNodeB = tempNodeB->next;
  247.  
  248. }
  249.  
  250. return newPolynomial;
  251.  
  252. }
  253. };
  254.  
  255.  
  256. int main(int argc, char* argv[])
  257. {
  258. ifstream infile;
  259. ofstream outfile;
  260.  
  261. infile.open(argv[1]);
  262. outfile.open(argv[2]);
  263. int exponent;
  264. int coefficient;
  265. string line;
  266. string line2;
  267.  
  268. linkedList polynomialA;
  269. linkedList polynomialB;
  270.  
  271. //Populating polynomial 1
  272. getline (infile, line);
  273. stringstream ss(line);
  274. while (ss >> coefficient >> exponent)
  275. {
  276. polynomialA.listInsert(coefficient, exponent);
  277. }
  278.  
  279. //Populating polynomial 2
  280. getline (infile, line2);
  281. stringstream ss2(line2);
  282. while (ss2 >> coefficient >> exponent)
  283. {
  284. polynomialB.listInsert(coefficient, exponent);
  285. }
  286.  
  287.  
  288. infile.close();
  289. polynomialA.printList(outfile);
  290. polynomialB.printList(outfile);
  291.  
  292.  
  293. linkedList polynomialC;
  294. polynomialC = polynomialC.polynomialMultiplication(polynomialA, polynomialB);
  295.  
  296. outfile << endl;
  297. outfile << "Printing the multiplication of the first polynomial and the second polynomial" << endl << endl;
  298. polynomialC.printList(outfile);
  299.  
  300. outfile << endl;
  301. outfile << "Printing addition of the first polynomial and the second polynomial" << endl << endl;
  302. linkedList polynomialD;
  303. polynomialD = polynomialD.polynomialAddition(polynomialA, polynomialB);
  304. polynomialD.printList(outfile);
  305.  
  306. outfile << endl;
  307. outfile << "Printing subtraction of the second polynomial from the first polynomial" << endl << endl;
  308. linkedList polynomialE;
  309. polynomialE = polynomialE.polynomialSubtraction(polynomialA, polynomialB);
  310. polynomialE.printList(outfile);
  311.  
  312.  
  313.  
  314. return 0;
  315. }
  316.  
  317. //output
  318.  
  319. Polynomial in canonical form -
  320. (6x^6) + (9x^2) + (-5)
  321.  
  322. Polynomial in canonical form -
  323. (7x^7) + (2x^5) + (7x^2) + (12)
  324.  
  325.  
  326. Printing the multiplication of the first polynomial and the second polynomial
  327.  
  328. Polynomial in canonical form -
  329. (42x^13) + (12x^11) + (63x^9) + (42x^8) + (-17x^7) + (72x^6) + (-10x^5) + (63x^4) + (73x^2) + (-60)
  330.  
  331.  
  332. Printing addition of the first polynomial and the second polynomial
  333.  
  334. Polynomial in canonical form -
  335. (7x^7) + (6x^6) + (2x^5) + (16x^2) + (7)
  336.  
  337.  
  338. Printing subtraction of the second polynomial from the first polynomial
  339.  
  340. Polynomial in canonical form -
  341. (0x^7) + (6x^6) + (0x^5) + (9x^2) + (-5)
Advertisement
Add Comment
Please, Sign In to add comment