新手求指导多项式除法函数
教授给我们一个题目是用已有的指定框架 设计一个多项式除法函数程式我个人在程式方面很不在行 只能写成这样(紅色部分为在下所写)
理所当然死得很惨 但想知道到底该怎麽做
不知有没有哪位大大能帮忙小弟解惑呢?希望能彻底了解怎麽做ˊˋ
#include <stdio.h>
#include <stdlib.h>
void PolyPrint(float[], int);
int PolyInput(float[]);
int PolyAdd(float[], int, float[], int, float[]);
float PolyEval(float[], int, float);
int PolyMul(float[], int, float[], int, float[]);
void PolyDiv(float[], int, float[], int, float[], int*, float[], int*);
int main(int argc, char *argv[])
{
int DegreeA, DegreeB, DegreeC, DegreeD;
int i;
float PolyA[20], PolyB[20], PolyC[20], PolyD[20];
float val;
for (i=0; i<20; i++)
PolyA[i] = PolyB[i] = PolyC[i] = PolyD[i] = 0.0;
printf("< Enter polynomial A >\n");
DegreeA = PolyInput(PolyA);
PolyPrint(PolyA, DegreeA);
printf("\n< Enter polynomial B >\n");
DegreeB = PolyInput(PolyB);
PolyPrint(PolyB, DegreeB);
printf("\n-------------- Polynomial C=A/B ----------------\n");
PolyDiv(PolyA, DegreeA, PolyB, DegreeB, PolyC, &DegreeC, PolyD, &DegreeD);
printf("Quotient Polynomial's degree = %d\n", DegreeC);
PolyPrint(PolyC, DegreeC);
printf("\n");
printf("Remainder Polynomial's degree = %d\n", DegreeD);
PolyPrint(PolyD, DegreeD);
printf("\n");
system("PAUSE");
return EXIT_SUCCESS;
}
//////////////////////////////////////////////////////////////////////
// PolyPrint() : print polynomial in style
//////////////////////////////////////////////////////////////////////
void PolyPrint(float p[], int deg)
{
int i;
printf("\n");
if (deg==0)
{
printf("(%4.2f)\n",p[0]);
return;
}
for (i=deg; i>1; i--)
printf("(%4.2f)*X^%d + ",p[i],i);
printf("(%4.2f)*X + (%4.2f)",p[1], p[0]);
printf("\n\n");
}
//////////////////////////////////////////////////////////////////////
// PolyInput() : enter the degree and coefficients of the polynomial
//////////////////////////////////////////////////////////////////////
int PolyInput(float p[])
{
int i, deg;
printf("Enter the highest degree of the polynomial: ");
scanf("%d",°);
for (i=deg; i>=0; i--)
{
printf("Enter coef. of the polynomial's term X^%d = ",i);
scanf("%f",&p[i]);
}
return deg;
}
//////////////////////////////////////////////////////////////////////
// PolyAdd() : Add two polynomials to the third polynomial
//////////////////////////////////////////////////////////////////////
int PolyAdd(float p1[], int deg1, float p2[], int deg2, float p3[])
{
int i;
if (deg1>=deg2)
{
for (i=deg1; i>=0; i--)
p3[i] = p1[i]+p2[i];
return deg1;
}
else
{
for (i=deg2; i>=0; i--)
p3[i] = p1[i]+p2[i];
return deg2;
}
}
//////////////////////////////////////////////////////////////////////
// PolyEval() : Evaluate the polynomial with a given X
//////////////////////////////////////////////////////////////////////
float PolyEval(float p[], int deg, float X)
{
int i;
float result = 0.0;
for (i=deg; i>0; i--)
{
result += p[i];
result *= X;
}
return (result += p[0]);
}
//////////////////////////////////////////////////////////////////////
// PolyMul() : polynomial multiplication
//////////////////////////////////////////////////////////////////////
int PolyMul(float p1[], int deg1, float p2[], int deg2, float p3[])
{
int i, k;
for (i=deg1; i>=0; i--)
for (k=deg2; k>=0; k--)
p3[i+k] += p1[i]*p2[k];
return deg1+deg2;
}
//////////////////////////////////////////////////////////////////////
// PolyDiv() : polynomial division
//////////////////////////////////////////////////////////////////////
void PolyDiv(float p1[], int deg1, float p2[], int deg2, float Q[], int* degQ, float R[], int* degR)
{
int i;
if(deg1>deg2)
{
while(deg1>=*degQ)
{
*degQ=deg1-deg2;
R[*degR]=Q[*degQ]*p2[deg2];
p1[deg1]=p1[deg1]-p2[deg2]*Q[*degQ];
if(deg1=deg2) break;
}
}
}