c - 多项式操作

标签 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++;

    }
          }

最佳答案

我认为这个版本的 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

关于c - 多项式操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53247017/

相关文章:

c - 寻找不同的逻辑

c++ - 为什么我需要使用 CGAL 和 CMake 构建我的 C++ 程序?

ubuntu - 有没有办法将winsock lib从windows移动到ubuntu?

c - c 中的结构,其成员也是结构

c - 在C语言中我们如何找到进程的作业ID?

c - 在 char 中传递参数可以使用 printf 正确打印,但不能使用 write

c - 删除数组中的空内容并在C中对数组进行排序

c - 使用结构体指针的两个函数(读取和显示数组)

c - 147次递归调用后,发生段错误。 (在C中)

c - 在 C 中使用 ncurses 的 wrefresh 函数时出现段错误