一元多项式加减乘除c语言
时间: 2024-01-04 08:43:28 浏览: 107
C语言版的一元多项式加减乘
以下是一元多项式加减乘除的C语言代码实现:
一、一元多项式的结构体定义
```
typedef struct PolyNode *PtrToPoly;
struct PolyNode {
int coef; // 系数
int expon; // 指数
PtrToPoly next;
};
typedef PtrToPoly Polynomial;
```
二、一元多项式的创建
```
Polynomial CreatePoly()
{
Polynomial p, rear, t;
int c, e, n;
printf("请输入多项式的项数:");
scanf("%d", &n);
rear = (Polynomial)malloc(sizeof(struct PolyNode));
rear->next = NULL;
p = rear;
while (n--) {
printf("请输入系数和指数:");
scanf("%d %d", &c, &e);
t = (Polynomial)malloc(sizeof(struct PolyNode));
t->coef = c;
t->expon = e;
t->next = NULL;
rear->next = t;
rear = t;
}
t = p;
p = p->next;
free(t);
return p;
}
```
三、一元多项式的加法
```
Polynomial AddPoly(Polynomial p1, Polynomial p2)
{
Polynomial front, rear, temp;
int sum;
rear = (Polynomial)malloc(sizeof(struct PolyNode));
front = rear;
while (p1 && p2) {
if (p1->expon == p2->expon) {
sum = p1->coef + p2->coef;
if (sum) {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = sum;
temp->expon = p1->expon;
rear->next = temp;
rear = temp;
}
p1 = p1->next;
p2 = p2->next;
} else if (p1->expon > p2->expon) {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = p1->coef;
temp->expon = p1->expon;
rear->next = temp;
rear = temp;
p1 = p1->next;
} else {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = p2->coef;
temp->expon = p2->expon;
rear->next = temp;
rear = temp;
p2 = p2->next;
}
}
while (p1) {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = p1->coef;
temp->expon = p1->expon;
rear->next = temp;
rear = temp;
p1 = p1->next;
}
while (p2) {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = p2->coef;
temp->expon = p2->expon;
rear->next = temp;
rear = temp;
p2 = p2->next;
}
rear->next = NULL;
temp = front;
front = front->next;
free(temp);
return front;
}
```
四、一元多项式的减法
```
Polynomial SubtractPoly(Polynomial p1, Polynomial p2)
{
Polynomial front, rear, temp;
int sum;
rear = (Polynomial)malloc(sizeof(struct PolyNode));
front = rear;
while (p1 && p2) {
if (p1->expon == p2->expon) {
sum = p1->coef - p2->coef;
if (sum) {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = sum;
temp->expon = p1->expon;
rear->next = temp;
rear = temp;
}
p1 = p1->next;
p2 = p2->next;
} else if (p1->expon > p2->expon) {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = p1->coef;
temp->expon = p1->expon;
rear->next = temp;
rear = temp;
p1 = p1->next;
} else {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = -p2->coef;
temp->expon = p2->expon;
rear->next = temp;
rear = temp;
p2 = p2->next;
}
}
while (p1) {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = p1->coef;
temp->expon = p1->expon;
rear->next = temp;
rear = temp;
p1 = p1->next;
}
while (p2) {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = -p2->coef;
temp->expon = p2->expon;
rear->next = temp;
rear = temp;
p2 = p2->next;
}
rear->next = NULL;
temp = front;
front = front->next;
free(temp);
return front;
}
```
五、一元多项式的乘法
```
Polynomial MultiplyPoly(Polynomial p1, Polynomial p2)
{
Polynomial p, rear, t, t1, t2;
int c, e;
if (!p1 || !p2) {
return NULL;
}
t1 = p1;
t2 = p2;
rear = (Polynomial)malloc(sizeof(struct PolyNode));
rear->next = NULL;
p = rear;
while (t2) {
c = t1->coef * t2->coef;
e = t1->expon + t2->expon;
t = (Polynomial)malloc(sizeof(struct PolyNode));
t->coef = c;
t->expon = e;
rear->next = t;
rear = t;
t2 = t2->next;
}
t1 = t1->next;
while (t1) {
t2 = p2;
rear = p;
while (t2) {
e = t1->expon + t2->expon;
c = t1->coef * t2->coef;
while (rear->next && rear->next->expon > e) {
rear = rear->next;
}
if (rear->next && rear->next->expon == e) {
if (rear->next->coef + c) {
rear->next->coef += c;
} else {
t = rear->next;
rear->next = t->next;
free(t);
}
} else {
t = (Polynomial)malloc(sizeof(struct PolyNode));
t->coef = c;
t->expon = e;
t->next = rear->next;
rear->next = t;
rear = rear->next;
}
t2 = t2->next;
}
t1 = t1->next;
}
t2 = p;
p = p->next;
free(t2);
return p;
}
```
六、一元多项式的除法
```
void DividePoly(Polynomial dividend, Polynomial divisor, Polynomial *pQuotient, Polynomial *pRemainder)
{
Polynomial temp;
if (!divisor) {
printf("除数不能为0!\n");
return;
}
*pQuotient = (Polynomial)malloc(sizeof(struct PolyNode));
(*pQuotient)->next = NULL;
*pRemainder = (Polynomial)malloc(sizeof(struct PolyNode));
(*pRemainder)->next = NULL;
while (dividend && dividend->expon >= divisor->expon) {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = dividend->coef / divisor->coef;
temp->expon = dividend->expon - divisor->expon;
temp->next = (*pQuotient)->next;
(*pQuotient)->next = temp;
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = dividend->coef - temp->coef * divisor->coef;
temp->expon = dividend->expon;
temp->next = (*pRemainder)->next;
(*pRemainder)->next = temp;
dividend = dividend->next;
}
while (dividend) {
temp = (Polynomial)malloc(sizeof(struct PolyNode));
temp->coef = dividend->coef;
temp->expon = dividend->expon;
temp->next = (*pRemainder)->next;
(*pRemainder)->next = temp;
dividend = dividend->next;
}
temp = (*pQuotient);
(*pQuotient) = (*pQuotient)->next;
free(temp);
temp = (*pRemainder);
(*pRemainder) = (*pRemainder)->next;
free(temp);
}
```
阅读全文