Sistem parkir C++ dengan Stack
Stack adalah struktur data linear yang mengikuti urutan tertentu di mana operasi dilakukan. Urutannya mungkin LIFO(Last In First Out) atau FILO(First In Last Out).
Terutama tiga operasi dasar berikut dilakukan dalam tumpukan:
- Dorong: Menambahkan item dalam tumpukan. Jika tumpukan penuh, maka dikatakan sebagai kondisi luapan.
- Pop: Menghapus item dari tumpukan. Item muncul dalam urutan terbalik di mana mereka didorong. Jika tumpukan kosong, maka dikatakan sebagai kondisi Underflow.
- Cuplikan atau Atas: Mengembalikan elemen atas stack.
- isEmpty: Mengembalikan true jika stack kosong, yang lain false.
Implementation:
Dua jalan mengimplementasikan Stack :
- Using array
- Using linked list
Cth :
#include<iostream>
#include<queue>
#include<stack>
#include<time.h>
#include<ctime>
#include<windows.h>
#include <iomanip>
#define capacity 3
using namespace std;
typedef struct Car {
string License="";
double begin=0;
double end=0;
string begin_time;
string end_time;
int order_number=0;
double cost=0;
}Car;
struct cmp2 {
bool operator ()(Car& a, Car& b) {
return a.begin > b.begin;
}
};
int total = 0;
int money = 2000;
bool vis[200];
queue<Car> Waiting;
stack<Car> Parking;
priority_queue<Car,vector<Car>,cmp2> history;
string getTime()
{
time_t timep;
time(&timep);
char tmp[64];
strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S", localtime(&timep));
return tmp;
}
void Wait(string license)
{
Car temp;
int auto t = clock();
temp.License = license;
temp.begin = t;
temp.begin_time = getTime();
temp.order_number = ++total;
Waiting.push(temp);
cout << temp.License << "Menunggu Antrian" << endl;
}
void Add(string license)
{
if (Parking.size()>=capacity)
{
Wait(license);
}
else
{
Car temp;
size_t t = clock();
temp.License = license;
temp.begin= t;
temp.begin_time = getTime();
temp.order_number = ++total;
Parking.push(temp);
cout << temp.License << "\n Masuk Parkir, dimulai dari waktu" << temp.begin_time << endl;
}
}
void Leave(string license, int t_out)
{
stack<Car> temp;
int cnt = Parking.size();
while(!Parking.empty())
{
Car car=Parking.top();
Parking.pop();
if (car.License == license)
{
cout << car.License << "\n Keluar Parkir \n";
size_t t = clock();
car.end = t;
car.end_time = getTime();
car.cost = ((t_out - 2) * (money + 1000));
if(t_out < 3)
{
car.cost = 2000;
cout << "Biaya Parkir: " << car.cost << endl;
}else
{
cout << "Biaya Parkir: " << car.cost << endl;
}
history.push(car);
break;
}
else
{
temp.push(car);
}
}
if (temp.size() == cnt)
{
cout << "Nomor Kendaraan Tidak Terdaftar \n" << endl;
while (!temp.empty())
{
Parking.push(temp.top());
temp.pop();
}
}
else
{
while(!temp.empty())
{
Parking.push(temp.top());
temp.pop();
}
if (!Waiting.empty())
{
Car car = Waiting.front();
Waiting.pop();
Add(car.License);
}
}
}
void Check()
{
if (!Parking.empty())
{
cout << "Daftar Kendaraan di parkiran: \n" << endl;
stack<Car> temp = Parking;
while (!temp.empty())
{
Car x = temp.top();
cout << "Plat Nomor:"<< x.License << "Masuk pada waktu:" << x.begin_time << endl;
temp.pop();
}
}
else
{
cout << "Tidak Ada Mobil Parkir" << endl;
}
if (!Waiting.empty())
{
cout << "Daftar Mobil Menunggu masuk parkir:" << endl;
queue<Car> temp1 = Waiting;
while (!temp1.empty())
{
Car x = temp1.front();
cout <<"No Polisi Yang menunggu:"<<x.License << "\n Menunggu masuk parkir pada waktu:" << x.begin_time << endl;
temp1.pop();
}
}
else
{
cout << "Tidak ada Waiting List Kendaraan" << endl;
}
}
void History_Check()
{
cout << "total" << history.size() << "\n Waktu Kendaraan Berhenti \n" << endl;
priority_queue<Car, vector<Car>, cmp2> temp = history;
int i = 1;
while (!temp.empty())
{
Car x = temp.top();
temp.pop();
cout <<" "<<i++<<"\n Plat Nomor Mobil:"<<x.License << "\n Waktu Pemberhentian:" << x.begin_time << "\n Waktu Keluar:" << x.end_time << "\n Biaya Parkir Adalah:" << x.cost << endl;
}
}
int main()
{
cout << "--------------------------Selamat Datang di Parkir Sistem------------------ --------" << endl;
cout << "1. Mobil Masuk " << endl;
cout << "2. Mobil Keluar " << endl;
cout << "3. Tampil Status Parkir" << endl;
cout << "4. Tampil History Parkir " << endl;
cout << "5. Kosongkan Layar" << endl;
while (1) {
int i;
cout << "Pilih Menu: ";
cin >> i;
switch (i)
{
case 1:
{
string in;
cout << "Silakan Masukan Plat Nomor Masuk:";
cin >> in;
Add(in);
}
break;
case 2:
{
string out;
int t_out;
cout << "Silakan Masukan Plat Nomor Keluar:";
cin >> out;
cout << "Silakan Masukan Lama Waktu:";
cin >> t_out;
Leave(out, t_out);
}
break;
case 3:
{
Check();
break;
}
case 4:
{
memset(vis, false, sizeof(vis));
History_Check();
break;
}
case 5:
{
system("cls");
cout << "--------------------------Selamat Datang di Parkir Sistem------------------ --------" << endl;
cout << "1. Mobil Masuk " << endl;
cout << "2. Mobil Keluar " << endl;
cout << "3. Tampil Status Parkir" << endl;
cout << "4. Tampil History Parkir " << endl;
cout << "5. Kosongkan Layar" << endl;
}
break;
}
}
}
Komentar
Posting Komentar