STL singkatan dari Standard Template Library. Compiler C++ telah menyediakan library khusus untuk menampung beberapa template yang memang secara umum sering dipakai dalam pembuatan program. Dengan adanya STL, programmer tidak perlu lagi coding dari nol dan mulai awal. Namun cukup memanfaatkan template yang sudah dijadikan library tersebut. Tugas programmer tinggal menganalisa dan memilih template mana yang paling cocok dengan kebutuhan programnya. Di dalam template sudah disediakan container class lengkap dengan fungsi-fungsinya. Berikut adalah salah satu contoh penggunanan STL list untuk mengimplementasikan linked list:
#include "iostream.h"
#include "list" //standard template library
using namespace std;
int main(){
list values, otherValues;
values.push_front(1);
values.push_front(2);
values.push_back(4);
values.push_back(3);
list::iterator p; //pointer u/ mengakses isi container class
for (p=values.begin(); p!=values.end(); p++)
{
cout << *p <<endl;
}
return 0;
}
Wednesday, 2 February 2011
Thursday, 20 January 2011
Contoh Program C++: Template
Dilihat dari aspek manfaatnya, pembuatan template hampir sama dengan function overloading. Cukup dipanggil satu yang sama, namun bisa menangani tipe data yang berbeda-beda. Namun, bedanya adalah jika kita memakai overloading maka untuk setiap fungsi harus kita tuliskan kembali definisinya.
Tidak demikian dengan template yang cukup ditulis satu kali saja (kerangkanya) maka compiler akan otomatis bisa membedakan jenis-jenis tipe datanya. Memang akan menjadi jauh lebih sederhana. Tetapi harus benar-benar dianalisa secara mendalam apakah konsep template sudah pas dengan yang dibutuhkan dalam rangka memecahkan sebuah studi kasus. Berikut adalah beberapa contohnya:
Template Fungsi
Template Class 1
Template Class 2
Note: Ubahlah terlebih dulu ekstensi doc menjadi cpp.
Tidak demikian dengan template yang cukup ditulis satu kali saja (kerangkanya) maka compiler akan otomatis bisa membedakan jenis-jenis tipe datanya. Memang akan menjadi jauh lebih sederhana. Tetapi harus benar-benar dianalisa secara mendalam apakah konsep template sudah pas dengan yang dibutuhkan dalam rangka memecahkan sebuah studi kasus. Berikut adalah beberapa contohnya:
Template Fungsi
Template Class 1
Template Class 2
Note: Ubahlah terlebih dulu ekstensi doc menjadi cpp.
Saturday, 25 December 2010
Contoh Program Java: Membaca Isi File Teks
import java.io.*;
public class baca_file {
public static void main(String arg[])
{
String file = "c://program1.txt"; //lokasi file
FileInputStream in = null;
try {
in = new FileInputStream(file);//
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
in.close();
}
} catch (IOException x) {
}
}
}
public class baca_file {
public static void main(String arg[])
{
String file = "c://program1.txt"; //lokasi file
FileInputStream in = null;
try {
in = new FileInputStream(file);//
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
in.close();
}
} catch (IOException x) {
}
}
}
Wednesday, 22 December 2010
Contoh Program C++: OOP Rekening Bank
#include "iostream.h"
#include "string.h"
#include "stdlib.h"
struct deposit {
double nominal;
struct deposit *next;
};//
typedef struct deposit *simpul;
class rekening_bank {
protected:
char nama[20];
double saldo;
simpul deposits;
int nomer_rek;
public:
void transaksi()
{
double trans=0.0;
simpul baru=NULL;
cout<<"Saldo Anda : "<<saldo<<endl;
cout<>trans;
if (trans<0) //penarikan
{
if ((abs(trans)+5000) nominal = trans;
baru->next = NULL;
if (deposits == NULL)
deposits = baru;
else
{
baru->next = deposits;
deposits = baru;
}
}
else
cout<<"Saldo tidak mencukupi"<nominal = trans;
baru->next = NULL;
if (deposits == NULL)
deposits = baru;
else
{
baru->next = deposits;
deposits = baru;
}
}
};
void cetak()
{
simpul temp=deposits;
cout<<"Saldo Anda : "<<saldo<<endl;
cout<<"Rekap Transaksi :"<next)
if(temp->nominal < 0)
cout<<"Penarikan "<nominal)<<" + 5000 [biaya]"<<endl;
else cout<<"Setoran "<nominal<<endl;
}
};
class cek : public rekening_bank
{
private:
public:
cek(double d) {
cout<<"Rekening cek dibuat..."<<endl;
deposits=NULL;
saldo=d;
};
};
class tabungan : public rekening_bank
{
private:
float bunga;
public:
tabungan(double d)
{
cout<<"Rekening tabungan dibuat..."<<endl;
deposits=NULL;
saldo=d;
};
void hitung_bunga()
{
simpul baru=NULL;
cout<>bunga;
baru = (simpul) malloc(sizeof(struct deposit));
baru->nominal = saldo*bunga;
baru->next = NULL;
saldo = saldo + (saldo*bunga);
if (deposits == NULL)
deposits = baru;
else
{
baru->next = deposits;
deposits = baru;
}
cout<<"saldo sekarang = "<<saldo<<endl;
}
};
void main()
{
cek c(100000);
c.transaksi();
c.cetak();
c.transaksi();
c.cetak();
tabungan b(130000);
b.transaksi();
b.cetak();
b.hitung_bunga();
b.cetak();
//lebih baik dibuatkan menu program
}
Download Source Code
#include "string.h"
#include "stdlib.h"
struct deposit {
double nominal;
struct deposit *next;
};//
typedef struct deposit *simpul;
class rekening_bank {
protected:
char nama[20];
double saldo;
simpul deposits;
int nomer_rek;
public:
void transaksi()
{
double trans=0.0;
simpul baru=NULL;
cout<<"Saldo Anda : "<<saldo<<endl;
cout<>trans;
if (trans<0) //penarikan
{
if ((abs(trans)+5000) nominal = trans;
baru->next = NULL;
if (deposits == NULL)
deposits = baru;
else
{
baru->next = deposits;
deposits = baru;
}
}
else
cout<<"Saldo tidak mencukupi"<nominal = trans;
baru->next = NULL;
if (deposits == NULL)
deposits = baru;
else
{
baru->next = deposits;
deposits = baru;
}
}
};
void cetak()
{
simpul temp=deposits;
cout<<"Saldo Anda : "<<saldo<<endl;
cout<<"Rekap Transaksi :"<next)
if(temp->nominal < 0)
cout<<"Penarikan "<nominal)<<" + 5000 [biaya]"<<endl;
else cout<<"Setoran "<nominal<<endl;
}
};
class cek : public rekening_bank
{
private:
public:
cek(double d) {
cout<<"Rekening cek dibuat..."<<endl;
deposits=NULL;
saldo=d;
};
};
class tabungan : public rekening_bank
{
private:
float bunga;
public:
tabungan(double d)
{
cout<<"Rekening tabungan dibuat..."<<endl;
deposits=NULL;
saldo=d;
};
void hitung_bunga()
{
simpul baru=NULL;
cout<>bunga;
baru = (simpul) malloc(sizeof(struct deposit));
baru->nominal = saldo*bunga;
baru->next = NULL;
saldo = saldo + (saldo*bunga);
if (deposits == NULL)
deposits = baru;
else
{
baru->next = deposits;
deposits = baru;
}
cout<<"saldo sekarang = "<<saldo<<endl;
}
};
void main()
{
cek c(100000);
c.transaksi();
c.cetak();
c.transaksi();
c.cetak();
tabungan b(130000);
b.transaksi();
b.cetak();
b.hitung_bunga();
b.cetak();
//lebih baik dibuatkan menu program
}
Download Source Code
Contoh Program C++: Konversi Heksa-Desimal
#include "iostream.h"
#include "string.h"
#include "math.h"
#include "stdlib.h"
#include "stdio.h"
class hexa {
int decimal;
public:
hexa() {decimal=0;};
hexa(int d) {decimal=d;};//
Maaf, berhubung banyak karakter source code yang autodelete oleh WP, maka silahkan download source code.
Setelah sukses di-download, ubahlah ekstensi file dari .doc menjadi .cpp. Semoga bermanfaat :)
#include "string.h"
#include "math.h"
#include "stdlib.h"
#include "stdio.h"
class hexa {
int decimal;
public:
hexa() {decimal=0;};
hexa(int d) {decimal=d;};//
Maaf, berhubung banyak karakter source code yang autodelete oleh WP, maka silahkan download source code.
Setelah sukses di-download, ubahlah ekstensi file dari .doc menjadi .cpp. Semoga bermanfaat :)
Monday, 20 December 2010
Contoh Program Java: Menjalankan Aplikasi Exe Lain
Aplikasi yang dibuat memakai bahasa Java bisa digunakan untuk memanggil/ menjalankan aplikasi executable lain. Misalnya membuka Notepad.exe, Microsoft Word, Aplikasi Game dsb. Syarat utamanya adalah harus diketahui lokasi/ path file .exe-nya. Library untuk menjalankan proses tersebut adalah java.io. Berikut contoh program untuk menjalankan Microsoft Word (mungkin file path di komputer Anda bisa jadi tidak sama):
import java.io.*;
/**
*
* @author bluejundi
*/
public class java_exec {
public static void main(String[] args) {
String path = new String("E:\\OFFICE\\OFFICE11\\winword.exe");
System.out.println("Menjalankan MS Word");
try {
Process theProcess = Runtime.getRuntime().exec(path);
} catch(IOException e)
{
System.err.println("Error on exec() method");
e.printStackTrace();
}
}
}
import java.io.*;
/**
*
* @author bluejundi
*/
public class java_exec {
public static void main(String[] args) {
String path = new String("E:\\OFFICE\\OFFICE11\\winword.exe");
System.out.println("Menjalankan MS Word");
try {
Process theProcess = Runtime.getRuntime().exec(path);
} catch(IOException e)
{
System.err.println("Error on exec() method");
e.printStackTrace();
}
}
}
Contoh Program Java: Exception Handling
Exception adalah setiap kejadian yang abnormal, tidak diharapkan, dan kondisi yang tidak umum yang mungkin terjadi pada saat run-time program. Java exception handling digunakan untuk menangani kondisi error dalam sebuah program secara sistematis dengan melakukan tindakan yang diperlukan. Dengan adanya exception, maka program akan terhindar dari kondisi hang, atau mati dalam kondisi yang tidak wajar/ tidak jelas. Java exception dimulai dengan kata kunci throw dan ditangani dalam sebuah blok catch. Berikut contoh sederhananya:
public class java_exception {
public static void main(String[] args) {
int result = division(100,0); // Line 2
System.out.println("hasil : "+result);
result = division(100,2);
System.out.println("hasil : "+result);
}
public static int division(int totalSum, int totalNumber) {
int quotient = -1;
System.out.println("Komputasi Pembagian "+totalSum+" dengan "+totalNumber);
try
{ quotient = totalSum/totalNumber; }
catch(Exception e)
{ System.out.println("Pesan Exception : "+ e.getMessage()); }
finally
{ if(quotient != -1)
{ System.out.println("Finally Block Dijalankan");
System.out.println("hasil fungsi : "+ quotient); }
else
{ System.out.println("Finally Block DIjalankan. Terjadi Exception");
System.out.println("hasil fungsi : "+ quotient);
return quotient;
}
}
return quotient;
}
}
public class java_exception {
public static void main(String[] args) {
int result = division(100,0); // Line 2
System.out.println("hasil : "+result);
result = division(100,2);
System.out.println("hasil : "+result);
}
public static int division(int totalSum, int totalNumber) {
int quotient = -1;
System.out.println("Komputasi Pembagian "+totalSum+" dengan "+totalNumber);
try
{ quotient = totalSum/totalNumber; }
catch(Exception e)
{ System.out.println("Pesan Exception : "+ e.getMessage()); }
finally
{ if(quotient != -1)
{ System.out.println("Finally Block Dijalankan");
System.out.println("hasil fungsi : "+ quotient); }
else
{ System.out.println("Finally Block DIjalankan. Terjadi Exception");
System.out.println("hasil fungsi : "+ quotient);
return quotient;
}
}
return quotient;
}
}
Subscribe to:
Posts (Atom)