Program kalkulator sederhana C++

0 komentar Jumat, 15 April 2011

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;
class KALKULATOR{
  friend ostream & operator << ( ostream &, const KALKULATOR & );
  friend istream & operator >> ( istream &, KALKULATOR & );

public:
  void MENU();
  void ulang();
  void tambah();
  void kurang();
  void kali();
  void bagi();
  void INPUT1();
  void INPUT2();
  void pilihan1();
  void pilihan2();
  void pilihan3();
  void sines();
  void cosines();
  void tangen();
  void logku();
  void log10ku();
  void sqrtku();
  void expku();
 
private:
  char  opt;     
  int pilih;
  float hasil0;
  float hasil1;
  float a;
  float b;
  float c;
 
};

 void KALKULATOR::MENU(){
  cout << endl;
  cout << " |===================================|" << endl;
  cout << " |       KALKULATOR SEDERHANA                     |" << endl;
  cout << " |===================================|" << endl;
  cout << " |===================================|" << endl;
  cout << " |  http://samsulfti.blogspot.com/                                 |" << endl;
  cout << " |===================================|" << endl;
  cout << endl;
  cout << " |==================================================================|" << endl;
  cout << " [ B ] Menu Pilihan1 kalkulator Biasa                                               ]" << endl;
  cout << " [ F ] Menu Untuk Kalkulator fungsi Matematika[sin][cos][tgen][dll] ]" << endl;
  cout << " [ ext ] Keluar program                                                                                                 ]" << endl;
  cout << " |==================================================================|" << endl;

  cout << " Pilih Kalkulator Yang Akan Di Gunakan :";
  cin >> opt;

  if ( opt == 'B' ){
    pilihan1();
  }
  else if ( opt == 'F' ){
    pilihan2();
  }
 
  else if ( opt == 'ext' ){
    exit(0);
  }
}

 void KALKULATOR::pilihan1(){
 
  cout << "Operator Yang Akan Di Gunakan" << endl;
  cout << "1. Tambah (+)" << endl;
  cout << "2. Kurang (-)" << endl;
  cout << "3. Kali   (*)" << endl;
  cout << "4. Bagi   (/)" << endl;

  cout << "Pilih Operator yang akan di gunakan : ";
  cin >> opt;

  if ( opt == '+' ){
    tambah();
  }
  else if ( opt == '-' ){
    kurang();
  }
  else if ( opt == '*' ){
    kali();
  }
  else if ( opt == '/' ){
    bagi();
  }
  else {
    cout << " Maaf Operator Yang Anda Masukan Tidak Tersedia ";
  }
}

 void KALKULATOR::pilihan2(){

  cout << "Operator fungsi matematika" << endl;
  cout << "1. Sin      [ S  ] " << endl;
  cout << "2. Cos      [ C  ]" << endl;
  cout << "3. Tan      [ T  ]" << endl;
  cout << "4. log      [ L  ]" << endl;
  cout << "5. log10    [ L10]" << endl;
  cout << "6. sqrt     [ Sq ]" << endl;
  cout << "7. exp      [ Ep ]" << endl;

  cout << "Pilih Operator yang akan di gunakan : ";
  cin >>opt;

  if ( opt == 'S' ){
    sines();
  }
  else if ( opt == 'C' ){
    cosines();
  }
  else if ( opt == 'Tg' ){
    tangen();
  }
  else if ( opt == 'L' ){
    logku();
  }
  else if ( opt == 'L10' ){
    log10ku();
  }
  else if ( opt == 'Sq' ){
    sqrtku();
  }
  else if ( opt == 'Ep' ){
    expku();
  }
  else {
    cout << " Maaf Operator Yang Anda Masukan Tidak Tersedia ";
  }

}
   void KALKULATOR::INPUT1(){
   cout << "Masukkan bilangan pertama:";
   cin >> a;
   cout << "Masukkan bilangan kedua:";
   cin >> b;
}

   void KALKULATOR::tambah(){
   INPUT1();

   hasil0 = a + b;
   cout <<"Hasilnya Adalah:"<< hasil0<<endl;
}

   void KALKULATOR::kurang(){
   INPUT1();

   hasil0 = a - b;
   cout <<"Hasilnya Adalah:"<< hasil0<<endl;
}

   void  KALKULATOR::kali(){
  INPUT1();
   hasil0 = a * b;
   cout <<"Hasilnya Adalah:"<< hasil0<<endl;
}

   void  KALKULATOR::bagi(){
   INPUT1();
   hasil0 = a / b;
   cout <<"Hasilnya Adalah:"<< hasil0<<endl;
}

   void KALKULATOR::INPUT2(){
   cout << "Masukkan bilangan:";
   cin >> c;

}

   void KALKULATOR::sines(){
   INPUT2();
   hasil1 = sin( c );
   cout <<"Hasilnya Adalah:"<< hasil1<<endl;
}
   void KALKULATOR::cosines(){
   INPUT2();
   hasil1 = cos( c );
   cout <<"Hasilnya Adalah:"<< hasil1<<endl;
}
   void KALKULATOR::tangen(){
   INPUT2();
   hasil1 = tan( c );
   cout <<"Hasilnya Adalah:"<< hasil1<<endl;
}
   void KALKULATOR::logku(){
   INPUT2();
   hasil1 = log( c );
   cout <<"Hasilnya Adalah:"<< hasil1<<endl;
}

   void KALKULATOR::log10ku(){
   INPUT2();
   hasil1 = log10( c );
   cout <<"Hasilnya Adalah:"<< hasil1<<endl;
}

   void KALKULATOR::sqrtku(){
   INPUT2();
   hasil1 = sqrt( c );
   cout <<"Hasilnya Adalah:"<< hasil1<<endl;
}
   void KALKULATOR::expku(){
   INPUT2();
   hasil1 = exp( c );
   cout <<"Hasilnya Adalah:"<< hasil1<<endl;
}          
  main(){
  KALKULATOR pheewe;
  pheewe.MENU();
 
  system("PAUSE");
  return EXIT_SUCCESS;
}

