Article...

Tampilkan postingan dengan label Pascal. Tampilkan semua postingan
Tampilkan postingan dengan label Pascal. Tampilkan semua postingan

Jumat, 06 Juni 2008

Crypto

{
Agung Fitriyanto 7644;
Firman Maulana 7666;
Tri Danarto 7806;
}

program kripto;
uses crt;
var kata1,kata2,kata3,sandi1,sandi2,terj1,terj2,bantu:string;
a,b,c:char;
i,j,k,l:integer;
jalan:boolean;

procedure caribinernya(var tempat:string;x:integer);
{Procedure untuk mengubah suatu nilai integer ke bilangan biner 8 digit
dengan format string (karena tidak ada format biner dalam pascal)untuk
kemudian dikodekan}
begin
if x>127 then begin tempat:=tempat+'1';x:=x-128 end else tempat:=tempat+'0';
if x>63 then begin tempat:=tempat+'1';x:=x-64 end else tempat:=tempat+'0';
if x>31 then begin tempat:=tempat+'1';x:=x-32 end else tempat:=tempat+'0';
if x>15 then begin tempat:=tempat+'1';x:=x-16 end else tempat:=tempat+'0';
if x>7 then begin tempat:=tempat+'1';x:=x-8 end else tempat:=tempat+'0';
if x>3 then begin tempat:=tempat+'1';x:=x-4 end else tempat:=tempat+'0';
if x>1 then begin tempat:=tempat+'1';x:=x-2 end else tempat:=tempat+'0';
if x=1 then begin tempat:=tempat+'1';x:=x-1 end else tempat:=tempat+'0';
end;

procedure cariintegernya(x:string;var y:integer);
{Procedure untuk mengubah suatu nilai biner (dalam variabel berbentuk string)
ke suatu nilai integernya dengan basis 10}
var anu:integer;
begin
y:=0;
if x[1]='1' then y:=y+128;
if x[2]='1' then y:=y+64;
if x[3]='1' then y:=y+32;
if x[4]='1' then y:=y+16;
if x[5]='1' then y:=y+8;
if x[6]='1' then y:=y+4;
if x[7]='1' then y:=y+2;
if x[8]='1' then y:=y+1;
end;

procedure acak(x:string;var y:string);
{Procedure untuk pengkodean dengan subsitusi}
begin
if x='000' then y:='010';
if x='001' then y:='011';
if x='010' then y:='100';
if x='011' then y:='101';
if x='100' then y:='110';
if x='101' then y:='111';
if x='110' then y:='000';
if x='111' then y:='001';
end;

procedure acak2(x:string;var y:string);
{Procedure untuk penerjemahan balik dari suatu kode}
begin
if x='000' then y:='110';
if x='001' then y:='111';
if x='010' then y:='000';
if x='011' then y:='001';
if x='100' then y:='010';
if x='101' then y:='011';
if x='110' then y:='100';
if x='111' then y:='101';
end;

begin
clrscr;
kata1:='';
while kata1='' do
begin
write(' Kata yang mau diacak = ');{Kata yang mau dikodekan}
readln(kata1);
end;

jalan:=true;
while jalan do
begin
a:=kata1[1];{ambil karakter terdepan}
i:=ord(a);{cari nilai ASCII-nya}
caribinernya(sandi1,i);{ubah ke bentuk biner 8 digit}
while length(sandi1)>=3 do {dipotong per-3 untuk dikodekan}
begin
terj1:=sandi1[1]+sandi1[2]+sandi1[3];{ambil 3 karakter terdepan}
acak(terj1,terj2);{pengkodean tiga karakter tadi}
sandi2:=sandi2+terj2; {hasil pengkodean disimpan di sandi2}
delete(sandi1,1,3);{tiga karakter terdepan yang sudah dikodekan dihapus}
end;

while length(sandi2)>=8 do {hasil pengkodean (sandi2) di terjemahkan ke
kode ASCII kembali per 8 digit (1 karakter)}
begin
bantu:='';
for j:=1 to 8 do bantu:=bantu+sandi2[j];{8 karakter terdepan diambil}
cariintegernya(bantu,k);{8 digit bilangan dicari nilai integernya}
kata2:=kata2+char(k);{hasil pengkodean di masukkan ke kata2}
delete(sandi2,1,8);{8 karakter yang sudah dikodekan dihapus}
end;
delete(kata1,1,1);{hapus karakter terdepan dari kata1}
if kata1='' then jalan:=false;{perulangan berhenti jika kata sudah habis}
end;

{Percabangan di bawah ini digunakan jika ternyata jumlah karakter yang
mau dikodekan bukan kelipatan 3 sehingga ada kekurangn digit pada
terjemahan per-3 digit}
if sandi1<>'' then {Sandi1 masih menyisakan karakter yang belum dikodekan}
begin
l:=0;
while length(sandi1)<3 do
begin
sandi1:=sandi1+'0';
l:=l+1;{l merupakan jumlah karakter yang ditambahkan agar genap 3 digit}
end;
terj1:=sandi1;sandi1:='';
acak(terj1,terj2);
sandi2:=sandi2+terj2;
if length(sandi2)>8 then delete(sandi2,9,l);{setelah dikodekan sandi2
dipotong sebanyak penambahannya}
cariintegernya(sandi2,k);
kata2:=kata2+char(k);
end;
writeln(' Kode yang dihasilkan = ',kata2); {Kode final}
readln;
{Untuk jumlah karakter kata1 yang tidak kelipatan 3 maka akan mengakibatkan
sandi1 sebagai variabel penyimpan bilangan-bilangan biner yang akan dikodekan
masih menyimpan beberapa digit sisa, sehingga harus ditambahkan suatu bilangan
ada 2 kemungkinan pada terjemahan per-3 yaitu kurang 1 digit atau 2 digit.
Untuk itu ditambahkan suatu bilangan di belakangnya sehingga:
xx0 --> 000 menjadi 010 atau 00_ menjadi 01_
010 menjadi 100 atau 01_ menjadi 10_
100 menjadi 110 atau 10_ menjadi 11_
110 menjadi 000 atau 11_ menjadi 00_
x00 --> 000 menjadi 010 atau 0__ menjadi 0__
100 menjadi 110 atau 1__ menjadi 1__
kemudian setelah pengacakan 3 digit bilangan tersebut pada penerjemahan ke
bilangan ASCII dipotong kembali pada digit belakang sesuai jumlah penambahan

Sedangkan untuk penerjemahan balik dari suatu kode maka tentunya akan mengalami
kendala yang sama, tapi pada digit yang kurang tersebut ditambahkan bilagan 1
sehingga :
xx1 --> 001 dibaca 111 atau 00_ menjadi 11_
011 dibaca 001 atau 01_ menjadi 00_
101 dibaca 011 atau 10_ menjadi 01_
111 dibaca 101 atau 11_ menjadi 10_
x11 --> 011 dibaca 001 atau 0__ menjadi 0__
111 dibaca 101 atau 1__ menjadi 1__

Dengan metode diatas terjadi kesesuaian dengan penambahan bilangan 0 pada
saat pengkodean.
Untuk membuktikan hal tersebut di bawah ini dibuat sebuah pengkodean balik
dari kode yang sudah dibuat diatas dengan cara seperti yang sudah disebutkan

}

jalan:=true;sandi1:='';sandi2:='';
while jalan do
begin
a:=kata2[1];
i:=ord(a);
caribinernya(sandi1,i);
while length(sandi1)>=3 do
begin
terj1:=sandi1[1]+sandi1[2]+sandi1[3];
acak2(terj1,terj2);
sandi2:=sandi2+terj2;
delete(sandi1,1,3);
end;

while length(sandi2)>=8 do
begin
bantu:='';
for j:=1 to 8 do bantu:=bantu+sandi2[j];
cariintegernya(bantu,k);
kata3:=kata3+char(k);
delete(sandi2,1,8);
end;
delete(kata2,1,1);
if kata2='' then jalan:=false;
end;

if sandi1<>'' then
begin
l:=0;
while length(sandi1)<3 do
begin
sandi1:=sandi1+'1';
l:=l+1;
end;
terj1:=sandi1;sandi1:='';
acak2(terj1,terj2);
sandi2:=sandi2+terj2;
if length(sandi2)>8 then delete(sandi2,9,l);
cariintegernya(sandi2,k);
kata3:=kata3+char(k);
end;
writeln(' Kode yang dibalikkan = ',kata3);
readln;
end.

Sabtu, 02 Februari 2008

Is it possible to write in a Pascal subset that will be acceptable to both ISO 7185 Pascal and Delphi?

Sure. You can use the ISO 7185 "level 0" Pascal with the following omissions:

1. Do not use procedure or function parameters. These have different syntax in ISO 7185 and Delphi.

2. Do not use intraprocedural gotos (gotos that leave the current procedure or function). If you need a deep nested bailout to a higher level procedure, try setting an error variable and checking that after each procedure/function call that might have an error, then skipping either out of the routine, or to the goto point.

3. Do not use file buffer handling (such as f^ accesses, where f is a file), nor the built in routines "get" and "put". Basically, this just means you cannot use the lookahead buffering that ISO 7185 Pascal provides.

4. Do not size your variant records with "new". Delphi knows about variant records, but not how to size them (at least not the standard way). This will have no functional effect on your program, it will just take more runtime space.

5. Always use the keyword "packed" on a string that is intended to be printed with "writeln". Delphi does not require this, but ISO 7185 does.

6. Always include the files "input" and/or "output" in the program header if they are used in the program (and remember that write/read with no file parameter is a defacto reference). Delphi simply ignores these header parameters, so they don't hurt.

7. Always use format specifications to set the width that will be output for integers. This will remove differences from varying default fields.

8. Don't use external files other than input and output, since most Delphi does nothing with header files).

9. Borland Delphi has the requirement that an

{$APPTYPE CONSOLE}

must exist at the top after the program header. If your non-Delphi compiler ignores this as a comment, then you can add it to also be compatible with Delphi. If your ISO 7185 compiler also treats "$" in a comment as an option, you are going to have to be prepared to remove it as need be.

Finally, note that you MUST also comply with the ISO 7185 standard requirements for this to work. For example, ISO 7185 forbids gotos into program structures like "for" loops, etc, whereas delphi does not. There are many other such restrictions of ISO 7185 that are relaxed in Delphi, not to mention the many Delphi extensions that you need to refrain from using (of course, all Pascal compilers have extensions, so that would apply to them as well). Also remember: Pascal originally had no "string" type. This might seem strange, but C (for example) does not have one either. Instead, you use an array of characters (which is not the same thing!). See other sections of this FAQ.

The old bit of cross compiler checking that applies here is to run a check compile on all of the different compilers you plan to be compatible with frequently. This will tell you about problems with different compilers before they get out of hand.

For this author's point of view, I lived with mutiple Pascal compilers for years, and made it work by isolating system specific code to modules that implemented that on a particular platform. For example, I had a set of routines in a module I called "basicio.pas" that contained things like opentext(f, filename), closetext(f) and similar functions. Then, this module is simply recoded or swapped out to move to a different compiler.

Jumat, 16 November 2007

Learn Pascal Programming Tutorial Lesson 2 - Colors, Coordinates, Windows and Sound

Colors

To change the color of the text printed on the screen we use the TextColor command.

program Colors;

uses
crt;

begin
TextColor(Red);
Writeln('Hello');
TextColor(White);
Writeln('world');
end.

The TextBackground command changes the color of the background of text. If you want to change the whole screen to a certain color then you must use ClrScr.

program Colors;

uses
crt;

begin
TextBackground(Red);
Writeln('Hello');
TextColor(White);
ClrScr;
end.

Screen coordinates

You can put the cursor anywhere on the screen using the GoToXY command. In DOS, the screen is 80 characters wide and 25 characters high. The height and width varies on other platforms. You may remember graphs from Maths which have a X and a Y axis. Screen coordinates work in a similar way. Here is an example of how to move the cursor to the 10th column in the 5th row.

program Coordinates;

uses
crt;

begin
GoToXY(10,5);
Writeln('Hello');
end.

