[ create a new paste ] login | about

Link: http://codepad.org/8zemm4qF    [ raw code | output | fork ]

C++, pasted on Sep 14:
#include <iostream>
#include <vector>
using namespace std;


class Polynomial {
public:
    vector<int> elements;

    Polynomial();
    Polynomial(vector<int>);
    Polynomial(const Polynomial &);
    ~Polynomial();

    void input();
    void output();

    void set(vector<int>);

    void multiplyPolynomial(Polynomial &);
    Polynomial multiplyNumber(int);
    Polynomial getPrototypal();

    Polynomial operator*(const Polynomial &);
    Polynomial operator*(int);
    Polynomial operator=(const Polynomial &);

    friend ostream &operator<<(ostream &, Polynomial &);
    friend istream &operator>>(istream &, Polynomial &);
};

Polynomial::Polynomial() {
    this->elements.push_back(0);
}

Polynomial::Polynomial(const Polynomial &polynomial) {
    this->elements = polynomial.elements;
}

Polynomial::Polynomial(vector<int> elements) {
    this->elements = elements;
}

Polynomial::~Polynomial() {
    this->elements.clear();
}

void Polynomial::input() {
    this->elements.clear();
    int n, num;
    cout << "Enter size: ";
    cin >> n;
    for (int i = 0; i < n; i ++) {
        cout << (i + 1) << " element: ";
        cin >> num;
        this->elements.push_back(num);
    }
    cout << "Input finish" << endl;
}

void Polynomial::output() {
    for (int i = 0; i < this->elements.size(); i ++) {
        if (this->elements[i] != 0) {
            if (i > 0) {
                cout << "+";
            }
            if (this->elements[i] != 1) {
                cout << this->elements[i];
            }
            if (this->elements[i] != 1 && this->elements.size() - i != 1) {
                cout << "*";
            }
            if (this->elements.size() - i != 1) {
                cout << "x";
            }
            if (this->elements.size() - i > 2) {
                cout << "^" << (this->elements.size() - i - 1);
            }
        }
    }
    cout << endl;
}

void Polynomial::set(vector<int> elements) {
    this->elements = elements;
}

void Polynomial::multiplyPolynomial(Polynomial &polynomial) {
    int max;
    Polynomial pnm, p1, p2;
    if (this->elements.size() > polynomial.elements.size()) {
        max = this->elements.size();
        p1 = *this;
        p2 = polynomial;
    } else {
        max = polynomial.elements.size();
        p1 = polynomial;
        p2 = *this;
    }
    for (int i = p1.elements.size(); i > 0; i --) {
        cout << i << endl;
        for (int j = 0; j < p1.elements.size(); j ++) {
            
        }
        //if (i <= polynomial.elements.size()) {
        //   //cout << "p2: " << polynomial.elements[i-1] << endl;
        //}
        //if (i <= this->elements.size()) {
        //   //cout << "p: " << this->elements[i-1] << endl;
        //}
        cout << endl;
    }
}

Polynomial Polynomial::multiplyNumber(int number) {
    Polynomial newp;
    //newp = new Polynomial();
    for (int i = 0; i < this->elements.size(); i ++) {
         newp.elements[i] =  this->elements[i] * number;
    }
    return newp;
}

Polynomial Polynomial::getPrototypal() {
    Polynomial newp;

    return newp;
}

Polynomial Polynomial::operator*(const Polynomial& polynomial) {
    //return this->multiplyPolynomial(polynomial);
    return polynomial;
}

/*Polynomial Polynomial::operator*(int number) {
    this->multiplyNumber(number);
    return *this;
}*/

Polynomial Polynomial::operator=(const Polynomial &polynomial) {
    if (this == &polynomial) {
        return *this;
    }
    this->elements = polynomial.elements;
    return *this;
}

ostream &operator<<(ostream &s, Polynomial &polynomial) {
    polynomial.output();
    return s;
}

istream &operator>>(istream &s, Polynomial &polynomial) {
    polynomial.input();
    return s;
}

int main() {
    Polynomial polynomial, polynomial2, p3;
    cin >> polynomial;
    cout << polynomial;
    //cout << "Enter number: ";
    //int number;
    //cin >> number;
    //polynomial.multiplyNumber(number);
    //cout << polynomial;
    cin >> polynomial2;
    p3 = polynomial * polynomial2;
    return 1;
}


Output:
1
2
3
4
5
6
7
cc1plus: warnings being treated as errors
t.cpp: In member function 'void Polynomial::output()':
Line 62: warning: comparison between signed and unsigned integer expressions
t.cpp: In member function 'void Polynomial::multiplyPolynomial(Polynomial&)':
Line 102: warning: comparison between signed and unsigned integer expressions
t.cpp: In member function 'Polynomial Polynomial::multiplyNumber(int)':
Line 118: warning: comparison between signed and unsigned integer expressions


Create a new paste based on this one


Comments: