Tuesday, December 13, 2011

program interface jam dalam c#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication33
{
    interface jamdunia
    {
        void jam();
    }
    class denpasar : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.ToLongTimeString();
            Console.WriteLine("jam sekarang di denpasar adalah {0}",a);
        }
    }
    class london : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.AddHours(-8).ToLongTimeString();
            Console.WriteLine("jam sekarang di london adalah {0}",a);
        }
    }
    class greenwich : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.AddHours(-8).ToLongTimeString();
            Console.WriteLine("jam sekarang di greenwich adalah {0}", a);
        }
    }
    class washington : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.AddHours(-14).ToLongTimeString();
            Console.WriteLine("jam sekarang di london adalah {0}", a);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            jamdunia waktu;
            Console.WriteLine("pilih lokasi = ");
            Console.WriteLine(" 1. denpasar");
            Console.WriteLine(" 2. greenwich");
            Console.WriteLine(" 3. london");
            Console.WriteLine(" 4. washington");
            Console.Write(" Pilih ");
            int a = int.Parse(Console.ReadLine());
            if (a == 1)
            {
                waktu = new denpasar();
                waktu.jam();
            }
            else
                if (a == 2)
                {
                    waktu = new greenwich();
                    waktu.jam();
                }
                else
                    if (a == 3)
                    {
                        waktu = new london();
                        waktu.jam();
                    }
                    else
                        if (a == 4)
                        {
                            waktu = new washington();
                            waktu.jam();
                        }
                        else
                            Console.WriteLine("pilihan salah");
        }
    }
}

program interface tanggal dan jam versi 2 dalam c#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication33
{
    interface jamdunia
    {
        void jam();
    }
    class denpasar : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.ToString();
            string[] b = a.Split(' ');
            Console.WriteLine("waktu denpasar = ");
            Console.WriteLine("tanggal = {0}", b[0]);
            Console.WriteLine("jam     = {0}", b[1]);
        }
    }
    class london : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.AddHours(-08).ToString();
            string[] b = a.Split(' ');
            Console.WriteLine("waktu london = ");
            Console.WriteLine("tanggal = {0}", b[0]);
            Console.WriteLine("jam     = {0}", b[1]);
        }
    }
    class greenwich : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.AddHours(-08).ToString();
            string[] b = a.Split(' ');
            Console.WriteLine("waktu greenwich = ");
            Console.WriteLine("tanggal = {0}", b[0]);
            Console.WriteLine("jam     = {0}", b[1]);
        }
    }
    class washington : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.AddHours(-14).ToString();
            string[] b = a.Split(' ');
            Console.WriteLine("waktu washington = ");
            Console.WriteLine("tanggal = {0}", b[0]);
            Console.WriteLine("jam     = {0}", b[1]);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            jamdunia waktu;
            Console.WriteLine("pilih lokasi = ");
            Console.WriteLine(" 1. denpasar");
            Console.WriteLine(" 2. greenwich");
            Console.WriteLine(" 3. london");
            Console.WriteLine(" 4. washington");
            Console.Write(" Pilih ");
            int a = int.Parse(Console.ReadLine());
            if (a == 1)
            {
                waktu = new denpasar();
                waktu.jam();
            }
            else
                if (a == 2)
                {
                    waktu = new greenwich();
                    waktu.jam();
                }
                else
                    if (a == 3)
                    {
                        waktu = new london();
                        waktu.jam();
                    }
                    else
                        if (a == 4)
                        {
                            waktu = new washington();
                            waktu.jam();
                        }
                        else
                            Console.WriteLine("pilihan salah");
        }
    }
}

program interface tanggal dan jam dalam c#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication33
{
    interface jamdunia
    {
        void jam();
    }
    class denpasar : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.ToString();
            Console.WriteLine("waktu denpasar = {0} ", a);
        }
    }
    class london : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.AddHours(-08).ToString();
            Console.WriteLine("waktu london = {0} ", a);
        }
    }
    class greenwich : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.AddHours(-08).ToString();
            Console.WriteLine("waktu greenwich = {0} ", a);
        }
    }
    class washington : jamdunia
    {
        public void jam()
        {
            string a = DateTime.Now.AddHours(-14).ToString();
            Console.WriteLine("waktu washington = {0} ", a);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            jamdunia waktu;
            Console.WriteLine("pilih lokasi = ");
            Console.WriteLine(" 1. denpasar");
            Console.WriteLine(" 2. greenwich");
            Console.WriteLine(" 3. london");
            Console.WriteLine(" 4. washington");
            Console.Write(" Pilih ");
            int a = int.Parse(Console.ReadLine());
            if (a == 1)
            {
                waktu = new denpasar();
                waktu.jam();
            }
            else
                if (a == 2)
                {
                    waktu = new greenwich();
                    waktu.jam();
                }
                else
                    if (a == 3)
                    {
                        waktu = new london();
                        waktu.jam();
                    }
                    else
                        if (a == 4)
                        {
                            waktu = new washington();
                            waktu.jam();
                        }
                        else
                            Console.WriteLine("pilihan salah");
        }
    }
}

Thursday, December 8, 2011

procedure bubble sort versi boolean dalam notasi algoritmik


procedure bubbleshort (m:array[1..20] of integer, input n:integer)
{Mengurutkan isi dari tabel m dari kecil ke besar dengan metode pengurutan bubble short}
Kamus
i,j,temp:integer {variable perulangan}
cek:boolean

Algoritma
i <- 1
cek <- true
while (i <= n-1) and (cek=true) do
cek <- false
for j <- n downto i+1
if m[j] < m[j-1] then
temp <- m[j]
m[j] <- m[j-1]
m[j-1] <- temp
cek <- true
endif
endfor
endwhile
i <- i+1

procedure bubble sort dalam notasi algoritmik


procedure bubbleshort (m:array[1..20] of integer, input n:integer)
{Mengurutkan isi dari tabel m dari kecil ke besar dengan metode pengurutan bubble short}

Kamus
i,j:integer {variable perulangan}

Algoritma
for i = 1 to n-1 do
for j = i+1 to n do
if m[j]< m[j+1] then
temp ← m[j]
m[j] ← m[j+1]
m[j+1] ← temp
end if
end for
end for

procedure selection sort dalam notasi algoritmik


Procedure SelectionSort (data : array[1..100] of integer; input n:integer)
{ Mengurutkan data [1..100] dengan algoritma Selection Sort.}

Kamus
i, j, temp, min : integer

Algoritma:
for i= 1 to n-1 do {notasi for}
min ← i
for j = i+1 to n do
if data[min] > data[j] then
temp ← data[j]
endif
endfor
temp ← data[i]
data[i] ← data[j]
data[j] ← temp
end for

Tuesday, December 6, 2011

program student dengan generic class dalam C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication35
{
    class student<T, C>
    {
        C name;
        T grade;
        public student(C name, T grade)
        {
            this.name = name;
            this.grade= grade;
        }
        public override string ToString()
        {
            return string.Format(" name : {0} \n grade : {1} ", name,grade);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            student<int, string> s = new student<int, string>("budi", 1);
            student<int, int> a = new student<int, int>(600, 1);
            Console.WriteLine(s);
            Console.WriteLine(a);
        }
    }
}

program saluran televisi versi 2 dengan C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication33
{
    interface acaratv
    {
        void berita();
        void olahraga();
        void movie();
        void hiburan();
    }
    class antv : acaratv
    {
        public void berita()
        {
            Console.WriteLine("siaran berita antv");
        }
        public void olahraga()
        {
            Console.WriteLine("siaran lensa olah raga");
        }
        public void movie()
        {
            Console.WriteLine("siaran movie antv");
        }
        public void hiburan()
        {
            Console.WriteLine("siaran hiburan antv");
        }
    }
    class trans7 : acaratv
    {
        public void berita()
        {
            Console.WriteLine("siaran berita trans7");
        }
        public void olahraga()
        {
            Console.WriteLine("siaran olah raga tran7");
        }
        public void movie()
        {
            Console.WriteLine("siaran boiskop trans7");
        }
        public void hiburan()
        {
            Console.WriteLine("siaran hiburan trans7");
        }
    }
    class rcti : acaratv
    {
        public void berita()
        {
            Console.WriteLine("siaran berita rcti");
        }
        public void olahraga()
        {
            Console.WriteLine("siaran lensa olah rcti");
        }
        public void movie()
        {
            Console.WriteLine("siaran movie rcti");
        }
        public void hiburan()
        {
            Console.WriteLine("siaran hiburan rcti");
        }
    }
    class sctv : acaratv
    {
        public void berita()
        {
            Console.WriteLine("siaran berita sctv");
        }
        public void olahraga()
        {
            Console.WriteLine("siaran olah raga sctv");
        }
        public void movie()
        {
            Console.WriteLine("siaran movie sctv");
        }
        public void hiburan()
        {
            Console.WriteLine("siaran hiburan sctv");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            acaratv tv;
            while (true)
            {
                Console.WriteLine("Program Acara TV");
                Console.WriteLine("pilih saluran = ");
                Console.WriteLine(" 1.  ANTV");
                Console.WriteLine(" 2.  Trans7");
                Console.WriteLine(" 3.  RCTI");
                Console.WriteLine(" 4.  SCTV");
                Console.WriteLine(" 5.  exit");
                Console.Write(" Pilih ");
                int a = int.Parse(Console.ReadLine());
                switch (a)
                {
                    case 1:
                        {
                            tv = new antv();
                            tv.berita();
                            tv.olahraga();
                            tv.movie();
                            tv.hiburan();
                            break;
                        }
                    case 2:
                        {
                            tv = new trans7();
                            tv.berita();
                            tv.olahraga();
                            tv.movie();
                            tv.hiburan();
                            break;
                        }
                    case 3:
                        {
                            tv = new rcti();
                            tv.berita();
                            tv.olahraga();
                            tv.movie();
                            tv.hiburan();
                            break;
                        }
                    case 4:
                        {
                            tv = new sctv();
                            tv.berita();
                            tv.olahraga();
                            tv.movie();
                            tv.hiburan();
                            break;
                        }
                    case 5:
                        {
                            return;
                        }
                }
            }
        }
    }
}

program saluran televisi dengan C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication33
{
    interface acaratv
    {
        void berita();
        void olahraga();
        void movie();
        void hiburan();
    }
    class antv : acaratv
    {
        public void berita()
        {
            Console.WriteLine("siaran berita antv");
        }
        public void olahraga()
        {
            Console.WriteLine("siaran lensa olah raga");
        }
        public void movie()
        {
            Console.WriteLine("siaran movie antv");
        }
        public void hiburan()
        {
            Console.WriteLine("siaran hiburan antv");
        }
    }
    class trans7 : acaratv
    {
        public void berita()
        {
            Console.WriteLine("siaran berita trans7");
        }
        public void olahraga()
        {
            Console.WriteLine("siaran olah raga tran7");
        }
        public void movie()
        {
            Console.WriteLine("siaran boiskop trans7");
        }
        public void hiburan()
        {
            Console.WriteLine("siaran hiburan trans7");
        }
    }
    class rcti : acaratv
    {
        public void berita()
        {
            Console.WriteLine("siaran berita rcti");
        }
        public void olahraga()
        {
            Console.WriteLine("siaran lensa olah rcti");
        }
        public void movie()
        {
            Console.WriteLine("siaran movie rcti");
        }
        public void hiburan()
        {
            Console.WriteLine("siaran hiburan rcti");
        }
    }
    class sctv : acaratv
    {
        public void berita()
        {
            Console.WriteLine("siaran berita sctv");
        }
        public void olahraga()
        {
            Console.WriteLine("siaran olah raga sctv");
        }
        public void movie()
        {
            Console.WriteLine("siaran movie sctv");
        }
        public void hiburan()
        {
            Console.WriteLine("siaran hiburan sctv");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            acaratv tv;
            Console.WriteLine("Program Acara TV");
            Console.WriteLine("pilih saluran = ");
            Console.WriteLine(" 1.  ANTV");
            Console.WriteLine(" 2.  Trans7");
            Console.WriteLine(" 3.  RCTI");
            Console.WriteLine(" 4.  SCTV");
            Console.Write(" Pilih ");
            int a = int.Parse(Console.ReadLine());
            if (a == 1)
            {
                tv = new antv();
                tv.berita();
                tv.olahraga();
                tv.movie();
                tv.hiburan();
            }
            else
                if (a == 2)
                {
                    tv = new trans7();
                    tv.berita();
                    tv.olahraga();
                    tv.movie();
                    tv.hiburan();
                }
                else
                    if (a == 3)
                    {
                        tv = new rcti();
                        tv.berita();
                        tv.olahraga();
                        tv.movie();
                        tv.hiburan();
                    }
                    else
                        if (a == 4)
                        {
                            tv = new sctv();
                            tv.berita();
                            tv.olahraga();
                            tv.movie();
                            tv.hiburan();
                        }
                        else
                            Console.WriteLine("tidak ada saluran");
        }
    }
}

program operator seluler dalam C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication33
{
    interface simcard
    {
        void call();
        void sms();
    }
    class xl : simcard
    {
        public void call()
        {
            Console.WriteLine("calling from xl");
        }
        public void sms()
        {
            Console.WriteLine("sms from xl");
        }
    }
    class telkomsel : simcard
    {
        public void call()
        {
            Console.WriteLine("calling from telkomsel");
        }
        public void sms()
        {
            Console.WriteLine("sms from telkomsel");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            simcard sim;
            Console.WriteLine("pilih operator = ");
            Console.WriteLine(" 1. xl");
            Console.WriteLine(" 2. telkomsel");
            Console.Write(" Pilih ");
            int a = int.Parse(Console.ReadLine());
            if (a == 1)
            {
                sim = new xl();
                sim.call();
                sim.sms();
            }
            else
            if (a==2)
            {
                sim = new telkomsel();
                sim.call();
                sim.sms();
            }
        }
    }
}

program bank inheritance versi 3 dalam c#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    abstract class bank
    {
        static int amount;
        public void deposit()
        {
            int amt = int.Parse(Console.ReadLine());
            amount += amt;
        }
        public void withdraw()
        {
            int amt = int.Parse(Console.ReadLine());
            if (amount < amt)
            {
                Console.WriteLine("Saldo Kurang");
            }
            else
            {
                amount -= amt;
            }
        }
        public void cek_saldo()
        {
            Console.WriteLine("Jumlah Saldo Anda = {0}", amount);
        }
    }
    class BNI : bank
    {
        public void jumlah_rekening_dari_nasabah()
        {
            base.deposit();
            base.withdraw();
            base.cek_saldo();
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Jumlah Nasabah = ");
            int n_nasabah = int.Parse(Console.ReadLine());
            BNI[] nasabah = new BNI[n_nasabah + 1];
            for (int i = 1; i <= n_nasabah; i++)
            {
                nasabah[i] = new BNI();
            }

            while (true)
            {
                Console.WriteLine("Pilihan : ");
                Console.WriteLine("     1. Menabung");
                Console.WriteLine("     2. Mengambil Uang");
                Console.WriteLine("     3. Cek_Saldo");
               
                Console.WriteLine("     4. keluar");
                Console.Write("Pilih : ");
                int pilih = int.Parse(Console.ReadLine());
                int no_rek;
               
                switch (pilih)
                {
                    case 1:
                        {
                            Console.Write("Nomor Rekening Anda : ");
                            no_rek = int.Parse(Console.ReadLine());
                            Console.Write("Jumlah Uang ");
                            BNI obj = new BNI();
                            obj.deposit();
                            break;
                        }
                    case 2:
                        {
                            Console.Write("Nomor Rekening Anda : ");
                            no_rek = int.Parse(Console.ReadLine());
                            Console.Write("Jumlah Uang ");
                            BNI obj = new BNI();
                            obj.withdraw();
                            break;
                        }
                    case 3:
                        {
                            Console.Write("Nomor Rekening Anda : ");
                            no_rek = int.Parse(Console.ReadLine());
                            BNI obj = new BNI();
                            obj.cek_saldo();
                            break;
                        }
                    case 4:
                        {
                            return;
                        }
                }
            }
        }
    }
}

