| [CT212] | [แนวข้อสอบ / โจทย์ให้ฝึกทำ] |
| ช่วยดูหน่อยนะครับ ว่า ควรปรับปรุง Algo ตรงไหนบ้างครับ โปรแกรมนี้จะมีข้อจำกัดคือตอน input ต้อง input power ของ X ไล่จากมากไปหาน้อยสุดครับ ไม่งั้นจะคำนวณและแสดงผลผิดพลาด และถ้าจะเลิกใส่แล้ว ต้องใส่ค่า coefficient เป็น 0 program Polynomial_Linked_List_LinkedQueue; uses wincrt; type nodeptr=^node; node=record co:integer; px:integer; link:nodeptr; end; var afront,arear,bfront,brear,cfront,crear:nodeptr; cotemp,pxtemp:integer; first:boolean; debug:char; {procedure InsertLinkList(coefficient:integer;power:integer;var head:nodeptr); var pred,succ,p:nodeptr; Begin new(p); p^.co:=coefficient; p^.px:=power; p^.link:=nil; pred:=nil; succ:=head; while (succ<>nil)and(p^.px>succ^.px) do begin pred:=succ; succ:=succ^.link; end; if pred=nil then begin p^.link:=head; head:=p; end else begin pred^.link:=p; p^.link:=succ; end; end; procedure DeleteLinkList(coefficient:integer;power:integer;var head:nodeptr); var prev,p:nodeptr; begin if head=nil then writeln('List is Empty') else begin prev:=nil; p:=head; while (p<>nil)and(p^.co<>coefficient)and(p^.px<>power) do begin prev:=p; p:=p^.link; end; if p<>nil then begin if prev=nil then head:=head^.link else prev^.link:=p^.link; dispose(p); end else writeln('No any data'); end; end;} procedure enqueuels(coefficient:integer;power:integer;var front,rear:nodeptr); {Enqueue Link Stack But not delete} var p:nodeptr; Begin new(p); p^.co:=coefficient; p^.px:=power; p^.link:=nil; if front=nil then begin front:=p; rear:=p; end else begin rear^.link:=p; rear:=p; end; End; procedure dequeuels(var coefficient,power:integer;var temp:nodeptr); {Dequeue Link Stack But not delete} Begin if temp<>nil then begin coefficient:=temp^.co; power:=temp^.px; temp:=temp^.link; end else writeln('List is Empty'); End; procedure dequeuelsdispose(var coefficient,power:integer;var front,rear:nodeptr); {Dequeue with Delete} var temp:nodeptr; Begin if front<>nil then begin coefficient:=front^.co; power:=front^.px; temp:=front; front:=front^.link; dispose(temp); end else writeln('List is Empty'); End; procedure sumpolynomial(var afront,arear,bfront,brear,cfront,crear:nodeptr); {Summation Polynomial in Linked Stack} var atemp,btemp:nodeptr; first:boolean; acotemp,apxtemp,bcotemp,bpxtemp,ccotemp:integer; begin atemp:=afront; btemp:=bfront; first:=true; while (atemp<>nil)or(btemp<>nil) do {Do until no data in LinkStack A and B} begin if first then {First Dequeue Both Link Stack} begin dequeuels(acotemp,apxtemp,atemp); dequeuels(bcotemp,bpxtemp,btemp); first:=false; end else {Categorize for What LinkStack that need to Dequeue} begin if apxtemp=bpxtemp then begin if atemp<>nil then dequeuels(acotemp,apxtemp,atemp) else begin acotemp:=0; apxtemp:=0; end; if btemp<>nil then dequeuels(bcotemp,bpxtemp,btemp) else begin bcotemp:=0; bpxtemp:=0; end; end else if apxtemp>bpxtemp then begin if atemp<>nil then dequeuels(acotemp,apxtemp,atemp) else begin acotemp:=0; apxtemp:=0; end; end else begin if btemp<>nil then dequeuels(bcotemp,bpxtemp,btemp) else begin bcotemp:=0; bpxtemp:=0; end; end; end; if apxtemp=bpxtemp then {Set Number For enqueue in New LinkStack C} begin ccotemp:=acotemp+bcotemp; enqueuels(ccotemp,apxtemp,cfront,crear); end else if apxtemp>bpxtemp then begin enqueuels(acotemp,apxtemp,cfront,crear); end else begin enqueuels(bcotemp,bpxtemp,cfront,crear); end; {read(debug);} end; end; procedure display(front,rear:nodeptr); {Display Polynomial in Linked Stack} var cotemp,pxtemp:integer; first:boolean; begin first:=true; while front<>nil do begin dequeuelsdispose(cotemp,pxtemp,front,rear); if first then begin write(cotemp,'X^',pxtemp,' '); first:=false; end else if pxtemp=0 then begin if cotemp>0 then write('+ ',cotemp) else begin cotemp:=abs(cotemp); write('- ',cotemp); end; end else if pxtemp=1 then begin if cotemp>0 then write('+ ',cotemp,'X ') else begin cotemp:=abs(cotemp); write('- ',cotemp,'X '); end; end else if cotemp>0 then write('+ ',cotemp,'X^',pxtemp,' ') else begin cotemp:=abs(cotemp); write('- ',cotemp,'X^',pxtemp,' '); end; end; writeln; end; begin afront:=nil; bfront:=nil; cfront:=nil; write('Coefficient of A :'); {Enter Polynomial Data For LinkedStack A} readln(cotemp); write('Power of X of A :'); readln(pxtemp); while cotemp<>0 do begin enqueuels(cotemp,pxtemp,afront,arear); write('Coefficient of A :'); readln(cotemp); write('Power of X of A :'); readln(pxtemp); end; write('Coefficient of B :'); {Enter Polynomial Data For LinkedStack B} readln(cotemp); write('Power of X of B :'); readln(pxtemp); while cotemp<>0 do begin enqueuels(cotemp,pxtemp,bfront,brear); write('Coefficient of B :'); readln(cotemp); write('Power of X of B :'); readln(pxtemp); end; sumpolynomial(afront,arear,bfront,brear,cfront,crear); write('Polynomial A : '); display(afront,arear); write('Polynomial B : '); display(bfront,brear); write('Summation of A and B : '); display(cfront,crear); end. สู้ๆ สู้ๆ สู้ๆ ลูกผู้ชายยืดได้หดได้ ตกแล้วก็เอาใหม่ คิกๆ จากคุณ : ตามล่าหาฝัน - [47/05/20 - 17:25] - [4424] |
| <ข้อความที่ 1> Coefficient of A :9 Power of X of A :10 Coefficient of A :1 Power of X of A :0 Coefficient of A :0 Power of X of A :0 Coefficient of B :2 Power of X of B :3 Coefficient of B :9 Power of X of B :10 Coefficient of B :0 Power of X of B :0 Polynomial A : 9X^10 + 1 Polynomial B : 2X^3 + 9X^10 Summation of A and B : 9X^10 + 2X^3 + 9X^10 ข้อนี้ออกข้อสอบป่ะครับ ผมแจ้ง error ครับ แบบว่าเห็นคนขยันกว่าเป็นไม่ได้ ต้องหา error ให้หมดความมั่นใจ ฮิฮิ จากคุณ : doraemon - [47/05/20 - 17:54] - [15248] |
| <ข้อความที่ 2> อ้าว หน้าแตกเองล่ะสิ ไม่ทันอ่านภาไทย ถ้าบอกข้อจำกัดแล้ว ที่ผมบอกไป ก็ไม่เรียกว่า error ล่ะครับ จากคุณ : doraemon - [47/05/20 - 17:56] - [15249] |
| <ข้อความที่ 3> แป่ว ลืมคิด linked Q linked Stack คิดแต่แบบธรรมดา พรุ่งนี้สอบแว้ว ไม่ทันแล้ว จากคุณ : เด็กศิลป์ 45 - [47/05/20 - 20:56] - [15252] |
| <ข้อความที่ 4> ขอบคุณ คุณ doraemon มากนะครับ ที่ช่วยหา error ให้ แต่ยังคิดวิธีแก้ไม่ได้ครับ เพราะถ้าจะแก้ ก็ต้องเอาออกมา แล้วใส่เข้าไปใหม่ โดยให้เรียงข้อมูลตามกำลัง ของ X ไว้สอบเสร็จก่อนค่อยมานั่งคิดแก้ ไม่ให้มี error จากการป้อนข้อมูล ไม่เรียงตามลำดับ โปรแกรมที่ผมเขียนตัวนี้ จะมีโพรซีเดอร์ที่ไม่ได้ใช้ อยู่สองตัวนะครับ ที่เป็น InsertLinkedList กับ DeleteLinkedList เพราะผมใช้ Enqueuels กะ Dequeuels แทน ผมเขียน Comment ผิดนะครับ จริงๆต้องเป็น Linked Queue นะครับ ไม่ใช่ Linked Stack ถ้าเป็น Linked Stack จะใช้ ตัวชี้แค่ Top ตัวเดียว แต่โปรแกรมนี้ผมใช้ Linked Queue ต้องใช้ตัวชี้ ข้อมูล สองตัวคือ Front และ Rear ขออภัยด้วยครับ พิมพ์คอมเมนต์ผิด สู้ๆๆ จากคุณ : ตามล่าหาฝัน - [47/05/20 - 21:09] - [15253] |
| <ข้อความที่ 5> แก้ไขให้คุณ Doraemon หาข้อผิดพลาดใหม่แล้วครับ คราวนี้ใช้ แบบ Insert Linked List ธรรมดา insert ข้อมูลของ power x ไม่เรียงลำดับได้แล้วนะครับ เด่ว procedure insert... จะเรียงให้เอง จากมากไปหาน้อย โดยที่หยุดใส่ข้อมูลเมื่อ coefficient = 0 ยังมีข้อผิดพลาดคือ ถ้าใส่ power ซ้ำกัน ของใน Polynomial นั้นๆ เช่น 3x^2 + 5x^2 แบบนี้ จะเออเรอ ครับ program Polynomial_Linked_List_LinkedQueue; uses wincrt; type nodeptr=^node; node=record co:integer; px:integer; link:nodeptr; end; var ahead,bhead,chead:nodeptr; cotemp,pxtemp:integer; procedure InsertLinkList(coefficient:integer;power:integer;var head:nodeptr); var pred,succ,p:nodeptr; Begin new(p); p^.co:=coefficient; p^.px:=power; p^.link:=nil; pred:=nil; succ:=head; while (succ<>nil)and(p^.px<succ^.px) do begin pred:=succ; succ:=succ^.link; end; if pred=nil then begin p^.link:=head; head:=p; end else begin pred^.link:=p; p^.link:=succ; end; end; procedure getdata(var coefficient,power:integer;var temp:nodeptr); {Dequeue Link Stack But not delete} Begin if temp<>nil then begin coefficient:=temp^.co; power:=temp^.px; temp:=temp^.link; end else writeln('List is Empty'); End; procedure sumpolynomial(ahead,bhead:nodeptr;var chead:nodeptr); {Summation Polynomial in Linked Stack} var first:boolean; acotemp,apxtemp,bcotemp,bpxtemp,ccotemp:integer; begin first:=true; while (ahead<>nil)or(bhead<>nil) do {Do until no data in LinkStack A and B} begin if first then {First Dequeue Both Link Stack} begin getdata(acotemp,apxtemp,ahead); getdata(bcotemp,bpxtemp,bhead); first:=false; end else {Categorize for What LinkStack that need to Dequeue} begin if apxtemp=bpxtemp then begin if ahead<>nil then getdata(acotemp,apxtemp,ahead) else begin acotemp:=0; apxtemp:=0; end; if bhead<>nil then getdata(bcotemp,bpxtemp,bhead) else begin bcotemp:=0; bpxtemp:=0; end; end else if apxtemp>bpxtemp then begin if ahead<>nil then getdata(acotemp,apxtemp,ahead) else begin acotemp:=0; apxtemp:=0; end; end else begin if bhead<>nil then getdata(bcotemp,bpxtemp,bhead) else begin bcotemp:=0; bpxtemp:=0; end; end; end; if apxtemp=bpxtemp then {Set Number For enqueue in New LinkStack C} begin ccotemp:=acotemp+bcotemp; InsertLinkList(ccotemp,apxtemp,chead); end else if apxtemp>bpxtemp then begin InsertLinkList(acotemp,apxtemp,chead); end else begin InsertLinkList(bcotemp,bpxtemp,chead); end; end; end; procedure display(head:nodeptr); {Display Polynomial in Linked Stack} var cotemp,pxtemp:integer; first:boolean; begin first:=true; while head<>nil do begin getdata(cotemp,pxtemp,head); if first then begin write(cotemp,'X^',pxtemp,' '); first:=false; end else if pxtemp=0 then begin if cotemp>0 then write('+ ',cotemp) else begin cotemp:=abs(cotemp); write('- ',cotemp); end; end else if pxtemp=1 then begin if cotemp>0 then write('+ ',cotemp,'X ') else begin cotemp:=abs(cotemp); write('- ',cotemp,'X '); end; end else if cotemp>0 then write('+ ',cotemp,'X^',pxtemp,' ') else begin cotemp:=abs(cotemp); write('- ',cotemp,'X^',pxtemp,' '); end; end; writeln; end; begin ahead:=nil; bhead:=nil; chead:=nil; write('Coefficient of A :'); {Enter Polynomial Data For LinkedStack A} readln(cotemp); write('Power of X of A :'); readln(pxtemp); while cotemp<>0 do begin InsertLinkList(cotemp,pxtemp,ahead); write('Coefficient of A :'); readln(cotemp); write('Power of X of A :'); readln(pxtemp); end; write('Coefficient of B :'); {Enter Polynomial Data For LinkedStack B} readln(cotemp); write('Power of X of B :'); readln(pxtemp); while cotemp<>0 do begin InsertLinkList(cotemp,pxtemp,bhead); write('Coefficient of B :'); readln(cotemp); write('Power of X of B :'); readln(pxtemp); end; sumpolynomial(ahead,bhead,chead); write('Polynomial A : '); display(ahead); write('Polynomial B : '); display(bhead); write('Summation of A and B : '); display(chead); end. จากคุณ : ตามล่าหาฝัน - [47/05/20 - 23:39] - [15256] |