自己在完成作业和学习C++过程中自己写的一个多项式类,可能不太科学,欢迎指正。
map容器可以在插入时自动排序 ,对于多项式的排序十分方便。这样对于多项式的储存在提取运算的过程中十分便捷,后面重载运算符的过程中会很简单。本文只是完成作业的时候写的一个类,仅仅是一个新的思路,可以供初map容器学者参考。从内存的角度来看,多项式类使用链表实现更加合理一点。
多项式的每一项以(指数,系数)的形式储存在map中,map容器是按照指数由大到小排序。
1,头文件和定义部分
#pragma once
#include<iostream>
#include<Map>
#include<cmath>
using namespace std;
class MyCompare {
public:
bool operator()(double v1, double v2)const {
return v1 > v2;
}
};
class Poly
{
public:
int n;//项数
map<double, double, MyCompare>m;//指数和系数
Poly()
{
this->n = 0;
//this->m.insert(make_pair(0, 0));
}
Poly(int num, double ploy[][2])
{
this->n = num;
for (int i = 0; i < this->n; i++)
{
this->m.insert(make_pair(ploy[i][0], ploy[i][1]));
}
}
Poly(Poly& p)
{
this->n = p.n;
this->m = p.m;
}
}
2,显示多项式
//显示多项式
void PrintPloy()
{
if (this->n == 0)
{
cout << 0 << endl;
}
else
{
for (map<double, double, MyCompare>::iterator it = m.begin(); it != m.end(); it++)
{
if (it->second >= 0 && it != m.begin())
{
if (it->second == 1 && it->first != 1 && it->first != 0)
{
cout << "+x^" << it->first;
}
else if (it->first == 1)
{
cout << "+" << it->second << "x";
}
else if (it->first == 0)
{
cout << "+" << it->second;
}
else
{
cout << "+" << it->second << "x" << "^" << it->first;
}
}
else
{
if (it->second == -1 && it->first != 1 && it->first != 0)
{
cout << "-x^" << it->first;
}
else if (it->first == 1)
{
cout << it->second << "x";
}
else if (it->first == 0)
{
cout << it->second;
}
else
{
cout << it->second << "x" << "^" << it->first;
}
}
}
cout << endl;
}
}
//加入项
void Addterm(double key, double c)
{
map<double, double, MyCompare>::iterator Pos = this->m.find(key);
if (Pos != m.end())
{
c += Pos->second;
m.erase(Pos);
if (c != 0)
{
m.insert(make_pair(key, c));
}
}
else
{
m.insert(make_pair(key, c));
}
n = m.size();
}
3,重载多项式加法,减法,乘法
//重载多项式加法
friend Poly operator+(Poly& c1, Poly& c2)
{
Poly c(c1);
for (map<double, double, MyCompare>::iterator it = c2.m.begin(); it != c2.m.end(); it++)
{
c.Addterm((*it).first, (*it).second);
}
return c;
}
//重载多项式减法
friend Poly operator-(Poly& c1, Poly& c2)
{
Poly c(c1);
for (map<double, double, MyCompare>::iterator it = c2.m.begin(); it != c2.m.end(); it++)
{
c.Addterm((*it).first, -(*it).second);
}
return c;
}
//重载多项式乘法
friend Poly operator*(Poly& c1, Poly& c2)
{
Poly c;
double k = 0, C = 0;
for (map<double, double, MyCompare>::iterator it2 = c2.m.begin(); it2 != c2.m.end(); it2++)
{
for (map<double, double, MyCompare>::iterator it1 = c1.m.begin(); it1 != c1.m.end(); it1++)
{
k = (*it1).first + (*it2).first;
C = (*it1).second * (*it2).second;
c.Addterm(k, C);
}
}
return c;
}
friend Poly operator*(double c, Poly& c1)
{
Poly P;
double k = 0, C = 0;
for (map<double, double, MyCompare>::iterator it1 = c1.m.begin(); it1 != c1.m.end(); it1++)
{
k = (*it1).first;
C = (*it1).second * c;
P.Addterm(k, C);
}
return P;
}
4,重载=,计算输入值
//重载=
Poly& operator=(const Poly& c1)
{
this->n = c1.n;
this->m = c1.m;
return *this;
}
//输入x求值
double Yvale(double x)
{
double y = 0;
for (map<double, double, MyCompare>::iterator it = this->m.begin(); it != this->m.end(); it++)
{
y += (*it).second * pow(x, (*it).first);
}
return y;
}