0% found this document useful (0 votes)
34 views7 pages

C Hackathon Aeropeurto

Source code

Uploaded by

jaar.skp12.5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views7 pages

C Hackathon Aeropeurto

Source code

Uploaded by

jaar.skp12.5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

TEAM AEROPEURTO

(Hack-a-thon)

Team members –
Shashank Sinha (PES1UG23CS542)
Shashwat (PES1UG23EC283)
Sanchit Etagi (PES1UG23AM268)
Shrest Kumar (PES1UG23CS557)

Tickets.txt
p_hackathon.h

// p_hackathon.h

#define NAME_LEN 30
#define MAX_PASSENGERS 300 // Max number of passengers inside the airport
#define AIRWAYS_LEN 30 // Air India, Lufthansa, etc
#define F_LEN 20
#define CITY_LEN 20 // Length of source or destination city
#define TIME_LEN 6 // Flight Boarding time
#define DATE_LEN 20 // dd:mm:yyyy

typedef struct ticket


{
char name [NAME_LEN];
char Airways [AIRWAYS_LEN]; // Air India, National Airways, etc
char flightNumber [F_LEN]; // Example NA4321
char source [CITY_LEN]; // Source city
char destination [CITY_LEN]; // Destination city
char type; // E - Economy or B - Business class
char category; // D - Domestic or I - International
char D_Date [DATE_LEN]; // dd:mm:yyyy

char DepartureTime [TIME_LEN]; // In the form hh:mm


} TICKET;
C_Hackathon.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "p_hackathon.h"

#define MAX_LINE_LENGTH 256


#define MAX_PASSENGERS 100

static TICKET *p_ticket[MAX_PASSENGERS];


static int num_passengers = 0;

void parseLine(char *line, TICKET *ticket) {


// Use sscanf to parse the line based on the '?' delimiter
sscanf(line, "%[^?]?%[^?]?%[^?]?%[^?]?%[^?]?%c?%c?%[^?]?%s",
ticket->name, ticket->Airways, ticket->flightNumber,
ticket->source, ticket->destination, &ticket->type,
&ticket->category, ticket->D_Date, ticket->DepartureTime);

// Remove any trailing newline character from DepartureTime


ticket->DepartureTime[strcspn(ticket->DepartureTime, "\n")] = '\0';
}

void PrintTicket(const TICKET *ticket) {


printf("Name: %s\n", ticket->name);
printf("Airways: %s\n", ticket->Airways);
printf("Flight Number: %s\n", ticket->flightNumber);
printf("Source: %s\n", ticket->source);
printf("Destination: %s\n", ticket->destination);
printf("Class: %c\n", ticket->type);
printf("Category: %c\n", ticket->category);
printf("Departure Date: %s\n", ticket->D_Date);
printf("Departure Time: %s\n", ticket->DepartureTime);
printf("\n");
}

void PrintSelectedTickets(int hours) {


for (int i = 0; i < num_passengers; i++) {
char tempTime[10];
strcpy(tempTime, p_ticket[i]->DepartureTime);
int ticketHour = atoi(strtok(tempTime, ":"));
if (ticketHour > hours) {
PrintTicket(p_ticket[i]);
}
}
}

void freeMem() {
for (int i = 0; i < num_passengers; i++) {
free(p_ticket[i]);
}
}

int main() {
int h;
FILE *file = fopen("Tickets.txt", "r");
if (file == NULL) {
perror("Unable to open file");
return EXIT_FAILURE;
}

char line[MAX_LINE_LENGTH];
int count = 0;

while (fgets(line, sizeof(line), file) != NULL) {


if (count < MAX_PASSENGERS) {
p_ticket[count] = (TICKET *)malloc(sizeof(TICKET));
if (p_ticket[count] == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return EXIT_FAILURE;
}
parseLine(line, p_ticket[count]);
count++;
} else {
fprintf(stderr, "Too many passengers, increase array size\n");
break;
}
}
num_passengers = count;
fclose(file);

for (int i = 0; i < count; i++) {


printf("Ticket %d:\n", i + 1);
PrintTicket(p_ticket[i]);
}

printf("Enter hour to filter tickets: \n");


scanf("%d", &h);
printf("-------------------------------------------------------------------
--------------------\n");
PrintSelectedTickets(h);

freeMem();
return EXIT_SUCCESS;
}

OUTPUT :

You might also like