多项式操作

时间:2018-11-11 08:27:05

标签: c codeblocks

我想添加两个多项式并将结果设置在多项式结果上我对第三个函数有疑问,请指出我确实需要帮助 PS:动态声明!看来我不太了解指针,因此,如果您向我建议可以帮助我理解本章的内容,将会很有帮助!

     typedef struct {
            int degree;
            float* coeff_tab;
        } Polynome;


        Polynome lire_polynome()
        {
            Polynome p;
            int i;

            printf("Degree du polynome? ");
            scanf("%d", &p.degree);
            p.coeff_tab = (float*)malloc((unsigned long)(p.degree+1)*sizeof (float));

            for(i=0; i<=p.degree; i++)
            {
                printf("coeff de degree %i: ", i);
                scanf("%f", &p.coeff_tab[i]);
            }
            //free(p.coeff_tab);

            return p;
        }


        void affiche_polynome(Polynome p)
        {
            int i;
            if(p.degree > 1){
                printf("%4.2fX^%i ", p.coeff_tab[p.degree], p.degree);
                for(i=p.degree-1; i>1; i--){
                    printf(" + %4.2fX^%i ", p.coeff_tab[i], i);
                }
                printf(" + %4.2fX + %4.2f\n", p.coeff_tab[1], p.coeff_tab[0]);
            } else {
                printf(" + %4.2f\n", p.coeff_tab[0]);
            }
        }
        void sommePoly(Polynome p1 , Polynome p2 , Polynome psom)
 {
     int degsom,deg1,deg2,i=0,j=0,k=0;
     float coeffsum,coeff1,coeff2;
     Polynome *P,*S ,*Psom;
     P=&p1; S=&p2;
     degsom=max(p1.degree,p2.degree);
     psom.coeff_tab=(float*)malloc((unsigned long)(degsom+1)*sizeof (float));
     Psom=&psom;
     if( p1.degree == p2.degree)

     {
        psom.coeff_tab[k]=p1.coeff_tab[i]+p2.coeff_tab[j];
        psom.degree=p1.degree;
        Psom ++;
     i++;j++;k++;
     }
     else if(p1.degree > p2.degree) {
        psom.degree=p1.degree;
        psom.coeff_tab[k]=p1.coeff_tab[i];
        i++;k++;
    }else {
        psom.coeff_tab[k]=p2.coeff_tab[j];
        psom.degree=p2.degree;
        j++;k++;

    }
          }

2 个答案:

答案 0 :(得分:0)

void sommePoly(Polynome p1 , Polynome p2 , Polynome psom)

在C语言中,

通过对变量值进行复制来传递参数。因此p1是您传递的变量的副本。您需要传递指针以修改指针后面的值。或像返回Polynome lire_polynome()一样返回值。

通常,我会期望功能:

int lire_polynome(Polynome *p);
int sommePoly(Polynome *p1 , Polynome *p2 , Polynome *psom);

使用一些返回值ex。 int,如果出现错误(例如malloc错误),您可以返回负数或非零数。

所以您的功能可能如下所示:

int sommePoly(Polynome *p1 , Polynome *p2 , Polynome *psom)
{
     int degsom, deg1, deg2, i = 0, j = 0, k = 0;
     float coeffsum, coeff1, coeff2;
     degsom = max(p1->degree, p2->degree);
     psom->coeff_tab = malloc((degsom + 1) * sizeof(float));
     if (psom->coeff_tab == NULL) { return -1; }
     if (p1->degree == p2->degree){
        psom->coeff_tab[k] = p1->coeff_tab[i] + p2->coeff_tab[j];
        psom->degree = p1->degree;
     ... // and so on replace psom. to psom-> ie. replace dot with ->
    }
    return 0;
}

然后您将其称为传递指向结构的指针:

Polynome p1;
if (lire_polynome(&p1) != 0) { /* error handling */ }
Polynome p2;
if (lire_polynome(&p2) != 0) { /* error ahdling */ }
Polynome p3;
if (sommePoly(&p1, &p2, &p3) != 0) { /* error handling */ }
  1. 记住要检查scanf错误if (scanf("%f", ...) != 1) { fprintf(stderr, "error in scanf"); exit(1); }
  2. 记住要检查malloc错误。
  3. Don't cast result of malloc
  4. (unsigned long)(p.degree+1)*sizeof (float)中强制转换为unsigned long很奇怪,而且容易出错。 sizeof(...)的类型为size_t,这是用于尺寸表示的正确类型,将其强制转换为unsigned long是不必要的,在某些情况下可能会导致错误。只是malloc((p.degree + 1) * sizeof(float))calloc(p->degree + 1, sizeof(float));
  5. 充分了解linux kernel coding style
  6. this thread中可以找到传递指针和修改值的更简单示例。

答案 1 :(得分:0)

我认为此版本的sommePoly是您要寻找的:

void sommePoly(Polynome *p1 , Polynome *p2 , Polynome *psom)
{
        int     i;
        Polynome        *big, *sml;

        if (p1->degree > p2->degree) {
                big = p1;
                sml = p2;
        }
        else {
                big = p2;
                sml = p1;
        }

        psom->degree = big->degree;
        psom->coeff_tab = calloc(psom->degree + 1, sizeof(float));

        for (i = 0; i < sml->degree + 1; i++)
        {
                psom->coeff_tab[i] = big->coeff_tab[i] + sml->coeff_tab[i];
        }
        for (; i < big->degree + 1; i++)
        {
                psom->coeff_tab[i] = big->coeff_tab[i];
        }
}

提示1:尝试使用calloc()而不是malloc()。尝试分配Z个大小的N个项的任何数组时,这要安全得多。 提示2:当然,在取消引用之前,一定要检查calloc()/ malloc()的返回值。

说明sommePoly的预期用途:

void printPoly(Polynome *p)
{
        int     i;
        for (i = p->degree; i > 0; i--) {
                if (0.0 != p->coeff_tab[i])
                        printf("%4.2fX^%i + ", p->coeff_tab[i], i);
        }
        if (0.0 != p->coeff_tab[i])
                printf("%4.2f\n", p->coeff_tab[0]);
}

int main(int argc, char *argv[])
{
        Polynome        a, b, sum;

        a.degree = 5;
        a.coeff_tab = calloc(a.degree + 1, sizeof(float));
        a.coeff_tab[0] =  1.0;
        a.coeff_tab[1] =  8.0;
        a.coeff_tab[2] = -2.0;
        a.coeff_tab[3] =  0.0;
        a.coeff_tab[4] =  3.0;
        a.coeff_tab[5] =  1.0;

        b.degree = 3;
        b.coeff_tab = calloc(b.degree + 1, sizeof(float));
        b.coeff_tab[0] =  1.0;
        b.coeff_tab[1] = -3.0;
        b.coeff_tab[2] =  5.0;
        b.coeff_tab[3] =  7.0;

        sommePoly(&a, &b, &sum);

        printPoly(&a);
        printPoly(&b);
        printPoly(&sum);

        free(a.coeff_tab);
        free(b.coeff_tab);
        free(sum.coeff_tab);

        return 0;
}

和输出:

$ cc -g -O0 -Wall  poly.c -o poly
$ ./poly
1.00X^5 + 3.00X^4 + -2.00X^2 + 8.00X^1 + 1.00
7.00X^3 + 5.00X^2 + -3.00X^1 + 1.00
1.00X^5 + 3.00X^4 + 7.00X^3 + 3.00X^2 + 5.00X^1 + 2.00