Selasa, 22 Maret 2011

Perulangan While Pada C++ 8

#include <iostream.h>
#include <conio.h>
main()
{
clrscr;
int n,p,o;
o=1;
p=1;
cout<< "input n: ";
cin>> n;
while (o<=n)
{
p=1;
while (p<=o)
{
cout<<"*";
p++;
}
cout<<endl;
o++;
}
n--;
while (n>=1)
{
p=n;
while (p>=1)
{
cout<<"*";
p--;
}
cout<<endl;
n--;
}
getch();
return 0;
}

Perulangan While Pada C++ 7

#include <iostream.h>
#include <conio.h>
main()
{
clrscr;
int n,o,p;
o=1;
cout<< "input n: ";
cin>> n;
while (o<=n)
{
p=1;
while (p<=o)
{
cout<<"*";
p++;
}
cout<<endl;
o++;
}
getch();
return 0;
}

Perulangan While Pada C++ 6

#include <iostream.h>
#include <conio.h>
main()
{
clrscr;
int n,o,p;
o=1;
cout<< "input n: ";
cin>> n;
while (o<=n)
{
p=1;
while (p<=o)
{
cout<<p;
p++;
}
cout<<endl;
o++;
}
getch();
return 0;
}

Perulangan While Pada C++ 5

#include <iostream.h>
#include <conio.h>
main()
{
clrscr;
int n;
cout<< "input n: ";
cin>> n;
while (n<9)
{
cout<<n<<",";
n++;
}
cout<<"fire"<<endl;
getch();
return 0;
}

Perulangan While Pada C++ 4

#include <iostream.h>
#include <conio.h>
main()
{
clrscr;
int n;
cout<< "input n: ";
cin>> n;
while (n>0)
{
cout<<n<<",";
--n;
}
cout<<"fire"<<endl;
getch();
return 0;
}

Perulangan While Pada C++ 3

#include <iostream.h>
#include <conio.h>
main()
{
clrscr;
int n,i;
i=1;
cout<< "input n: ";
cin>> n;
while (i<n)
{
cout<<i<<",";
i+=2;
}
cout<<"fire"<<endl;
getch();
return 0;
}

Perulangan While Pada C++ 2

#include <iostream.h>
#include <conio.h>
int main()
{
clrscr;
int n,i;
i=0;
cout<< "input n: ";
cin>> n;
while (i<n)
{
i+=2;
cout<<i<<",";
}
cout<<"fire"<<endl;
getch();
return 0;
}

Perulangan While Pada C++

#include <iostream.h>
#include <conio.h>
main()
{
clrscr;
int n,i;
i=1;
cout<< "input n: ";
cin>> n;
while (i<=n)
{
cout<<i<<",";
i++;
}
cout<<"fire"<<endl;
getch();
return 0;
}

Rabu, 09 Maret 2011

listing infix posfix, pascal

Program InFixToPostFix;



Label finis;

TYPE link = ^node; { link is a pointer to a node }
node = record
nxt:link; { points to next node (or nil) }
dat:string[12]; {length of 12 is arbitrary}
end;
VAR
head, p1, p2, op:link;
s, postfix:string;

Procedure MakeWrdList(ss:string);
VAR
wrd:string[12]; { 12 is arbitrary }
s1, s2, len:integer;
pt1, pt2:link;

Begin
pt1 := nil;
s1 := 1;
len := Length(ss);

While s1 < len do
Begin
{ skip spaces }
While (ss[s1] = ' ') AND (s1 < len) Do Inc(s1);
s2 := s1; {start of word}
{ parse to next space }
While (ss[s2] <> ' ') AND (s2 <= len) Do Inc(s2);
wrd := Copy(ss, s1, s2 - s1); {extract wrd sans spaces}
s1 := s2; {advance string index}

pt2 := pt1; {initially pt2 to nil, normally move down list}
new(pt1) ; {get address for pt1 from heap}
if pt2 = nil then head := pt1; {head-->first node}
pt2^.nxt := pt1; {links old node to new}
pt1^.nxt := nil; {last node in list points to nil}
pt1^.dat := wrd; {stores wrd in node}
End;

{ After above: pt1 and pt2 no longer used
head-->[arg1]-->[op1]-->[arg2]-->[ .... ]-->nil }
End;

