Presentation is loading. Please wait.

Presentation is loading. Please wait.

5주차 실습 - solution.

Similar presentations


Presentation on theme: "5주차 실습 - solution."— Presentation transcript:

1 5주차 실습 - solution

2 poly_add2() 2개의 타입 poly, term_type 정의 type poly = record
terms: array [1..maxterms] of term_type; // 항의 정보 num: integer; // 항의 개수 end; type term_type = record coef: real; // 계수 expo: integer; // 차수

3 poly_add2() 분석 구분 내용 I p, q: poly
p = (np, (coefi, expoi)), i=1, 2, …, np q = (nq, (coefj, expoj)), j=1, 2, …, nq O r: poly r = (nr, (coefk, expok)), i=1, 2, …, nr P 0<= r.nr <= p.np + q.nq r.(coefk, expok) = (p.coefi+q.coefj, p.expoi) if э i, j s.t. expok = p.expoi and expok = q.expoj (p.coefi, p.expoi) if э i s.t. expok = p.expoi and ¬э j s.t. expok = q.expoj (q.coefj, q.expoj) if ¬э i s.t. expok = p.expoi and э j s.t. expok = q.expoj E 분석

4 poly_add2(): 알고리즘 알고리즘 procedure poly_add2(p, q: poly) r: poly;
pf <- 0; qf<- 0; rf <-0; // 다항식의 현재 항 인덱스 설정 while pf <= p.num and qf <= q.num do if p[pf].terms.expo = q[qf].terms.expo then r[rf].terms.coef <- p[pf].terms.coef + q[qf].terms.coef; r[rf].terms.expo <- p[pf].terms.expo; rf <- rf +1; pf <- pf+1; qf <- qf+1; // 인덱스 갱신 else if p[pf].terms.expo > q[qf].terms.expo then r[rf].terms.coef <- p[pf].terms.coef; rf <- rf +1; pf <- pf+1; else r[rf].terms.coef <- p[qf].terms.coef; r[rf].terms.expo <- p[qf].terms.expo; rf <- rf +1; qf <- qf+1; end if repeat // 어느 한 쪽이 먼저 소진된 경우 처리 if pf <= p.num then // p의 나머지 항들을 r로 이동 // q의 나머지 항들을 r로 이동 endif end poly_add2 알고리즘


Download ppt "5주차 실습 - solution."

Similar presentations


Ads by Google