list program contoh pemrograman terstruktur 1

Post on 18-Jun-2015

1.702 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Listing Program Contoh Pemrograman Terstruktur

TRANSCRIPT

DAFTAR ISI

1. Program Konversi Suhu 22. Unit Menampilkan Pesan 33. Program Lingkaran Konsul 44. Unit Lingkaran 55. Program Nilai Tertinggi Antara 2 Bilangan 66. Program Nilai Tertinggi Antara 3 Bilangan 77. Unit Password 88. Program Tes Case 99. Unit Kalkulator 1010. Program Rerata 1 1211. Program Rerata 2 1312. Program Test While Do 1413. Program Repeat Until 1514. Program Daftar Sin-Cos 1615. Program Persamaan Aljabar 1716. Unit Animasi Lingkaran 1817. Program Contoh Prosedur 1918. Program Contoh Prosedur Lainnya 2019. Program Contoh Ruang Lingkup 2120. Program Contoh Fungsi 2221. Program Contoh Parameter 2322. Unit Terbilang 2423. Program Hanoi Konsul 2624. Unit Menara Hanoi 27

Create by nova elins03 1

program konversi_suhu;{$apptype console}uses Forms;

{$R *.res}const num = 10;var i,c : integer; f,r : real;begin Application.Initialize; Application.Run; write(' masukan suhu dalam Celcius: '); readln(c); for i := 1 to num do begin r := 4*c/5; f := 9*c/5+32; c := c+num; writeln(' Celcius = ',c, ' Reamur = ',r, 'Fahrenheit = ',f); end; readln;end.

Create by nova elins03 2

unit menampilkan_pesan;

interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;

var Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);begin {adalah perintah untuk

memulai statement}showmessage('selamat pagi'); //ini adalah perintah untuk

menampilkan pesanend; (*adalah perintah untuk akhir

program*)

end.

Create by nova elins03 3

program lingkaran_konsol;{$APPTYPE CONSOLE}{program menghitung keliling dan luas lingkaran}uses Forms;

const pi = 3.14159;var radius : integer; keliling, luas : double;{$R *.res}

begin Application.Initialize; Application.Run; writeln('Lingkaran'); write('Radius = '); readln(radius); keliling := 2*pi*radius; luas := pi*radius*radius; writeln('Keliling = ',keliling:10:3); write('Luas = ',luas:10:3); readln;end.

Create by nova elins03 4

unit lingkaran;

interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type TForm1 = class(TForm) Button1: TButton; Label1: TLabel; Label2: TLabel; Edit1: TEdit; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;

var Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);var radius : Integer; keliling, luas : Real;begin Memo1.Clear; radius := StrToInt(Edit1.Text); keliling := 2*pi*radius; luas := pi*radius*radius; Memo1.Lines.Add('Keliling = '+FloatToStr(keliling)); Memo1.Lines.Add('Luas = '+FloatToStr(luas));end;

end.

Create by nova elins03 5

program nilai_tertinggi_2bilangan;{$apptype console}uses Forms;

{$R *.res}var i,j: integer;

begin Application.Initialize; Application.Run; write('Masukan sebuah bilangan bulat: '); readln(i); write('Masukan lagi sebuah bilangan bulat: '); readln(j); if (i<j) then writeln(i, ' kurang dari ', j) else writeln(i, ' lebih dari ',j); readln;end.

Create by nova elins03 6

program nilai_terbesar_3bilangan;{$apptype console}

uses Forms;

{$R *.res}var a,b,c,m : integer;

begin Application.Initialize; Application.Run; write('Ketik 3 bilangan: '); readln(a,b,c); if (a>b) then m := a else m := b; if (m<c) then m := c; writeln('Nilai terbesar adalah: ',m); readln;end.

Create by nova elins03 7

unit password;

interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type TForm1 = class(TForm) Label1: TLabel; Edit1: TEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;

var Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);begin if Edit1.Text = 'asdfg' then ShowMessage('password benar') else ShowMessage('password salah');end;

end.

Create by nova elins03 8

program testcase;{$apptype console}uses Forms;

{$R *.res}var i : integer;begin Application.Initialize; Application.Run; write('Ketik bilangan bulat antara 0 sampai 100: '); readln(i); case i of 0,2,4,6,8 : writeln('Bilangan Genap'); 1,3,5,7,9 : writeln('Bilangan Ganjil'); 10..100 : writeln('Antara 10 sampai 100'); else writeln('Bilangan Negatif atau Lebih dari 100'); end; readln;end.

Create by nova elins03 9

unit kalkulator;

interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;

type TForm1 = class(TForm) Bilangan1: TRadioGroup; Operasi: TRadioGroup; Bilangan2: TRadioGroup; Edit1: TEdit; Hasil: TButton; procedure HasilClick(Sender: TObject); private { Private declarations } public { Public declarations } end;

var Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.HasilClick(Sender: TObject);var bil1, bil2 : integer; Hasil : single; opr : char;begin case Bilangan1.ItemIndex of 0 : bil1 := 1; 1 : bil1 := 2; 2 : bil1 := 3; 3 : bil1 := 4; 4 : bil1 := 5; else begin showmessage('Bilangan pertama belum dipilih!'); exit; end; end; case Bilangan2.ItemIndex of 0 : bil2 := 1; 1 : bil2 := 2; 2 : bil2 := 3; 3 : bil2 := 4;

Create by nova elins03 10

4 : bil2 := 5; else begin showmessage('Bilangan kedua belum dipilih!'); exit; end; end; case Operasi.ItemIndex of 0 : hasil := bil1+bil2; 1 : hasil := bil1-bil2; 2 : hasil := bil1*bil2; 3 : hasil := bil1/bil2; else begin showmessage('Operasi belum dipilih!'); exit; end; end; Edit1.Text := FloatToStr(Hasil);end;

end.

Create by nova elins03 11

program rerata1;{$apptype console}uses Forms;

{$R *.res}var i : integer; jml, r : real; arr : array[1..5] of real;begin Application.Initialize; Application.Run; jml := 0; for i := 1 to 5 do begin write('masukan sebuah bilangan: '); readln(arr[i]); jml := jml+arr[i]; end; r := jml/5; writeln('Reratanya adalah: ',r:10:3); readln;end.

Create by nova elins03 12

program rerata2;{$apptype console}uses Forms;

{$R *.res}var i : integer; jml, r : real; arr : array[1..5] of real;begin Application.Initialize; Application.Run; jml := 0; for i := 5 downto 1 do begin write('masukan sebuah bilangan: '); readln(arr[i]); jml := jml+arr[i]; end; r := jml/5; writeln('Reratanya adalah: ',r:10:3); readln;end.

Create by nova elins03 13

program tesWhileDo;{$apptype console}uses Forms;

{$R *.res}var ch: char;begin Application.Initialize; Application.Run; write('Masukan sebuah karakter: '); readln(ch); while(ch <> '.') do begin writeln(ch); write('Masukan sebuah karakter: '); readln(ch); endend.

Create by nova elins03 14

program testRepeatUntil;{$apptype console}uses Forms;

{$R *.res}var ch : char;begin Application.Initialize; Application.Run; repeat write('masukan sebuah karakter: '); readln(ch); writeln(ch); until (ch = '.');end.

Create by nova elins03 15

program daftar_SinCos;{$apptype console}uses Forms;

{$R *.res}var x, dx, xrad : real;begin Application.Initialize; Application.Run; x := 0; dx := 0.5; writeln('x':10, 'sin(x)':15, 'cos(x)':15); while x <= 90 do begin xrad := pi/180*x; writeln(x:10:1, sin(xrad):15:10, cos(xrad):15:10); x := x+dx; end; readln;end.

Create by nova elins03 16

program persamaan_aljabar;{$apptype console}uses Forms;

{$R *.res}var Cacah, x : Integer; Tebakan, Akar, Cek, Kesalahan : Double; Tanda : Boolean;begin Application.Initialize; Application.Run; Tanda := True; Cacah := 0; Write('Nilai awal x: '); Readln(Tebakan); While Tanda do Begin inc(Cacah); if Cacah = 50 then Tanda := False; Cek := 10 - 3 * sqr(Tebakan); if Cek > 0 then begin Akar := exp(0.2*ln(Cek)); writeln('Iterasi ke-',Cacah,' x = ',Akar:7:5); Kesalahan := abs(Akar-Tebakan); if Kesalahan > 0.00001 then Tebakan := Akar else begin Tanda := False; writeln; writeln('Akar = ',Akar:7:5,'dengan iterasi sejumlah ',Cacah); end; end else begin Tanda := False; writeln; writeln('Bilangan berada di luar area, coba lagi dengan tebakan yang baru'); end; end; if (Cacah = 50) and (Kesalahan > 0.00001) then begin writeln; writeln('Konvergensi tidak tidak dapat terjadi saat iterasi sudah mencapai 50 kali'); end; readln;end.

Create by nova elins03 17

unit animasi_lingkaran;

interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls;

type TForm1 = class(TForm) Shape1: TShape; procedure FormActivate(Sender: TObject); private { Private declarations } public { Public declarations } end;

var Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);var dx, dy, i: integer;begin dx := 10; dy := 10; i := 0; while (i < 1000) do begin Shape1.Left := Shape1.Left + dx; Shape1.Top := Shape1.Top + dx; if (Shape1.Left < 0) or (Shape1.Left > ClientWidth ) then dx := - dx; if (Shape1.Top < 0) or (Shape1.Top > ClientHeight) then dy := - dy; Application.ProcessMessages; Sleep(100); inc(i); end;end;

end.

Create by nova elins03 18

program contoh_prosedur;{$apptype console}uses Forms;

{$R *.res}var a,b : integer;procedure Cetak_Bilangan;begin writeln('Hasil pertama = ',a); writeln('Hasil kedua = ',b);end;

begin Application.Initialize; Application.Run; write('Masukan bilangan pertama: '); readln(a); write('Masukan bilangan kedua: '); readln(b); Cetak_Bilangan; a := a+10; b := b+5; Cetak_Bilangan; readln;end.

Create by nova elins03 19

program prosedur_lain;{$apptype console}

uses Forms;

{$R *.res}

var a,b,c:integer;

procedure maksimum;var maks:integer;begin if a>b then maks := a else maks:=b; if c>maks then maks:=c; writeln('nilai maksimumnya adalah: ',maks);end;

begin Application.Initialize; Application.Run; writeln('masukan nilai a,b,c: '); readln(a,b,c); while a<>0 do begin maksimum; readln(a,b,c) end;end.

Create by nova elins03 20

program ContohRuangCakupan;{$apptype console}

uses Forms;

{$R *.res}var i,j:integer; //variabel global

procedure satu;var i:integer; //variabel lokalbegin i:=i*10; j:=j*5; //variabel global writeln('Nilai i sekarang = ',i); //i lokal writeln('Nilai j sekarang = ',j); //j globalend;

begin Application.Initialize; Application.Run; write('Masukan dua integer: '); readln(i,j); writeln('Nilai i dan j sekarang = ',i,' ',j); satu; writeln('Nilai i dan j sekarang = ',i,' ',j); readln;end.

Create by nova elins03 21

program contoh_fungsi;{$apptype console}

uses Forms;

{$R *.res}var a:array [1..5] of integer; total:integer;

function hitung:integer;//mengembalikan nilai bertipe integervar i,jumlah:integer;begin jumlah:=0; for i:= 1 to 5 do jumlah:= jumlah+a[i]; //hitung jumlah bilangan //kembalikan ke pemanggil hitung:=jumlah;end;

procedure masukdata;var i:integer;begin for i:=1 to 5 do begin write('Masukan bilangan integer ke-',i,': '); readln(a[i]); end;end;

begin Application.Initialize; Application.Run; masukdata; //panggil procedure total := hitung; //panggil function, tampung nilai kembalian writeln('Jumlah semua bilangan adalah: ',total); readln;end.

Create by nova elins03 22

program contoh_parameter;{$apptype console}

uses Forms;

{$R *.res}var a,b,maks:integer;

function maksimal(x,y:integer):integer; //x dan y parameter nilaibegin if x>y then maksimal:=x else maksimal:=y;end;

procedure tukar(var x,y:integer); //x dan y parameter variabelvar temp:integer;begin temp:=x; x:=y; y:=temp;end;

begin Application.Initialize; Application.Run; write('Masukan dua bilangan integer: '); readln(a,b); writeln('Maksimum : ',maksimal(a,b)); //panggil function maksimal tukar(a,b); //panggil procedure writeln('Nilai a dan b sekarang: ',a,' ',b); readln;end.

Create by nova elins03 23

unit terbilang;

interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Edit1: TEdit; Edit2: TEdit; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;

var Form1: TForm1;

implementation

{$R *.dfm}function Terbilang(x:integer):string;

function Bilang(x:integer):string;begin case x of 0 : Result :=''; 1 : Result :='satu'; 2 : Result :='dua'; 3 : Result :='tiga'; 4 : Result :='empat'; 5 : Result :='lima'; 6 : Result :='enam'; 7 : Result :='tujuh'; 8 : Result :='delapan'; 9 : Result :='sembilan'; 10: Result :='sepuluh'; 11: Result :='sebelas'; end;end;

begin if x < 12 then

Create by nova elins03 24

Result :=''+Bilang(x) else if x < 20 then Result := Terbilang(x-10)+' belas' else if x < 100 then Result := Terbilang(x div 10)+ ' puluh' + Terbilang(x mod 10) else if x < 200 then Result :=' seratus'+Terbilang(x-100) else if x < 1000 then Result := Terbilang( x div 100)+ ' ratus'+ Terbilang(x mod 100) else if x < 2000 then Result :=' seribu'+Terbilang(x-1000) else if x < 1000000 then Result := Terbilang(x div 1000)+ ' ribu'+Terbilang(x mod 1000) else if x < 1000000000 then Result :=Terbilang(x div 1000000)+' juta'+Terbilang(x mod 1000000)+Terbilang(x mod 1000000) else Result := Terbilang(x div 1000000000)+' milyar'+Terbilang(x mod 1000000000)end;

procedure TForm1.Button1Click(Sender: TObject);beginEdit2.Text := Terbilang(StrToInt(Edit1.Text));end;

end.

Create by nova elins03 25

program hanoi_konsol;{$apptype console}

uses Forms;

{$R *.res}

procedure Hanoi(N:integer; TiangAsal, TiangTujuan, TiangBantu:char);begin if N > 0 then begin Hanoi(N-1,TiangAsal,TiangBantu,TiangTujuan); writeln('Pindah piringan ke-',N,'dari ',TiangAsal,' ke ',TiangTujuan); Hanoi(N-1, TiangBantu, TiangTujuan, TiangAsal); end;end;

var N:integer; Tiang1, Tiang2, Tiang3:char;begin Application.Initialize; Application.Run; Tiang1 :='A'; Tiang2:='B'; Tiang3:='C'; N:=4; Hanoi(N,Tiang1,Tiang2,Tiang3); readln;end.

Create by nova elins03 26

unit menara_Hanoi;

interface

uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls;

type TForm1 = class(TForm) Label1: TLabel; Label2: TLabel; Edit1: TEdit; Edit2: TEdit; Button1: TButton; Panel1: TPanel; procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } LA,LB,LC : TStringList; //tiang pertama, kedua dan ketiga Kotak : array of TShape; //array piringan procedure Hanoi(N:integer; A,T,S:TStringList); procedure HanoiMove(A,T:TStringList); public { Public declarations } end;

var Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);var i:integer;begin LA.Clear; LB.Clear; LC.Clear; for i:= 0 to Length(Kotak)-1 do //kosongkan memori yang pernah digunakan Kotak[i].Free; SetLength(Kotak,StrToInt(Edit1.Text)); for i:=0 to Length(Kotak)-1 do begin //buat piringan sejumlah N Kotak[i]:=TShape.Create(Self); Kotak[i].Shape:=stRoundRect;

Create by nova elins03 27

Kotak[i].Width:=20*(i+1); Kotak[i].Height:=20; Kotak[i].Left:=Length(Kotak)*10-(i+1)*10; Kotak[i].Top:=Panel1.Height+(1-Length(Kotak))*20; Kotak[i].Parent:=Panel1; Kotak[i].Brush.Color:=RGB(Random(256), Random(256), Random(256)); end; LA.Add('1'); LB.Add('2'); LC.Add('3'); for i:=StrToInt(Edit1.Text) downto 1 do //no pada tiang LA.Add(IntToStr(i)); Hanoi(LA.Count-1,LA,LC,LB); end;

procedure TForm1.HanoiMove(A,T:TStringList);var x1,y1,x2,y2,dx,tunda,maks:integer;begin tunda:=StrToInt(Edit2.Text); maks:=StrToInt(Edit1.Text); T.Add(A[A.Count-1]); A.Delete(A.Count-1); x1:=Kotak[StrToInt(T[T.Count-1])-1].Left; //x asal y1:=Kotak[StrToInt(T[T.Count-1])-1].Top; //y asal x2:=(StrToInt(T[0])-1)*(maks+1)*20+length(Kotak)*10-StrToInt(T[T.Count-1])*10; //x tujuan y2:=Panel1.Height-(T.Count-1)*20; //y tujuan dx := (x2-x1) div abs(x2-x1); dx:=5*dx; //delta gerak mendatar while (y1>0) do begin //gerak naik dec(y1,5); Kotak[StrToInt(T[T.Count-1])-1].Top:=y1; Sleep(tunda); Application.ProcessMessages; end; while (x2*dx)>(x1*dx) do begin //gerak mendatar x1:=x1+dx; Kotak[StrToInt(T[T.Count-1])-1].Left:=x1; Sleep(tunda); Application.ProcessMessages; end; while (y2>y1) do begin //gerak turun inc(y1,5); Kotak[StrToInt(T[T.Count-1])-1].Top:=y1; Sleep(tunda); Application.ProcessMessages; end;end;

procedure TForm1.Hanoi(N:integer; A,T,S:TStringList); //inti prosedure hanoi

Create by nova elins03 28

begin if N=1 then HanoiMove(A,T) else begin Hanoi(N-1,A,S,T); HanoiMove(A,T); Hanoi(N-1,S,T,A); end;end;

procedure TForm1.FormCreate(Sender: TObject);begin LA:= TStringList.Create; LB:= TStringList.Create; LC:= TStringList.Create; Panel1.Caption :='';end;

end.

Create by nova elins03 29

top related