Program tahun kabisat ( jeliot )

0 komentar Selasa, 12 April 2011

import jeliot.io.*;

public class tahun_kabisat{
int x;
public int bil(int x){
return x;
}
            public final void kabisat()
            {
            int bil;
             bil=Input.readInt();
               if(bil % 4 == 0)
              {
                               System.out.print("Tahun kabisat.\n");
                  }
              else
              {
                  System.out.print("Tahun bukan kabisat.\n");
              }
          
              System.out.print(bil);
          
           }
            public tahun_kabisat()
            {
            System.out.print("Program untuk menampilkan tahun kabisat");
            System.out.print("\n");
                                  }
}
public class MyClass {
    public static void main() {
   tahun_kabisat x = new tahun_kabisat();
        x.kabisat();
System.out.print("\n");
    }
}

convert C++ to java (jeliot)

0 komentar


C++

#include <iostream.h>
#include <conio.h>

void cetak_array(int A[], int n) {
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[ i ] << endl;
    getch(); 
}

void main() {
    int A[10] = {1,2,3,4,5,6,7,8,9,10};
    int n = 10;
    cout << "Nilai array asal : ";
    cetak_array(A, n);
    cout << "Setelah dikurangi 1 menjadi : " << endl;
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[ i ]-1 << endl;       
    getch();
    cout << "Nilai array dicetak kembali" << endl;
    cetak_array(A, n);
    cout << "Ternyata tidak berubah. Perhatikan perbedaannya " << endl;   
    for (int i=0; i<n; i++)
        cout << "Nilai indeks ke-[" << i << "] adalah " << A[ i ]-- << endl;       
    cout << "Nilai array dicetak kembali" << endl;
    cetak_array(A, n);
}

Jeliot

import jeliot.io.*;

