利用map容器实现多项式类(C++)

自己在完成作业和学习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;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值