Procedure ShowList;
VAR tmp:link;
Begin
tmp := head;
postfix := '';
While tmp <> nil do
begin
postfix := postfix + tmp^.dat + ' '; {concatanate string}
tmp := tmp^.nxt; {traverse the list head to tail}
end;
Writeln(postfix);
End;

Procedure InsertOp;
{Inserts Op node(s) into PostFix linked list}
Begin
p1^.nxt := op; {insert op, the last op node points to nil}
While p1^.nxt <> nil do p1 := p1^.nxt;
p1^.nxt := p2^.nxt; {remove p2 node from list}
p1 := p1^.nxt; {last node of prev op now linked to list}
op := p2; {new op becomes p2}
op^.nxt := nil;
p2 := p1; {both now point to next argument}
End;

Procedure ExtendOp;
{Extracts math symbol node from PostFix list and extends Op list}
Begin
p1^.nxt := p2^.nxt; {remove p2 from list}
p1 := p1^.nxt; {relink arg-->arg}
p2^.nxt := op; {place p2 in front of old op}
op := p2; {now op linked list has 2 nodes}
p2 := p1 ; {both now point to next argument}
End;

Procedure DoPostFix(st:string);
Const
Hi = ['*', '/']; {Hi, Lo are math precedence rank of symbols}
Lo = ['-', '+'];

Begin
MakeWrdList(st);
p1 := head; {After this initialization, }
op := nil; {p1, p2, arg1 --> arg2 }
p2 := p1^.nxt; {op --> op1 --> nil }
ExtendOp;

While p2^.nxt <> nil Do {last node points to nil}
Begin
p2 := p2^.nxt; {p2 now pointing to math operation}
{Conditional char comparisons follow}
If (op^.dat[1] in Hi) OR (p2^.dat[1] in Lo) then
InsertOp
Else ExtendOp;
End;
p1^.nxt := op; {links final math operation(s)}
End;

BEGIN {main program}

Writeln('Just press Enter to quit.'); Writeln;
{ Example }
s := '3 - 6 * 7 + 2 / 4 * 5 - 8';

DoPostFix(s);
Writeln('In postfix notation, the infix string:');
Writeln(s, ' becomes:');
ShowList;
Repeat
Writeln;
Writeln('Infix math string (spaces between everything):');
Readln(s);
If Length(s) < 5 then goto finis;

DoPostFix(s);
ShowList;
Until Length(s) < 5;

finis:
END.

Rabu, 02 Maret 2011

STACK (STRUKTUR DATA)

Dalam ilmu komputer, stack atau tumpukan merupakan sebuah koleksi objek yang menggunakan prinsip LIFO (Last In First Out), yaitu data yang terakhr kali dimasukkan akan pertama kali keluar dari stack tersebut. Stack dapat diimplementasikan sebagai representasi berkait atau kontigu (dengan tabel fix).
Stack/Tumpukan adalah bentuk khusus dari list linear. Pada stack, penghapusan serta pemasukan elemennya hanya dapat dilakukan pada satu posisi, yakni posisi akhir dari list. Posisi ini disebut puncak atau Top dari stack. Elemen stack S pada posisi ini dinyatakan dengan TOP(S).
       Jelasnya, bila stack S = [A, B, C, D, E], maka TOP(S) adalah E. banyaknya elemen pada stack S pada suatu saat tertentu biasa kita sebut sebagai NOEL(S). Jadi untuk stack diatas NOEL(S) = 5.
       Seperti halnya link list, pada stack juga terdapat operasi penghapusan dan pemasukkan elemen list. Operator penghapusan pada stack kita kenal dengan nama POP, sedangkan operator pemasukkan elemen disebut PUSH. Berikut ini merupakan ilustrasi dari penggunaan operator-operator diatas yang dimulai dari sebuah stack S kosong,
S = [ ].
 
Ciri Stack :
  • Elemen TOP (puncak) diketahui
  • penisipan dan penghapusan elemen selalu dilakukan di TOP
  • LIFO
Pemanfaatan Stack :
  • Perhitungan ekspresi aritmatika (posfix)
  • algoritma backtraking (runut balik)
  • algoritma rekursif
Operasi Stack yang biasanya :
  1. Push (input E : typeelmt, input/output data : stack): menambahkan sebuah elemen ke stack
  2. Pop (input/output data : stack, output E : typeelmt ) : menghapus sebuah elemen stack
  3. IsEmpty ()
  4. IsFull ()
  5. dan beberapas selektor yang lain