Created
March 18, 2016 17:38
-
-
Save salayhin/d844141c524a74d9b9a3 to your computer and use it in GitHub Desktop.
OCT 2015 ICT 5101 Assignment 1014311039
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
/** | |
--------------------------------------------------------------------------------------------------------- | |
Student Name: Md Sirajus Salayhin | |
Student ID: 1014311039 | |
Email: [email protected] | |
--------------------------------------------------------------------------------------------------------- | |
OCT 2015 ICT 5101 Assignment 1014311039 | |
--------------------------------------------------------------------------------------------------------- | |
Problem Statement (Bus Terminal): | |
You have to implement a bus terminal system. First, there will be 4 options to choose. | |
1 for printing all available buses in the terminal, | |
2 for a new bus entry in the terminal (if there is empty place), | |
3 for exiting the first bus in the queue and | |
4 for exiting the program. | |
There are only 5 places in the bus stand and bus number would be from 1 to 30. | |
You have to print mesage after each successful entry or exit. | |
You also have to print appropriate message each unsuccessful event or unusual condition. | |
--------------------------------------------------------------------------------------------------------- | |
*/ | |
#include <stdio.h> | |
#define MAX 5 | |
/* Initialization */ | |
int rear = -1; | |
int front = -1; | |
int busTerminal[MAX]; | |
/* Function for print terminal and bus status */ | |
void printBusStatus(){ | |
int i; | |
if(front == -1) | |
printf("\n \tThere is no bus in Terminal."); | |
else | |
{ | |
printf("\n \tAvailable buses in the terminal\n "); | |
for(i = front; i <= rear; i++) | |
{ | |
printf("\t %d", busTerminal[i]); | |
} | |
} | |
} | |
/* Function for enter new bus to the terminal*/ | |
void enterBus(){ | |
int num, i, j, k; | |
int flag = 0; | |
printf("\n \tWhich bus you want to enter to the terminal?: "); | |
scanf("%d", &num); | |
if(front != -1){ | |
for(i = front; i<= rear; i++) | |
{ | |
if(busTerminal[i] == num){ | |
flag = 1; | |
break; | |
} | |
} | |
} | |
if(num <1 || num >30){ | |
printf("\n \tBus number is invalid."); | |
printf("\n \tPlease enter valid bus number. Valid bus number are 1-30"); | |
}else if(flag == 1){ | |
printf("\n \tBus is already in the terminal"); | |
} | |
else if(front == 0 && rear == MAX-1){ | |
printf("\n \tNo empty space in the terminal"); | |
} | |
else if(front == -1 && rear == -1) | |
{ | |
front = rear = 0; | |
busTerminal[rear] = num; | |
} | |
else if(rear == MAX-1 && front !=0) | |
{ | |
k = front; | |
for(j=0; j<= rear-front; j++){ | |
busTerminal[j] = busTerminal[k]; | |
k++; | |
} | |
rear = rear - front + 1; | |
front = 0; | |
busTerminal[rear] = num; | |
} | |
else | |
{ | |
rear++; | |
busTerminal[rear] = num; | |
} | |
} | |
/* Function for exit bus from terminal */ | |
void exitBus(){ | |
int bus; | |
if(front == -1) | |
{ | |
printf("\n \tNo bus in the terminal."); | |
} | |
else{ | |
bus = busTerminal[front]; | |
front++; | |
printf("\n \tBus number %d left the terminal.",bus); | |
} | |
if(front == rear) | |
front = rear = -1; | |
else | |
{ | |
if(front == MAX-1) | |
front = 0; | |
} | |
} | |
/** Main function of the program **/ | |
void main(){ | |
printf("Bus Terminal:"); | |
while(1){ | |
printf("\n\n\n1. Print available buses in the terminal"); | |
printf("\n2. New bus entry in the terminal"); | |
printf("\n3. Exit the first bus in the queue"); | |
printf("\n4. Exit the program"); | |
printf("\n\n\tChoose a Number for desired operation: "); | |
int option; | |
scanf("%d", &option); | |
switch(option){ | |
case 1: | |
printBusStatus(); | |
break; | |
case 2: | |
enterBus(); | |
break; | |
case 3: | |
exitBus(); | |
break; | |
case 4: | |
printf("\n \tThank you for using this program"); | |
return 0; | |
default: | |
printf("\n \tOoOpSs!!! Invalid choice, please choice from 1 to 4."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment