Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
4.7 kB
3
Indexable
Never
#include <iostream>
#include <string>
#include <vector>

using namespace std;

const int TABLE_SIZE = 11;

class Mahasiswa {
public:
    string nim;
    int nilai;

    Mahasiswa(string nim, int nilai) {
        this->nim = nim;
        this->nilai = nilai;
    }
};

class HashNode {
public:
    string key;
    vector<Mahasiswa*> mahasiswas;

    HashNode(string key) {
        this->key = key;
    }

    void addMahasiswa(string nim, int nilai) {
        mahasiswas.push_back(new Mahasiswa(nim, nilai));
    }
};

class HashMap {
private:
    vector<HashNode*> table[TABLE_SIZE];

public:
    int hashFunc(string key) {
        int hash_val = 0;
        for (char c : key) {
            hash_val += c;
        }
        return hash_val % TABLE_SIZE;
    }

    void insert(string key, string nim, int nilai) {
        int hash_val = hashFunc(key);
        for (auto node : table[hash_val]) {
            if (node->key == key) {
                node->addMahasiswa(nim, nilai);
                return;
            }
        }
        HashNode* new_node = new HashNode(key);
        new_node->addMahasiswa(nim, nilai);
        table[hash_val].push_back(new_node);
    }

    void remove(string key) {
        int hash_val = hashFunc(key);
        for (auto it = table[hash_val].begin(); it != table[hash_val].end(); it++) {
            if ((*it)->key == key) {
                table[hash_val].erase(it);
                return;
            }
        }
    }

    void searchByNIM(string key, string nim) {
        int hash_val = hashFunc(key);
        for (auto node : table[hash_val]) {
            if (node->key == key) {
                for (auto mahasiswa : node->mahasiswas) {
                    if (mahasiswa->nim == nim) {
                        cout << "Mahasiswa dengan NIM " << nim << " ditemukan, nilai: " << mahasiswa->nilai << endl;
                        return;
                    }
                }
                cout << "Mahasiswa dengan NIM " << nim << " tidak ditemukan" << endl;
                return;
            }
        }
        cout << "Data tidak ditemukan" << endl;
    }

    void searchByRange(string key, int min_range, int max_range) {
        int hash_val = hashFunc(key);
        for (auto node : table[hash_val]) {
            if (node->key == key) {
                cout << "Mahasiswa dengan nilai antara " << min_range << " dan " << max_range << ": ";
                for (auto mahasiswa : node->mahasiswas) {
                    if (mahasiswa->nilai >= min_range && mahasiswa->nilai <= max_range) {
                        cout << mahasiswa->nim << " ";
                    }
                }
                cout << endl;
                return;
            }
        }
        cout << "Data tidak ditemukan" << endl;
    }

};

int main() {
    HashMap mahasiswa_map;
    char choice;
    string nim, key;
    int nilai, min_range, max_range;

    do {
        cout << "Menu:" << endl;
        cout << "1. Tambah data mahasiswa" << endl;
        cout << "2. Hapus data mahasiswa" << endl;
        cout << "3. Cari mahasiswa berdasarkan NIM" << endl;
        cout << "4. Cari mahasiswa berdasarkan rentang nilai (80 - 90)" << endl;
        cout << "5. Keluar" << endl;
        cout << "Pilihan Anda: ";
        cin >> choice;

        switch (choice) {
            case '1':
                cout << "Masukkan NIM: ";
                cin >> nim;
                cout << "Masukkan nilai: ";
                cin >> nilai;
                cout << "Masukkan key: ";
                cin >> key;
                mahasiswa_map.insert(key, nim, nilai);
                break;
            case '2':
                cout << "Masukkan key: ";
                cin >> key;
                mahasiswa_map.remove(key);
                break;
            case '3':
                cout << "Masukkan key: ";
                cin >> key;
                cout << "Masukkan NIM yang ingin dicari: ";
                cin >> nim;
                mahasiswa_map.searchByNIM(key, nim);
                break;
            case '4':
                min_range = 80;
                max_range = 90;
                cout << "Masukkan key: ";
                cin >> key;
                mahasiswa_map.searchByRange(key, min_range, max_range);
                break;
            case '5':
                cout << "Keluar dari program..." << endl;
                break;
            default:
                cout << "Pilihan tidak valid. Silakan coba lagi." << endl;
                break;
        }
    } while (choice != '5');

    return 0;
}
Leave a Comment