Untitled

 avatar
unknown
c_cpp
4 months ago
6.3 kB
7
Indexable
#include <iostream>
#include <string>
#include <chrono>
#include <iomanip>
using namespace std;

// Fungsi iteratif untuk menghitung total biaya berdasarkan kWh
int hitung_kwh_iterative(int kwh) {
    int cost = 0;
    int arr_kwh[] = {450, 900};
    int arr_cost[] = {4150, 4660, 4960};

    for (int i = 0; i < kwh; i++) {
        if (i < arr_kwh[0]) {
            cost += arr_cost[0];
        } else if (i >= arr_kwh[0] && i < arr_kwh[1]) {
            cost += arr_cost[1];
        } else {
            cost += arr_cost[2];
        }
    }

    return cost;
}

// Fungsi rekursif untuk menghitung total biaya berdasarkan kWh
int hitung_kwh_recursive(int kwh) {
    int arr_kwh[] = {450, 900};
    int arr_cost[] = {4150, 4660, 4960};

    if (kwh <= 0) return 0;

    if (kwh <= arr_kwh[0]) {
        return arr_cost[0] + hitung_kwh_recursive(kwh - 1);
    } else if (kwh > arr_kwh[0] && kwh <= arr_kwh[1]) {
        return arr_cost[1] + hitung_kwh_recursive(kwh - 1);
    } else {
        return arr_cost[2] + hitung_kwh_recursive(kwh - 1);
    }
}

void print_execution_table() {
    using namespace chrono;

    int test_values[] = {1000, 2000, 3000, 4000, 5000};
    cout << left << setw(10) << "n" << setw(20) << "Recursive Time (ms)" << setw(20) << "Iterative Time (ms)" << endl;
    cout << string(50, '-') << endl;

    for (int n : test_values) {
        auto start_recursive = high_resolution_clock::now();
        hitung_kwh_recursive(n);
        auto end_recursive = high_resolution_clock::now();
        auto recursive_time = duration_cast<milliseconds>(end_recursive - start_recursive).count();

        auto start_iterative = high_resolution_clock::now();
        hitung_kwh_iterative(n);
        auto end_iterative = high_resolution_clock::now();
        auto iterative_time = duration_cast<milliseconds>(end_iterative - start_iterative).count();

        cout << left << setw(10) << n << setw(20) << recursive_time << setw(20) << iterative_time << endl;
    }

    cout << "\nTekan enter untuk melanjutkan...";
    cin.get();
}

void transaction(string history[][5], int& length_history) {
    cout << "============================================" << endl;
    cout << "|                TRANSACTION               |" << endl;
    cout << "============================================" << endl;
    int no_customer, kwh;
    string nama_customer;
    string metode_pembayaran[] = {"Transfer Bank", "Kartu Kredit", "E-Wallet"};

    cout << "\nMasukkan Nomor Customer   : ";
    cin >> no_customer;

    cout << "Masukkan Nama Customer    : ";
    cin >> nama_customer;

    cout << "Masukkan Jumlah Kwh       : ";
    cin >> kwh;

    // Menggunakan metode iteratif secara default
    int total_biaya = hitung_kwh_iterative(kwh);

    cout << "\nTotal Biaya per-kWh       : RP" << total_biaya << endl;

    cout << "============================================" << endl;
    cout << "|             Metode Pembayaran            |" << endl;
    cout << "============================================" << endl;

    for (int i = 0; i < sizeof(metode_pembayaran) / sizeof(*metode_pembayaran); i++) {
        cout << i + 1 << ". " << metode_pembayaran[i] << endl;
    }

    int pilihan_metode = 0;
    cout << "\nPilih metode pembayaran : ";
    cin >> pilihan_metode;

    // Menambahkan transaksi ke dalam riwayat
    history[length_history][0] = to_string(no_customer);
    history[length_history][1] = nama_customer;
    history[length_history][2] = to_string(kwh);
    history[length_history][3] = to_string(total_biaya); // biaya kwh
    history[length_history][4] = metode_pembayaran[pilihan_metode - 1]; // metode pembayaran

    length_history++;  // Update panjang riwayat
}

void tampilkan_riwayat(string history[][5], int length_history) {
    cout << "============================================" << endl;
    cout << "|             Riwayat Transaksi            |" << endl;
    cout << "============================================" << endl;

    if (length_history == 0) {
        cout << "Tidak ada riwayat transaksi.\n";
        return;
    }

    for (int i = 0; i < length_history; i++) {
        if (history[i][0] != "") {
            cout << "Nomor Customer   : " << history[i][0] << endl;
            cout << "Nama Customer    : " << history[i][1] << endl;
            cout << "Jumlah Kwh       : " << history[i][2] << endl;
            cout << "Biaya per-Kwh    : " << history[i][3] << endl;
            cout << "Metode Pembayaran: " << history[i][4] << endl;
            cout << "-----------------------------\n";
        }
    }
}

int main() {
    string history[15][5] = {};
    int length_history = 0;
    int menu;

    do {
        cout << "============================================" << endl;
        cout << "|                   MENU                   |" << endl;
        cout << "============================================" << endl;
        cout << "1. Transaction\n";
        cout << "2. Riwayat Transaksi\n";
        cout << "3. Cetak Tabel Perbandingan Waktu\n";
        cout << "4. Keluar\n\n";
        cout << "============================================" << endl;

        cout << endl << "Pilih Menu : ";
        cin >> menu;
        cin.ignore(); // Untuk membersihkan buffer setelah input angka
        system("clear");

        switch (menu) {
            case 1:
                transaction(history, length_history);
                system("clear");
                break;

            case 2:
                tampilkan_riwayat(history, length_history);
                cout << "\nTekan enter untuk melanjutkan...";
                cin.get();
                system("clear");
                break;

            case 3:
                print_execution_table();
                cout << "\nTekan enter untuk melanjutkan...";
                cin.get();
                system("clear");
                break;

            case 4:
                exit(0);
                break;
            default:
                cout << "\nPilihan tidak terdaftar\n\n";
                cin.get();
                system("clear");
                break;
        }
    } while (menu != 4);

    return 0;
}
Editor is loading...
Leave a Comment