最近在学数据结构
动手写一些实验,算是每章的总结吧
1.头文件polynomal
#ifndef POLYNOMAL_H
#define POLYNOMAL_H
#include<iostream>
using namespace std;
struct Term{//多项式结点定义
float coef;
int exp;
Term* link;
Term(float c,int e,Term* next=NULL)
{
coef=c;
exp=e;
link=next;
}
Term* Insert(float c, int e);
friend ostream& operator<<(ostream&,const Term&);
};
Term* Term::Insert(float c,int e)
{
link=new Term(c,e);
return link;
}
ostream& operator<<(ostream& out, const Term& x)
{
//term的友元函数:输出一个项x的内容到输出流out中
if(x.coef==0.0) return out;
out<<x.coef;
switch(x.exp)
{
case 0:
break;
case 1:
out<<"X";break;
default:
out<<"X^"<<x.exp;break;
}
return out;
}
class Polynomal{
public:
Polynomal(){first=new Term(0,-1);}
Polynomal(Polynomal& R);
int maxOrder();
Term* getHead() const{return first;}
private:
T