program bank inheritance versi 2 dalam c#



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    abstract class bank
    {
        private int amount;
        public void deposit(int amt)
        {
            amount += amt;
        }
        public void withdraw(int amt)
        {
            if (amount < amt)
            {
                Console.WriteLine("Saldo Kurang");
            }
            else
            {
                amount -= amt;
            }
        }
        public void cek_saldo()
        {
            Console.WriteLine("Jumlah Saldo Anda = {0}", amount);
        }
    }
    class BNI : bank
    {
        private int temp;
        public void a1()
        {
            base.deposit(temp);
            base.withdraw(temp);
            base.cek_saldo();
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Jumlah Nasabah = ");
            int n_nasabah = int.Parse(Console.ReadLine());
            BNI[] nasabah = new BNI[n_nasabah + 1];
            for (int i = 1; i <= n_nasabah; i++)
            {
                nasabah[i] = new BNI();
            }
         
            while (true)
            {
                Console.WriteLine("Pilihan : ");
                Console.WriteLine("     1. Menabung");
                Console.WriteLine("     2. Mengambil Uang");
                Console.WriteLine("     3. Cek_Saldo");
                Console.WriteLine("     4. keluar");
                Console.Write("Pilih : ");
                int pilih = int.Parse(Console.ReadLine());
                int no_rek;
                int jumlah;
                switch (pilih)
                {
                    case 1:
                        {
                            Console.Write("Nomor Rekening Anda : ");
                            no_rek = int.Parse(Console.ReadLine());
                            Console.Write("Jumlah Uang ");
                            jumlah = int.Parse(Console.ReadLine());
                            nasabah[no_rek].deposit(jumlah);
                            break;
                        }
                    case 2:
                        {
                            Console.Write("Nomor Rekening Anda : ");
                            no_rek = int.Parse(Console.ReadLine());
                            Console.Write("Jumlah Uang ");
                            jumlah = int.Parse(Console.ReadLine());
                            nasabah[no_rek].withdraw(jumlah);
                            break;
                        }
                    case 3:
                        {
                            Console.Write("Nomor Rekening Anda : ");
                            no_rek = int.Parse(Console.ReadLine());
                            nasabah[no_rek].cek_saldo();
                            break;
                        }
                    case 4:
                        return;
                }
            }
        }
    }
}

Monday, December 5, 2011

Counting Sort dalam notasi algoritmik

Procedure CountingSort (Input/output T:array [1..N] of integer)
{Mengurutkan Tabel T integer [1..N] dengan pencacahan, dimana range atau rentang nilainya [1..k] dengan hasil akhir T terurut menaik , dengan asumsi:Nilai N (jumlah elemen T) dan k diketahui
Asumsi : Nilai N (jumlah elemen T) dan k diketahui, rentang nilai untuk elemen T integer antara 1..k }

Kamus
C : array [1..k] of integer {Tabel array pencacahan}
i, j : Integer {indek perulangan}
c : Integer {jumlah elemen T yang sudah diisi pada pembentukank kembali}

Algoritma
for i = 1 to k do {Loop 1}
C[ i ] <-- 0;
endfor

for i = 1 to N do {Loop 2}
C[ T[ i ] ] <-- C[ T [ i ] ] + 1
endfor

c <-- 0

for i = 1 to k do {Loop 3}
if C[ i ] <> 0 then
for j = 1 to C[ i ] do {Loop 4}
c <--c + 1
T[c] <-- i
endfor;
endif;
endfor;

bucket Sort dalam notasi algoritmik


Algoritma Bucket Sort
Procedure BucketSort ( Input/Output T: array[1..n] of integer, b: integer Output T: Array[1..n], Input n,max_n:integer)
{procedure untuk mengurutkan T array secara ascending, dengan asumsi jumlah data dan nilai terbesar diketahui}
Kamus
range                : integer                                                      {rentang nilai bucket}
x,y, i, j              : integer
baris, kolom      : integer
isi_bucket         : array [1..b] of integer                                 {penyimpan nilai isi tiap bucket}
lengh_bucket    : array [1..n] of integer                                 {kapasitas bucket menyimpan nilai}
bucket              : array [1..b] of lengh_bucket                      {bucket; kumpulan lengh_bucket}
Algoritma
x ß max_n div b
y ß max_n mod b
if (y > 0) then
                range ß x + 1
else                                                                                     {menentukan interval @bucket}
                range ß x
endif
for i = 1 to b do
                isi_bucket[i] ß 0                                                 {inisialisasi isi @bucket = 0}
endfor
for i = 1 to n do
                x              ß T[i] div range
                y              ß T[i] mod range

if (y > 0) then
                                x              ß x + 1                                 {loop; membagi nilai T ke dalam @bucket}
                endif

                isi_bucket[x]         ß isi_bucket[x] + 1
                baris                      ß x
                kolom                    ß isi_bucket[x]
                bucket[m, n]         ßT[i]     
endfor


for i = 1 to b do
                                                           {pengurutan @ bucket dengan algoritma pengurutan tertentu}
Endfor
n ß 1
for i = 1 to b do
                for  j = 1 to isi_bucket[i] do
                                T[n] ß bucket[i,j]                                  {penggabungan bucket}
                                n ß n + 1
                endfor
endfor

program bank inheritance dalam c#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    abstract class bank
    {
        private int amount;
        private static int total;
        public void deposit(int amt)
        {
            amount += amt;
            total = total + amt;
        }
        public void withdraw(int amt)
        {
            if (amount < amt)
            {
                Console.WriteLine("Saldo Kurang");
            }
            else
            {
                amount -= amt;
                total = total - amt;
            }
        }
        public void cek_saldo()
        {
            Console.WriteLine("Jumlah Saldo Anda = {0}", amount);
        }
        public static void show_total()
        {
            Console.WriteLine("Jumlah Uang di Bank =  {0}", total);
        }
    }
    class BNI : bank
    {  
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Jumlah Nasabah = ");
            int n_nasabah = int.Parse(Console.ReadLine());
            BNI[] nasabah = new BNI[n_nasabah + 1];
            for (int i = 1; i <= n_nasabah; i++)
            {
                nasabah[i] = new BNI();
            }

            while (true)
            {
                Console.WriteLine("Pilihan : ");
                Console.WriteLine("     1. Menabung");
                Console.WriteLine("     2. Mengambil Uang");
                Console.WriteLine("     3. Cek_Saldo");
                Console.WriteLine("     4. Cek_saldo_bank");
                Console.WriteLine("     5. keluar");
                Console.Write("Pilih : ");
                int pilih = int.Parse(Console.ReadLine());
                int no_rek;
                int jumlah;
                switch (pilih)
                {
                    case 1:
                        {
                            Console.Write("Nomor Rekening Anda : ");
                            no_rek = int.Parse(Console.ReadLine());
                            Console.Write("Jumlah Uang ");
                            jumlah = int.Parse(Console.ReadLine());
                            nasabah[no_rek].deposit(jumlah);
                            break;
                        }
                    case 2:
                        {
                            Console.Write("Nomor Rekening Anda : ");
                            no_rek = int.Parse(Console.ReadLine());
                            Console.Write("Jumlah Uang ");
                            jumlah = int.Parse(Console.ReadLine());
                            nasabah[no_rek].withdraw(jumlah);
                            break;
                        }
                    case 3:
                        {
                            Console.Write("Nomor Rekening Anda : ");
                            no_rek = int.Parse(Console.ReadLine());
                            nasabah[no_rek].cek_saldo();
                            break;
                        }
                    case 4:
                        {
                            bank.show_total();
                            Console.WriteLine();
                            break;
                        }
                    case 5:
                        return;
                }
            }
        }
    }
}

Tuesday, November 29, 2011

Bubble Sort dalam bahasa C


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 30
#define DELAY 10000000
#define TRUE 1
#define FALSE 0
#define INPUT 'i'
#define OUTPUT 'o'
#define _MY_DEBUG
#if defined(_MY_DEBUG)
    #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
    #define TRACE_LINE
#endif

void BubbleSort(int*, int),
    Swap(int*, int*),
        InputOutput(int*, const int, const char),
    Delay(void),
FreeBuffer(int*);

int main(int argc, char *argv[]) {
    system("COLOR 5");
    int *buffer = NULL, max;
    printf("Implementasi Bubble Sort\nMasukkan banyak data [max:30] : ");
    scanf("%d",&max);
    fflush(stdin);
    if((max > FALSE) && (max <= MAX)) {
        buffer = (int*)calloc(max,sizeof(int));
        InputOutput(buffer,max,INPUT);
        printf("\nData yang anda masukkan : ");
        InputOutput(buffer,max,OUTPUT);
        BubbleSort(buffer,max);
        printf("\nData setelah disorting : ");
        InputOutput(buffer,max,OUTPUT);
        FreeBuffer(buffer);
    } TRACE_LINE;
    getch();
    fflush(stdin);
    return(EXIT_SUCCESS);
}

void BubbleSort(int* buffer, int max) {
    int i, j;
    for(i = 0; i < max; ++i) {
        for(j = 0; j < max-i; ++j) {
            if(*(buffer+j) > *(buffer+(j+TRUE))) { // if(buffer[j] > buffer[j+TRUE]) {
                Swap((buffer+j),(buffer+(j+TRUE))); // Swap(&(*(buffer+j)),&(*(buffer+(j+TRUE))));
            }
        }
    }
}

void Swap(int* buffer1, int* buffer2) {
    int tmp = *buffer1;
    *buffer1 = *buffer2;
    *buffer2 = tmp;
}

void InputOutput(int* buffer, const int max, const char STAT) {
    int i;
    /* switch(STAT) {
  case(INPUT) : {
   for(i = 0; i < max; ++i) {
    printf("%d. Data ke-%d : ",(i+TRUE),(i+TRUE));
    scanf("%d",&buffer[i]);
    fflush(stdin);
   }
  } break;

  case(OUTPUT) : {
   for(i = 0; i < max; ++i) {
    printf("%d ",buffer[i]);
    Delay();
   }
  } break;

  default :
   break;
    } */

    if('i' == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d. Data ke-%d : ",(i+TRUE),(i+TRUE));
            scanf("%d",&buffer[i]);
            fflush(stdin);
        }
    } else if('o' == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d ",buffer[i]);
            Delay();
        }
    }
}

void Delay(void) {
    int i = FALSE;
    while(i < DELAY) {
        ++i;
    }
}

void FreeBuffer(int* buffer) {
    free(buffer);
    buffer = NULL;
}

Shell Sort dalam bahasa C


#
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 100
#define INPUT 'i'
#define OUTPUT 'o'
#define _MY_DEBUG
#if defined(_MY_DEBUG)
 #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
 #define TRACE_LINE
#endif

// int CheckForBadSorting(int*, const int);
void ShellSortPass(int*, const int, const int);
void ShellSort(int*, const int);
void InputOutput(int*, const int, const char);
void FreeBuffer(int*);

int main(int argc, char* argv[]) {
 system("COLOR 5");
 int *buffer = NULL, max;
 printf("Implementasi Shell Sort\nMasukkan jumlah data [MAX:100] : ");
 scanf("%d",&max);
 fflush(stdin);
 if((max > 0) && (max <= MAX)) {
  buffer = (int*)calloc(max,sizeof(int));
  InputOutput(buffer,max,INPUT);
  printf("\nData yang anda masukkan : ");
  InputOutput(buffer,max,OUTPUT);
  ShellSort(buffer,max);
  printf("\nData setelah disorting : ");
  InputOutput(buffer,max,OUTPUT);
  FreeBuffer(buffer);
  /* if(CheckForBadSorting(buffer,max)) {
   goto MARK;
  } else {
   InputOutput(buffer,max,OUTPUT);
  } */
 } /* MARK : if(buffer != NULL) {
  FreeBuffer(buffer);
 } */
 TRACE_LINE;
    getch();
    fflush(stdin);
    return(EXIT_SUCCESS);
}

/* int CheckForBadSorting(int* buffer, const int max) {
 int i;
 for(i = 1; i < max; ++i) {
  if(buffer[i-1] > buffer[i]) {
   printf("Bad Sorting!");
   return(1);
  }
 }
 return(0);
} */

void ShellSortPass(int* buffer, const int max, const int interval) {
 int i;
    for(i = 0; i < max; ++i) {
        int j, tmp = buffer[i];
        for(j = i-interval; j >= 0; j -= interval) {
            if(buffer[j] <= tmp) {
             break;
            } buffer[j+interval] = buffer[j];
        } buffer[j+interval] = tmp;
 }
}

void ShellSort(int* buffer, const int max) {
 int CiuraIntervals[] = {701, 301, 132, 57, 23, 10, 4, 1},
 IntervalIdx = 0, interval = CiuraIntervals[0];
    double ExtendCiuraMultiplier = 2.3;
    if(max > interval) {
     while(max > interval) {
      --IntervalIdx;
            interval = (int)(interval*ExtendCiuraMultiplier);
        }
    } else {
        while(max < interval) {
            ++IntervalIdx;
            interval = CiuraIntervals[IntervalIdx];
        }
    }
    while(interval > 1) {
        ++IntervalIdx;
  if(IntervalIdx >= 0) {
   interval = CiuraIntervals[IntervalIdx];
  } else {
   interval = (int)(interval/ExtendCiuraMultiplier);
  } ShellSortPass(buffer,max,interval);
    }
}

void InputOutput(int* buffer, const int max, const char STAT) {
 int i;
 if('i' == STAT) {
  for(i = 0; i < max; ++i) {
   printf("%d. Data ke-%d : ",(i+1),(i+1));
   scanf("%d",&buffer[i]);
   fflush(stdin);
  }
 } else if('o' == STAT) {
  for(i = 0; i < max; ++i) {
   printf("%d ",buffer[i]);
  }
 }
}

void FreeBuffer(int* buffer) {
 free(buffer);
 buffer = NULL;
}

bucket Sort dalam bahasa C


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 100
#define INPUT 'i'
#define OUTPUT 'o'
#define _MY_DEBUG
#if defined(_MY_DEBUG)
 #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
 #define TRACE_LINE
#endif

void InputOutput(int*, const int, const char);
void BucketSort(int*, const int);
void FreeBuffer(int*);

int main(int argc, char *argv[]) {
 system("COLOR 5");
 int *buffer = NULL, max;
 printf("Implementasi Bucket Sort\nMasukkan jumlah data [MAX:100] : ");
 scanf("%d",&max);
 fflush(stdin);
 if((max > 0) && (max <= MAX)) {
  buffer = (int*)calloc(max,sizeof(int));
  InputOutput(buffer,max,INPUT);
  printf("\nData yang anda masukkan : ");
  InputOutput(buffer,max,OUTPUT);
  BucketSort(buffer,max);
  printf("\nData setelah disorting  : ");
  InputOutput(buffer,max,OUTPUT);
  FreeBuffer(buffer);
 }
 TRACE_LINE;
 getch();
 fflush(stdin);
 return(EXIT_SUCCESS);
}

void InputOutput(int* buffer, const int max, const char STAT) {
 int i;
 if('i' == STAT) {
  for(i = 0; i < max; ++i) {
   printf("%d. Data ke-%d : ",(i+1),(i+1));
   scanf("%d",&buffer[i]);
   fflush(stdin);
  }
 } else if('o' == STAT) {
  for(i = 0; i < max; ++i) {
   printf("%d ",buffer[i]);
  }
 }
}

void BucketSort(int* buffer, const int max) {
 int i, j, *count = (int*)calloc(max,sizeof(int));
 for(i = 0; i < max; ++i) {
  count[i] = 0;
 } for(i = 0; i < max; ++i) {
  ++(count[buffer[i]]);
 } for(i = 0, j = 0; i < max; ++i) {
  for(; count[i] > 0; --(count[i])) {
   buffer[j++] = i;
  }
 } FreeBuffer(count);
}

void FreeBuffer(int* buffer) {
 free(buffer);
 buffer = NULL;
}

Quick Sort dalam bahasa C


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 30
#define INPUT 'i'
#define OUTPUT 'o'
#define TRUE 1
#define FALSE 0
#define _MY_DEBUG
#if defined(_MY_DEBUG)
    #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
    #define TRACE_LINE
#endif

int ChoosePivot(int, int);
    void InputOutput(int*, const int, const char),
        QuickSort(int*, int, int),
    Swap(int*, int*),
FreeBuffer(int*);

int main(int argc, char *argv[]) {
    system("COLOR 5");
    int *buffer = NULL, max;
    printf("Implementasi Quick Sort [Ascending]\nJumlah data [MAX:30] : ");
    scanf("%d",&max);
    fflush(stdin);
    if((max > FALSE) && (max <= MAX)) {
        buffer = (int*)calloc(max,sizeof(int));
        InputOutput(buffer,max,INPUT);
        printf("\n1. Data yang anda masukkan : ");
        InputOutput(buffer,max,OUTPUT);
        QuickSort(buffer,FALSE,(max-TRUE));
        printf("\n2. Data setelah disorting : ");
        InputOutput(buffer,max,OUTPUT);
        FreeBuffer(buffer);
    }
    TRACE_LINE;
    getch();
    fflush(stdin);
    return(EXIT_SUCCESS);
}

void InputOutput(int* buffer, int max, const char STAT) {
    int i;
    if(INPUT == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d. Data ke-%d : ",(i+TRUE),(i+TRUE));
            scanf("%d",&buffer[i]);
            fflush(stdin);
        }
    } else if(OUTPUT == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d ",buffer[i]);
        }
    }
}

int ChoosePivot(int top, int bottom) {
    return((top+bottom)/2);
}

void QuickSort(int* buffer, int bottom, int top) {
    int i, j, k, pivot; // m = bottom, n = top;
    if(bottom < top) {
        pivot = ChoosePivot(bottom,top);
        Swap(&buffer[bottom],&buffer[pivot]);
        i = bottom+TRUE; j = top; k = buffer[bottom];
        while(i <= j) {
            while((i <= top) && (buffer[i] <= k)) {
                ++i;
            }
            while((j >= bottom) && (buffer[j] > k)) {
                --j;
            }
            if(i < j) {
                Swap(&buffer[i],&buffer[j]);
            }
        }
        Swap(&buffer[bottom],&buffer[j]);
        QuickSort(buffer,bottom,(j-TRUE));
        QuickSort(buffer,(j+TRUE),top);
    }
}

void Swap(int* buffer1, int* buffer2) {
    int tmp = *buffer1;
    *buffer1 = *buffer2;
    *buffer2 = tmp;
}

void FreeBuffer(int* buffer) {
    free(buffer);
    buffer = NULL;
}

Exchange sort dalam bahasa C


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 30
#define DELAY 10000000
#define TRUE 1
#define FALSE 0
#define INPUT 'i'
#define OUTPUT 'o'
#define _MY_DEBUG
#if defined(_MY_DEBUG)
    #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
    #define TRACE_LINE
#endif

void ExchangeSort(int*, int),
    Swap(int*, int*),
        InputOutput(int*, const int, const char),
    Delay(void),
FreeBuffer(int*);

int main(int argc, char *argv[]) {
    system("COLOR 5");
    int *buffer = NULL, max;
    printf("Implementasi Exchange Sort\nMasukkan banyak data [max:30] : ");
    scanf("%d",&max);
    fflush(stdin);
    if((max > FALSE) && (max <= MAX)) {
        buffer = (int*)calloc(max,sizeof(int));
        InputOutput(buffer,max,INPUT);
        printf("\nData yang anda masukkan : ");
        InputOutput(buffer,max,OUTPUT);
        ExchangeSort(buffer,max);
        printf("\nData setelah disorting : ");
        InputOutput(buffer,max,OUTPUT);
        FreeBuffer(buffer);
    }
    TRACE_LINE;
    getch();
    fflush(stdin);
    return(EXIT_SUCCESS);
}

void ExchangeSort(int* buffer, int max) {
    int i, j;
    for(i = 0; i < (max-TRUE); ++i) {
        for(j = (i+TRUE); j < max; ++j) {
            if(*(buffer+i) > *(buffer+(j))) {
                Swap((buffer+i),(buffer+(j))); // Swap(&(*(buffer+j)),&(*(buffer+(j+TRUE))));
            }
        }
    }
}

void Swap(int* buffer1, int* buffer2) {
    int tmp = *buffer1;
    *buffer1 = *buffer2;
    *buffer2 = tmp;
}

void InputOutput(int* buffer, int max, const char STAT) {
    int i;
    if('i' == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d. Data ke-%d : ",(i+TRUE),(i+TRUE));
            scanf("%d",&buffer[i]);
            fflush(stdin);
        }
    } else if('o' == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d ",buffer[i]);
            Delay();
        }
    }
}

void Delay(void) {
    int i = FALSE;
    while(i < DELAY) {
        ++i;
    }
}

void FreeBuffer(int* buffer) {
    free(buffer);
    buffer = NULL;
}

Gnome sort dalam bahasa C


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 100
#define INPUT 'i'
#define OUTPUT 'o'
#define _MY_DEBUG
#if defined(_MY_DEBUG)
 #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
 #define TRACE_LINE
#endif

void InputOutput(int*, const int, const char);
void GnomeSort(int*, const int);
void Swap(int*, int*);
void FreeBuffer(int*);

int main(int argc, char *argv[]) {
 system("COLOR 3");
 int *buffer = NULL, max;
 printf("Implementasi Gnome Sort\nMasukkan jumlah data [MAX:100] : ");
 scanf("%d",&max);
 fflush(stdin);
 if((max > 0) && (max <= MAX)) {
  buffer = (int*)calloc(max,sizeof(int));
  InputOutput(buffer,max,INPUT);
  printf("\nData yang anda masukkan : ");
  InputOutput(buffer,max,OUTPUT);
  GnomeSort(buffer,max);
  printf("\nData setelah disorting : ");
  InputOutput(buffer,max,OUTPUT);
  FreeBuffer(buffer);
 } TRACE_LINE;
 getch();
 fflush(stdin);
 return(EXIT_SUCCESS);
}

void InputOutput(int* buffer, const int max, const char STAT) {
 int i;
 if('i' == STAT) {
  for(i = 0; i < max; ++i) {
   printf("%d. Data ke-%d : ",(i+1),(i+1));
   scanf("%d",&buffer[i]);
   fflush(stdin);
  }
 } else if('o' == STAT) {
  for(i = 0; i < max; ++i) {
   printf("%d ",buffer[i]);
  }
 }
}

void GnomeSort(int* buffer, const int max) {
 int i;
 for(i = 1; i < max;) {
  if(buffer[i-1] <= buffer[i]) {
   ++i;
  } else {
   Swap(&buffer[i],&buffer[i-1]); --i;
   if(!i) {
    i = 1;
   }
  }
 }
}

void Swap(int* buffer1, int* buffer2) {
 int tmp = *buffer1;
 *buffer1 = *buffer2;
 *buffer2 = tmp;
}

void FreeBuffer(int* buffer) {
 free(buffer);
 buffer = NULL;
}

Insertion Sort dalam bahasa C


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 30
#define DELAY 10000000
#define TRUE 1
#define FALSE 0
#define INPUT 'i'
#define OUTPUT 'o'
#define _MY_DEBUG
#if defined(_MY_DEBUG)
    #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
    #define TRACE_LINE
#endif

void InsertionSort(int*, const int);
void InputOutput(int*, const int, const char);
void Delay(void);
void FreeBuffer(int*);

int main(int argc, char *argv[]) {
    system("COLOR 5");
    int *buffer = NULL, max;
    printf("Implementasi Insertion Sort\nMasukkan banyak data [max:30] : ");
    scanf("%d",&max);
    fflush(stdin);
    if((max > FALSE) && (max <= MAX)) {
        buffer = (int*)calloc(max,sizeof(int));
        InputOutput(buffer,max,INPUT);
        printf("\nData yang anda masukkan : ");
        InputOutput(buffer,max,OUTPUT);
        InsertionSort(buffer,max);
        printf("\nData setelah disorting : ");
        InputOutput(buffer,max,OUTPUT);
        FreeBuffer(buffer);
    }
    TRACE_LINE;
    getch();
    fflush(stdin);
    return(EXIT_SUCCESS);
}

void InsertionSort(int* buffer, const int max) {
    int i, j, tmp;
    for(i = 1; i < max; ++i) {
        tmp = buffer[i];
        j = i-TRUE;
        while((j >= FALSE) && (buffer[j] > tmp)) {
            buffer[j+TRUE] = buffer[j];
            --j;
        }
        buffer[j+TRUE] = tmp;
    }
}

void InputOutput(int* buffer, const int max, const char STAT) {
    int i;
    if('i' == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d. Data ke-%d : ",(i+TRUE),(i+TRUE));
            scanf("%d",&buffer[i]);
            fflush(stdin);
        }
    } else if('o' == STAT) {
        for(i = 0; i < max; ++i) {
            printf("%d ",buffer[i]);
            Delay();
        }
    }
}

void Delay(void) {
    int i = FALSE;
    while(i < DELAY) {
        ++i;
    }
}

void FreeBuffer(int* buffer) {
    free(buffer);
    buffer = NULL;
}

Counting Sort dalam bahasa C


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define UINT unsigned int
#define MAX 100
#define INPUT 'i'
#define OUTPUT 'o'
#define _MY_DEBUG
#if defined(_MY_DEBUG)
 #define TRACE_LINE printf("\n\n- Program Statistics :\n1. File : %s\n2. Date : %s\n3. Time : %s\n",__FILE__,__DATE__,__TIME__);
#else
 #define TRACE_LINE
#endif

void InputOutput(long*, const long, const char);
int CountingSort(long*, const long, const long);
void FreeBuffer(long*);

int main(int argc, char *argv[]) {
 system("COLOR 5");
 long *buffer = NULL, max;
 printf("Implementasi Counting Sort\nMasukkan jumlah data [MAX:100] : ");
 scanf("%ld",&max);
 fflush(stdin);
 if((max > 0) && (max <= MAX)) {
  buffer = (long*)calloc(max,sizeof(long));
  InputOutput(buffer,max,INPUT);
  printf("\nData yang anda masukkan : ");
  InputOutput(buffer,max,OUTPUT);
  if(!CountingSort(buffer,max,65535)) { // 2147483647
   /* Jika user menginputkan nilai lebih besar atau sama dengan 65535
   kedalam array buffer, maka Fungsi CountingSort tidak akan bekerja
   sesuai dengan fungsinya atau bisa mengakibatkan program Error / akan
   terjadi Segmentation Fault */
   printf("\nData setelah disorting : ");
   InputOutput(buffer,max,OUTPUT);
  } FreeBuffer(buffer);
 }
 TRACE_LINE;
 getch();
 fflush(stdin);
 return(EXIT_SUCCESS);
}

void InputOutput(long* buffer, const long max, const char STAT) {
 long i;
 if('i' == STAT) {
  for(i = 0; i < max; ++i) {
   printf("%ld. Data ke-%ld : ",(i+1),(i+1));
   scanf("%ld",&buffer[i]);
   fflush(stdin);
  }
 } else if('o' == STAT) {
  for(i = 0; i < max; ++i) {
   printf("%ld ",buffer[i]);
  }
 }
}

int CountingSort(long* buffer, const long max, const long value) {
 long i, j, OutputPos;
    long* counts = (long*)calloc(value,sizeof(long));
    if(counts == NULL) {
     printf("\nError : No Memory Available for Counts!");
     return(1);
    } else {
     for(i = 0; i < max; ++i) {
   ++counts[buffer[i]];
  } OutputPos = 0;
  for(i = 0; i < value; ++i) {
   for(j = 0; j < counts[i]; ++j) {
    buffer[OutputPos] = i;
    ++OutputPos;
   }
  } FreeBuffer(counts);
    }
    return(0);
}

void FreeBuffer(long* buffer) {
 free(buffer);
 buffer = NULL;
}

procedure counting sort

Procedure CountingSort (Input/output T:array [1..N] of integer)
{Mengurutkan Tabel T integer [1..N] dengan pencacahan, dimana range atau rentang nilainya [1..k] dengan hasil akhir T terurut menaik , dengan asumsi:Nilai N (jumlah elemen T) dan k diketahui 
Asumsi : Nilai N (jumlah elemen T) dan k diketahui, rentang nilai untuk elemen T integer antara 1..k }

Kamus 
C : array [1..k] of integer {Tabel array pencacahan}
i, j : Integer {indek perulangan}
c : Integer {jumlah elemen T yang sudah diisi pada pembentukan kembali}

Algoritma 
for i = 1 to k do {Loop 1}
C[ i ] <-- 0;
endfor

for i = 1 to N do {Loop 2}
C[ T[ i ] ] <-- C[ T [ i ] ] + 1
endfor

C <-- 0

for i = 1 to k do {Loop 3}
if C[ i ] < > 0 then 
for j = 1 to C[ i ] do {Loop 4}
C <-- C + 1
T[C] <-- i
endfor;
endif;
endfor;