#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct duoxiangshi {
int xishu;
int zhishu;
struct duoxiangshi* next;
}Polyn,*LinkPolyn;
void InitPolyn(LinkPolyn& L) {
L = new Polyn;
L->next = NULL;
}
void Create(LinkPolyn& L) {
InitPolyn(L);
int x, z;
LinkPolyn p;
LinkPolyn r = L;
printf("系数 指数:\n");
scanf_s("%d %d", &x, &z);
while (x != 0) {
p = new Polyn;
p->next = NULL;
p->xishu = x;
p->zhishu = z;
r->next = p;
r = p;
scanf_s("%d %d", &x, &z);
}
r->next = NULL;
}
void PrintPolyn(LinkPolyn& L) {
LinkPolyn p = L->next;
while (p->next!= NULL) {
printf("%d", p->xishu);
printf("x(%d)+", p->zhishu);
p = p->next;
}
printf("%d", p->xishu);
printf("x(%d)\n", p->zhishu);
}
void addPolyn(LinkPolyn L1, LinkPolyn L2, LinkPolyn& L3) {
LinkPolyn p1 = L1->next;
LinkPolyn p2 = L2->next;
LinkPolyn r = L3;
LinkPolyn p;
while (p1 != NULL && p2 != NULL) {
p = new Polyn;
if (p1->zhishu == p2->zhishu) {
p->zhishu = p1->zhishu;
p->xishu = p1->xishu + p2->xishu;
r->next=p;
r = p;
p1 = p1->next;
p2 = p2->next;
}
else if (p1->zhishu > p2->zhishu) {
p->xishu = p1->xishu;
p->zhishu = p1->zhishu;
p1 = p1->next;
r->next = p;
r = p;
}
else {
p->xishu = p2->xishu;
p->zhishu = p2->zhishu;
p2 = p2->next;
r->next = p;
r = p;
}
}
while (p1 != NULL) {
p = new Polyn;
p->xishu = p1->xishu;
p->zhishu = p1->zhishu;
r->next = p;
r = p;
p1 = p1->next;
}
while (p2 != NULL) {
p = new Polyn;
p->xishu = p2->xishu;
p->zhishu = p2->zhishu;
r->next = p;
r = p;
p2 = p2->next;
}
r->next = NULL;
}
void minusPolyn(LinkPolyn L1, LinkPolyn L2, LinkPolyn& L3) {
LinkPolyn p = L2->next;
while (p) {
p->xishu = -1 * p->xishu;
p = p->next;
}
addPolyn(L1, L2, L3);
}
int ValPolyn(LinkPolyn L) {
int x;
printf("输入x的值\n");
scanf_s("%d", &x);
int val=0;
LinkPolyn p = L->next;
while (p != NULL) {
int xishu = p->xishu;
int zhishu = p->zhishu;
val += xishu * pow(x,zhishu);
p = p->next;
}
return val;
}
void Derivation(LinkPolyn L) {
LinkPolyn p = L->next;
while (p) {
p->xishu = p->xishu * p->zhishu;
p->zhishu = p->zhishu - 1;
p = p->next;
}
}
int main() {
LinkPolyn p1,p2,p3,p4;
printf("输入第一个多项式:\n");
Create(p1);
printf("输入第二个多项式:\n");
Create(p2);
InitPolyn(p3);
InitPolyn(p4);
printf("第一个多项式:\n");
PrintPolyn(p1);
//printf("p1->value=%d ", ValPolyn(p1));
printf("第二个多项式:\n");
PrintPolyn(p2);
//printf("两个多项式和为:\n");
//addPolyn(p1, p2, p3);
//PrintPolyn(p3);
minusPolyn(p1, p2, p4);
PrintPolyn(p4);
//Derivation(p1);
//printf("求导后的多项式为:");
//PrintPolyn(p1);
}
多项式求值
最新推荐文章于 2024-11-11 20:40:17 发布