Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Assignment3.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class Polynomial {
- private:
- int exponent;
- int coefficient;
- char variable;
- public:
- Polynomial(int exponent = 0., int coefficient = 0., char variable = 'x');
- void setExponent(int);
- void setCoefficient(int);
- void setVariable(char);
- int getExponent() const;
- int getCoefficient() const;
- char getVariable() const;
- Polynomial operator+ (const Polynomial& c) const;
- Polynomial operator- (const Polynomial& c) const;
- Polynomial operator* (const Polynomial& c) const;
- };
- Polynomial::Polynomial(int e, int c, char v) {
- exponent = e;
- c = coefficient;
- v = variable;
- }
- void Polynomial::setExponent(int e) {
- exponent = e;
- }
- void Polynomial::setCoefficient(int c) {
- coefficient = c;
- }
- void Polynomial::setVariable(char v) {
- variable = v;
- }
- char Polynomial::getVariable() const {
- return variable;
- }
- int Polynomial::getCoefficient() const {
- return coefficient;
- }
- int Polynomial::getExponent() const {
- return exponent;
- }
- void print(Polynomial object) {
- cout << "The polynomial is " << object.getCoefficient() << object.getVariable() << "^" << object.getExponent() << endl;
- }
- Polynomial Polynomial::operator+ (const Polynomial& c) const {
- Polynomial result;
- if (result.exponent = c.exponent) {
- result.coefficient = (this->coefficient + c.coefficient);
- result.variable = c.variable;
- return result;
- }
- }
- Polynomial Polynomial::operator- (const Polynomial& c) const {
- Polynomial result;
- if (result.exponent = c.exponent) {
- result.coefficient = (this->coefficient - c.coefficient);
- result.variable = c.variable;
- return result;
- }
- }
- Polynomial Polynomial::operator* (const Polynomial& c) const {
- Polynomial result;
- result.coefficient = (this->coefficient * c.coefficient);
- result.exponent = (this->exponent * c.exponent);
- result.variable = c.variable;
- return result;
- }
- int main() {
- Polynomial object;
- int b, c;
- char x;
- cout << "What is the coefficient?";
- cin >> b;
- object.setCoefficient(b);
- cout << "What is the exponent?";
- cin >> c;
- object.setExponent(c);
- object.setVariable('x');
- print(object);
- Polynomial object2;
- int d, e;
- cout << "What is the second coefficient?";
- cin >> d;
- cout << "What is the second exponent?";
- cin >> e;
- object2.setCoefficient(d);
- object2.setExponent(e);
- object2.setVariable('x');
- int f;
- cout << "Do you want to 1. add \n2. subtract \n3. multiply";
- cin >> f;
- if (f == 1) {
- Polynomial object3 = object + object2;
- print(object3);
- }
- else if (f == 2) {
- Polynomial object3 = object - object2;
- print(object3);
- }
- else if (f == 3) {
- Polynomial object3 = object * object2;
- print(object3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement