【数据结构】1-3 多项式相加
其实这个还是有点问题的,在偶见情况下会打印出0*x,目前无解唔。。。
原理就是借用线性表,然后做运算直接先插入到后面。
然后遍历一下,只要指数相同就合并在前面一个里面,后面的归0(不删除)。
打印的时候加一个判断是否为0的条件就行了。
下面是源码:
#include<iostream> #include<cstring> using namespace std; struct Node { double coe;//系数 int index;//指数 Node *link; }; class polynomial { private: Node *head, *tail; //定义头指针,尾指针 public: polynomial();//无参构造函数 polynomial(double a[], int n[], int number); //有参构造函数 ~polynomial(); //析构函数 void setup(); //求值函数 void add(double a[], int n[], int number); void subtract(double a[], int n[], int number); void print(); }; polynomial::polynomial() { head = NULL; tail = NULL; } polynomial::polynomial(double a[], int n[], int number) { head = new Node; head->index = 0; head->coe = 0; tail = head; for (int i = 0; i < number; i++) { Node *p = new Node; p->coe = a[i]; //存储系数 p->index = n[i]; //存储指数 p->link = NULL; tail->link = p; tail = p; } setup(); } polynomial::~polynomial() { if (head != NULL) { head = tail = NULL; } } void polynomial::add(double a[], int n[], int number) { for (int i = 0; i < number; i++) { Node *add = new Node; add->coe = a[i]; add->index = n[i]; tail->link = add; tail = add; } tail->link = NULL; setup(); } void polynomial::subtract(double a[], int n[], int number) { for (int i = 0; i < number; i++) { Node *add = new Node; add->coe = -a[i]; add->index = n[i]; tail->link = add; tail = add; } tail->link = NULL; setup(); } void polynomial::setup() { Node *m = head->link; Node *p; while (m != NULL) { p = m->link; while (p != NULL) { if (m->index == p->index) { m->coe = m->coe + p->coe; p->coe = 0; } p = p->link; } m = m->link; } tail->link = NULL; } void polynomial::print() { if (head == NULL) { cout << "错误,无数据!" << endl; exit(1); } Node *p = head; while (p != NULL) { p = p->link; if (p == NULL) { cout << endl; break; } if (p->coe == 0) { continue; } else { cout << p->coe << "x^" << p->index; if (p->link!= NULL&&p->link->coe>0) { cout << "+"; } } } } int main() { int number; int *index; double *coe; cout << "欢迎使用多项式计算器,请输入多项式的元素个数:" << endl; cin >> number; index = new int[number]; coe = new double[number]; for (int i = 0; i < number; i++) { cout << "请输入第" << i + 1 << "个多项式的系数: "; cin >>coe[i]; cout << "请输入第" << i + 1 << "个多项式的指数: "; cin >> index[i]; } polynomial test1(coe, index, number); test1.print(); cout << "请输入相加的多项式的元素个数:" << endl; cin >> number; delete []index; delete []coe; index = new int[number]; coe = new double[number]; for (int i = 0; i < number; i++) { cout << "请输入第" << i + 1 << "个多项式的系数: "; cin >> coe[i]; cout << "请输入第" << i + 1 << "个多项式的指数: "; cin >> index[i]; } test1.add(coe, index, number); cout << "相加成功!" << endl; test1.print(); cout << "请输入相减的多项式的元素个数:" << endl; cin >> number; delete[]index; delete[]coe; index = new int[number]; coe = new double[number]; for (int i = 0; i < number; i++) { cout << "请输入第" << i + 1 << "个多项式的系数: "; cin >> coe[i]; cout << "请输入第" << i + 1 << "个多项式的指数: "; cin >> index[i]; } test1.subtract(coe, index, number); cout << "相减成功!" << endl; test1.print(); system("pause"); return 0; }