Addition, Substraction & Multiplication of two polynomials using C++

#include<iostream>
#include<stdlib.h>

#define RESET_COLOR "\e[m"
#define GREEN  "\e[32m"

using namespace std;

class polynomial {
public:
    int *coeff, degree; /* variable declaration */

    int get_data(); /*function declaration */
    int display(int *coeff, int degree);
    void addition(polynomial P1, polynomial P2);
    void substraction(polynomial P1, polynomial P2);
    void multiplication(polynomial P1, polynomial P2);
};

int polynomial::display(int *coeff, int degree) {
    int i, j;
    for (i = degree; i >= 0; i--) {
        cout << coeff[i] << "x^" << i;
        if ((i - 1) != -1)
            cout << "+";
    }
    cout << "\n";
    return 0;
}

int polynomial::get_data() {
    int i;
    cout << "Enter Degree Of Polynomial:";
    cin >> degree;
    coeff = new int[degree + 1];
    for (i = degree; i >= 0; i--) {
        cout << "Enter coefficient of x^" << i << ":";
        cin >> coeff[i];
    }

    return 0;
}

void polynomial::addition(polynomial P1, polynomial P2) {
    int max, i;
    max = (P1.degree > P2.degree) ? P1.degree : P2.degree;
    int *add = new int[max + 1];
    if (P1.degree == P2.degree) {
        for (i = P1.degree; i >= 0; i--)
            add[i] = P1.coeff[i] + P2.coeff[i];
    }

    if (P1.degree > P2.degree) {
        for (i = P1.degree; i > P2.degree; i--)
            add[i] = P1.coeff[i];
        for (i = P2.degree; i >= 0; i--)
            add[i] = P1.coeff[i] + P2.coeff[i];
    }

    if (P1.degree < P2.degree) {
        for (i = P2.degree; i > P1.degree; i--)
            add[i] = P2.coeff[i];
        for (i = P1.degree; i >= 0; i--)
            add[i] = P1.coeff[i] + P2.coeff[i];
    }
    cout << "\nAddition:";
    display(add, max);
    cout << "\n";
}

void polynomial::substraction(polynomial P1, polynomial P2) {
    int max, i;
    max = (P1.degree > P2.degree) ? P1.degree : P2.degree;
    int *sub = new int[max + 1];
    if (P1.degree == P2.degree) {
        for (i = P1.degree; i >= 0; i--)
            sub[i] = P1.coeff[i] - P2.coeff[i];
    }

    if (P1.degree > P2.degree) {
        for (i = P1.degree; i > P2.degree; i--)
            sub[i] = P1.coeff[i];
        for (i = P2.degree; i >= 0; i--)
            sub[i] = P1.coeff[i] - P2.coeff[i];
    }

    if (P1.degree < P2.degree) {
        for (i = P2.degree; i > P1.degree; i--)
            sub[i] = -P2.coeff[i];
        for (i = P1.degree; i >= 0; i--)
            sub[i] = P1.coeff[i] - P2.coeff[i];
    }
    cout << "\nSubstraction:";
    display(sub, max);
    cout << "\n";
}

void polynomial::multiplication(polynomial P1, polynomial P2) {
    int i, j, max;
    max = P1.degree + P2.degree;
    int *mul = new int[max + 1];

    for (i = P1.degree; i >= 0; i--)
        for (j = P2.degree; j >= 0; j--)
            mul[i + j] += P1.coeff[i] * P2.coeff[j];
    cout << "\nMultiplication:";
    display(mul, max);
}

int main() {
    int choice;
    polynomial P1, P2, P3;
    cout << GREEN << "Instruction:- \nExample:-\nP(x)=5x^3+3x^1\nEnter the Polynomial like\nP(x)=5x^3+0x^2+3x^1+0x^0\n" << RESET_COLOR;
    cout << "Enter Polynomial1:-" << endl;
    P1.get_data();
    cout << "Enter Polynomial2:-" << endl;
    P2.get_data();

    while (1) {
        cout << "\n****** Menu Selection ******" << endl;
        cout << "1: Addition\n2: Substraction\n3: Multiplication\n0: Exit" << endl;
        cout << "Enter ypur choice:";
        cin >> choice;
        switch (choice) {
            case 1:
                cout << GREEN << "\n--------------- Addition ---------------\n";
                cout << "Polynomial1:";
                P1.display(P1.coeff, P1.degree);
                cout << "Polynomial2:";
                P2.display(P2.coeff, P2.degree);
                P3.addition(P1, P2);
                cout << "----------------------------------------\n" << RESET_COLOR;
                break;
            case 2:

                cout << GREEN << "\n------------- Substraction -------------\n";
                cout << "Polynomial1:";
                P1.display(P1.coeff, P1.degree);
                cout << "Polynomial2:";
                P2.display(P2.coeff, P2.degree);
                P3.substraction(P1, P2);
                cout << "----------------------------------------\n" << RESET_COLOR;
                break;
            case 3:
                cout << GREEN << "\n----------- Multiplication -------------\n";
                cout << "Polynomial1:";
                P1.display(P1.coeff, P1.degree);
                cout << "Polynomial2:";
                P2.display(P2.coeff, P2.degree);
                P3.multiplication(P1, P2);
                cout << "----------------------------------------\n" << RESET_COLOR;
                break;
            case 0:
                cout << "Good Bye...!!!" << endl;
                exit(0);
        }
    }
    return 0;
}

Output :
Instruction:-
Example:-
P(x)=5x^3+3x^1
Enter the Polynomial like
P(x)=5x^3+0x^2+3x^1+0x^0
Enter Polynomial1:-
Enter Degree Of Polynomial:3
Enter coefficient of x^3:3
Enter coefficient of x^2:4
Enter coefficient of x^1:5
Enter coefficient of x^0:9
Enter Polynomial2:-
Enter Degree Of Polynomial:2
Enter coefficient of x^2:4
Enter coefficient of x^1:3
Enter coefficient of x^0:1

****** Menu Selection ******
1: Addition
2: Substraction
3: Multiplication
0: Exit
Enter ypur choice:1

--------------- Addition ---------------
Polynomial1:3x^3+4x^2+5x^1+9x^0
Polynomial2:4x^2+3x^1+1x^0

Addition:3x^3+8x^2+8x^1+10x^0

----------------------------------------

Enter ypur choice:2

------------- Substraction -------------
Polynomial1:3x^3+4x^2+5x^1+9x^0
Polynomial2:4x^2+3x^1+1x^0

Substraction:3x^3+0x^2+2x^1+8x^0

----------------------------------------

Enter ypur choice:3

----------- Multiplication -------------
Polynomial1:3x^3+4x^2+5x^1+9x^0
Polynomial2:4x^2+3x^1+1x^0

Multiplication:12x^5+25x^4+35x^3+55x^2+32x^1+9x^0
----------------------------------------

Popular posts from this blog

8 Bit Plane Slicing of an image in Image Processing

Code to upload multiple files simultaneously using JSP, Servlet .

STRING PALINDROME USING STACK AND QUEUE