Bài toán Pascal: Viết chương trình cho phép cộng hai đa thức

Bài toán Pascal: Viết chương trình cho phép cộng hai đa thức

LÝ THUYẾT: CHUYÊN ĐỀ ĐA THỨC (Một biến).

- Để lưu trữ một đa thức ta chỉ cần lưu các hệ số (bằng một mảng)và bậc của đa thức.

- Các phép toán đối với một đa thức gồm cộng, trừ, nhân, chia hai đa thức.

- Việc tính giá trị đa thức tại một giá trị của biến được thực hiện thông qua lược đồ hooc nơ hoặc tính trực tiếp. 

BÀI TẬP

Viết chương trình cho phép cộng hai đa thức. Kết quả in ra dạng: a­nx^n + ... + a1x + a0

Giải thuật:

- Dùng hai mảng A, B để lưu hệ số của hai đa thức. Có thể dùng mảng thứ ba C hoặc dùng lại một trong hai mảng A, B  để lưu hệ số của đa thức tổng.

- Khi in kết quả cần kiểm tra hệ số để in dấu cho đúng.

Cài đặt:

Program Cong_da_thuc;

uses crt;

Type KM = array[0..10] of integer;

Var A,B: KM;

      n: byte;

Procedure NhapDT(Var A: KM; n:byte);

Var i: byte;

Begin

     For i:=n downto 0 do

     Begin Write('M[',i,']='); Read(A[i]);  End;

End;

Procedure CONG(Var A:KM;B:KM;n:byte);

Var i: byte;

Begin

    For i:= 0 to n do A[i]:=A[i]+B[i];

End;

Procedure INDT(A:KM;n:byte);

Var i: byte;

Begin

    if A[n] <> 0 then Write(A[n],'x^',n);

    For i:=n-1 downto 0 do

     if A[i] <> 0 then

      if A[i] < 0 then write(A[i],'x^',i) else Write('+',A[i],'x^',i);

End;

Begin

     clrscr;

     Write('Nhap bac n: ');Readln(n);

     Writeln('Nhap da thuc A: '); NhapDT(A,n);

     Writeln('Nhap da thuc B: '); NhapDT(B,n);

     clrscr;

     Indt(A,n); Writeln;

     Indt(B,n); Writeln;

     Cong(A,B,n);

     Writeln('Da thuc tong: ');

     Indt(A,n);

     Readln;

     Readln

End.

 

 

Bạn thấy bài viết này như thế nào?: 
Average: 8.7 (3 votes)
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
D8 to D3: Using Drupal for data visualization

D3 JavaScript library : Làm Drupal for data visualization

One of the exciting features that’s on its way in Drupal 8 is the ability to use Drupal as a RESTful web service. This means that Drupal 8 core allows you to expose data to external applications in a number of standardized formats

Giới thiệu 106 thủ thuật với Microsoft Office

Giới thiệu 106 thủ thuật với Microsoft Office

Bạn có biết là bạn hoàn toàn có thể bổ sung thêm một máy tính vào trong Word không

Sony Ericsson Xperia mini Review

Sony Ericsson Xperia mini Review

Sony Ericsson has over the years remained committed to customizing their Android experiences with the latest development being the Sony Ericsson Xperia mini.

Công ty diệt chuột T&C

 

Diet con trung