public class myclass {
  public void cetak_array(int A[],int n)
{
   for (int i =0; i<n; i++)
   {
      System.out.print("Nilai indeks ke-[");
      System.out.print(i);
      System.out.print("] adalah ");
      System.out.print(A);
      System.out.print("\n");
   }

}

public void nilai_array()
{
   int[] A = {1,2,3,4,5,6,7,8,9,10};
   int n = 10;
   System.out.print("Nilai array asal : ");
   cetak_array(A, n);
   System.out.print("Setelah dikurangi 1 menjadi : ");
   System.out.print("\n");
   for (int i =0; i<n; i++)
   {
      System.out.print("Nilai indeks ke-[");
      System.out.print(i);
      System.out.print("] adalah ");
      System.out.print(A-1);
      System.out.print("\n");
   }
  
   System.out.print("Nilai array dicetak kembali");
   System.out.print("\n");
   cetak_array(A, n);
   System.out.print("Ternyata tidak berubah. Perhatikan perbedaannya ");
   System.out.print("\n");
   for (int i =0; i<n; i++)
   {
      System.out.print("Nilai indeks ke-[");
      System.out.print(i);
      System.out.print("] adalah ");
      System.out.print(A--);
      System.out.print("\n");
   }
   System.out.print("Nilai array dicetak kembali");
   System.out.print("\n");
   cetak_array(A, n);
}
private int[] B;
private int x;

public static void main(){
myclass X = new myclass();
X.nilai_array();
X.cetak_array(B,x);

}
}

“Membuat Kalkulator Sederhana di Android”

0 komentar Jumat, 08 April 2011

Kali ini saya akan mencoba men-sharing sama teman-teman bagaimana cara membuat kalkulator sederhana di android. Pada kesempatan ini kita aka membuat kalkulator menggunakan radio button, dimana hanya ada satu operator saja yang berfungsi apabila tombol radio button dipilih. Langsung ajah deh.,,.,
1.       Buat project baru di eclipse, beri nama calcu.java
2.      Lalu buka file main.xml. Ketikkan script beriku pada file ini :<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background = “@drawable/splash”>
3.      Kemudian buat file xml baru dengan nama hitung.xml. File ini berfungsi sebagai disain dari       program    kalkulator yang akan kita buat. Ketikkan script ini pada file hitung.xml :<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background = “@drawable/background”>
<TextView
android:text=”Masukkan Bilangan Pertama”
android:id=”@+id/TextView01″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_marginTop = “70px”
android:textColor =”#FF0034″>
</TextView>
<EditText
android:text=” “
android:id=”@+id/EditTxt1″
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_marginTop =”90px” android:singleLine=”true” android:numeric=”integer|signed|decimal”>
</EditText>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Masukkan Bilangan Kedua”
android:id=”@+id/TextView02″
android:layout_below=”@+id/EditTxt1″
android:textColor=”#FF0034″
android:layout_marginTop = “5px”>
</TextView> 
<EditText
android:layout_width=”fill_parent”
android:text=”"
android:layout_height=”wrap_content”
android:layout_below=”@+id/TextView02″
android:id=”@+id/EditTxt2″ android:numeric=”integer|signed|decimal” android:singleLine=”true”>
</EditText>
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Pilih Operasi”
android:id=”@+id/TextView03″
android:layout_below=”@+id/EditTxt2″
android:textColor = “#FF0034″
android:layout_marginTop = “5px”>
</TextView>
<RadioGroup
android:id=”@+id/RadioButtonGrp”
android:layout_below=”@+id/EditTxt2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:orientation=”horizontal”
android:layout_marginTop = “20px”
android:layout_centerHorizontal=”true” >
<RadioButton
android:text=”+”
android:id=”@+id/Rjumlah”
android:layout_width=”wrap_content”
android:textColor=”#FF0034″
android:layout_height=”wrap_content”></RadioButton>
<RadioButtonandroid:text=”-”
android:id=”@+id/Rkurang”
android:textColor=”#FF0034″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”></RadioButton>
<RadioButton
android:text=”*”
android:id=”@+id/Rkali”
android:textColor=”#FF0034″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”/”
android:id=”@+id/Rbagi”
android:textColor=”#FF0034″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
<RadioButton
android:text=”%”
android:id=”@+id/Rmodulus”
android:textColor=”#FF0034″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”>
</RadioButton>
</RadioGroup>
<Button
android:layout_height=”wrap_content”
android:text=” Calculate “
android:layout_width=”wrap_content”
android:layout_below=”@+id/EditTxt2″
android:id=”@+id/hitung”
android:layout_marginTop = “120px”
android:layout_alignParentRight=”true”>
</Button>
<EditText
android:layout_width=”75px”
android:text=”"
android:layout_height=”wrap_content”
android:layout_below=”@+id/RadioButtonGrp”
android:id=”@+id/EditTxt3″
android:layout_centerHorizontal=”true”
android:singleLine=”true”
android:numeric=”integer|signed|decimal”
android:layout_marginTop=”10px”
android:textStyle=”bold”
android:gravity=”center”>
</EditText>
</RelativeLayout>
4.      Lalu buka file calcu.java yang telah kita buat tadi. Ketikkan script ini pada file calcu.java :
package calcu.android;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup; 
public class calcu extends Activity implements OnClickListener {
// Deklarisasi Variabel
EditText txt1, txt2, txt3;
RadioButton r1, r2, r3, r4, r5;
RadioGroup rgp;
double bil1, bil2, hasil;
Button btn1;
boolean btnr1, btnr2, btnr3, btnr4,btnr5;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hitung);
// Proses Inisialisasi
// Tombol hitung
btn1 = (Button) findViewById(R.id.hitung);
btn1.setOnClickListener(this);
// Inputan bilangan pertama
txt1 = (EditText) findViewById(R.id.EditTxt1);
txt1.getText();
// Inputan bilangan kedua
txt2 = (EditText) findViewById(R.id.EditTxt2);
txt2.getText();
// Outputan hasil perhitungan
txt3 = (EditText) findViewById(R.id.EditTxt3);
txt3.getText();
// Operator perhitungan
rgp = (RadioGroup) findViewById(R.id.RadioButtonGrp);
r1 = (RadioButton) findViewById(R.id.Rjumlah);
r1.setOnClickListener(this);
r2 = (RadioButton) findViewById(R.id.Rkurang);
r2.setOnClickListener(this);
r3 = (RadioButton) findViewById(R.id.Rkali);
r3.setOnClickListener(this);
r4 = (RadioButton) findViewById(R.id.Rbagi);
r4.setOnClickListener(this);
r5 = (RadioButton) findViewById(R.id.Rmodulus);
r5.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Proses inisialisasi dan pengambilan data bil1 dan bil2
bil1 = Double.valueOf((String.valueOf(txt1.getText())));
bil2 = Double.valueOf((String.valueOf(txt2.getText())));
//Proses perhitungan aritmatikanya :
//Penjumlahan
if (v==r1) {
btnr1 = true;
btnr2 = false;
btnr3 = false;
btnr4 = false;
btnr5 = false;
//Pengurangan
} else if (v==r2) {
btnr1 = false;
btnr2 = true;
btnr3 = false;
btnr4 = false;
btnr5 = false;
} else if (v==r3) {
btnr1 = false;
btnr2 = false;
btnr3 = true;
btnr4 = false;
btnr5 = false;
}else if (v==r4) {
btnr1 = false;
btnr2 = false;
btnr3 = false;
btnr4 = true;
btnr5 = false;
}else if (v==r5) {
btnr1 = false;
btnr2 = false;
btnr3 = false;
btnr4 = false;
btnr5 = true;
}
if (v == btn1) {
if (btnr1 == true) {
hasil = bil1 + bil2;
}else if(btnr2==true){
hasil = bil1 – bil2;
}else if (btnr3==true) {
hasil = bil1 * bil2;
}else if (btnr4==true) {
hasil = bil1 / bil2;
}else if (btnr5==true) {
hasil = bil1 % bil2;
}
txt3.setText(“” + hasil);
}
}
}
5.      Kemudian tambahkan splash screen pada program ini, agar pada saat program dijalankan tidak langsung masuk ke aplikasi. Buat class baru dengan nama splash.java dan ketikkan script ini pada file tersebut :
package calcu.android;import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle; 
public class splash extends Activity {
protected boolean active = true;
protected int splashTime = 3000;
public static int time =25;
Context ctx=this;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Thread splashThread = new Thread(){
@Override
public void run (){
try{
int waited=0;
while (active &&(waited < splashTime)){
sleep(100);
if(active){
waited +=100;
}
}
}catch (InterruptedException e){
}finally{
finish();
Intent i=new Intent(ctx,calcu.class);
startActivity(i);
stop();
}
}
};
splashThread.start();
}
}