Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

#include<stdio.

h>
#include<stdlib.h>
#include<string.h>
struct obat{
char nama[50];
char resep[100];
struct obat *next;
}*head = NULL, *tail = NULL, *head2 = NULL, *tail2 = NULL;
void insert(char nama[], char resep[]){
struct obat *newObat = (struct obat*)malloc(sizeof(struct obat));
strcpy(newObat->nama, nama);
strcpy(newObat->resep, resep);
newObat->next = NULL;
if(head == NULL){
head = newObat;
tail = newObat;
}
else{
tail->next = newObat;
tail = newObat;
}
}
void pop_head(){
if(head != NULL){
struct obat *temp = head;

if(head == tail){
head = NULL;
tail = NULL;
}
else{
head = head->next;
}
free(temp);
}
}
void pop_mid(){
if(head != NULL){
if(head->next != NULL){
struct obat *temp = head->next;
head->next = temp->next;
temp->next = NULL;
free(temp);
}
}
}
void view_all_antrian(){
struct obat *curr = head;
if(curr == NULL){
printf("Tidak ada obat!\n");
return;
}

while(curr != NULL){
printf("[%s | %s]\n", curr->nama, curr->resep);
curr = curr->next;
}
}
void print_obat_selesai(char nama[]){
printf("Obat berikut selesai diracik\n");
struct obat *newObat = (struct obat*)malloc(sizeof(struct obat));
strcpy(newObat->nama, nama);
newObat->next = NULL;
if(head2 == NULL){
head2 = newObat;
tail2 = newObat;
}
else{
tail2->next = newObat;
tail2 = newObat;
}
struct obat *curr = head2;
if(curr == NULL){
printf("Tidak ada obat!\n");
return;
}
while(curr != NULL){
printf("[%s]\n", curr->nama);
curr = curr->next;
}
}
void tangani_obat(){
printf("Obat yang dilayani:\n");
view_all_antrian();
struct obat *apoteker1 = head;
struct obat *apoteker2 = head->next;
char nama[50];
printf("%s , %s sedang ditangani\n", apoteker1->nama, apoteker1->resep);
printf("%s , %s sedang ditangani\n", apoteker2->nama, apoteker2->resep);

printf("masukan obat yg sudah beres: ");


scanf("%s", nama);
if(strcmp(nama, apoteker1->nama) == 0){
pop_head();
printf("obat %s berhasil diracik\n", nama);
}
else{
pop_mid();
printf("obat %s berhasil diracik\n", nama);
}
printf("\nAntrian tersisa: \n");
view_all_antrian();
print_obat_selesai(nama);
printf("\n");
}
int main(){
insert("Panadol", "abc");
insert("Neozepam", "def");
insert("Extrasol", "ghi");
insert("Salonpas", "jkl");
tangani_obat();
tangani_obat();
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct obat{
char nama[50];
char resep[100];
struct obat *next;
}*head = NULL, *tail = NULL, *head2 = NULL, *tail2 = NULL;
struct obat *apoteker1 = NULL, *apoteker2 = NULL;
void insert(char nama[], char resep[]){
struct obat *newObat = (struct obat*)malloc(sizeof(struct obat));
strcpy(newObat->nama, nama);
strcpy(newObat->resep, resep);
newObat->next = NULL;

if(head == NULL){
head = newObat;
tail = newObat;
}
else{
tail->next = newObat;
tail = newObat;
}
}
void pop_head(){
if(head != NULL){

struct obat *temp = head;


if(head == tail){
head = NULL;
tail = NULL;
}
else{
head = head->next;
}
free(temp);
}
}
void pop_mid(){
if(head != NULL){
if(head == tail){
head = NULL;
tail = NULL;
}
else if(head->next != NULL){
struct obat *temp = head->next;
head->next = temp->next;
temp->next = NULL;
free(temp);
}
}
}
void view_all_antrian(struct obat *start){
struct obat *curr = start;
if(curr == NULL){
printf("Tidak ada obat!\n");
return;
}
while(curr != NULL){
if(curr == apoteker1 || curr == apoteker2){
printf("[%s | %s] (sedang diproses)\n", curr->nama, curr->resep);
}else{
printf("[%s | %s]\n", curr->nama, curr->resep);
}
curr = curr->next;
}
}

void obat_selesai(char nama[], char resep[]){


struct obat *obatBeres = (struct obat*)malloc(sizeof(struct obat));
strcpy(obatBeres->nama, nama);
strcpy(obatBeres->resep, resep);
obatBeres->next = NULL;
if(head2 == NULL){
head2 = obatBeres;
tail2 = obatBeres;
}
else{
tail2->next = obatBeres;
tail2 = obatBeres;
}
}
void tangani_obat(){
printf("\nObat yang ditangani:\n");
if(head != NULL){
apoteker1 = head;
}
if(head->next != NULL){
apoteker2 = head->next;
}
printf("%s, %s sedang ditangani\n", apoteker1->nama, apoteker1->resep);
printf("%s, %s sedang ditangani\n", apoteker2->nama, apoteker2->resep);
}
void confirm_done(){
char nama[50];
char resep[100];
if(!apoteker1 && !apoteker2){
printf("Tidak ada obat diproses.\n");
return;
}
printf("masukan obat yg sudah beres [x untuk keluar]: ");
scanf("%s", nama);
if(strcmp(nama, "x") == 0){
return;
}
else if(apoteker1 != NULL && strcmp(nama, apoteker1->nama) == 0){
strcpy(resep, apoteker1->resep);
pop_head();
printf("obat %s berhasil diracik\n", nama);
apoteker1 = NULL;
}
else if(apoteker2 != NULL && strcmp(nama, apoteker2->nama) == 0){
strcpy(resep, apoteker2->resep);
pop_mid();
printf("obat %s berhasil diracik\n", nama);
apoteker2 = NULL;
}
obat_selesai(nama, resep);
}
void option2(){
char answer;
if(apoteker1 != NULL && apoteker2 != NULL){
printf("%s, %s sedang ditangani\n", apoteker1->nama, apoteker1->resep);
printf("%s, %s sedang ditangani\n", apoteker2->nama, apoteker2->resep);
printf("Proses penuh.\n");
}
else{
view_all_antrian(head);
if(apoteker1 != NULL){
printf("%s, %s sedang ditangani\n", apoteker1->nama, apoteker1->resep);
}
else if(apoteker2 != NULL){
printf("%s, %s sedang ditangani\n", apoteker2->nama, apoteker2->resep);
}

if(head != tail){
printf("Proses obat? [y/n] : ");
scanf(" %c", &answer);
if(answer == 'y'){
tangani_obat();
}
}
}
confirm_done();
}
void option1(){
char nama[50];
char resep[100];

printf("Masukkan nama obat baru : ");


scanf("%s", nama);
printf("Masukkan resepnya : ");
scanf("%s", resep);
insert(nama, resep);
printf("\nObat berhasil masuk antrian.\n");
}
void menu(){
printf("1. Masuk obat baru\n");
printf("2. Proses obat\n");
printf("3. Obat selesai di proses\n");
printf("4. Tampilkan antrian obat\n");
printf("0. Exit\n");
}

int main() {
int choice;
do{
system("cls");
menu();
printf(">> ");
scanf("%d", &choice);
switch (choice){
case 1 :
printf("1. Masuk obat baru\n");
option1();
break;
case 2 :
printf("2. Proses obat\n");
option2();
break;
case 3 :
printf("3. Obat selesai di proses\n");
view_all_antrian(head2);
break;
case 4:
printf("4. Tampilkan antrian obat\n");
view_all_antrian(head);
break;
default :
break;
}
system("pause");
}while (choice != 0);

return 0;
}

Copy protected with Online-PDF-No-Copy.com

You might also like