c++请帮帮忙
来源:百度知道 编辑:UC知道 时间:2024/07/02 20:52:41
我用stl把加法和乘法都实现了。vector肯定是顺序存储的,不过如果你想需要重新定义Terms的话,需要定义拷贝构造函数、虚构函数、赋值操作符、size函数、resize函数和[]操作符。
#include <string>
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
class Polynomial
{
public:
Polynomial()
{
setTimes(0);
setCoefficient(0, 0);
}
Polynomial(unsigned int times) { mCoefficients.resize(times+1); }
void setName(const string& name) { mName = name; }//设置未知数的名字以便输出
string& getName() { return mName; }
const string& getName() const { return mName; }
void setCoefficient(unsigned int index, double value) { mCoefficients[index] = value; }
double& getCoefficient(unsigned int index) { return mCoefficients[index]; }
const double& getCoefficient(unsigned int index) const { return mCoefficients[index]; }
void setTimes(unsigned int times) { mCoefficients.resize(times+1); }