# 【数据结构 链表的应用】一元多项式相加及相乘 和对问题的分析

#ifndef _POLYN_H
#define _POLYN_H

#include<iostream>
#include <malloc.h>
#include <stdio.h>

using namespace std;
#define _CRT_SECURE_NO_DEPRECATE
#define NULL 0

typedef struct NODE {
float  coef;                  //系数
int    expn;                  //指数
struct NODE *next;
}NODE;

NODE *Creat(int n);
void InsertBefore(NODE *p1, NODE *p2);
int  compare(int a, int b);
int inputnum(int n);
NODE *MulPolyn(NODE *A, NODE *B);

#endif


#include "polyn.h"
#include<iostream>

{
cout << "								   " << endl;
cout << "**********************************" << endl;
cout << "* [0] quit_system                *" << endl;
cout << "* [1] polynadd [2] polynmul      *" << endl;
cout << "**********************************" << endl;
cout << "plese chose:>";
}

int inputnum(int n)
{
while (n <= 0 )
{
cout<<"输入的数值有误，请确认输入的数值为大于0的整数！: ";
cin>>n;
}
return n;
}

/*创建链表*/
NODE *Creat(int n)
{
int i;

for (i = 0; i < n; i++)
{
current = (NODE *)malloc(sizeof(NODE));

cout<<"请输入系数和指数 : ";
cin >> current->coef;
cin>>current->expn;

previous->next = current;
previous = current;
}
previous->next = NULL;
}

/*排序*/
{
NODE* p = NULL;
NODE* q = NULL;
for (p = head->next; p != NULL; p = p->next)
{
for (q = p->next; q != NULL; q = q->next)
{
if (p->expn < q->expn)
{
p->expn = p->expn   ^    q->expn;
q->expn = p->expn   ^    q->expn;
p->expn = p->expn   ^    q->expn;

p->coef = p->coef   +    q->coef;
q->coef = p->coef   -    q->coef;
p->coef = p->coef   -    q->coef;
}
}
}
return true;
}

{
NODE* p = NULL;
NODE* q = NULL;
for (p = head->next; p != NULL; p = p->next)
{
for (q = p->next; q != NULL; q = q->next)
{
if (p->expn > q->expn)
{
p->expn = p->expn   ^    q->expn;
q->expn = p->expn   ^    q->expn;
p->expn = p->expn   ^    q->expn;

p->coef = p->coef + q->coef;
q->coef = p->coef - q->coef;
p->coef = p->coef - q->coef;
}
}
}
return true;
}

/*比较*/
int compare(int a, int b)
{
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}

/*删除结点q*/
NODE *Delfirst(NODE *p1, NODE *q)
{
p1->next = q->next;
return (q);
}

/*插入结点,引入结点p,可以让p插入到p2和p1之间*/
void InsertBefore(NODE *p1, NODE *p2)
{
NODE *p;
p = p1->next;
p1->next = p2;
p2->next = p;
}

/*打印,为了美观程序分开打印*/
{
NODE *cur;

while (cur->next != NULL)
{
cout<<cur->coef<<"*x^"<<cur->expn<< " + ";
cur = cur->next;
}
cout << cur->coef << "*x^" << cur->expn ;
cout << endl;
}

/*一元多项式的相加，总体考虑，可分qa的指数比qb小，或等于pb(如果系数相加等于0和不等于0),或大于pb

{

NODE *ha, *hb, *qa, *qb;
int a, b;
float sum;
{
if (hb == NULL)
{
}
else
{

qa = ha->next;            /*qa和qb指向头结点的下一个结点*/
qb = hb->next;
while (qa && qb)             /*qa和qb均非空*/
{
a = qa->expn;
b = qb->expn;
switch (compare(a, b)) {
case -1:              /*qa->expn < qb->expn*/
ha = qa;
qa = qa->next;
break;
case 0:
sum = qa->coef + qb->coef;  /*系数的和*/
if (sum != 0.0) {            /*如果不是0.0*/
qa->coef = sum;         /*改变系数*/
ha = qa;
}
else {
free(Delfirst(ha, qa));
}
free(Delfirst(hb, qb));
qa = ha->next;
qb = hb->next;              /*qb释放后要重新赋值*/
break;
case 1:                        /*如果qa-> expn > qb -> expn*/
Delfirst(hb, qb);
InsertBefore(ha, qb);       /*把qb插入到ha下一个结点之前*/
qb = hb->next;
ha = ha->next;
break;
}
}
}
if (qb)
ha->next = qb;                  /*插入剩余的pb*/
}

