자료구조 다항식 덧셈 연산 하는 코드를 짰는데 결과가 이상해요

조회수 681회

세 개의 다항식을 모두 더하려고 했는데 그건 너무 어려워서 먼저 앞의 두 식을 더한 후에 나머지 하나를 더하는 식으로 짜보려 했는데 뒤에 나머지 식을 더하는 걸 어떻게 해야 할지 모르겠더라구요... 일단 지금까지 짜본 걸 실행시키면 에러가 나지는 않는데 앞의 두 식만 연산되서 출력이 되는 상태에요. 아래의 코드에서 어디를 수정하고 뭘 넣어야 하는지 도와주세요 고수님들~~ㅜㅜ

#include <stdio.h>
struct poly{
    int degree; //차수
    int coe[50]; //계수 
};
int MAX(int x, int y)
{
    return x>y?x:y;//x가 y보다 크면 x, 아니면 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 식의 차수가 큰 경우
        {
            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, B 식의 차수가 같은 경우
            A_degree--;
            B_degree--;
        }
        else
        {
            C.coe[C_index++]=B.coe[B_index++];//B 식의 차수가 큰 경우
            B_degree--;
        }
    }
    return C;
}
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, 3}};//첫 번째 식
    poly B={4, {5, 0, 0, 3, 0}};//두 번째 식
    poly D={3, {1, 0, 0, 7}};//세 번째 식

    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);//합계

    return 0;
}
  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 아래 코드 참고하세요.

    • 코드
    #include <stdio.h>
    
    typedef struct poly {
        int degree;     //차수
        int coe[50];    //계수 
    } 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} };    //첫 번째 식
        poly B = { 4, {5, 0, 0, 3, 0} };    //두 번째 식
        poly C = { 3,    {1, 0, 0, 7} };    //세 번째 식
    
        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;
    }
    
    • 결과

    이미지

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)