Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. // Assignment3.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. class Polynomial {
  8. private:
  9. int exponent;
  10. int coefficient;
  11. char variable;
  12.  
  13. public:
  14. Polynomial(int exponent = 0., int coefficient = 0., char variable = 'x');
  15. void setExponent(int);
  16. void setCoefficient(int);
  17. void setVariable(char);
  18. int getExponent() const;
  19. int getCoefficient() const;
  20. char getVariable() const;
  21. Polynomial operator+ (const Polynomial& c) const;
  22. Polynomial operator- (const Polynomial& c) const;
  23. Polynomial operator* (const Polynomial& c) const;
  24. };
  25.  
  26. Polynomial::Polynomial(int e, int c, char v) {
  27. exponent = e;
  28. c = coefficient;
  29. v = variable;
  30. }
  31. void Polynomial::setExponent(int e) {
  32. exponent = e;
  33. }
  34.  
  35. void Polynomial::setCoefficient(int c) {
  36. coefficient = c;
  37. }
  38.  
  39. void Polynomial::setVariable(char v) {
  40. variable = v;
  41. }
  42.  
  43. char Polynomial::getVariable() const {
  44. return variable;
  45. }
  46.  
  47. int Polynomial::getCoefficient() const {
  48. return coefficient;
  49. }
  50.  
  51. int Polynomial::getExponent() const {
  52. return exponent;
  53. }
  54.  
  55. void print(Polynomial object) {
  56.  
  57. cout << "The polynomial is " << object.getCoefficient() << object.getVariable() << "^" << object.getExponent() << endl;
  58. }
  59.  
  60. Polynomial Polynomial::operator+ (const Polynomial& c) const {
  61.  
  62. Polynomial result;
  63. if (result.exponent = c.exponent) {
  64. result.coefficient = (this->coefficient + c.coefficient);
  65. result.variable = c.variable;
  66. return result;
  67. }
  68. }
  69.  
  70. Polynomial Polynomial::operator- (const Polynomial& c) const {
  71.  
  72. Polynomial result;
  73. if (result.exponent = c.exponent) {
  74. result.coefficient = (this->coefficient - c.coefficient);
  75. result.variable = c.variable;
  76. return result;
  77. }
  78. }
  79.  
  80. Polynomial Polynomial::operator* (const Polynomial& c) const {
  81.  
  82. Polynomial result;
  83. result.coefficient = (this->coefficient * c.coefficient);
  84. result.exponent = (this->exponent * c.exponent);
  85. result.variable = c.variable;
  86. return result;
  87. }
  88.  
  89. int main() {
  90.  
  91. Polynomial object;
  92. int b, c;
  93. char x;
  94. cout << "What is the coefficient?";
  95. cin >> b;
  96. object.setCoefficient(b);
  97. cout << "What is the exponent?";
  98. cin >> c;
  99. object.setExponent(c);
  100. object.setVariable('x');
  101. print(object);
  102.  
  103. Polynomial object2;
  104. int d, e;
  105. cout << "What is the second coefficient?";
  106. cin >> d;
  107. cout << "What is the second exponent?";
  108. cin >> e;
  109. object2.setCoefficient(d);
  110. object2.setExponent(e);
  111. object2.setVariable('x');
  112.  
  113. int f;
  114. cout << "Do you want to 1. add \n2. subtract \n3. multiply";
  115. cin >> f;
  116.  
  117. if (f == 1) {
  118. Polynomial object3 = object + object2;
  119. print(object3);
  120. }
  121.  
  122. else if (f == 2) {
  123. Polynomial object3 = object - object2;
  124. print(object3);
  125. }
  126.  
  127. else if (f == 3) {
  128. Polynomial object3 = object * object2;
  129. print(object3);
  130. }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement