//
you're reading...
00 - Chủ đề chung, Dùng cho cấp THCS, Dùng cho cấp THPT

Bài 8: Chương trình con: Thủ tục và hàm

Bài tập

Program Chu_vi_Dien_tich;
Var
d,r,p,s:Integer;
tiep:Char;
(*——————*)
PROCEDURE Chuvi(x,y:Integer);
Begin
p:=(x+y)*2;
End;
(*——————*)
PROCEDURE Dientich(x,y:Integer);
Begin
s:=x * y;
End;
(*——————*)
BEGIN
Repeat
Writeln(‘TINH CHU VI, DIEN TICH HINH CHU NHAT’);
Writeln(‘ Su dung thu tuc’);
Writeln(‘————————————‘);
Write(‘-Nhap chieu rong: ‘);
Readln(r);
Write(‘-Nhap chieu dai : ‘);
Readln(d);
Chuvi(r,d);
Dientich(r,d);
Writeln;
Writeln(‘+Chu vi hinh chu nhat= ‘,p);
Writeln(‘+Dien tich hinh chu nhat= ‘,s);
Writeln;
Write(‘ Co thuc hien tiep khong ? (c/k) ‘);
Readln(tiep);
Until (tiep=’k’) Or (tiep=’K’);
END.

Program Ve_hinh_chu_nhat;
CONST
Hoathi=’*’;
Var
r,d,i:Integer;
tiep:Char;
(*—————*)
PROCEDURE Ve(x,y:Integer);
Var
k:Integer;
Begin
For k:=1 To y Do
Write(Hoathi);
Writeln;
For k:=1 To x-2 Do
Writeln(Hoathi,Hoathi:y-1);
For k:=1 To y Do
Write(Hoathi);
Writeln;
End;
(*—————*)
BEGIN
Repeat
Writeln(‘VE HINH CHU NHAT RONG’);
Writeln(‘ Dung thu tuc’);
Writeln(‘———————‘);
Write(‘-Chieu rong = ‘);
Readln(r);
Write(‘-Chieu dai = ‘);
Readln(d);
Ve(r,d);
Writeln;
Write(‘ Co tiep tuc khong ? (c/k) ‘);
Readln(tiep);
Until Upcase(tiep)=’K’;
END.

Program Doi_cho;
Var
so1,so2:Integer;
(*—————–*)
PROCEDURE Doi(Var n,m:Integer);
Var
tam:Integer;
Begin
tam:=n;
n:=m;
m:=tam;
End;
(*—————–*)
BEGIN
Writeln(‘DOI CHO 2 SO’);
Writeln(‘Dung thu tuc’);
Writeln(‘————‘);
Write(‘-Nhap so thu nhat: ‘);
Readln(so1);
Write(‘-Nhap so thu hai: ‘);
Readln(so2);
Writeln(‘=================’);
Writeln(‘ Truoc khi doi cho’);
Writeln(‘+So thu nhat = ‘,so1);
Writeln(‘+So thu hai = ‘,so2);
Writeln(‘ Sau khi doi cho’);
Doi(so1,so2);
Writeln(‘*So thu nhat : ‘,so1);
Writeln(‘*So thu hai : ‘,so2);
Writeln;
Write(‘ Bam phim de ket thuc ‘);
Readln
END.

Program Dao_so;
Var
n:LongInt;
(*——————-*)
PROCEDURE Daoso(x:LongInt);
Begin
Write(x Mod 10);
If (x Div 10) 0 Then
Daoso(x Div 10)
End;
(*——————-*)
BEGIN
Writeln(‘DAO NGUOC MOT SO’);
Writeln(‘ Dung thu tuc’);
Writeln(‘————–‘);
Write(‘-Nhap mot so nguyen: ‘);
Readln(n);
Writeln;
Writeln(‘+So nhap vao la = ‘,n);
Write(‘+Bay gio la = ‘);
Daoso(n);
Writeln;
Write(‘ Bam phim de ket thuc ‘);
Readln
END.

Program Doi_chu_hoa;
Var
Chuoi:String;
(*—————–*)
PROCEDURE Doi(Var St:String);
Var
i:Byte;
Begin
For i:=1 To Length(St) Do
St[i]:=Upcase(St[i]);
End;
(*—————–*)
BEGIN
Writeln(‘DOI CHU THUONG RA CHU HOA’);
Writeln(‘ Dung thu tuc’);
Writeln(‘————————-‘);
Write(‘-Nhap mot chuoi ky tu : ‘);
Readln(Chuoi);
Writeln;
Doi(Chuoi);
Writeln(‘+Doi ra chu hoa = ‘,chuoi);
Writeln;
Write(‘ Bam phim de ket thuc ‘);
Readln
END.

Program Tong_nghich_dao;
Var
n,k :1..30; {Kieu mien con }
s:Real;
(*——————-*)
FUNCTION So(i:Integer):Integer;
Var
j:1..30;
tam:LongInt;
Begin
tam:=1;
For j:=1 To i Do
tam:=tam * j;
So:=tam;
End;
(*——————-*)
BEGIN
Writeln(‘TONG NGHICH DAO CUA N SO NGUYEN’);
Writeln(‘ Su dung ham’);
Writeln(‘——————————-‘);
Write(‘-Nhap so N = ‘);
Readln(n);
s:=0;
For k:=1 To n Do
s:=s+1/so(k);
Writeln;
Writeln(‘+Tong nghich dao cua ‘,n,’ so nguyen la: ‘,s:10:2);
Writeln;
Write(‘ Bam phim de ket thuc ‘);
Readln
END.

Program Giai_thua;
Var
n:Integer;
(*———————*)
FUNCTION Gt(x:Integer):Integer;
Var
k,s:Integer;
Begin
s:=1;
For k:=1 To x Do
s:=s*k;
Gt:=s;
End;
(*———————*)
BEGIN
Writeln(‘TINH GIAI THUA CUA N’);
Writeln(‘ Su dung ham’);
Writeln(‘——————–‘);
Write(‘-Nhap N= ‘);
Readln(n);
Writeln;
Writeln(‘+Giai thua cua ‘,n,’ = ‘,Gt(n));
Writeln;
Write(‘ Bam de ket thuc ‘);
Readln
END.

Program Doi_he_so;
Var
n:Integer;
(*——————*)
FUNCTION Doi(so:Integer):String;
Const
ky_so:Array[0..15] Of Char =(‘0′,’1′,’2′,’3′,’4′,’5’,
‘6’,’7′,’8′,’9′,’A’,’B’,’C’,’D’,’E’,’F’);
Var
tam1:Integer;
tam2:String;
Begin
tam1:=Hi(so);
tam2:=ky_so[tam1 Shr 4] + ky_so[tam1 And $0F];
tam1:=Lo(so);
Doi:=tam2+ky_so[tam1 Shr 4] +Ky_so[tam1 And $0F]
End;
(*——————*)
BEGIN
Repeat
Writeln(‘DOI SO HE 10 SANG HE 16’);
Writeln(‘ Su dung ham ‘);
Writeln(‘———————–‘);
Writeln(‘Nhap mot so nguyen tu -32768 den 32767’);
Write(‘neu nhap so 0 se ket thuc ‘);
Readln(n);
Writeln;
Writeln(‘+So he 10 la = ‘,n);
Writeln(‘+Doi sang he 16 la = ‘,Doi(n));
Writeln;
Until n=0;
END.

Program Doi_he_so;
Var
Ky_so:String;
(*——————*)
FUNCTION Doiso(x:String):Integer;
Var
j,tong,nhan,so:Integer;
tam:Char;
Begin
tong:=0;
nhan:=1;
For j:= length(x) Downto 1 Do
Begin
tam:=x[j];
If Ord(tam)= 32 Then
tam:=’0’;
If tam in[‘0′..’9′,’A’..’F’] Then {Toan tu in}
Begin
so:=Ord(tam) – 48;
If so > 9 Then
so:=so-7;
tong:=tong + so*nhan;
End
Else
Begin
Doiso:=0;
Exit;
End;
nhan:=nhan*16
End;
Doiso:=tong;
End;
(*——————*)
BEGIN
Writeln(‘DOI SO HE 16 SANG HE 10’);
Writeln(‘ Su dung ham’);
Writeln(‘————————‘);
Repeat
Write(‘Nhap ky so he 16 (go kk de dung): ‘);
Readln(ky_so);
Writeln;
Writeln(‘+Doi sang he 10 la = ‘,Doiso(ky_so));
Writeln;
Until ky_so=’kk’;
END.

Program Kiem_tra_ngay;
TYPE
ngay=1..31;
thang=1..12;
Var
d:ngay;
m:thang;
y:Word;
(*——————–*)
FUNCTION Nhuan(Nam:Word):Boolean;
Begin
If nam Mod 100 = 0 Then
Nhuan:=(Nam Mod 400)=0
Else
Nhuan:=(Nam Mod 4)=0;
End;
(*——————–*)
FUNCTION So_ngay(d:Ngay; m:Thang; Y:Word): Boolean;
Var
t1,t2,t3:Boolean;
Begin
t1:=(d=30) And (m=2);
t2:=(d=29) And (m=2) And (Not Nhuan(y));
t3:=(d=31) And (m in [2,4,6,9,11]);
If t1 Or t2 Or t3 Then
so_ngay:=False
Else
so_ngay:=True;
End;
(*——————–*)
FUNCTION Ket_thuc:Boolean;
Var
ch:Char;
Begin
Write(‘-Tiep tuc nua khong ? (c/k) ‘);
Readln(ch);
Ket_thuc:=Upcase(ch)=’K’;
End;
(*——————–*)
BEGIN
Writeln(‘KIEM TRA NGAY HOP LE ?’);
Writeln(‘ Su dung ham’);
Writeln(‘———————-‘);
Repeat
Write(‘-Ngay: ‘);
Readln(d);
Write(‘-Thang: ‘);
Readln(m);
Write(‘-Nam: ‘);
Readln(y);
If so_ngay(d,m,y) Then
Writeln(‘*Ngay ‘,d:2,’ Thang ‘,m:2,’ Nam ‘,y:4,’ la hop le’)
Else
Writeln(‘*Ngay ‘,d:2,’ Thang ‘,m:2,’ Nam ‘,y:4,’ khong hop le’)
Until Ket_thuc;
END.

Program Doi_nhiet_do;
Var
t:Real;
tiep:Char;
(*———————*)
FUNCTION Doi(x:Real):Real;
Begin
Doi:=((9*x/5) + 32);
End;
(*———————*)
BEGIN
Repeat
Writeln(‘DOI DO BACH PHAN SANG DO FARENHEIT’);
Writeln(‘ Su dung ham’);
Writeln(‘———————————-‘);
Write(‘-Nhap do bach phan: ‘);
Readln(t);
Writeln(‘*Doi sang do Farenheit = ‘,Doi(t):6:2);
Writeln;
Write(‘-Co tiep tuc khong ? (c/k) ‘);
Readln(tiep);
Until tiep=’k’ ;
END.

Program Tong_cac_so_chan;
Var
m,n:LongInt;
(*——————–*)
FUNCTION Tong(x,y:LongInt):LongInt;
Var
i,t:LongInt;
Begin
t:=0;
For i:=x To y Do
If (i Mod 2 = 0) Then
Begin
Write(i,’ ‘);
t:=t+i;
End;
Write(‘ = ‘);
tong:=t;
End;
(*——————–*)
BEGIN
Writeln(‘TINH TONG CAC SO CHAN TU M DEN N’);
Writeln(‘ Su dung ham’);
Writeln(‘——————————–‘);
Write(‘-Nhap so dau (M) : ‘);
Readln(m);
Write(‘-Nhap so cuoi (N): ‘);
Readln(n);
Writeln(‘ *Tong cua cac so chan tu ‘,m,’ den ‘,n,’ la:’);
Writeln(Tong(m,n));
Writeln;
Write(‘ Bam phim de ket thuc’);
Readln;
END.

Program So_nho;
Var
so1,so2,so3:Integer;
(*———————*)
FUNCTION nho(x,y:Integer):Integer;
Begin
If x < y Then
nho:=x
Else
nho:=y;
End;
(*———————*)
BEGIN
Writeln(‘SO NHO NHAT TRONG 3 SO’);
Writeln(‘ dung ham ‘);
Writeln(‘—————————–‘);
Write(‘-So thu nhat = ‘);
Readln(so1);
Write(‘-So thu hai = ‘);
Readln(so2);
Write(‘-So thu ba = ‘);
Readln(so3);
Writeln;
Writeln(‘*So nho nhat la: ‘,nho(nho(so1,so2),so3));
Writeln;
Write(‘ Bam phim de ket thuc ‘);
Readln
END.

Program In_ten_thang;
Var
n:Byte;
(*—————–*)
FUNCTION thang(x:Byte):String;
Const
Ten_thang:Array[1..12] Of String=(‘Gieng’,’Hai’,’Ba’,
‘Tu’,’Nam’,’Sau’,’Bay’,’Tam’,’Chin’,’Muoi’,’Mot’,’Chap’);
Begin
thang:=’ Thang ‘+Ten_thang[x];
End;
(*—————–*)
BEGIN
Writeln(‘DOI TEN THANG TU SO RA CHU’);
Writeln(‘ Su dung ham’);
Writeln(‘————————–‘);
Write(‘-Thang : ‘);
Readln(n);
If (n 12) Then
Write(‘Khong hop le’)
Else
Write(thang(n));
Writeln;
Write(‘ Bam phim de ket thuc ‘);
Readln
END.

Program Lam_tron;
Var
so:Real;
le:Integer;
(*—————*)
FUNCTION Tron(so:Real;le:Integer):Real;
Var
i,n:LongInt;
Begin
For i:=1 To le+1 Do
so:=so*10;
n:=Trunc(so);
If (n Mod 10) >= 5 Then
n:=(n Div 10) +1
Else
n:=n Div 10;
so:=n;
For i:=1 To le Do
so:=so/10;
Tron:=so;
End;
(*—————*)
BEGIN
Writeln(‘LAM TRON SO THUC’);
Writeln(‘ Su dung ham’);
Writeln(‘—————-‘);
Write(‘-Nhap so: ‘);
Readln(so);
Write(‘-Can bao nhieu so le: ‘);
Readln(le);
Writeln;
Writeln(‘ KET QUA’);
Writeln(‘*So nhap vao = ‘,so:12:10);
Writeln(‘*So lam tron = ‘,Tron(so,le):12:le);
Writeln;
Write(‘ Bam phim de ket thuc ‘);
Readln
END.

Program Doi_tien;
Var
sotien:LongInt;
(*—————–*)
PROCEDURE Doi(St:LongInt);
Const
Loai:Array[1..6] Of Integer =(50,20,10,5,2,1);
Var
i,soto:Integer;
Begin
i:=1;
Repeat
soto:=st Div loai[i];
If soto 0 Then
Writeln(soto,’ to loai ‘,loai[i],’ ngan dong’);
st:=st Mod loai[i];
i:=i+1;
Until st=0;
End;
(*—————–*)
BEGIN
Writeln(‘DOI TIEN SAO CHO SO TO IT NHAT’);
Writeln(‘ Dung thu tuc’);
Writeln(‘——————————‘);
Repeat
Write(‘-So tien (ngan) co ( so 0 de ngung) : ‘);
Readln(Sotien);
Doi(Sotien);
Writeln;
Until sotien=0;
END.

Program X_mu_n;
Var
x:Integer;
n:Word;
(*—————–*)
FUNCTION ham_mu(i:Integer;k:Word):LongInt;
Var
tam,j:Integer;
Begin
tam:=1;
For j:=1 To k Do
tam:=tam*i;
Ham_mu:=tam;
End;
(*—————–*)
BEGIN
Writeln(‘HAM X MU N’);
Writeln(‘———-‘);
Write(‘-Nhap X = ‘);
Readln(x);
Write(‘-Nhap N = ‘);
Readln(n);
Writeln(‘*Ket qua = ‘,Ham_mu(x,n));
Writeln;
Write(‘ Bam phim de ket thuc ‘);
Readln
END.

Program Xo_so;
Uses Crt;
Var
quacau,i:Byte;
ch:Char;
BEGIN
ClrScr;
TextColor(Red);
Writeln(‘ QUAY XO SO’);
TextColor(Magenta);
Writeln(‘ Su dung ham Random’);
TextColor(Yellow);
Writeln(‘ ——————-‘);
Writeln;
TextColor(Green);
Write(‘ Cho so qua cau de quay: ‘);
Readln(quacau);
Writeln;
TextColor(Cyan);
Write(‘ Bam phim bat ky de bat dau quay xo so’);
Repeat
i:= Random(9);
Until KeyPressed;
Writeln;
TextColor(LightBlue);
Write(‘ Bam phim de ngung quay’);
Readln;
ch:=ReadKey;
Writeln;
TextColor(Yellow);
Writeln(‘ KET QUA TRUNG THUONG LA SO: ‘);
Writeln;
TextColor(Red);
Write(‘ ‘);
For i:=1 To quacau Do
Write(‘ ‘,Random(9):3);
Writeln;
Writeln;
TextColor(Magenta);
Write(‘ Bam phim de ket thuc’);
Readln
END.

Program Doi_chu_hoa;
Uses Chuhoa;
Var
chuoi:String;
Begin
Writeln(‘Nhap mot chuoi: ‘);
Readln(chuoi);
Writeln(‘Doi ra chu hoa: ‘,Hoa(chuoi));
Writeln;
Write(‘ Bam phim de ket thuc ‘);
Readln
End.

Advertisements

About pascalteacher

Trang thông tin Toán học và Tin học

Thảo luận

Không có bình luận

Trả lời

Mời bạn điền thông tin vào ô dưới đây hoặc kích vào một biểu tượng để đăng nhập:

WordPress.com Logo

Bạn đang bình luận bằng tài khoản WordPress.com Đăng xuất / Thay đổi )

Twitter picture

Bạn đang bình luận bằng tài khoản Twitter Đăng xuất / Thay đổi )

Facebook photo

Bạn đang bình luận bằng tài khoản Facebook Đăng xuất / Thay đổi )

Google+ photo

Bạn đang bình luận bằng tài khoản Google+ Đăng xuất / Thay đổi )

Connecting to %s

Các tác giả

Categories

Tháng Mười 2014
H B T N S B C
« Th11   Th11 »
 12345
6789101112
13141516171819
20212223242526
2728293031  

NCT Computer

Flickr Photos

Thống kê

  • 209,599 lượt xem

pascalteacher.nct@gmail.com


Trang huấn luyện học sinh giỏi Tin học

%d bloggers like this: