**2.5 多项式加法、乘法、输入、输出运算符重载

帅比萌擦擦* 提交于 2020-03-14 11:17:19

Polynomial.h

#pragma once
#include<iostream>
using namespace std;
class Term {
    friend class Polynomial;
public:
    int a;
    int x;
    Term* next;
};

class Polynomial {
    Term* first;
public:
    Polynomial() {
        first = new Term();
        first->x = NULL;
        first->a = NULL;
        first->next = nullptr;
    }
    void insert(int x, int a) {
        Term* p;
        p = first;
        Term* s;
        while (p->next != nullptr) {
            p = p->next;
        }
        s = new Term();
        s->a = a;
        s->x = x;
        s->next = p->next;
        p->next = s;
    }
    int maxE() {
        Term* p;
        int max = -999999;
        p = first->next;
        while (p != nullptr) {
            if (p->x > max) {
                max = p->x;
            }
            p = p->next;
        }
        return max;
    }
    Term* getFirst() {
        return first;
    }
    friend Polynomial operator + (Polynomial& A, Polynomial& B) {
        Polynomial C;
        Term* pa, * pb, * pc;
        pa = A.first->next;
        pb = B.first->next;
        pc = C.first->next;
        while (pa != nullptr && pb != nullptr) {
            if (pa->x > pb->x) {
                C.insert(pb->x, pb->a);
                pb = pb->next;
            }
            else if (pa->x == pb->x) {
                if (pa->a + pb->a != 0) {
                    C.insert(pa->x, pa->a + pb->a);
                }
                    pa = pa->next;
                    pb = pb->next;
            }
            else if (pa->x < pb->x) {
                C.insert(pa->x, pa->a);
                pa = pa->next;

            }
        }
        if (pa == nullptr) {
            while (pb != nullptr) {
                C.insert(pb->x, pb->a);
                pb = pb->next;
            }
        }
        else if (pb == nullptr) {
            while (pa != nullptr) {
                C.insert(pa->x, pa->a);
                pa = pa->next;
            }
        }
        return C;
    }
    friend Polynomial operator * (Polynomial& A, Polynomial& B) {
        int* arr;
        Term* pa, * pb, * pc;
        int index, elem;
        Polynomial C;
        pc = C.first;
        int max = A.maxE() + B.maxE();
        arr = new int[max + 1];
        for (int i = 0; i < max + 1; i++) {
            arr[i] = 0;
        }
        pa = A.first->next;
        while (pa != nullptr) {
            pb = B.first->next;
            while (pb != nullptr) {
                index = pa->x + pb->x;
                elem = pa->a * pb->a;
                arr[index] += elem;
                pb = pb->next;
            }
            pa = pa->next;
        }
        for (int i = 0; i < max + 1; i++) {
            if (arr[i] != 0) {
                C.insert(i, arr[i]);
            }
        }
        return C;

    }
    friend ostream& operator<<(ostream& out, Polynomial& po) {
        Term* p;
        int num=0;
        p = po.getFirst();
        p = p->next;
        while (p != nullptr) {
                if (p->a >= 0&&num!=0) {
                    out << "+";
                }
            
                out << p->a;
                if (p->x > 0) {
                    out << "x^";
                    if (p->x > 1) {
                    out<<p->x;
                    }
                }
            p = p->next;
            num++;
        }
        out << endl;
        return out;
    }
    friend istream& operator>>(istream& in, Polynomial& po) {
        int a=-1, x=-1;
        while (1) {
            in >> a>> x;
            if (x >= 0) {
                po.insert(x, a);
            }
            if (x < 0) {
                break;
            }
        }
        return in;
    }
};

main.cpp

#include"Polynomial.h"

int main() {
    Polynomial A, B, C, D;
    cout << "Please inputa A:" << endl;
    cin >> A;
    cout << "A:" << endl;
    cout << A << endl;
    cout << "Please inputa B:" << endl;
    cin >> B;
    cout << "B:" << endl;
    cout << B << endl;
    C = A + B;
    cout << "A+B:" << endl;
    cout << C << endl;
    D = A * B;
    cout << "A*B:" << endl;
    cout << D << endl;
    return 0;
}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!