105.多项式乘法

#include <stdio.h>
#include <math.h>
#define MAX 50
/* 下面的两个数组可以根据具体要求解的多项式来决定其值*/
static double p[6]={4,-6,3,1,-1,5};	/*表示多项式4x^5 - 6x^4 + 3x^3 + x^2 - x + 5 */
static double q[4]={3,2,-5,1};		/*表示多项式3x^3 + 2x^2 - 5x + 1 */
static double result[9]={0,0,0,0,0,0,0,0,0};		/*存放乘积多项式*/

void npmul(p,m,q,n,s)
int m,n;
double p[],q[],s[];
{
	int i,j;
	for (i=0; i<=m-1; i++)
	for (j=0; j<=n-1; j++)
		s[i+j]=s[i+j]+p[i]*q[j];		/*迭带计算各项系数*/
	return;
}

double compute(s,k,x)					/*计算所给多项式的值*/
double s[];
int k;
float x;
{
	int i;
	float multip = 1;
	double sum = 0;
	for (i=0;i<k;i++)
		multip = multip * x;			/*先求出x的最高次项的值*/
	for (i=k-1;i>=0;i--)
	{
		sum = sum + s[i] * multip;		/*依次从高到低求出相对应次项的值*/
		if (x!=0)
		multip = multip / x;
	}
	return sum;
}

void main()
{
	int i,j,m,n;
	double  px[MAX],qx[MAX],rx[MAX];
	float x;
	clrscr();
	for(i=0;i<MAX;i++)
		rx[i]=0;
	puts("      This is a polynomial multiplication program.");
	puts("It calculate the product of two polynomials: P(x) and Q(x)");
	puts("       P(x)=Pm-1*x^(m-1)+Pm-2*x^(m-2)+...+P1*x+P0");
	puts("       Q(x)=Qn-1*x^(n-1)+Qn-2*x^(n-2)+...+Q1*x+Q0");
	printf("\n >> Please input m (>=1): ");
	scanf("%d",&m);
	printf(" >> Please input P%d, P%d, ... P1, P0 one by one:\n",m-1,m-2);
	for(i=0;i<m;i++)
		scanf("%f",&px[i]);
	printf("\n >> Please input n (>=1): ");
	scanf("%d",&n);
	printf(" >> Please input Q%d, Q%d, ... Q1, Q0 one by one:\n",n-1,n-2);
	for(i=0;i<n;i++)
		scanf("%f",&qx[i]);
	npmul(p,m,q,n,rx);
	printf("\nThe product of two polynomials R(x) is :\n");
	for (i=m+n-1,j=0;i>=1;i--)					/*逐行逐项打印出结果多项式*/
	{
		printf(" (%f*x^%d) + ",rx[m+n-1-i],i-1);
		if(j==2)
		{
			printf("\n");
			j=0;
		}
		else
			j++;
	}
	printf("\n");
	printf("Input the value of x: ");
	scanf("%f",&x);
	printf("\nThe value of the R(%f) is: %13.7f",x,compute(rx,m+n-1,x));
	puts("\n Press any key to quit...");
	getch();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程与实战

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值