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

 
Style đẹp dành cho các thiết kế đồ họa

Mẫu Style đẹp dành cho nhà thiết kế đồ họa

Photoshop có rất nhiều Plugin hỗ trợ tốt trong việc thiết kế như Brush, Action, Pattern, Gradients, Style…

Stanford Law School

Thiết kế Drupal site: Stanford Law School

Stanford Law School (SLS) wanted to migrate their unwieldy proprietary legacy CMS called Pentagram to Drupal. The primary reasons for the migration include:

Thiết kế Drupal website: www.PopSci.com

Thiết kế Drupal website: www.PopSci.com

Made with Drupal 5 this site is still an awesome example of successful implementation. The owners of this site are not going to upgrade it until D8 is in production.

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

 

Diet con trung