c++请帮帮忙

来源:百度知道 编辑:UC知道 时间:2024/07/02 20:52:41
老师要求我写个实验报告,题目是“一元多项式的表示及相加和相乘”,要求是要用顺序存储结构,但是我的C++学的不好,搞不定请帮我编写个符合要求的C++程序,谢谢!!!我会追加分的

我用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); }