Windows

Windows let you define a part of the screen that your output will be confined to. If you create a window and clear the screen it will only clear what is in the window. The Window command has 4 parameters which are the top left coordinates and the bottom right coordinates.

program Coordinates;

uses
crt;

begin
Window(1,1,10,5);
TextBackground(Blue);
ClrScr;
end.

Using window(1,1,80,25) will set the window back to the normal size.

Sound

The Sound command makes a sound at the frequency you give it. It does not stop making a sound until the NoSound command is used. The Delay command pauses a program for the amount of milliseconds you tell it to. Delay is used between Sound and NoSound to make the sound last for a certain amount of time.

program Sounds;

uses
crt;

begin
Sound(1000);
Delay(1000);
NoSound;
end.

Learn Pascal Programming Tutorial Lesson 1 - Introduction to Pascal

About Pascal

The Pascal programming language was created by Niklaus Wirth in 1970. It was named after Blaise Pascal, a famous French Mathematician. It was made as a language to teach programming and to be reliable and efficient. Pascal has since become more than just an academic language and is now used commercially.

What you will need

Before you start learning Pascal, you will need a Pascal compiler. This tutorial uses the Free Pascal Compiler. You can find a list of other Pascal compilers at TheFreeCountry's Pascal compiler list.

Your first program

The first thing to do is to either open your IDE if your compiler comes with one or open a text editor.

We always start a program by typing its name. Type program and the name of the program next to it. We will call our first program "Hello" because it is going to print the words "Hello world" on the screen.

program Hello;

Next we will type begin and end. We are going to type the main body of the program between these 2 keywords. Remember to put the full stop after the end.

program Hello;

begin
end.

The Write command prints words on the screen.

program Hello;

begin
Write('Hello world');
end.

You will see that the "Hello world" is between single quotes. This is because it is what is called a string. All strings must be like this. The semi-colon at the end of the line is a statement separator. You must always remember to put it at the end of the line.

The Readln command will now be used to wait for the user to press enter before ending the program.

program Hello;

begin
Write('Hello world');
Readln;
end.

You must now save your program as hello.pas.

Compiling

Our first program is now ready to be compiled. When you compile a program, the compiler reads your source code and turns it into an executable file. If you are using an IDE then pressing CTRL+F9 is usually used to compile and run the program. If you are compiling from the command line with Free Pascal then enter the following:

fpc hello.pas

If you get any errors when you compile it then you must go over this lesson again to find out where you made them. IDE users will find that their programs compile and run at the same time. Command line users must type the name of the program in at the command prompt to run it.

You should see the words "Hello world" when you run your program and pressing enter will exit the program. Congratulations! You have just made your first Pascal program.

More commands

Writeln is just like Write except that it moves the cursor onto the next line after it has printed the words. Here is a program that will print "Hello" and then "world" on the next line:

program Hello;

begin
Writeln('Hello');
Write('world');
Readln;
end.

If you want to skip a line then just use Writeln by itself without any brackets.

Using commands from units

The commands that are built into your Pascal compiler are very basic and we will need a few more. Units can be included in a program to give you access to more commands. The crt unit is one of the most useful. The ClrScr command in the crt unit clears the screen. Here is how you use it:

program Hello;

uses
crt;

begin
ClrScr;
Write('Hello world');
Readln;
end.

Comments

Comments are things that are used to explain what parts of a program do. Comments are ignored by the compiler and are only there for the people who use the source code. Comments must be put between curly brackets. You should always have a comment at the top of your program to say what it does as well as comments for any code that is difficult to understand. Here is an example of how to comment the program we just made:

{This program will clear the screen, print "Hello world" and wait for the user to press enter.}

program Hello;

uses
crt;

begin
ClrScr;{Clears the screen}
Write('Hello world');{Prints "Hello world"}
Readln;{Waits for the user to press enter}
end.

Indentation

You will notice that I have put 3 spaces in front of some of the commands. This is called indentation and it is used to make a program easier to read. A lot of beginners do not understand the reason for indentation and don't use it but when we start making longer, more complex programs, you will understand.

My Headlines