<pre style="margin:0px;line-height:125%;"><span style="color:rgb(136,136,136);">#include <stdio.h> </span><span style="color:rgb(136,136,136);">#include <stdlib.h></span><span style="color:rgb(136,136,136);">#define MAX_TERMS 101</span><span style="color:rgb(136,136,136);">#pragma warning(disable:4996)</span> struct { <span style="color:rgb(0,112,32);">float</span> coef; <span style="color:rgb(0,112,32);">int</span> expon; }terms[MAX_TERMS]; <span style="color:rgb(0,112,32);">int</span> avail <span style="color:rgb(51,51,51);">=</span> <span style="color:rgb(0,0,221);font-weight:bold;">6</span>; <span style="color:rgb(51,51,51);">//</span><span style="color:rgb(0,0,221);font-weight:bold;">2</span><span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">개의</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">정수를</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">비교</span>char compare(<span style="color:rgb(0,112,32);">int</span> a, <span style="color:rgb(0,112,32);">int</span> b) { <span style="color:rgb(0,136,0);font-weight:bold;">if</span> (a <span style="color:rgb(51,51,51);">></span> b) <span style="color:rgb(0,136,0);font-weight:bold;">return</span> <span style="background-color:rgb(255,240,240);">'>'</span>; <span style="color:rgb(0,136,0);font-weight:bold;">else</span> <span style="color:rgb(0,136,0);font-weight:bold;">if</span> (a <span style="color:rgb(51,51,51);">==</span> b) <span style="color:rgb(0,136,0);font-weight:bold;">return</span> <span style="background-color:rgb(255,240,240);">'='</span>; <span style="color:rgb(0,136,0);font-weight:bold;">else</span> <span style="color:rgb(0,136,0);font-weight:bold;">return</span> <span style="background-color:rgb(255,240,240);">'<'</span>; } <span style="color:rgb(51,51,51);">//</span><span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">새로운</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">항을</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">다항식에</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">추가한다</span><span style="color:rgb(51,51,51);">.</span>void attach(<span style="color:rgb(0,112,32);">float</span> coef, <span style="color:rgb(0,112,32);">int</span> expon) { <span style="color:rgb(0,136,0);font-weight:bold;">if</span> (avail <span style="color:rgb(51,51,51);">></span> MAX_TERMS) { fprintf(stderr, <span style="background-color:rgb(255,240,240);">"항의 개수가 너무 많음</span><span style="color:rgb(102,102,102);font-weight:bold;background-color:rgb(255,240,240);">\n</span><span style="background-color:rgb(255,240,240);">"</span>); <span style="color:rgb(0,112,32);">exit</span>(<span style="color:rgb(0,0,221);font-weight:bold;">1</span>); } terms[avail]<span style="color:rgb(51,51,51);">.</span>coef <span style="color:rgb(51,51,51);">=</span> coef; terms[avail]<span style="color:rgb(51,51,51);">.</span>expon <span style="color:rgb(51,51,51);">=</span> expon; } <span style="color:rgb(51,51,51);">//</span>C <span style="color:rgb(51,51,51);">=</span> A <span style="color:rgb(51,51,51);">+</span> B void poly_add2(<span style="color:rgb(0,112,32);">int</span> As, <span style="color:rgb(0,112,32);">int</span> Ae, <span style="color:rgb(0,112,32);">int</span> Bs, <span style="color:rgb(0,112,32);">int</span> Be, <span style="color:rgb(0,112,32);">int</span> <span style="color:rgb(51,51,51);">*</span>Cs, <span style="color:rgb(0,112,32);">int</span><span style="color:rgb(51,51,51);">*</span>Ce) { <span style="color:rgb(0,112,32);">float</span> tempcoef; <span style="color:rgb(51,51,51);">*</span>Cs <span style="color:rgb(51,51,51);">=</span> avail; <span style="color:rgb(0,136,0);font-weight:bold;">while</span> (As <span style="color:rgb(51,51,51);"><=</span> Ae <span style="color:rgb(51,51,51);">&&</span> Bs <span style="color:rgb(51,51,51);"><=</span> Be) switch (compare(terms[As]<span style="color:rgb(51,51,51);">.</span>expon, terms[Bs]<span style="color:rgb(51,51,51);">.</span>expon)) { case <span style="background-color:rgb(255,240,240);">'>'</span>: attach(terms[As]<span style="color:rgb(51,51,51);">.</span>coef, terms[As]<span style="color:rgb(51,51,51);">.</span>expon); As<span style="color:rgb(51,51,51);">++</span>; <span style="color:rgb(0,136,0);font-weight:bold;">break</span>; case <span style="background-color:rgb(255,240,240);">'='</span>: tempcoef <span style="color:rgb(51,51,51);">=</span> terms[As]<span style="color:rgb(51,51,51);">.</span>coef <span style="color:rgb(51,51,51);">+</span> terms[Bs]<span style="color:rgb(51,51,51);">.</span>coef; <span style="color:rgb(0,136,0);font-weight:bold;">if</span> (tempcoef) attach(tempcoef, terms[As]<span style="color:rgb(51,51,51);">.</span>expon); As<span style="color:rgb(51,51,51);">++</span>; Bs<span style="color:rgb(51,51,51);">++</span>; <span style="color:rgb(0,136,0);font-weight:bold;">break</span>; case <span style="background-color:rgb(255,240,240);">'<'</span>: attach(terms[Bs]<span style="color:rgb(51,51,51);">.</span>coef, terms[Bs]<span style="color:rgb(51,51,51);">.</span>expon); Bs<span style="color:rgb(51,51,51);">++</span>; <span style="color:rgb(0,136,0);font-weight:bold;">break</span>; } <span style="color:rgb(51,51,51);">//</span>A<span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">의</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">나머지</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">항들을</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">이동함</span> <span style="color:rgb(0,136,0);font-weight:bold;">for</span> (; As <span style="color:rgb(51,51,51);"><=</span> Ae; As<span style="color:rgb(51,51,51);">++</span>) attach(terms[As]<span style="color:rgb(51,51,51);">.</span>coef, terms[As]<span style="color:rgb(51,51,51);">.</span>expon); <span style="color:rgb(51,51,51);">//</span>B<span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">의</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">나머지</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">항들을</span> <span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">이동함</span> <span style="color:rgb(0,136,0);font-weight:bold;">for</span> (; Bs <span style="color:rgb(51,51,51);"><=</span> Be; Bs<span style="color:rgb(51,51,51);">++</span>) attach(terms[Bs]<span style="color:rgb(51,51,51);">.</span>coef, terms[Bs]<span style="color:rgb(51,51,51);">.</span>expon); <span style="color:rgb(51,51,51);">*</span>Ce <span style="color:rgb(51,51,51);">=</span> avail <span style="color:rgb(51,51,51);">-</span> <span style="color:rgb(0,0,221);font-weight:bold;">1</span>; } void main() { <span style="color:rgb(0,112,32);">int</span> Cs, Ce; <span style="color:rgb(0,112,32);">int</span> i<span style="color:rgb(51,51,51);">=</span><span style="color:rgb(0,0,221);font-weight:bold;">0</span>; <span style="color:rgb(0,112,32);">int</span> len1<span style="color:rgb(51,51,51);">=</span><span style="color:rgb(0,0,221);font-weight:bold;">0</span>, len2; <span style="color:rgb(0,136,0);font-weight:bold;">while</span> (<span style="color:rgb(0,0,221);font-weight:bold;">1</span>) { scanf(<span style="background-color:rgb(255,240,240);">"</span><span style="background-color:rgb(238,238,238);">%f</span><span style="background-color:rgb(255,240,240);"> </span><span style="background-color:rgb(238,238,238);">%d</span><span style="background-color:rgb(255,240,240);">"</span>, <span style="color:rgb(51,51,51);">&</span>terms[i]<span style="color:rgb(51,51,51);">.</span>coef, <span style="color:rgb(51,51,51);">&</span>terms[i]<span style="color:rgb(51,51,51);">.</span>expon); <span style="color:rgb(0,136,0);font-weight:bold;">if</span> (terms[i]<span style="color:rgb(51,51,51);">.</span>coef <span style="color:rgb(51,51,51);">==</span> <span style="color:rgb(0,0,221);font-weight:bold;">0</span> <span style="color:rgb(51,51,51);">&&</span> terms[i]<span style="color:rgb(51,51,51);">.</span>expon <span style="color:rgb(51,51,51);">==</span> <span style="color:rgb(0,0,221);font-weight:bold;">0</span>)<span style="color:rgb(51,51,51);">//</span><span style="color:rgb(255,0,0);background-color:rgb(255,170,170);">길이측정</span> { <span style="color:rgb(0,136,0);font-weight:bold;">if</span> (len1 <span style="color:rgb(51,51,51);">==</span> <span style="color:rgb(0,0,221);font-weight:bold;">0</span>) { i<span style="color:rgb(51,51,51);">--</span>; len1 <span style="color:rgb(51,51,51);">=</span> i; } <span style="color:rgb(0,136,0);font-weight:bold;">else</span> { i<span style="color:rgb(51,51,51);">--</span>; len2 <span style="color:rgb(51,51,51);">=</span> i <span style="color:rgb(51,51,51);">-</span> len1; <span style="color:rgb(0,136,0);font-weight:bold;">break</span>; } } i<span style="color:rgb(51,51,51);">++</span>; } poly_add2(<span style="color:rgb(0,0,221);font-weight:bold;">0</span>, <span style="color:rgb(0,0,221);font-weight:bold;">2</span>, <span style="color:rgb(0,0,221);font-weight:bold;">3</span>, <span style="color:rgb(0,0,221);font-weight:bold;">5</span>, <span style="color:rgb(51,51,51);">&</span>Cs, <span style="color:rgb(51,51,51);">&</span>Ce); <span style="color:rgb(0,136,0);font-weight:bold;">for</span> (i <span style="color:rgb(51,51,51);">=</span> Ce; i <span style="color:rgb(51,51,51);"><=</span> Cs; i<span style="color:rgb(51,51,51);">++</span>) { <span style="color:rgb(0,136,0);font-weight:bold;">if</span> (i <span style="color:rgb(51,51,51);"><</span> Cs) printf(<span style="background-color:rgb(255,240,240);">"</span><span style="background-color:rgb(238,238,238);">%f</span><span style="background-color:rgb(255,240,240);">X^</span><span style="background-color:rgb(238,238,238);">%d</span><span style="background-color:rgb(255,240,240);"> + "</span>, terms[i]<span style="color:rgb(51,51,51);">.</span>coef,terms[i]<span style="color:rgb(51,51,51);">.</span>expon); <span style="color:rgb(0,136,0);font-weight:bold;">else</span> printf(<span style="background-color:rgb(255,240,240);">"</span><span style="background-color:rgb(238,238,238);">%f</span><span style="background-color:rgb(255,240,240);">X^</span><span style="background-color:rgb(238,238,238);">%d</span><span style="color:rgb(102,102,102);font-weight:bold;background-color:rgb(255,240,240);">\n\n</span><span style="background-color:rgb(255,240,240);">"</span>, terms[i]<span style="color:rgb(51,51,51);">.</span>coef, terms[i]<span style="color:rgb(51,51,51);">.</span>expon); } }</pre><pre style="margin:0px;line-height:125%;"> </pre><pre style="margin:0px;line-height:125%;">일단 코드는 이겁니다</pre><pre style="margin:0px;line-height:125%;">지금 자료구조 공부중인데 다항식 계산하는게 나왔어요</pre><pre style="margin:0px;line-height:125%;">항은 (3,2) (4,1) (0,0) 이면 3x^2 + 4x^1 이렇게 봐줍니다.</pre><pre style="margin:0px;line-height:125%;">그래서 2개 항을 구해서 배열에 차곡차곡 쌓고</pre><pre style="margin:0px;line-height:125%;">후에 A,B로 나눠서 As,Bs로 비교해가면서 계산해서 Cs로 차곡차곡 쌓아두는겁니다.</pre><pre style="margin:0px;line-height:125%;"> </pre><pre style="margin:0px;line-height:125%;">예를들면 이런거죠</pre><pre style="margin:0px;line-height:125%;"> </pre><pre style="margin:0px;line-height:125%;">A---B----C----- 이렇게 배열이 들어가는 거;;</pre><pre style="margin:0px;line-height:125%;">그런데 여기서 C를 출력하고 싶은데 terms[i].expon이 계속 쓰레기값이 뜨네요 ㅠㅠ</pre><pre style="margin:0px;line-height:125%;">혹시 제가 어디 잘못썻나요? 으으</pre><pre style="margin:0px;line-height:125%;">출력부분에서 범위 정하는게 문제가 있는지</pre><pre style="margin:0px;line-height:125%;">아니면 처음에 배열에 A,B 값 넣어주는게 문제가 있는지 모르겠습니다 ㅠㅠ</pre><pre style="margin:0px;line-height:125%;">나머지 코드는 책에 나오는 예시인데 그대로만 하면 출력하는게 없어서 확인해보려고 손댔다가..</pre><pre style="margin:0px;line-height:125%;">아으 너무 궁금해서 미칠것같아요ㅠㅠ 확인.. 확인을 하자 </pre>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.