發新話題

[問題] 新手求指導

新手求指導

教授給我們一個題目是以期指定框架 設計一個多項式除法函數
我個人在程式方面很不在行 只能寫成這樣
#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 = PolyB = PolyC = PolyD = 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);
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",&deg);
    for (i=deg; i>=0; i--)
       {
       printf("Enter coef. of the polynomial's term X^%d = ",i);
       scanf("%f",&p);
       }
    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 = p1+p2;
         return deg1;
         }
    else
         {
         for (i=deg2; i>=0; i--)
            p3 = p1+p2;
         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;
   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*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;                    
                }             
                  
                     
        }
     

}

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。