Guest User

Untitled

a guest
Jan 9th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include "poly.h"
  2. using namespace std;
  3.  
  4. //Constructors/Destructor
  5.  
  6. //----------------------------Poly()------------------------------------------//
  7. // Base constructor. 0x^0
  8. //----------------------------------------------------------------------------//
  9. Poly::Poly() {
  10. size = 1;
  11. polyArray = new int[size];
  12. polyArray[0] = 0;
  13. }
  14.  
  15. //----------------------------Poly(int coeff)---------------------------------//
  16. // Constructor using a poly of (coeff)x^0.
  17. //----------------------------------------------------------------------------//
  18. Poly::Poly(int coeff) {
  19. size = 1;
  20. polyArray = new int[1];
  21. polyArray[0] = coeff;
  22. }
  23.  
  24. //----------------------------Poly(int coeff)---------------------------------//
  25. // Constructor using a poly of (coeff)x^(poly).
  26. //----------------------------------------------------------------------------//
  27. Poly::Poly(int coeff, int pow) {
  28. size = pow + 1;
  29. polyArray = new int[size];
  30. for (int i = 0; i < size - 1; i++) {
  31. polyArray[i] = 0;
  32. }
  33. polyArray[pow] = coeff;
  34. }
  35.  
  36. Poly::Poly(const Poly& insertPoly) {
  37. size = insertPoly.size;
  38. polyArray = new int[size];
  39. for (int i = 0; i < size; i++) {
  40. polyArray[i] = insertPoly.polyArray[i];
  41. }
  42. }
  43.  
  44. Poly::~Poly() {
  45. size = 0;
  46. delete[] polyArray;
  47. polyArray = NULL;
  48. }
  49.  
  50. //Getters/Setters
  51. int Poly::getCoeff(int pow) {
  52. if (pow > size || pow < 0) return 0;
  53. return polyArray[pow];
  54. }
  55.  
  56. void Poly::setCoeff(int coeff, int pow) {
  57. //put in protection for negative powers
  58. polyArray[pow] = coeff;
  59. }
  60.  
  61. //Binary Math operator overloads
  62. Poly Poly::operator+(const Poly& input) const {
  63. if (size < input.size) {
  64. Poly tempPoly(input);
  65. // Since the size is smaller, we only copy
  66. for (int i = 0; i < size; i++) {
  67. tempPoly.polyArray[i] = polyArray[i] + input.polyArray[i];
  68. }
  69.  
  70. return tempPoly;
  71. }
  72. else {
  73. Poly tempPoly(*this);
  74. for (int i = 0; i < input.size; i++) {
  75. tempPoly.polyArray[i] = polyArray[i] + input.polyArray[i];
  76. }
  77. return tempPoly;
  78. }
  79.  
  80. }
  81.  
  82. //Bolean Operator Overloads
  83. bool Poly::operator==(const Poly& input) const {
  84. if (size == input.size) return true;
  85. for (int i = 0; i < input.size; i++) { // Check each coeff if they're the same.
  86. if (polyArray[i] != input.polyArray[i]) return false;
  87. }
  88. return true;
  89. }
  90.  
  91. bool Poly::operator!=(const Poly& input) const {
  92. return !(*this == input);
  93. }
  94.  
  95.  
  96. //IO Operstors
Advertisement
Add Comment
Please, Sign In to add comment