본문 바로가기
컴퓨터과학[2-2]/[2-2]자료구조

다항식의 덧셈(배열) II

by boolean 2015. 10. 13.
728x90

다항식의 덧셈(배열) II

결과 화면

소스코드

<p><br /></p> /* ============================================================================ Name : Data_02_polytype2.c Author : Kim Hwa Joong Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include <stdio.h> #include <stdlib.h> #define SIZE 32 typedef struct{ int exp; //지수 (exponential : [èkspounénʃəl]) int coef; //계수 (coefficient) }poly; typedef struct{ int count; //항의 개수 카운트 poly p[SIZE]; }polys; int maxExp(polys* ps); int isZeroP(polys* ps); int coef(polys* ps, int e); void addTerm(polys* ps, int coef, int exp); void printPolys(polys* ps); void delTerm(polys* ps, int e); char compare(int a, int b); polys polyAdd(polys* p1, polys* p2); int main(void) { setvbuf(stdout, NULL, _IONBF, 0); puts("!!!잘 해보세!!"); /* prints !!!Hello World!!! */ polys p1; polys p2; polys p3; p1.count = 0; p2.count = 0; int a, b; printf("P1의 항(계수, 지수)을 입력하시오.(0 0 이면 종료) :\n"); while(1){ scanf("%d %d", &a, &b); if(a==0 && b==0) break; addTerm(&p1, a, b); } printf("P1다항식 : "); printPolys(&p1); printf("P2의 항(계수, 지수)을 입력 하시오. (0 0이면 종료) : \n"); while(1){ scanf("%d %d", &a, &b); if(a ==0 && b ==0) break; addTerm(&p2, a, b); } printf("P2다항식 : "); printPolys(&p2); printf("\n"); printf("P3 다항식(P1 + P2) : "); p3 = polyAdd(&p1, &p2); printPolys(&p3); return EXIT_SUCCESS; } int maxExp(polys* ps) { int i; int result = ps ->p[0].exp; for(i=0; i<ps->count; i++) { if(ps ->p[i].exp > result) { result = ps ->p[i].exp; } } return result; } void addTerm(polys* ps, int coef, int exp) { ps ->p[ps ->count].exp = exp; ps ->p[ps ->count].coef = coef; ps ->count ++; } void printPolys(polys* ps) { int i; for(i = 0; i < ps->count; i ++) { if(ps ->p[i].coef ==0) { printf(" "); }else if(ps ->p[i].exp == 1){ printf("%d", ps ->p[i].coef); }else{ printf("%dx^%d ", ps ->p[i].coef, ps ->p[i].exp); printf("+"); } } printf("\n"); } polys polyAdd(polys* p1, polys* p2) { polys p3; p3.count = 0; int sum; while(!isZeroP(p1) && !isZeroP(p2)) { switch(compare(maxExp(p1), maxExp(p2))) { case '<': addTerm(&p3,coef(p2,maxExp(p2)), maxExp(p2)); delTerm(p2, maxExp(p2)); break; case '=': sum = coef(p1, maxExp(p1)) + coef(p2, maxExp(p2)); if(sum != 0) { addTerm(&p3, sum, maxExp(p1)); } delTerm(p1, maxExp(p1)); delTerm(p2, maxExp(p2)); break; case '>': addTerm(&p3, coef(p1,maxExp(p1)), maxExp(p1)); delTerm(p1,maxExp(p1)); break; } } while(!isZeroP(p1)) { addTerm(&p3, coef(p1, maxExp(p1)), maxExp(p1)); delTerm(p1, maxExp(p1)); } while(!isZeroP(p2)) { addTerm(&p3, coef(p2, maxExp(p2)), maxExp(p2)); delTerm(p2, maxExp(p2)); } return p3; } int isZeroP(polys* ps) { int result = 0; //항이 있을 경우 0 (false) if(ps ->count == 0) { result = 1; //항이 없을 경우 1(true) } return result; } int coef(polys* ps, int e) { int i; int result = 0; for(i = 0; i <ps ->count; i ++) { if(ps -> p[i].exp == e) { result = ps -> p[i].coef; break; } } return result; } void delTerm(polys* ps, int e) { int i; int flag = 0; if(isZeroP(ps))return; for(i = 0; i < ps ->count; i ++) { if(ps -> p[i].exp == e) { flag = 1; } if(flag) { ps -> p[i] = ps -> p[i+1]; } } if(flag) { ps -> count --; } } char compare(int a, int b) { if(a == b) return '='; else if(a < b) return '<'; else return '>'; }

댓글