#include<iostream>
using namespace std;
struct polynomial;
typedef polynomial* PtrToPoly;
typedef PtrToPoly List;
typedef PtrToPoly Position;
typedef struct polynomial {
int coef;
int power;
Position next;
};
void Addpoly(List& L1, List& L2, List& L3)
{
Position p;
p = L1->next;
Position w = L3;
while (p != NULL)
{
Position q;
q = L2->next;
bool flag = true;
while (q != NULL)
{
if (p->power == q->power)
{
flag = false;
Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
TmpCell->coef = p->coef + q->coef;
TmpCell->power = q->power;
w->next = TmpCell;
w = w->next;
break;
}
q = q->next;
}
if (flag)
{
Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
TmpCell->coef = p->coef;
TmpCell->power = p->power;
w->next = TmpCell;
w = w->next;
}
p = p->next;
}
Position q2 = L2->next;
while (q2 != NULL)
{
bool flag = true;
Position p2 = L1->next;
while (p2 != NULL)
{
if (q2->power == p2->power)
flag = false;
p2 = p2->next;
}
if (flag)
{
Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
TmpCell->coef = q2->coef;
TmpCell->power = q2->power;
w->next = TmpCell;
w = w->next;
}
q2 = q2->next;
}
w->next = NULL;
}
void Subpoly(List& L1, List& L2, List& L3)
{
Position p;
p = L1->next;
Position w = L3;
while (p != NULL)
{
Position q;
q = L2->next;
bool flag = true;
while (q != NULL)
{
if (p->power == q->power)
{
flag = false;
Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
TmpCell->coef = p->coef - q->coef;
TmpCell->power = q->power;
w->next = TmpCell;
w = w->next;
break;
}
q = q->next;
}
if (flag)
{
Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
TmpCell->coef = p->coef;
TmpCell->power = p->power;
w->next = TmpCell;
w = w->next;
}
p = p->next;
}
Position q2 = L2->next;
while (q2 != NULL)
{
bool flag = true;
Position p2 = L1->next;
while (p2 != NULL)
{
if (q2->power == p2->power)
flag = false;
p2 = p2->next;
}
if (flag)
{
Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
TmpCell->coef = 0 - q2->coef;
TmpCell->power = q2->power;
w->next = TmpCell;
w = w->next;
}
q2 = q2->next;
}
w->next = NULL;
}
void Copypoly(List& L3, List& L4);
void Multipoly(List& L1, List& L2, List& L3)
{
Position p = L1->next;
Position w = L3;
Position r = L3->next;
while (p != NULL)
{
Position q = L2->next;
while (q != NULL)
{
bool flag = true;
Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
TmpCell->coef = p->coef * q->coef;
TmpCell->power = p->power + q->power;
while (r != NULL)
{
if (TmpCell->power == r->power)
{
flag = false;
r->coef = r->coef + TmpCell->coef;
break;
}
r = r->next;
}
if (flag)
{
w->next = TmpCell;
w = w->next;
w->next = NULL;
}
q = q->next;
}
p = p->next;
}
}
void Diffpoly(List& L)
{
List p = L->next;
while (p)
{
if (p->power != 0)
{
p->coef = p->coef * p->power;
p->power--;
p = p->next;
}
else
p = p->next;
}
}
void input(List& L)
{
int N;
Position p = L;
cout << "請輸入項數:";
cin >> N;
for (int i = 1; i < N+1; i++)
{
Position TmpCell = (PtrToPoly)malloc(sizeof(struct polynomial));
cout << "請輸入第" << i << "項的係數:";
cin >> TmpCell->coef;
cout << "請輸入第" << i << "項的次數:";
cin >> TmpCell->power;
p->next = TmpCell;
p = p->next;
cout << endl;
}
p->next = NULL;
}
void Showpoly(List& L)
{
Position p;
p = L->next;
for (int i = 1; p != NULL; i++)
{
if (p->coef != 0)
{
cout << "第" << i << "項係數為:" << p->coef << endl;
cout << "第" << i << "項指數為:" << p->power << endl;
cout << endl;
if (p->next != NULL)
p = p->next;
else
break;
}
else
{
i--;
if (p->next != NULL)
p = p->next;
else
break;
}
}
}
int main()
{
List L1;
L1 = (PtrToPoly)malloc(sizeof(struct polynomial));
if (L1 == NULL)
cout << "分配記憶體不成功" << endl;
L1->next = NULL;
List L2;
L2 = (PtrToPoly)malloc(sizeof(struct polynomial));
L2->next = NULL;
List L3;
L3 = (PtrToPoly)malloc(sizeof(struct polynomial));
L3->next = NULL;
cout << "請輸入第一個多項式:" << endl;
input(L1);
cout << endl << "--------顯示L1---------" << endl;
Showpoly(L1);
cout << "請輸入第二個多項式:" << endl;
input(L2);
cout << endl << "--------顯示L2---------" << endl;
Showpoly(L2);
Addpoly(L1, L2, L3);
cout << endl << "--------加法·顯示L3---------" << endl;
Showpoly(L3);
free(L3);
L3 = (PtrToPoly)malloc(sizeof(struct polynomial));
L3->next = NULL;
Subpoly(L1, L2, L3);
cout << endl << "--------減法·顯示L3---------" << endl;
Showpoly(L3);
free(L3);
L3 = (PtrToPoly)malloc(sizeof(struct polynomial));
L3->next = NULL;
Multipoly(L1, L2, L3);
cout << endl << "--------乘法·顯示L3---------" << endl;
Showpoly(L3);
Diffpoly(L1);
cout << endl << "--------微分·顯示L3---------" << endl;
Showpoly(L1);
return 0;
}