多项式求值

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值