Friday 17 May 2013

C++: Program Konversi Suhu (Part 1)

Pada kesempatan kali ini saya akan share tutorial C++, yaitu program konversi suhu.

1. Langsung aja, buka Microsoft Visual Studio (karena saya menggunakan Microsoft Visual Studio, tapi teman-teman bisa menggunakan code editor lain seperti Borland C++).
2. Buat project baru, saya beri nama projectnya konversi_suhu, pilih WIN32 Console Application.
3. Masukkan code berikut :

// konversi_suhu.cpp : Defines the entry point for the console application.
// Developer : Erik Gunawan
// Nickname  : EXz

#include "stdafx.h"
#include <iostream>
#include <math.h>

using namespace std;

void pilihan();

void header() {
    cout << "Program Konversi " << endl;
    cout << "=============================================" << endl;
    cout << "A = Celsius ke Reamur, Fahrenheit, Kelvin" << endl;
    cout << "B = Reamur ke Celsius, Fahrenheit, Kelvin" << endl;
    cout << "C = Fahrenheit ke Celsius, Reamur, Kelvin" << endl;
    cout << "D = Kelvin ke Celsius, Reamur, Fahrenheit" << endl;
    cout << "=============================================" << endl;
    cout << endl;
}

double celsius, reamur, fahrenheit, kelvin;

void celsiusToAll() {
    cout << "Suhu Celsius    : "; cin >> celsius;

    reamur = (4 * celsius)/5;
    fahrenheit = (9 * celsius)/5 + 32;
    kelvin = celsius + 273;

    cout << endl;
    cout << "Suhu Reamur     : " << reamur << endl;
    cout << "Suhu Fahrenheit : " << fahrenheit << endl;
    cout << "Suhu Kelvin     : " << kelvin << endl;
}

void reamurToAll() {
    cout << "Suhu Reamur     : "; cin >> reamur;

    celsius = (5*reamur)/4;
    fahrenheit = (9*reamur)/4 + 32;
    kelvin = (reamur*5)/4 + 273;

    cout << endl;
    cout << "Suhu Celsius    : " << celsius << endl;
    cout << "Suhu Fahrenheit : " << fahrenheit << endl;
    cout << "Suhu Kelvin     : " << kelvin << endl;
}

void fahrenheitToAll() {
    cout << "Suhu Fahrenheit : "; cin >> fahrenheit;

    celsius = (fahrenheit-32)*5/9;
    reamur = (fahrenheit-32)*4/9;
    kelvin = (fahrenheit-32)*5/9 + 273;

    cout << endl;
    cout << "Suhu Celsius    : " << celsius << endl;
    cout << "Suhu Reamur     : " << reamur << endl;
    cout << "Suhu Kelvin     : " << kelvin << endl;
}

void kelvinToAll() {
    cout << "Suhu Kelvin     : "; cin >> kelvin;

    celsius = kelvin-273;
    reamur = (kelvin-273)*4/5;
    fahrenheit = (kelvin-273)*9/5 + 32;

    cout << endl;
    cout << "Suhu Celsius    : " << celsius << endl;
    cout << "Suhu Reamur     : " << reamur << endl;
    cout << "Suhu Fahrenheit : " << fahrenheit << endl;
}

void pilihan() {
    char pilih, coba_lagi;

    pilih:
    header();
    cout << "Masukkan Pilihan : "; cin >> pilih;
    cout << endl;

    if (pilih=='a' || pilih=='A') {
        celsiusToAll();
    }
    else if (pilih=='b' || pilih=='B') {
        reamurToAll();
    }
    else if (pilih=='c' || pilih=='C') {
        fahrenheitToAll();
    }
    else if (pilih=='d' || pilih=='D') {
        kelvinToAll();
    }
    else {
        system("cls");
        cout << "Kode yang Anda masukkan salah!!" << endl;
        cout << endl;
        goto pilih;
    }

    coba:
    cout << endl;
    cout << "Ingin coba lagi (Y/N) ? "; cin >> coba_lagi;
    if (coba_lagi=='Y' || coba_lagi=='y') {
        system("cls");
        goto pilih;
    } 
    else if (coba_lagi=='N' || coba_lagi=='n') {
        _exit(0);
    } 
    else {
        system("cls");
        cout << "Kode yang Anda masukkan salah!!" << endl;
        cout << endl;
        goto coba;
    }
}

void main() {
    pilihan();
}
4. Run file, hasilnya akan seperti ini :


 5. Masukkan suhu yang ingin dicari.



Untuk download contoh project di atas, klik disini atau konversi_suhu.
Ditunggu kritik saran pertanyaan dan komentarnya. :D

Semoga bermanfaat.

0 komentar:

Post a Comment