Chương trình tính phân số

đăng 19:36, 26 thg 2, 2012 bởi Thanh Vũ Nguyễn   [ đã cập nhật 19:36, 26 thg 2, 2012 ]
program PhepTinhPhanSo;
type KieuPhanSo = record
       ts, ms: integer;
     end;
{------------------------------------}
{        CHUONG TRINH CON            }
{------------------------------------}
procedure GPS(ps:KieuPhanSo);
begin
  write(ps.ts,'/',ps.ms);
end;

function USCLN(a, b: integer):integer;
begin
  while (a<>b) do
  begin
    if a>b then a:=a - b
    else b:=b - a;
  end;
  USCLN:=a;
end;

procedure ToiGian(var ps: KieuPhanSo);
  var us:integer;
begin
  us:=USCLN(ps.ts,ps.ms);
  ps.ts:=ps.ts div us;
  ps.ms:=ps.ms div us;
end;

procedure CongPS(ps1, ps2: KieuPhanSo; var kq: KieuPhanSo);
begin
  kq.ms:=ps1.ms*ps2.ms;
  kq.ts:=ps1.ts*ps2.ms + ps2.ts*ps1.ms;
  ToiGian(kq);
end;

procedure NhapPS(var ps:KieuPhanSo);
begin
  write('Tu so = '); readln(ps.ts);
  write('Mau so = '); readln(ps.ms);
end;
{------------------------------------}
{        CHUONG TRINH CHINH          }
{------------------------------------}
var
  ps1,ps2,kq:KieuPhanSo;
begin
  writeln('Phan so 1');
  NhapPS(ps1);
  writeln('Phan so 2');
  NhapPS(ps2);
  GPS(ps1);
  write(' + ');
  GPS(ps2);
  write(' = ');
  CongPS(ps1,ps2,kq);
  GPS(kq);
  readln;
end.
ċ
PHANSO.PAS
(1k)
Thanh Vũ Nguyễn,
19:37, 26 thg 2, 2012
Comments