Advertisement
huyhung94

Cộng hai đa thức sử dụng DSLK

Sep 18th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 11.60 KB | None | 0 0
  1. Uses crt;
  2. Type
  3.    point= ^da_thuc;
  4.    da_thuc= record
  5.             he_so, so_mu: integer;
  6.             link: point;
  7.    end;
  8. Var
  9.    firstT, lastT, firstP, firstQ, T, P, Q : point;
  10.    stt,vtriQ,num1,num2:integer;
  11.    tl1:byte;
  12. {Ve 1 phan tu}
  13. Procedure Ve_DS(a,x,stt,mau:integer);
  14.    var hag,cot:integer;
  15.    begin
  16.          textcolor(mau);
  17.          hag:=stt div 6+1;
  18.          cot:=(stt mod 6)+1;
  19.          if ((hag<>2) and (hag<>3) and (hag>1) and (cot=1)) then
  20.          begin
  21.             writeln;
  22.             write('-',#16);
  23.          end;
  24.      {Ve khung}
  25.          gotoxy(-10+cot*13,hag*3+5); writeln('ÉÍÍÍËÍÍÍËÍ»');
  26.          gotoxy(-10+cot*13,hag*3+6); writeln('º',a:2,' º',x:2,' º º');
  27.          gotoxy(-10+cot*13,hag*3+7); writeln('ÈÍÍÍÊÍÍÍÊͼ');
  28.          {Ve them dau ->}
  29.          if cot>1 then
  30.             begin
  31.               gotoxy(-1+(cot-1)*13,hag*3+6);
  32.               writeln('ĺ-',#16);
  33.             end;
  34.          textcolor(white);
  35.    End;
  36. {Ve khung hien thi tren dau}
  37. Procedure Box(s:string);
  38.    var k:integer;
  39.    begin
  40.         textcolor(white);
  41.         gotoxy(20,2); write('É');
  42.         for k:=21 to 54 do write('Í');
  43.         write('»');
  44.         gotoxy(20,3); write('º');
  45.         gotoxy(55,3); write('º');
  46.         gotoxy(20,4); write('º');
  47.         gotoxy(55,4); write('º');
  48.         textcolor(red);
  49.         gotoxy(31,3); writeln('Cong hai da thuc');
  50.         textcolor(yellow);
  51.         gotoxy(25,4); writeln(s);
  52.         textcolor(white);
  53.         gotoxy(20,5); write('È');
  54.         for k:=21 to 54 do write('Í');
  55.         writeln('¼');
  56.    End;
  57. {Ve tat ca cac phan tu}
  58. Procedure Display(Z,first:point; vtri,mau:integer; name:char);
  59.     var vt:integer;
  60.     begin
  61.        Z:=first;
  62.        vt:=vtri;
  63.        gotoxy(1,9+vt div 2); writeln(name);
  64.        while z<> nil do
  65.              begin
  66.                   Ve_DS(z^.he_so,z^.so_mu,vt,mau);
  67.                   inc(vt);
  68.                   z:=z^.link;
  69.              end;
  70.     End;
  71. {Nhap thong tin cho da thuc}
  72. Procedure input(Var X,firstX:point;var num:integer; name:char);
  73.           var n,a,temp,i: integer;
  74.               answer:char;
  75.               s:string[40];
  76.           begin
  77.                s:='Nhap du lieu cho da thuc '+name;
  78.                firstX:=nil; i:=0;
  79.                repeat
  80.                  clrscr;
  81.              Box(s);
  82.                      inc(i);
  83.                      writeln('Nhap hang tu thu ', i,': ');
  84.                      if i=1 then
  85.                         begin
  86.                             write('nhap so mu: ');
  87.                             readln(n);
  88.                         end
  89.                      else
  90.                         repeat
  91.                               textcolor(red);
  92.                   Writeln('Luu y: so mu phai lon hon so mu truoc');
  93.                   writeln('So mu cua phan tu truoc la: ',temp);
  94.                               textcolor(white);
  95.                               write('nhap so mu cua hang tu: '); readln(n);
  96.                         until n>temp;
  97.  
  98.                      Repeat
  99.                            write('nhap he so(a # 0): '); readln(a);
  100.                      until a<>0;
  101.  
  102.                      new(x);
  103.                      x^.so_mu:=n;
  104.                      x^.he_so:=a;
  105.                      x^.link:=firstX;
  106.                      firstX:=x;
  107.              temp:=n; {Luu lai so mu cua phan tu de so sanh voi so mu cua ptu tiep theo}
  108.                      write('Tiep tuc (C/K)?????: ');
  109.                      readln(answer);
  110.                until (answer='K') or (answer='k');
  111.                num:=i;
  112.           End;
  113. {In da thuc voi cac truong hop dac biet }
  114. procedure writeout(X:point);
  115.           begin
  116.                if x^.he_so=1 then
  117.                   if x^.so_mu=0 then write('1')
  118.                      else
  119.                          if x^.so_mu=1 then write('x ')
  120.                          else
  121.                              write('x',x^.so_mu,' ')
  122.                else
  123.                    if x^.he_so=-1 then
  124.                       if x^.so_mu=0 then write('-1')
  125.                      else
  126.                          if x^.so_mu=1 then write('-x ')
  127.                          else
  128.                              write('-x',x^.so_mu,' ')
  129.                    else
  130.                        if x^.so_mu=0 then write(x^.he_so,' ')
  131.                      else
  132.                          if x^.so_mu=1 then write(x^.he_so,'x ')
  133.                          else
  134.                              write(x^.he_so,'x',x^.so_mu,' ')
  135.           End;
  136. {In da thuc}
  137. procedure output(X,firstX:point; name:char; mau:integer);
  138.           begin
  139.               textcolor(mau);
  140.               write('Da thuc ',name, '= ');
  141.                x:=firstX;
  142.                if firstX=nil then write('0'); {danh sach khong co phan tu nao}
  143.                while x<>nil do
  144.                      begin
  145.                           if x^.he_so < 0 then
  146.                  writeout(x)
  147.                           else
  148.                               begin
  149.                                    if x=firstX then writeout(x) {In phan tu dau tien >0}
  150.                                    else  {In cac phan tu tiep theo}
  151.                                        begin
  152.                                             write('+ ');
  153.                                             writeout(x);
  154.                                        end;
  155.                               end;
  156.                           x:=x^.link;
  157.                      end;
  158.                writeln;
  159.                textcolor(white);
  160.           End;
  161. {Ghi thong tin vao DSLK Tong}
  162. procedure attach(a,n,stt: integer; var Z, firstZ, lastZ: point);
  163.           begin
  164.               new(z);
  165.               z^.he_so:=a;
  166.               z^.so_mu:=n;
  167.               Ve_DS(a,n,stt,12);
  168.               writeln;
  169.               if firstZ=nil then
  170.                  begin
  171.                       firstZ:=z;
  172.                       lastZ:=z;
  173.                       lastZ^.link:=nil;
  174.                  end
  175.               else
  176.                   begin
  177.                        lastZ^.link:=z;
  178.                        lastZ:=z;
  179.                        lastZ^.link:=nil;
  180.                   end;
  181.               delay(2500);
  182.           End;
  183. {Tinh tong hai da thuc}
  184. procedure plus(X,firstX,Y,firstY: point; var Z,firstZ,lastZ:point);
  185.           var add,vtP,vtQ:integer;
  186.           begin
  187.                x:=firstX;
  188.                y:=firstY;
  189.                firstZ:=nil;
  190.                lastZ:=nil;
  191.                vtP:=0;  vtQ:=vtriQ;
  192.                delay(1000);
  193.            {To mau cho phan tu dau tien cua 2 da thuc}
  194.                Ve_DS(x^.he_so,x^.so_mu,vtP,4);
  195.                Ve_DS(y^.he_so,y^.so_mu,vtQ,4);
  196.                delay(1111);
  197.                while (x<>nil) and (y<>nil) do
  198.                      begin
  199.                           if x^.so_mu=y^.so_mu then
  200.                              begin
  201.                                   add:=x^.he_so+y^.he_so;
  202.                                   if add<>0 then
  203.                                      begin
  204.                                           attach(add,x^.so_mu,stt,Z,firstZ,lastZ);
  205.                                           inc(stt);
  206.                                      end;
  207.                                   x:=x^.link;
  208.                                   y:=y^.link;
  209.                                   inc(vtP); inc(vtQ);
  210.                       {To mau cho phan tu tiep theo cua 2 da thuc neu con}
  211.                                  if (x<>nil)then
  212.                                  Ve_DS(x^.he_so,x^.so_mu,vtP,4);
  213.                  if (y<>nil)then
  214.                                  Ve_DS(y^.he_so,y^.so_mu,vtQ,4);
  215.                                   delay(1000);
  216.                              end
  217.                      else
  218.                         begin
  219.                            if x^.so_mu>y^.so_mu then
  220.                                begin
  221.                                   attach(x^.he_so,x^.so_mu,stt,Z,firstZ,lastZ);
  222.                                   inc(stt);
  223.                                   x:=x^.link;
  224.                                   inc(vtP);
  225.                                   if x<> nil then
  226.                                     begin
  227.                                       Ve_DS(x^.he_so,x^.so_mu,vtP,4); {To mau phan tu tiep theo neu con}
  228.                                       delay(1000);
  229.                                     end;
  230.                                end
  231.                            else
  232.                                begin
  233.                                   attach(y^.he_so,y^.so_mu,stt,Z,firstZ,lastZ);
  234.                                   inc(stt);
  235.                                   y:=y^.link;
  236.                                   inc(vtQ);
  237.                                   if y<>nil then
  238.                                     begin
  239.                                       Ve_DS(y^.he_so,y^.so_mu,vtQ,4); {To mau phan tu tiep theo neu con}
  240.                                       delay(1000);
  241.                                     end;
  242.                                end;
  243.                      end;
  244.                end;
  245.            {Khi da thuc Q het}
  246.                while x<>nil do
  247.                      begin
  248.                           attach(x^.he_so,x^.so_mu,stt,Z,firstZ,lastZ);
  249.                           inc(stt);
  250.                           x:=x^.link;
  251.                           inc(vtP);
  252.                           if x<>nil then
  253.                             begin
  254.                               Ve_DS(x^.he_so,x^.so_mu,vtP,4);
  255.                               delay(1000);
  256.                            end;
  257.                      end;
  258.            {Khi da thuc P het}
  259.                while y<>nil do
  260.                      begin
  261.                           attach(y^.he_so,y^.so_mu,stt,Z,firstZ,lastZ);
  262.                           inc(stt);
  263.                           y:=y^.link;
  264.                           inc(vtQ);
  265.                           if y<> nil then
  266.                            begin
  267.                              Ve_DS(y^.he_so,y^.so_mu,vtQ,4);
  268.                              delay(1000);
  269.                            end;
  270.                      end;
  271.           End;
  272. BEGIN
  273.   Repeat
  274.      clrscr;
  275.          Box('        Bai tap so 4');
  276.      gotoxy(10,7);  write('Menu Chuc nang.');
  277.      gotoxy(10,9);  write('Phim 1. Nhap da thuc.');
  278.      gotoxy(10,10); write('Phim 2. Mo phong qua trinh tinh tong.');
  279.      gotoxy(10,11); write('Phim 3. Ket qua.');
  280.      gotoxy(10,12); write('Phim 4. Thoat.');
  281.          gotoxy(10,14); write('Phim chuc nang: '); readln(tl1);
  282.  
  283.      case tl1 of
  284.         1: begin
  285.             writeln('nhap da thuc P:');
  286.             input(P,firstP,num1,'P');
  287.             writeln('nhap da thuc Q:');
  288.             input(Q,firstQ,num2,'Q');
  289.                         writeln('Nhan Enter de quay lai Menu chuc nang.');
  290.                         readln;
  291.             end;
  292.         2: begin
  293.                         clrscr;
  294.             Box('Mo phong qua trinh tinh tong.');
  295.             output(P,firstP,'P',3);
  296.             output(Q,firstQ,'Q',2);
  297.             Display(P,firstP,0,3,'P');
  298.             writeln;
  299.             if num1<7 then
  300.                 begin
  301.                     vtriQ:=6;
  302.                     if num2<7 then
  303.                         stt:=12
  304.                     else
  305.                         stt:=18;
  306.                 end
  307.             else
  308.                 begin
  309.                     vtriQ:=12;
  310.                     if num2<7 then
  311.                         stt:=18
  312.                     else
  313.                         stt:=24;
  314.                 end;
  315.             Display(Q,firstQ,vtriQ,2,'Q');
  316.             plus(P,firstP,Q,firstQ,T,firstT,lastT);
  317.             output(T,firstT,'T',7);
  318.                         writeln('Nhan Enter de quay lai Menu chuc nang.');
  319.                         readln;
  320.             end;
  321.         3: begin
  322.             clrscr;
  323.             Box('          Ket qua');
  324.             output(P,firstP,'P',3);
  325.             output(Q,firstQ,'Q',2);
  326.             output(T,firstT,'T',7);
  327.                         writeln('Nhan Enter de quay lai Menu chuc nang.');
  328.                         readln;
  329.             end;
  330.         4: begin
  331.             Textmode(c40);
  332.                         textcolor(red);
  333.             gotoxy(16,13); write('Hen gap lai.');
  334.                         delay(1250);
  335.             end;
  336.          end;
  337.   until tl1=4;
  338. END.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement