Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- using namespace std;
- class Poly {
- public:
- Poly();
- Poly(double c);
- Poly(double a[], int size);
- Poly(const Poly& source);
- ~Poly();
- Poly& operator=(const Poly& source);
- friend const Poly operator + (const Poly& p1, const Poly& p2);
- friend const Poly operator - (const Poly& p1, const Poly& p2);
- friend const Poly operator * (const Poly& p1, const Poly& p2);
- friend ostream& operator << (ostream& output, const Poly& p);
- friend void maxPoly(const Poly p1, const Poly p2);
- double& operator [] (const int position);
- double polyVal(double x) const;
- void print() const;
- private:
- double* coef;
- int degree;
- };
- ostream& operator<<(ostream& output, const Poly& p) {
- p.print();
- return output;
- }
- void Poly::print() const {
- bool firstNonZero = false;
- for (int i = 0; i < degree; i++) {
- if (coef[degree - i - 1] != 0) {
- firstNonZero = true;
- cout << coef[degree - i - 1];
- if ((degree - i - 1) != 0) {
- cout << "x^" << degree - i - 1 ;
- }
- }
- if (firstNonZero && (i < (degree - 1))) {
- cout << " + ";
- }
- }
- if (firstNonZero == false) {
- cout << "0";
- }
- }
- Poly::Poly() {
- degree = 1;
- coef = new double[degree];
- coef[0] = 0;
- }
- Poly::Poly(double c) {
- coef = new double[degree];
- coef[0] = c;
- }
- Poly::Poly(double a[], int SIZE) {
- degree = SIZE;
- coef = new double[SIZE];
- for (int i = 0; i <= SIZE; i++) {
- coef[i] = a[i];
- }
- }
- Poly::Poly(const Poly& source) {
- if (degree != source.degree) {
- delete [] coef;
- degree = source.degree;
- coef = new double[degree];
- }
- for (int i = 0; i < degree; i++) {
- coef[i] = source.coef[i];
- }
- }
- Poly::~Poly() {
- delete [] coef;
- }
- Poly& Poly::operator=(const Poly& source) {
- if (degree != source.degree) {
- delete [] coef;
- degree = source.degree;
- coef = new double[degree];
- }
- for (int i = 0; i < degree; i++) {
- coef[i] = source.coef[i];
- }
- return *this;
- }
- const Poly operator+(const Poly& p1, const Poly& p2) {
- Poly sum;
- if (p1.degree >= p2.degree) {
- sum.degree = p1.degree;
- } else {
- sum.degree = p2.degree;
- }
- delete [] sum.coef;
- sum.coef = new double[sum.degree];
- for (int i = 0; i < sum.degree; i++) {
- sum.coef[i] = 0;
- if (i < p1.degree) {
- sum.coef[i] += p1.coef[i];
- }
- if (i < p2.degree) {
- sum.coef[i] += p2.coef[i];
- }
- }
- return sum;
- }
- const Poly operator-(const Poly& p1, const Poly& p2) {
- Poly diff;
- if (p1.degree >= p2.degree) {
- diff.degree = p1.degree;
- } else {
- diff.degree = p2.degree;
- }
- delete [] diff.coef;
- diff.coef = new double[diff.degree];
- for (int i = 0; i < diff.degree; i++) {
- diff.coef[i] = 0;
- if (i < p1.degree) {
- diff.coef[i] += p1.coef[i];
- }
- if (i < p2.degree) {
- diff.coef[i] -= p2.coef[i];
- }
- }
- return diff;
- }
- const Poly operator*(const Poly& p1, const Poly&p2) {
- Poly product;
- product.degree = p1.degree + p2.degree;
- delete [] product.coef;
- product.coef = new double[product.degree];
- for (int i = 0; i < product.degree; i++) {
- product.coef[i] = 0;
- }
- for (int i = 0; i < p1.degree; i++) {
- for (int j = 0; j < p2.degree; j++) {
- product.coef[i + j] += p1.coef[i] * p2.coef[j];
- }
- }
- return product;
- }
- double& Poly::operator [ ] (const int position) {
- return coef[position];
- }
- double Poly::polyVal(double x) const {
- double polySum = 0;
- for (int i = 0; i <= degree - 1; i++) {
- polySum += coef[i] * pow(x,(degree - 1 - i));
- }
- return polySum;
- }
- void maxPoly(const Poly p1, const Poly p2) {
- double p3 = p1.polyVal(5);
- double p4 = p2.polyVal(5);
- if (p3 > p4) {
- cout << "The first array is larger than the second.";
- }
- else {
- cout << "The second array is larger than the first.";
- }
- }
- int main() {
- Poly p1;
- Poly p2(3);
- double a[] = {1, -2, 3, 4};
- double b[] = {50, 0, 1};
- Poly p3(a, 4);
- Poly p4(b, 3);
- cout << "p1: " << p1 << endl;
- cout << "p2: " << p2 << endl;
- cout << "p3: " << p3 << endl;
- cout << "p4: " << p4 << endl;
- cout << "------------------------------" << endl;
- Poly p5 = p1 + p2 + p3;
- cout << "p5 = p1 + p2 + p3: \n" << "p5: " << p5 << endl;
- Poly p6 = p1 - p2;
- cout << "p1 - p2\n" << p6 << endl;
- p6 = p2 - p2;
- cout << "p2 - p2\n" << p6 << endl;
- p6 = p2 - p1;
- cout << "p2 - p1\n" << p6 << endl;
- cout << "p4 - p3: " << (p4 - p3) << endl;
- cout << "p1 * p3: " << (p1 * p3) << endl;
- cout << "p2 * p3: " << (p2 * p3) << endl;
- cout << "p4 * p3: " << (p4 * p3) << endl;
- cout << p3.polyVal(0);
- cout<< p3[2];
- maxPoly(p3,p4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement