Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctype.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <string.h>
- // IMPORTANT VARIABLES
- const int MAX_COLUMNS = 12;
- const int MAX_ROWS = 6;
- // FUNCTION DECLARATIONS
- bool check_empty_seat(int row, int column, bool (*seats)[MAX_COLUMNS]);
- bool check_seat(int row, int column, bool (*seats)[MAX_COLUMNS]);
- bool is_empty(bool (*seats)[MAX_COLUMNS]);
- bool is_full(bool (*seats)[MAX_COLUMNS]);
- int get_column(char *seat);
- int get_row(char *seat);
- void color_green();
- void color_red();
- void remove_newline(char *seat);
- void show_seats(bool seats[MAX_ROWS][MAX_COLUMNS]);
- // DRIVER CODE
- int main() {
- bool seat_state[MAX_ROWS][MAX_COLUMNS];
- bool given_seat_status = false;
- bool main_loop = true;
- int row, column;
- char response[8];
- char choice; int num;
- // Initialize all seat's state to FALSE
- for(int i = 0; i < MAX_ROWS; i++) {
- for(int j = 0; j < MAX_COLUMNS; j++) {
- seat_state[i][j] = false;
- }
- }
- // MAIN LOOP
- while(main_loop) {
- system("clear");
- show_seats(seat_state);
- printf("\n");
- printf("\033[1;37m");
- printf("+-----+CUSTOM BOOKING SYSTEM+-----+\n\n");
- printf("[1] Fill-up Seats\n");
- printf("[2] Free-up Seats\n");
- printf("[0] Exit\n");
- printf(">>> ");
- scanf("%c", &choice);
- getchar();
- if(choice < '0' || choice > '9') {
- num = -1;
- } else {
- num = choice - '0';
- }
- switch (num) {
- case 0:
- main_loop = false;
- break;
- case 1:
- printf("\n+---------+FILL-UP SEATS+---------+\n");
- if(is_full(seat_state)) {
- printf("All seats are filled!!\n");
- printf("Press enter to continue...");
- getchar();
- break;
- }
- printf("Type \"EXIT\" to exit...\n");
- printf("Enter seat number: ");
- fgets(response, 8, stdin);
- remove_newline(response);
- if(strcmp(response, "EXIT") == 0) {
- main_loop = false;
- break;
- } else if(strlen(response) < 3 || strlen(response) > 3) {
- printf("Invalid seat number!!\n");
- printf("Press enter to continue...");
- getchar();
- continue;
- }
- row = get_row(response);
- column = get_column(response);
- if(row == -1 || column == -1) {
- printf("Invalid seat number!!\n");
- } else {
- given_seat_status = check_seat(row, column, seat_state);
- if(!given_seat_status) {
- printf("Given seat was already taken!!\n");
- } else {
- printf("Seat #%s successfully booked!!\n", response);
- }
- }
- printf("Press enter to continue...");
- getchar();
- break;
- case 2:
- printf("\n+---------+FREE-UP SEATS+---------+\n");
- if(is_empty(seat_state)) {
- printf("All seats are empty!!\n");
- printf("Press enter to continue...");
- getchar();
- break;
- }
- printf("Type \"EXIT\" to exit...\n");
- printf("Enter seat number: ");
- fgets(response, 8, stdin);
- remove_newline(response);
- if(strcmp(response, "EXIT") == 0) {
- main_loop = false;
- break;
- } else if(strlen(response) < 3 || strlen(response) > 3) {
- printf("Invalid seat number!!\n");
- printf("Press enter to continue...");
- getchar();
- continue;
- }
- row = get_row(response);
- column = get_column(response);
- if(row == -1 || column == -1) {
- printf("Invalid seat number!!\n");
- } else {
- given_seat_status = check_empty_seat(row, column, seat_state);
- if(!given_seat_status) {
- printf("Given seat was already empty!!\n");
- } else {
- printf("Seat #%s successfully freed!!\n", response);
- }
- }
- printf("Press enter to continue...");
- getchar();
- break;
- default:
- printf("Invalid choice!!\n");
- printf("Press enter to continue..");
- getchar();
- break;
- }
- }
- printf("\n+-----+CUSTOM BOOKING SYSTEM+-----+\n");
- return 0;
- }
- // FUNCTION DEFINITIONS
- int get_column(char* seat) {
- // DEFINITION: Returns the integer-equivalent of the column part of the given seat
- // If the row part not a number, return -1
- if(seat[1] < '0' || seat[1] > '9') {
- return -1;
- } else if(seat[2] < '0' || seat[2] > '9') {
- return -1;
- }
- // Get the integer-equivalent of the column part of the given seat
- int res = (((seat[1] - '0') * 10) + (seat[2] - '0')) - 1;
- // If the integer-equivalent is greater than the maximum columns, we change "res" to -1
- if(res > MAX_COLUMNS - 1) {
- res = -1;
- }
- return res;
- }
- int get_row(char* seat) {
- // DEFINITION: Returns the integer-equivalent of the row part of the given seat
- // If the row part is not an alphabet, return -1;
- if(!isalpha(seat[0])) {
- return -1;
- }
- // Get the integer-equivalent of the row part of the given seat
- int res = toupper(seat[0]) - 'A';
- // If the integer-equivalent is greater than the maximum rows, we change "res" to -1
- if(res > MAX_ROWS - 1) {
- res = -1;
- }
- return res;
- }
- bool is_empty(bool (*seats)[MAX_COLUMNS]) {
- // DEFINITION: Returns true if all seats are empty
- for(int i = 0; i < MAX_ROWS; i++) {
- for(int j = 0; j < MAX_COLUMNS; j++) {
- if(seats[i][j]) {
- return false;
- }
- }
- }
- return true;
- }
- bool is_full(bool (*seats)[MAX_COLUMNS]) {
- // DEFINITION: Returns true if all seats are full
- for(int i = 0; i < MAX_ROWS; i++) {
- for(int j = 0; j < MAX_COLUMNS; j++) {
- if(!seats[i][j]) {
- return false;
- }
- }
- }
- return true;
- }
- bool check_empty_seat(int row, int column, bool(*seats)[MAX_COLUMNS]) {
- // DEFINITION: Checks if given seat is not empty, frees it, and returns true after freeing seat
- if(!seats[row][column]) {
- return false;
- }
- seats[row][column] = false;
- return true;
- }
- bool check_seat(int row, int column, bool (*seats)[MAX_COLUMNS]) {
- // DEFINITION: Checks if given seat is empty, fills it, and returns true after filling seat
- if(seats[row][column]) {
- return false;
- }
- seats[row][column] = true;
- return true;
- }
- void color_green() {
- // DEFINITION: Changes the text color to green
- printf("\033[1;32m");
- }
- void color_red() {
- // DEFINITION: Changes the text color to red
- printf("\033[1;31m");
- }
- void show_seats(bool seats[MAX_ROWS][MAX_COLUMNS]) {
- //DEFINITION: Shows all seat and their current booking status by colors
- for(int i = 0; i < MAX_ROWS; i++) {
- for(int j = 0; j < MAX_COLUMNS; j++) {
- if(seats[i][j]) {
- color_red();
- } else {
- color_green();
- }
- printf("%c", 'A' + i);
- if(j < 9) {
- printf("0%d ", j + 1);
- } else {
- printf("%d ", j + 1);
- }
- }
- printf("\n");
- }
- }
- void remove_newline(char *seat) {
- // DEFINITION: Removes the newline character from fgets function
- int i = 0;
- int len = strlen(seat);
- while(i < len) {
- if(seat[i] == '\n') {
- seat[i] = '\0';
- return;
- }
- i++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement