과제 도전기 - 4

김치전사·2022년 4월 11일
0

과제 도전기

목록 보기
4/4

연결리스트를 활용한 다항식 덧셈 계산을 했다

AList, BList, CList를 생성 후 AList, BList에 원하는 다항식을 (계수 지수)로 연결한 뒤 addPoly라는 함수를 이용해 AList+BList = CList를 만들었다

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct _LISTNODE_ {
	int coef;
    int expo;
	struct _LISTNODE_* next;
} LISTNODE;

typedef struct _LINKEDLIST_ {
	LISTNODE* head;
	int numOfData;
} LINKEDLIST;

void InitList(LINKEDLIST* pList)
{
	pList->head = (LISTNODE*)calloc(1, sizeof(LISTNODE));
	pList->head->next = NULL;
	pList->numOfData = 0;
}

void FreeList(LINKEDLIST* pList)
{
	LISTNODE* pre = pList->head;
	LISTNODE* cur = pList->head->next;

	while (cur != NULL)
	{
		pre = cur;
		cur = cur->next;

		free(pre);
		pre = NULL;
	}
	free(pList->head);
	pList->head = NULL;
}

void InsertLast(LINKEDLIST* pList)
{   
        int coef, expo;
        printf("coef가 0이면 자동으로 종료됩니다. \n");
        while(1)
        {
        printf("계수와 지수항을 입력하세요 :");
        scanf("%d %d",&coef,&expo);
        if(coef==0){
            break;
        }
        LISTNODE* pre = NULL;
        LISTNODE* cur = NULL;
        LISTNODE* newNode = NULL;

        newNode = (LISTNODE*)calloc(1, sizeof(LISTNODE));
        newNode->coef=coef;
        newNode->expo=expo;
        newNode->next = NULL;

        pre = pList->head;
        cur = pList->head->next;
        while (cur != NULL)
        {
            pre = cur;
            cur = cur->next;
        }
        newNode->next = pre->next;
        pre->next = newNode;

    
    }
}

void addPoly(LINKEDLIST* aList, LINKEDLIST* bList, LINKEDLIST* cList) {
    LISTNODE* aCur = NULL;
    LISTNODE* bCur = NULL;

    aCur = aList->head->next;
    bCur = bList->head->next;

    while (aCur != NULL && bCur != NULL) {
        LISTNODE* newNode = NULL;
        newNode = (LISTNODE*)calloc(1, sizeof(LISTNODE));
        LISTNODE* pre = cList->head;
        LISTNODE* cur = cList->head->next;
        if (aCur->expo > bCur->expo) {
            newNode->coef = aCur->coef;
            newNode->expo = aCur->expo;
            newNode->next = NULL;
            while (cur != NULL)
            {
                pre = cur;
                cur = cur->next;
            }
            newNode->next = pre->next;
            pre->next = newNode;
            aCur = aCur->next;
        }
        else if (aCur->expo < bCur->expo) {
            newNode->coef = bCur->coef;
            newNode->expo = bCur->expo;
            newNode->next = NULL;
            while (cur != NULL)
            {
                pre = cur;
                cur = cur->next;
            }
            newNode->next = pre->next;	
            pre->next = newNode;
            bCur = bCur->next;
        }
        else if (aCur->expo == bCur->expo) {
            newNode->coef = (bCur->coef) + (aCur->coef);
            newNode->expo = bCur->expo;
            newNode->next = NULL;
            while (cur != NULL)
            {
                pre = cur;
                cur = cur->next;
            }
            newNode->next = pre->next;		
            pre->next = newNode;
            bCur = bCur->next;
            aCur = aCur->next;
        }
    }
    while(aCur!=NULL){
            LISTNODE* newNode = NULL;
            newNode = (LISTNODE*)calloc(1, sizeof(LISTNODE));
            LISTNODE* pre = cList->head;
            LISTNODE* cur = cList->head->next;        
            newNode->coef = aCur->coef;
            newNode->expo = aCur->expo;
            newNode->next = NULL;
            while (cur != NULL)
            {
                pre = cur;
                cur = cur->next;
            }
            newNode->next = pre->next;
            pre->next = newNode;
            aCur = aCur->next;        
    }
    while(bCur!=NULL){
            LISTNODE* newNode = NULL;
            newNode = (LISTNODE*)calloc(1, sizeof(LISTNODE));
            LISTNODE* pre = cList->head;
            LISTNODE* cur = cList->head->next;        
            newNode->coef = bCur->coef;
            newNode->expo = bCur->expo;
            newNode->next = NULL;
            while (cur != NULL)
            {
                pre = cur;
                cur = cur->next;
            }
            newNode->next = pre->next;
            pre->next = newNode;
            bCur = bCur->next;                
    }
}

void PrintList(LINKEDLIST* cList)
{
    LISTNODE* cur = NULL;

    cur = cList->head->next;
    printf("L = (");
    while (cur != NULL)
    {
        printf("%dx^%d", cur->coef, cur->expo);
        cur = cur->next;
        if (cur != NULL)
        {
            printf("+ ");
        }
    }
    printf(")\n");
}


int main()
{
    LINKEDLIST aList;
    LINKEDLIST bList;
    LINKEDLIST cList;
    LISTNODE* aFind = NULL;
    LISTNODE* bFInd = NULL;


    InitList(&aList);
    InitList(&bList);
    InitList(&cList);


    InsertLast(&aList);
    InsertLast(&bList);
 
    addPoly(&aList, &bList, &cList);

    PrintList(&aList);
    PrintList(&bList);    
    PrintList(&cList);

    FreeList(&aList);
    FreeList(&bList);
    FreeList(&cList);
    return 0;
}

계수가 0일 때 자동으로 멈추게 했고 최고차항부터 입력을 하게 했다

최고차항->다음 최고차항 ->... 순으로 연결했다

addPoly함수는 AList,BList의 지수가 AList>BList일 때, AList< BList, AList==BList일 때를 구분해서 CList를 생성하게 했다

결과

profile
개인공부 블로그입니다. 상업적 용도 X

0개의 댓글