Programming/์๋ฃ๊ตฌ์กฐ
[์ฐ๊ฒฐ๋ฆฌ์คํธ] ๋จ์ผ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ก ๋คํญ์ ๊ณ์ฐ ๊ตฌํํ๊ธฐ
ํด๋์๊ทธ
2021. 4. 8. 22:21
๋ฐ์ํ
์์ฌ์ฝ๋:
์ ๋ ฅ ์์ 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;
}
์ ์ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ ์นํด์ง๊ณ ์๋๋ฏ.. ๋ ์นํด์ง์ ์ฐ๋ฆฌ......(โฌโฌ๏นโฌโฌ)
๋ฐ์ํ