I made a code for adding polynomials to the data structure, but the result is weird

Asked 2 years ago, Updated 2 years ago, 112 views

I tried to add all three polynomials, but it was so difficult that I tried to squeeze them together by adding the first two equations and then adding the other one, but I didn't know what to do with the rest of the equation. If you run what you've planned so far, there won't be an error, but only the first two expressions are calculated and printed. Please help me with the code below and what I need to add~ㅜ/

#include <stdio.h>
struct poly{
    int degree; //order
    int coe[50]; // coefficient 
};
int MAX(int x, int y)
{
    return x>y?x:y;//x greater than y, x or y
}
poly addPoly(poly A, poly B)
{
    int A_index=0, B_index=0, C_index=0;
    int A_degree=A.degree, B_degree=B.degree;
    poly C;
    C.degree=MAX(A.degree, B.degree);
    while(A_index<=A.degree && B_index<=B.degree)
    {
        If (A_degree > B_degree)//A is large
        {
            C.coe[C_index++]=A.coe[A_index++];
            A_degree--;
        }
        else if(A_degree == B_degree)
        {
            C.coe[C_index++]=A.coe[A_index++]+B.coe[B_index];//A, if the order of B is the same
            A_degree--;
            B_degree--;
        }
        else
        {
            C.coe[C_index++]=B.coe[B_index++];//B When the order of expression is large
            B_degree--;
        }
    }
    return C;
}
poly addPoly1(poly A, poly B, poly D)
{
    poly t = addPoly( A, B );
    poly C = addPoly(t, D);
    return C;
} //I have no idea how to calculate the three polynomials 
void printPoly(poly P)
{
    int degree;
    degree=P.degree;
    for(int i=0; i<=P.degree;i++)
    {
        printf("%dx^%d", P.coe[i], degree--);
        if(i<P.degree)
            printf(" + ");
    }
    printf("\n");
}
int main()
{
    poly A={2, {3, 0, 2, 3};//First expression
    poly B={4, {5, 0, 0, 3, 0};//second expression
    poly D={3, {1, 0, 0, 7};//Third expression

    poly C;

    C=addPoly(A, B);

    printf("A(x)="); printPoly(A);
    printf("B(x)="); printPoly(B);
    printf("D(x)="); printPoly(D);
    printf("-----result-----\n");
    printf("C(x)="); printPoly(C);//Total

    return 0;
}

data-structure

2022-09-20 20:08

1 Answers

Please refer to the code below.

#include <stdio.h>

typedef struct poly {
    int degree; //order
    int coe[50]; // coefficient 
} } poly;

poly addPoly(poly A, poly B)
{
    poly big, small;

    if (A.degree >= B.degree)
    {
        big = A;
        small = B;
    }
    else
    {
        big = B;
        small = A;
    }

    for (int i = 0; i <= small.degree; i++)
    {
        big.coe[i + (big.degree - small.degree)] += small.coe[i];
    }

    return big;
}

poly addPoly1(poly A, poly B, poly D)
{
    poly t = addPoly(A, B);
    poly C = addPoly(t, D);
    return C;
}

void printPoly(poly P)
{
    int degree;
    degree = P.degree;
    for (int i = 0; i <= P.degree; i++)
    {
        printf("%dx^%d", P.coe[i], degree--);
        if (i < P.degree)
            printf(" + ");
    }
    printf("\n");
}
int main()
{
    poly A = {2,{3,0,2}); // First expression
    poly B = {4, {5, 0, 0, 3, 0}; // Second expression
    poly C = {3, {1, 0, 0, 7} ; // Third expression

    printf("A(x)="); printPoly(A);
    printf("B(x)="); printPoly(B);
    printf("C(x)="); printPoly(C);

    printf("-----result-----\n");

    poly D, E, F;

    D = addPoly(A, B);
    printf("D(x)="); printPoly(D);
    E = addPoly(C, D);
    printf("E(x)="); printPoly(E);

    F = addPoly1(A, B, C);
    printf("F(x)="); printPoly(F);

    return 0;
}


2022-09-20 20:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.