Skip to content

Instantly share code, notes, and snippets.

@mufti1
Created March 10, 2019 09:05
Show Gist options
  • Save mufti1/0b800ca0d54f3ee521fb48b455422baa to your computer and use it in GitHub Desktop.
Save mufti1/0b800ca0d54f3ee521fb48b455422baa to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
struct exam {
char examName[40];
char examCode[5];
int date;
int timeExam;
int room;
struct exam *next;
}*head, *tail, *curr;
void display(){
int j=0;
printf("------------------- Jadwal Ujian ----------------\n");
for(j = 0; j < 6; j++)
printf("=");
printf("\n");
printf("| %-15s | %-20s | %-15s | %-8s | %-4s |\n", "Kode", "Nama Matkul", "Tanggal", "Jam", "Ruangan");
for(j =0; j < 6; j++)
printf("=");
printf("\n");
curr = head;
while(curr) {
printf("| %-15s | %-20s | %-15d | %-8d | %-4d |\n", curr->examCode, curr->examName, curr->date, curr->timeExam, curr->room);
curr = curr->next;
}
for(j=0; j<6; j++)
printf("=");
printf("\n");
getchar();
fflush(stdin);
}
void push(char examCode[], char examName[], int date, int timeExam, int room) {
curr = (struct exam*)malloc(sizeof(struct exam));
strcpy(curr->examCode, examCode);
strcpy(curr->examName, examName);
curr->date = date;
curr->timeExam = timeExam;
curr->room = room;
if (head==NULL) {
head = tail = curr;
}else{
tail->next = curr;
tail = curr;
}
tail->next = NULL;
}
void pop(char examCode[]){
curr = head;
while(curr != NULL && strcmp(curr->examCode, examCode) != 0) {
curr = curr->next;
}
if(curr == NULL){
printf("Kode ujian tidak ditemukan\n");
}else if(head==curr){
head = head->next;
free(curr);
printf("Jadwal ujian berhasil dihapus\n");
}else if(head!=curr && tail!=curr){
struct exam * temp;
temp = head;
while(temp->next != curr) temp = temp->next;
temp->next = curr->next;
free(curr);
printf("Jadwal ujian berhasil dihapus\n");
}else if(tail==curr){
exam *current=new exam;
exam *previous=new exam;
current=head;
while(current->next!=NULL) {
previous=current;
current=current->next;
}
tail=previous;
previous->next=NULL;
free(current);
printf("Jadwal ujian berhasil dihapus\n");
}
}
void popall(){
while(head!=NULL){
curr=head;
head=head->next;
free(curr);
}
}
void menu(){
printf("\n Jadwal Ujian");
printf("\n =============");
printf("\n 1. Lihat Jadwal");
printf("\n 2. Tambah Jadwal");
printf("\n 3. Hapus Jadwal");
printf("\n 4. Keluar");
printf("\n >> Silakan pilih salah satu menu: ");
}
void clear(){
for(int i=0; i<3; i++)
printf("\n");
}
int main() {
int choice;
char examCode[5];
char examName[40];
int date;
int timeExam;
int room;
do{
clear();
menu();
scanf("%d", &choice);
fflush(stdin);
switch(choice) {
case 1 : display();
break;
case 2 :
printf("Masukan Kode Ujian[5]: ");
scanf("%s", examCode);
if (strlen(examCode)>5){
printf("maksimal panjang kode adalah 5");
break;
}
fflush(stdin);
printf("\n");
getchar();
printf("Masukan Nama Mata Kuliah[40]: ");
scanf("%[^\n]s",examName);
if (strlen(examName)>40){
printf("maksimal nama matkul adalah 40");
break;
}
fflush(stdin);
printf("\n");
getchar();
printf("Masukan Tanggal [1...30]: ");
scanf("%d", &date);
if (date == 0 || date > 31) {
printf("maksimal tanggal adalah 31");
break;
}
fflush(stdin);
printf("\n");
getchar();
printf("Masukan Waktu Ujian [8...17]: ");
scanf("%d", &timeExam);
if (timeExam < 8 || timeExam > 17){
printf("waktu jam ujian adalah jam 8 sampai 17");
break;
}
fflush(stdin);
printf("\n");
getchar();
printf("Masukan Ruang Ujian [3]: ");
scanf("%d", &room);
fflush(stdin);
push(examCode, examName, date, timeExam, room);
getchar();
fflush(stdin);
break;
case 3 :
if(head==NULL){
printf("Tidak ada jadwal ujian..");
}else{
printf("Masukan Kode Ujian[5]: ");
scanf("%s", examCode);
fflush(stdin);
pop(examCode);
}
getchar();
fflush(stdin);
break;
case 4 :
popall();
break;
}
}while(choice!=4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment