-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <iostream> | ||
#include "stack.h" | ||
#include "queue.h" | ||
#include <stdlib.h> | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
Stack S; | ||
cout<<"PERHATIKAN "<<endl; | ||
cout<<"cara mendeklarasikan address pada stack : addressS P"<<endl; | ||
cout<<"cara mendeklarasikan address pada queue : addressQ P"<<endl; | ||
cout<<"cara mendeklarasikan isi elemen Stack : info(P)"<<endl; | ||
cout<<"cara mendeklarasikan isi elemen waktu pada queue : waktu(P)"<<endl; | ||
cout<<"cara mendeklarasikan isi elemen ID pada queue : ID(P)"<<endl; | ||
cout<<"\t[press enter to continue]"<<endl<<endl; | ||
cin.get(); | ||
system("CLS"); | ||
cout<<"SOAL 1 : isi prosedur dalam stack.cpp"<<endl; | ||
cout<<"contoh output"<<endl; | ||
cout<<"Masukkan Operasi Matematika : ( a * ( b - c ) + ( ( j + k ) / ( p + q * r ) ) ^ y ). "<<endl; | ||
cout<<"SEIMBANG"<<endl<<endl; | ||
|
||
cout<<"Masukkan Operasi Matematika : (a + b) * c + (a - b "<<endl; | ||
cout<<"TIDAK SEIMBANG"<<endl<<endl; | ||
|
||
cout<<"Program anda "<<endl<<endl; | ||
soal1(); | ||
cin.get(); | ||
cout<<endl<<endl<<"\t[press enter to continue]"<<endl; | ||
cin.get(); | ||
system("CLS"); | ||
|
||
|
||
cout<<"SOAL 2 : isi prosedur dalam stack.cpp"<<endl; | ||
cout<<"buatlah 3 Stack baru dengan createS lalu inputkan beberapa karakter ke dalam S1"<<endl; | ||
cout<<"lalu S1 tersebut dimasukkan ke dalam S2 dan S3 serta tampilkan isi dari ketiga Stack tersebut"<<endl; | ||
cout<<"perhatikan ilustrasi dalam soal sbg contoh"<<endl<<endl; | ||
cout<<"Program anda "<<endl<<endl; | ||
|
||
soal2(); | ||
cin.get(); | ||
cout<<"\t[press enter to continue]"<<endl; | ||
cin.get(); | ||
system("CLS"); | ||
|
||
cout<<"SOAL 3 : isi prosedur dalam queue.cpp"<<endl; | ||
cout<<"inputkan 5 data ke dalam Queue"<<endl; | ||
cout<<"program anda harus menampilkan seluruh data yg telah di input"<<endl; | ||
cout<<"lakukan proses dequeu enqueu di setiap literasi dan tampilkan setiap perubahannya sampai Queuenya kosong"<<endl; | ||
|
||
cout<<"Program anda"<<endl<<endl; | ||
soal3(); | ||
cin.get(); | ||
cout<<"\t[press enter to continue]"<<endl; | ||
cin.get(); | ||
system("CLS"); | ||
|
||
cout<<endl; | ||
|
||
return 0; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include<iostream> | ||
#include"queue.h" | ||
|
||
bool isEmptyQ(Queue Q){ | ||
|
||
if(head(Q) == NULL && tail(Q)==NULL){ | ||
return true; | ||
}else{ | ||
return false; | ||
} | ||
|
||
} | ||
|
||
void createQ(Queue &Q){ | ||
head(Q) = NULL; | ||
tail(Q) = NULL; | ||
} | ||
|
||
void createNewElmtQ(string ID,int waktu,addressQ &P){ | ||
P = new elmQ; | ||
ID(P) = ID; | ||
waktu(P) = waktu; | ||
next(P) = NULL; | ||
} | ||
|
||
void enqueue(Queue &Q,addressQ P) | ||
{ | ||
if(isEmptyQ(Q)) | ||
{ | ||
head(Q) = P; | ||
tail(Q) = P; | ||
} | ||
else | ||
{ | ||
next(tail(Q)) = P; | ||
tail(Q) = P; | ||
} | ||
} | ||
|
||
void dequeue(Queue &Q,addressQ &P) | ||
{ | ||
|
||
|
||
if(!isEmptyQ(Q)) | ||
{ | ||
P = head(Q); | ||
if(head(Q) == tail(Q)) | ||
{ | ||
head(Q) = NULL; | ||
tail(Q) = NULL; | ||
} | ||
else | ||
{ | ||
head(Q) = next(head(Q)); | ||
next(P) = NULL; | ||
} | ||
|
||
} | ||
else{ | ||
P = NULL; | ||
} | ||
} | ||
|
||
void soal3() | ||
{ | ||
Queue Q; addressQ W; | ||
|
||
createQ(Q); | ||
createNewElmtQ("T25", 13, W); | ||
enqueue(Q, W); | ||
createNewElmtQ("T1", 4, W); | ||
enqueue(Q, W); | ||
createNewElmtQ("T11", 5, W); | ||
enqueue(Q, W); | ||
createNewElmtQ("T21", 21, W); | ||
enqueue(Q, W); | ||
createNewElmtQ("T8", 3, W); | ||
enqueue(Q, W); | ||
addressQ R; | ||
while(!isEmptyQ(Q)){ | ||
dequeue(Q, W); | ||
if (waktu(W)>5){ | ||
waktu(W) = waktu(W)-5; | ||
enqueue(Q, W); | ||
} | ||
R = head(Q); | ||
if (!isEmptyQ(Q)){ | ||
cout<<"Antrian saat ini :"; | ||
while (R!=NULL){ | ||
cout<<" ID : "<<ID(R)<<" waktu: "<<waktu(R)<<" "; | ||
R = next(R); | ||
} | ||
cout<<endl; | ||
} else{ | ||
cout<<"Antrian sudah kosong"<<endl; | ||
} | ||
} | ||
|
||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef QUEUE_H_INCLUDED | ||
#define QUEUE_H_INCLUDED | ||
#include <iostream> | ||
#define Nil NULL | ||
#define ID(P) P->ID | ||
#define waktu(P) P->waktu | ||
#define next(P) P->next | ||
#define head(Q) Q.head | ||
#define tail(Q) Q.tail | ||
|
||
using namespace std; | ||
|
||
typedef struct elmQ * addressQ; | ||
|
||
struct elmQ{ | ||
string ID; | ||
int waktu; | ||
addressQ next; | ||
}; | ||
|
||
struct Queue{ | ||
addressQ head; | ||
addressQ tail; | ||
}; | ||
bool isEmptyQ(Queue Q); | ||
void createQ(Queue &Q); | ||
void createNewElmtQ(string ID,int waktu,addressQ &P); | ||
void enqueue(Queue &Q,addressQ P); | ||
void dequeue(Queue &Q,addressQ &P); | ||
void soal3(); | ||
|
||
|
||
|
||
|
||
#endif // QUEUE_H_INCLUDED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include <stdio.h> | ||
#include "stack.h" | ||
|
||
|
||
bool isEmptyS(Stack S){ | ||
|
||
|
||
if (top(S) == Nil) { | ||
return true; | ||
} | ||
else{ | ||
return false; | ||
} | ||
|
||
} | ||
|
||
void createS(Stack &S){ | ||
top(S) = NULL; | ||
} | ||
|
||
void createNewElmtS(infotype X, addressS &P){ | ||
P = new elmS; | ||
info(P) = X; | ||
next(P) = Nil; | ||
} | ||
|
||
void push(Stack &S, addressS P){ | ||
|
||
if(isEmptyS(S)) | ||
{ | ||
top(S) = P; | ||
} | ||
else{ | ||
next(P) = top(S); | ||
top(S) = P; | ||
} | ||
} | ||
|
||
void pop(Stack &S, addressS &P){ | ||
|
||
if (!isEmptyS(S)){ | ||
P = top(S); | ||
top(S) = next(top(S)); | ||
next(P) = Nil; | ||
} else { | ||
P = NULL; | ||
} | ||
} | ||
|
||
void soal1() | ||
{ | ||
Stack S; addressS Q; | ||
|
||
createS(S); infotype input; | ||
int jumB = 0; | ||
int jumT = 0; | ||
cin>>input; | ||
while(input!='.'){ | ||
createNewElmtS(input, Q); | ||
push(S, Q); | ||
cin>>input; | ||
} | ||
while(!isEmptyS(S)){ | ||
pop(S, Q); | ||
if (info(Q)=='('){ | ||
jumB++; | ||
}else if (info(Q)==')'){ | ||
jumT++; | ||
} | ||
} | ||
if (jumB == 0 && jumT ==0){ | ||
cout<<"Tidak ada tanda kurung"; | ||
}else if (jumB==jumT){ | ||
cout<<"SEIMBANG"; | ||
}else{ | ||
cout<<"TIDAK SEIMBANG"; | ||
} | ||
// | ||
} | ||
|
||
void soal2() | ||
{ | ||
|
||
Stack S1,S2,S3; | ||
|
||
infotype input; | ||
addressS Q; | ||
createS(S1); | ||
createS(S2); | ||
createS(S3); | ||
cin>>input; | ||
while(input!='.'){ | ||
if ( (input>= 'A' && input<='Z') ||(input>= 'a' && input<='z') ){ | ||
createNewElmtS(input, Q); | ||
push(S1, Q); | ||
} | ||
cin>>input; | ||
} | ||
while(!isEmptyS(S1)){ | ||
pop(S1, Q); | ||
if (info(Q)=='a' || info(Q)=='i' || info(Q)=='u' || info(Q)=='e' || info(Q)=='o' || info(Q)=='A' || info(Q)=='I' || info(Q)=='U' || info(Q)=='E' || info(Q)=='O'){ | ||
push(S2, Q); | ||
}else { | ||
push(S3, Q); | ||
} | ||
} | ||
if (isEmptyS(S1)){ | ||
cout<<"Stack S1 sudah kosong."; | ||
} | ||
cout<<endl<<"Stack S2 : "; | ||
while (!isEmptyS(S2)){ | ||
pop(S2, Q); | ||
cout<<info(Q); | ||
} | ||
cout<<endl<<"Stack S3 : "; | ||
while (!isEmptyS(S3)){ | ||
pop(S3, Q); | ||
cout<<info(Q); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef STACK_H_INCLUDED | ||
#define STACK_H_INCLUDED | ||
|
||
#include<iostream> | ||
#define Nil NULL | ||
#define info(P) (P)->info | ||
#define next(P) (P)->next | ||
#define top(S) ((S).top) | ||
|
||
using namespace std; | ||
|
||
typedef char infotype; | ||
typedef struct elmS *addressS; | ||
|
||
struct elmS{ | ||
infotype info; | ||
addressS next; | ||
}; | ||
|
||
struct Stack{ | ||
addressS top; | ||
}; | ||
|
||
bool isEmptyS(Stack S); | ||
void createS(Stack &S); | ||
void createNewElmtS(infotype X, addressS &P); | ||
void push(Stack &S, addressS P); | ||
void pop(Stack &S, addressS &P); | ||
void soal1(); | ||
void soal2(); | ||
|
||
|
||
#endif // STACK_H_INCLUDED |