C++ code example: simple calculation tool for polynomial division

Article directory

  • Preface
  • code repository
  • code
    • illustrate
    • core fragment
  • result
  • Summarize
  • References
  • Author’s words

Foreword

C++ code example: simple calculation tool for polynomial division.

Code repository

  • yezhening/Programming-examples: Programming examples (github.com)
  • Programming-examples: Programming examples (gitee.com)

Code

Description

  • Due to the large length of the code, it would be verbose to paste and present directly in the blog, so the complete code is not placed in the blog and only the core fragment code is displayed. Please go to “Code Warehouse” to view or obtain related codes
  • Simple calculation of polynomial division
  • Comments with detailed step-by-step analysis
  • A useful online calculation website for polynomial division: Online Calculator: Polynomial Division (planetcalc.com)
  • Polynomial division, dividend and divisor are written in the program: main.exe, main.cpp, polynomial.cpp, polynomial.h
  • Polynomial division, enter the dividend and divisor in the terminal: main1.exe, main1.cpp, polynomial.cpp, polynomial.h

Note: If you complete it in one night and one morning, most of the results should be correct. If a small part of the results are wrong, don’t bother to worry about it. The logic is almost the same.

Core fragment

polynomial.h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <vector>
#include <iostream>

using std::ostream;
using std::vector;

//polynomial class
classPolynomial
{<!-- -->
public:
    explicit Polynomial(const vector<int> & amp;p) : poly_vec(p) {<!-- -->} // Private members can only be initialized inside the member function of the class

    // Overloaded operator "<<" output polynomial
    // friend: This operator will be used outside the class to access the members of the class. If not written, an error will be reported: too many parameters for this operator function
    //Return the output stream object reference, which can be chained
    friend ostream & amp;operator<<(ostream & amp;output, const Polynomial & amp;obj);

    // Overload operator "/" for polynomial division
    //Return value: quotient
    // Parameter: divisor
    Polynomial operator/(const Polynomial & amp;divisor) const; // const means that the dividend of calling this method will not be changed

    // Overload operator "%" to perform polynomial division to find remainder
    //Return value: remainder
    // Parameter: divisor
    //Copy the division logic, only change the return value of the last return, the final remainder is the last updated dividend
    Polynomial operator%(const Polynomial & amp;divisor) const; // const means that the dividend of calling this method will not be changed

private:
    const vector<int> poly_vec; // Vector representation of polynomial
};

#endif // POLYNOMIAL_H

polynomial.cpp

// Overloaded operator “<<” output polynomial
// No need for friend or class scope declaration, because it is an operator outside the class
ostream & amp;operator<<(ostream & amp;output, const Polynomial & amp;obj)
{<!-- -->
    UINT terminal_code_page = GetConsoleOutputCP(); // Get the output code page of the current terminal
    if (terminal_code_page != 65001)
    {<!-- -->
        system("chcp 65001"); // The terminal switches to display using the utf8 character set so that the superscript of utf8 can be displayed
        // Note: The terminal page will be switched and the program needs to be re-run.
    }
    else // 65001
    {<!-- -->
    }

    for (int i = obj.poly_vec.size() - 1; i >= 0; --i) // From high to low i represents both the index and the number of times
    {<!-- -->
        // 1. The sign of the coefficient
        // Positive numbers display +, negative numbers do not display -, and negative coefficients already have -
        if (obj.poly_vec.at(i) >= 0)
        {<!-- -->
            cout << " + ";
        }
        else // obj.poly.at(i) < 0
        {<!-- -->
            // cout << " - ";
        }

        // 2. Coefficient
        cout << obj.poly_vec.at(i);

        // 3. Formal parameters
        cout << "x";

        // 4. times
        // UTF8 encoding of superscript
        // For example: the utf8 encoding of 0 superscript ? is \\? at index 0, the utf8 encoding of 1 superscript 1 is \\1 at index 1, and so on.
        vector<string> superscript_utf8{<!-- -->"\\?", "\\1", "\\2", "\\3", "\\? ", "\\?", "\\?", "\\?", "\\?", "\\?"};

        cout << superscript_utf8.at(i);
    }

    return output;
}

// Overload operator "/" for polynomial division
//Return value: quotient
// Parameter: divisor
Polynomial Polynomial::operator/(const Polynomial & amp;divisor) const // const means that the dividend of calling this method will not be changed
{<!-- -->
    // 1. Prepare dividend, divisor and quotient
    // Decapsulate and obtain the dividend and divisor vectors
    // 1.1 Convenient operation
    // 1.2 It is no longer const and can be modified and then encapsulated.
    vector<int> dividend_vec(this->poly_vec); // dividend
    vector<int> divisor_vec(divisor.poly_vec); // divisor

    // The initial degree of the quotient is the highest degree of the dividend - the highest degree of the divisor + 1
    // like:
    // 4x3 + 3x2 + 2x + 1, the number is 4, the highest degree is 4 - 1 = 3
    //The number of x2 + 2x + 1 is 3, and the degree of the highest term is 3 - 1 = 2
    // The highest term is divided, 4x3 / x2 = 4x, the highest degree of the quotient is 3 - 2 = 1
    // Because the index starts from 0, + 1 is needed, the number of quotient coefficients is 1 + 1 = 2, initialized to 0
    vector<int> quotient_vec((dividend_vec.size() - 1) - (divisor_vec.size() - 1) + 1, 0); // quotient

    // 2. Division operation
    // 2.1 If the highest number of terms of the dividend <the highest number of terms of the divisor, that is, dividend_vec.size() - 1 < divisor_vec.size() - 1, the quotient returns 0
    // For example: 4x3 + 3x2 + 2x + 1 / x? + 2x + 3, the highest term of the dividend 4x3 degree 4 <the highest term of the divisor x? degree 5, cannot be divided, return 0 (only 1 element, index 0 value 0)
    if ((dividend_vec.size() - 1) < (divisor_vec.size() - 1))
    {<!-- -->
        quotient_vec.resize(1, 0);
    }
    // ...
}

// Overload operator "%" to perform polynomial division to find remainder
//Return value: remainder
// Parameter: divisor
// Copy the division logic, only change the return value of the last return, the final remainder is the last updated dividend
Polynomial Polynomial::operator%(const Polynomial & amp;divisor) const // const means that the dividend of calling this method will not be changed
{<!-- -->
// ...
}

main.cpp

#include "polynomial.h"

using std::cout;
using std::endl;

int main()
{<!-- -->
    // The int type vector represents the polynomial.
    // Such as {1, 2}, index 0 value 1 represents 1x?, index 1 value 2 represents 2x1, so the polynomial is: 2x1 + 1x? = 2x + 1
    const vector<int> dividend_vec{<!-- -->1, 2, 3, 4}; // dividend, 4x3 + 3x2 + 2x + 1
    const vector<int> divisor_vec{<!-- -->3,2,1}; // Divisor, x2 + 2x + 3

    // The polynomial class represents polynomials and further encapsulates them
    const Polynomial dividend(dividend_vec);
    const Polynomial divisor(divisor_vec);
    cout << "Dividend: " << dividend << endl;
    cout << "Divisor: " << divisor << endl;

    // Polynomial division quotient
    Polynomial quotient = dividend / divisor;
    cout << "quotient: " << quotient << endl;

    // Polynomial division to find the remainder
    Polynomial remainder = dividend % divisor;
    cout << "Remainder: " << remainder << endl;

    return 0;
}

main1.cpp

#include <sstream> //istringstream

#include "polynomial.h"

using std::cin;
using std::cout;
using std::endl;
using std::getline;
using std::istringstream;
using std::string;

int main()
{<!-- -->
    //Terminal input dividend and divisor
    // The int type vector represents a polynomial.
    // For example {<!-- -->1, 2}, the index 0 value 1 represents 1x?, the index 1 value 2 represents 2x1, so the polynomial is: 2x1 + 1x? = 2x + 1
    // 1. Dividend
    vector<int> dividend_vec(0);

    cout << "Divisor coefficient, enter from high to low, separated by spaces, end with carriage return: " << endl;
    string input("");
    getline(cin, input); // Get a line of input from the terminal

    isstringstream iss(input); // string -> input stream object
    int num(0);
    while (iss >> num) // input stream object -> number
    {<!-- -->
        dividend_vec.insert(dividend_vec.begin(), num); // Insert from back to front
    }

    // 2. Divisor
    vector<int> divisor_vec(0);

    cout << "Divisor coefficient, enter from high to low, separated by spaces, end with carriage return: " << endl;
    input = "";
    getline(cin, input);

    iss.clear(); // Clear the stream status, otherwise the number cannot be obtained
    iss.str(input);
    num = 0;
    while (iss >> num)
    {<!-- -->
        divisor_vec.insert(divisor_vec.begin(), num);
    }

    // The polynomial class represents polynomials and further encapsulates them
    const Polynomial dividend(dividend_vec);
    const Polynomial divisor(divisor_vec);
    cout << "Dividend: " << dividend << endl;
    cout << "Divisor: " << divisor << endl;

    // Polynomial division quotient
    Polynomial quotient = dividend / divisor;
    cout << "quotient: " << quotient << endl;

    // Polynomial division to find the remainder
    Polynomial remainder = dividend % divisor;
    cout << "Remainder: " << remainder << endl;

    return 0;
}

Makefile

.PHONY : all
all : main.exe main1.exe

main.exe : main.cpp polynomial.cpp
g + + -o $@ $^

main1.exe : main1.cpp polynomial.cpp
g + + -o $@ $^

.PHONY: clean
clean :
del*.exe

Results

Summary

C++ code example: simple calculation tool for polynomial division.

Reference materials

  • No specific reference

Author’s words

  • Thanks to the author/blogger of the reference material
  • Author: Ye Zhi
  • All rights reserved. Please indicate the source when reprinting, thank you~
  • If the article is helpful to you, please give it a like or add a fan. Your support is the author’s motivation~
  • If you have any questions about the description of the article, please leave a message and we will patiently discuss and answer them one by one.
  • There are some mistakes in understanding in this article. Please criticize and correct me.
  • I hope readers can gain something from it