/*
本程序实现了多项式的加减乘除及微分操作。
将多项式每一项用链表节点表示,程序首先定义了一个链表节点类和一个链表类,并在链表类中分别定义了将字符串转换成多项式链表(READ)、将多项式以固定格式输出(Output)、多项式加减乘除及微分(ADD、SUB、MUL、DIV、DIFF)、
在屏幕上打印(Print)的操作。
最终实现了从txt文件中读取字符串建立多项式链表,识别要进行的操作,进行相应操作并输出至txt文件的功能。
*/
#include <iostream>
#include <fstream>
using namespace std;
class ChainNode
{
public:
friend class Chain;//将链表类声明为其友元,使Chain可访问ChainNode所有成员(务必注意友元类模板声明格式!)
private:
double p,q; //链表类节点数据域
ChainNode* link;//链表类节点链接域
};
class Chain
{
public:
Chain(){ head = NULL; tail = NULL; judge=false;}
~Chain();
//插入节点
void Insert(int k, const double&x,const double&y);
void Append(const double&x,const double&y);
void Read(char* str);
void Output();
void ADD(Chain& pol);
void SUB(Chain& pol);
void DIFF();
Chain* MUL(Chain& pol);
Chain* DIV(Chain& pol);
void Print();
private:
ChainNode *head;//头指针
ChainNode *tail;//尾指针
bool judge;//在多项式除法算法中判断是否有余数
};
Chain::~Chain()
{
ChainNode *next;
while (head)
{
next = head->link;
delete head;
head = next;//移至下一个将要delete的节点
}
}
//在链表第k项后插入节点,若k==0则插入为头指针
void Chain::Insert(int k, const double& x,const double& y)
{
if (k < 0)
{
cout << "NO EXIST!";
return;
}
ChainNode *curr = head;
for (int index = 1; index < k&&curr; i