ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

์˜์‚ฌ์ฝ”๋“œ:

 

 

 

์ž…๋ ฅ ์˜ˆ์‹œ 1

์ถœ๋ ฅ ์˜ˆ์‹œ 1

3 โ†ฆ ์ฒซ ๋ฒˆ์งธ ๋‹คํ•ญ์‹์˜ ํ•ญ์˜ ๊ฐœ์ˆ˜

5 3 3 2 3 1 โ†ฆ 5x3 + 3x2 + 3x

3 โ†ฆ ๋‘ ๋ฒˆ์งธ ๋‹คํ•ญ์‹์˜ ํ•ญ์˜ ๊ฐœ์ˆ˜

2 6 2 3 1 0 โ†ฆ 2x6 + 2x3 + 1

โ–ก2 6 7 3 3 2 3 1 1 0 โ†ฆ 2x6+7x3+3x2+3x+1

์ž…๋ ฅ ์˜ˆ์‹œ 2

์ถœ๋ ฅ ์˜ˆ์‹œ 2

2 โ†ฆ ์ฒซ ๋ฒˆ์งธ ๋‹คํ•ญ์‹์˜ ํ•ญ์˜ ๊ฐœ์ˆ˜

2 7 3 0 โ†ฆ 2x7 + 3

3 โ†ฆ ๋‘ ๋ฒˆ์งธ ๋‹คํ•ญ์‹์˜ ํ•ญ์˜ ๊ฐœ์ˆ˜

-3 10 3 7 -3 0 โ†ฆ -3x10 + 3x7 -3

โ–ก-3 10 5 7 โ†ฆ -3x10 + 5x7

 

์ฝ”๋“œ:

 

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

typedef struct listnode {// ๋…ธ๋“œ
	int coef;// ํ•ญ์˜ ๊ณ„์ˆ˜
	int exp;// ํ•ญ์˜ ์ฐจ์ˆ˜
	struct listnode* next;// ๋‹ค์Œ ๋…ธ๋“œ์˜ ์ฃผ์†Œ ์ €์žฅ
}node;

typedef struct listhead {// ๋ฆฌ์ŠคํŠธ
	node* head;
}listhead;

void appendTerm(node *head, int coef, int exp) {// ๊ธฐ์กด ๋‹คํ•ญ์‹์— ํ•ญ ์ถ”๊ฐ€
	node* newnode = (node*)malloc(sizeof(node));
	newnode->coef = coef;
	newnode->exp = exp;
	newnode->next = NULL;
	while (head->next != NULL) {
		head = head->next;
	}
	head->next = newnode;
}

node* addPoly(node* firstpolynomial, node* secondpolynomial) {
	node* first = firstpolynomial->next;
	node* second = secondpolynomial->next;
	node* sumpolynomial = (node*)malloc(sizeof(node));
	sumpolynomial->next = NULL;

	int check;
	while ((first != NULL) && (second != NULL)) {
		if (first->exp > second->exp) {//์ฒซ๋ฒˆ์งธ ๋‹คํ•ญ์‹์˜ ์ฐจ์ˆ˜๊ฐ€ ๋‘๋ฒˆ์งธ ๋‹คํ•ญ์‹์˜ ์ฐจ์ˆ˜๋ณด๋‹ค ํฌ๋‹ค๋ฉด
			appendTerm(sumpolynomial, first->coef, first->exp);
			first = first->next;
		}
		else if (first->exp < second->exp) {//์ฒซ๋ฒˆ์งธ ๋‹คํ•ญ์‹์˜ ์ฐจ์ˆ˜๊ฐ€ ๋‘๋ฒˆ์งธ ๋‹คํ•ญ์‹์˜ ์ฐจ์ˆ˜๋ณด๋‹ค ํฌ๋‹ค๋ฉด
			appendTerm(sumpolynomial, second->coef, second->exp);
			second = second->next;
		}
		else {// ๋‘ ๋‹คํ•ญ์‹์˜ ์ฐจ์ˆ˜๊ฐ€ ๊ฐ™๋‹ค๋ฉด
			check = first->coef + second->coef;
			if (check != 0) {
				appendTerm(sumpolynomial, check, first->exp);
			}
			first = first->next;
			second = second->next;
		}
	}

	while (first != NULL) {
		appendTerm(sumpolynomial, first->coef, first->exp);
		first = first->next;
	}
	while (second != NULL) {
		appendTerm(sumpolynomial, second->coef, second->exp);
		second = second->next;
	}
	return sumpolynomial;
}

void printPolynomial(node *header) {// ๋‹คํ•ญ์‹ ์ถœ๋ ฅ
	node* p = header->next;
	
	while (p != NULL) {
		printf(" %d %d", p->coef, p->exp);// ์ถœ๋ ฅ
		p = p->next;// ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ๊ธฐ
	}
}

void freePolynomial(node* header) {
	node* p = header;// p์— ํ—ค๋” ์—ฐ๊ฒฐ
	while (p != NULL) {
		header = header->next;// ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์—ฐ๊ฒฐ
		free(p);// ํ•ด์ œํ•ด์ฃผ๊ธฐ
		p = header;// p์— ๋นˆ ํ—ค๋” ์—ฐ๊ฒฐ
	}
}

int main() {
	int num, coef, exp;// ํ•ญ์˜ ๊ฐฏ์ˆ˜, ๊ณ„์ˆ˜, ์ฐจ์ˆ˜
	node* firstpolynomial = (node*)malloc(sizeof(node));
	node* secondpolynomial = (node*)malloc(sizeof(node));
	node* sumpolynomial;

	firstpolynomial->next = NULL;// ์ดˆ๊ธฐํ™”
	secondpolynomial->next = NULL;

	scanf("%d", &num);
	for (int i = 0; i < num; i++) {
		scanf("%d", &coef);
		scanf("%d", &exp);
		appendTerm(firstpolynomial, coef, exp);
	}// ์ฒซ๋ฒˆ์งธ ๋‹คํ•ญ์‹ ์ž…๋ ฅ๋ฐ›๊ธฐ

	scanf("%d", &num);
	for (int i = 0; i < num; i++) {
		scanf("%d", &coef);
		scanf("%d", &exp);
		appendTerm(secondpolynomial, coef, exp);
	}// ๋‘๋ฒˆ์งธ ๋‹คํ•ญ์‹ ์ž…๋ ฅ๋ฐ›๊ธฐ

	sumpolynomial = addPoly(firstpolynomial, secondpolynomial);

	printPolynomial(sumpolynomial);// ์ถœ๋ ฅ

	freePolynomial(firstpolynomial);
	freePolynomial(secondpolynomial);
	freePolynomial(sumpolynomial);

	return 0;
}

 

์ ์  ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ž‘ ์นœํ•ด์ง€๊ณ  ์žˆ๋Š”๋“ฏ.. ๋” ์นœํ•ด์ง€์ž ์šฐ๋ฆฌ......(โ”ฌโ”ฌ๏นโ”ฌโ”ฌ)

๋ฐ˜์‘ํ˜•