这是我写的一个多项式的类,因为是作业,所以只实现了输入、输出和两个多项式相加而已。
polynomial.h:#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
#include <map>
#include <functional>
class Polynomial
{
private:
// using map container to store the terms, all terms are in descending order of exponentials
typedef std::map<int, int, std::greater<int> > TermsMap;
TermsMap terms;
bool printTerm(std::ostream &out, int coefficient, int exponential, bool firstTerm) const;
public:
typedef TermsMap::iterator Iterator; // the iterator of terms
typedef TermsMap::const_iterator ConstIterator; // the const iterator of terms
// here are some interfaces, but some of them not be used in this project
// they are only for integrality of the class
Iterator begin(void) { return terms.begin(); }
Iterator end(void) { return terms.end(); }
ConstIterator begin(void) const { return terms.begin(); }
ConstIterator end(void) const { return terms.end(); }
int getCoefficient(int exponential) { return terms[exponential]; }
void clear(void) { terms.clear(); }
Polynomial &operator+=(const Polynomial &p);
friend std::istream &operator>>(std::istream &in, Polynomial &p);
friend std::ostream &operator<<(std::ostream &out, const Polynomial &p);
};
Polynomial operator+(const Polynomial &leftPolynomial, const Polynomial &rightPolynomial);
std::istream &operator>>(std::istream &in, Polynomial &p);
std::ostream &operator<<(std::ostream &out, const Polynomial &p);
#endif
#define POLYNOMIAL_H
#include <iostream>
#include <map>
#include <functional>
class Polynomial
{
private:
// using map container to store the terms, all terms are in descending order of exponentials
typedef std::map<int, int, std::greater<int> > TermsMap;
TermsMap terms;
bool printTerm(std::ostream &out, int coefficient, int exponential, bool firstTerm) const;
public:
typedef TermsMap::iterator Iterator; // the iterator of terms
typedef TermsMap::const_iterator ConstIterator; // the const iterator of terms
// here are some interfaces, but some of them not be used in this project
// they are only for integrality of the class
Iterator begin(void) { return terms.begin(); }
Iterator end(void) { return terms.end(); }
ConstIterator begin(void) const { return terms.begin(); }
ConstIterator end(void) const { return terms.end(); }
int getCoefficient(int exponential) { return terms[exponential]; }
void clear(void) { terms.clear(); }
Polynomial &operator+=(const Polynomial &p);
friend std::istream &operator>>(std::istream &in, Polynomial &p);
friend std::ostream &operator<<(std::ostream &out, const Polynomial &p);
};
Polynomial operator+(const Polynomial &leftPolynomial, const Polynomial &rightPolynomial);
std::istream &operator>>(std::istream &in, Polynomial &p);
std::ostream &operator<<(std::ostream &out, const Polynomial &p);
#endif
polynomial.cpp:
#include <iostream>
#include "polynomial.h"
using namespace std;
// print a term in the correct form, return printed or not
bool Polynomial::printTerm(ostream &out, int coefficient, int exponential, bool firstTerm) const
{
if (coefficient == 0) // do not print terms with coefficient zero
return false;
// print the coefficient
if (coefficient > 0 && !firstTerm)
out << "+";
else if (coefficient < 0)
out << "-";
coefficient = abs(coefficient);
if (coefficient != 1 || exponential == 0)
out << coefficient;
if (exponential > 0)
out << 'X';
if (exponential > 1)
out << '^' << exponential;
return true;
}
// the '+='operation of polynomial, more operations can be added
Polynomial &Polynomial::operator+=(const Polynomial &p)
{
for (ConstIterator it = p.begin(); it != p.end(); ++it)
terms[it->first] += it->second;
return *this;
}
// the plus operation of polynomial, more operations can be added
Polynomial operator+(const Polynomial &leftPolynomial, const Polynomial &rightPolynomial)
{
Polynomial result(leftPolynomial);
result += rightPolynomial;
return result;
}
// input a polynomial
istream &operator>>(istream &in, Polynomial &p)
{
char separator;
p.clear();
do {
int coefficient, exponential;
in >> coefficient >> exponential;
p.terms[exponential] = coefficient;
in >> noskipws >> separator >> skipws; // not skip white spaces, in order to read a space or a newline
} while (separator != ' '); // input until a newline
return in;
}
// print a polynomial
ostream &operator<<(ostream &out, const Polynomial &p)
{
bool printed = false; // whether there is a term printed or not
for (Polynomial::ConstIterator it = p.begin(); it != p.end(); ++it)
printed = p.printTerm(out, it->second, it->first, !printed) || printed;
if (!printed) // no terms printed, print a zero to mark an empty polynomial
out << "0";
out << endl;
return out;
}
#include "polynomial.h"
using namespace std;
// print a term in the correct form, return printed or not
bool Polynomial::printTerm(ostream &out, int coefficient, int exponential, bool firstTerm) const
{
if (coefficient == 0) // do not print terms with coefficient zero
return false;
// print the coefficient
if (coefficient > 0 && !firstTerm)
out << "+";
else if (coefficient < 0)
out << "-";
coefficient = abs(coefficient);
if (coefficient != 1 || exponential == 0)
out << coefficient;
if (exponential > 0)
out << 'X';
if (exponential > 1)
out << '^' << exponential;
return true;
}
// the '+='operation of polynomial, more operations can be added
Polynomial &Polynomial::operator+=(const Polynomial &p)
{
for (ConstIterator it = p.begin(); it != p.end(); ++it)
terms[it->first] += it->second;
return *this;
}
// the plus operation of polynomial, more operations can be added
Polynomial operator+(const Polynomial &leftPolynomial, const Polynomial &rightPolynomial)
{
Polynomial result(leftPolynomial);
result += rightPolynomial;
return result;
}
// input a polynomial
istream &operator>>(istream &in, Polynomial &p)
{
char separator;
p.clear();
do {
int coefficient, exponential;
in >> coefficient >> exponential;
p.terms[exponential] = coefficient;
in >> noskipws >> separator >> skipws; // not skip white spaces, in order to read a space or a newline
} while (separator != ' '); // input until a newline
return in;
}
// print a polynomial
ostream &operator<<(ostream &out, const Polynomial &p)
{
bool printed = false; // whether there is a term printed or not
for (Polynomial::ConstIterator it = p.begin(); it != p.end(); ++it)
printed = p.printTerm(out, it->second, it->first, !printed) || printed;
if (!printed) // no terms printed, print a zero to mark an empty polynomial
out << "0";
out << endl;
return out;
}