else
{
}

}

/*实现相加及创建多个一元多项式 */
{
int i, a;
char x;
for (i = 1; i <= n; i++)
{

cout << "\n请输入第" << i << "个多项式的数目: ";
//scanf_s("%d", &a);
cin >> a;

a = inputnum(a);

cout << "（请按指数大小依次输入系数和指数）" << endl;

cout << "下式是你所输入的第" << i << "个多项式: " << endl;

cout << "请确认输入是否正确，若有误请输入n重新输入上式，正确请输入y确认（小写）："; //确认输入
cin >> x;

if (x == 'n')   //如果有误则更正
{
if (i == 1)   //如果第一项有误i不减，将第一个一元多项式清空
{
}
else{      //如果不是第一项，将已输入的多项式清空，并i减1
//			i--;
}

}

if (x == 'n') //如果第一项有误i--
{
i--;
}
}
}

/*

*/

NODE *MulPolyn(NODE *A, NODE *B)
{
NODE *pa, *pb, *pc, *u, *head;
int k = 0;
int maxExp = 0;//maxExp 为两个链表指数和的最大值
float coef = 0.0;

return NULL;

SortDown(A);
SortDown(B);

if (A->next != NULL && B->next != NULL)
maxExp = (A->next->expn) + (B->next->expn);
else
SortUp(B);
for (k = maxExp; k >= 0; k--)   /*确定多项式乘积的指数范围为0到maxExp*/
{
pa = A->next;
while (pa != NULL && pa->expn > k)  /*找到Pa位置*/
pa = pa->next;
pb = B->next;

while (pb != NULL && pa != NULL  && (pa->expn + pb->expn) < k)
/*如果和小于K，pb移动到下一个节点*/
pb = pb->next;
coef = 0.0;

while (pa != NULL && pb != NULL)
{
if ((pa->expn) + (pb->expn) == k)
{
coef += (pa->coef) * (pb->coef);
pa = pa->next;
pb = pb->next;
}
else if ((pa->expn) + (pb->expn) > k)  /*如果和大于K，pa移动到下一个节点*/
pa = pa->next;
else
pb = pb->next;					/*如果和小于K，pb移动到下一个节点*/
}

if (coef != 0.0)
{
/*如果系数不为0，则生成新的节点，并将系数和指数分别赋值给新节点，并插入到链表中*/
u = (NODE*)malloc(sizeof(NODE));
u->coef = coef;
u->expn = k;
u->next = pc->next;
pc->next = u;
pc = u;
}
}

SortDown(B);
}


#include "polyn.h"

void main(int n)
{
char y;
int select = 1;

NODE *A;
NODE *B;
int NumA = 0;
int NumB = 0;

cin >> select;
switch (select)
{

case 1:
/*相加*/
cout << "****************************欢迎使用一元多项式相加程序******************" << endl;

cout << "请输入多项式的数目（大于0的整数）: ";
cin >> n;
n = inputnum(n);

cout << endl;
cout << "相加后的多项式的显示 : " << endl;

cout << "****************************谢谢使用***********************************" << endl;
cout << "继续?请输入（y/n）: ";
cin >> y;
if (y == 'y')
{
main(1);
}
break;

case 2:
/*相乘*/
cout << "****************************欢迎使用两个一元多项式相乘程序******************" << endl;

cout << "请输入第1个多项式的数目：";
cin >> NumA;
cout << "（请按指数大小依次输入系数和指数）" << endl;
A = Creat(NumA);
cout << "下式是你所输入的第1个多项式: " << endl;
print(A);

cout << "请输入第2个多项式的数目：";
cin >> NumB;
cout << "（请按指数大小依次输入系数和指数）" << endl;
B = Creat(NumB);
cout << "下式是你所输入的第1个多项式: " << endl;
print(B);

cout << endl;
cout << "相乘后的多项式的显示 : " << endl;

cout << "****************************谢谢使用***********************************" << endl;
cout << "继续请输入（y/n）: ";
cin >> y;
if (y == 'y')
{
main(1);
}
break;
break;

default:
break;

}
}