| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 407 人关注过本帖
标题:新手求指导多项式除法函数
只看楼主 加入收藏
a88669955
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2014-11-18
收藏
 问题点数:0 回复次数:0 
新手求指导多项式除法函数
教授给我们一个题目是用已有的指定框架 设计一个多项式除法函数程式
我个人在程式方面很不在行 只能写成这样(紅色部分为在下所写)
理所当然死得很惨 但想知道到底该怎麽做
不知有没有哪位大大能帮忙小弟解惑呢?希望能彻底了解怎麽做ˊˋ
#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",&deg);
    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;                    
                }         
                  
                     
    }
     

}

搜索更多相关主题的帖子: include 多项式 
2014-11-18 23:00
快速回复:新手求指导多项式除法函数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014851 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved