Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 200
- struct passenger{
- char *name;
- struct passenger *next;
- };
- struct flightRecord{
- int flightNo;
- int flightCapacity;
- int reservationCount;
- struct passenger *passengersOnFlight;
- struct passenger *reservesForFlight;
- int dailyNo;
- };
- flightRecord* initialise(struct flightRecord *flights){
- FILE *text;
- int noOfDailyFlights;
- text = (fopen("text.txt","r"));
- fscanf(text, "%d ", &noOfDailyFlights);
- printf("%d flights are in today.\n\n",noOfDailyFlights);
- flights =(flightRecord *)malloc(noOfDailyFlights*sizeof(struct flightRecord));
- printf("Flight Numbers Flight Capacity\n");
- int i = 0;
- while (!feof(text)){
- fscanf(text,"%d %d ",&(flights+i)->flightNo,&(flights+i)->flightCapacity);
- flights[i].dailyNo = noOfDailyFlights;
- flights[i].reservationCount = 0;
- printf(" %3d %d\n",flights[i].flightNo,flights[i].flightCapacity);
- i++;
- }
- return flights;
- }
- void status(struct flightRecord *flights){
- int i = 0;
- struct flightRecord *temp= flights;
- for (i = 0;i < flights->dailyNo ; i++){
- printf("Flight number is: %d \n",temp[i].flightNo);
- printf("Flight capacity is: %d \n",temp[i].flightCapacity);
- printf("The people on flight are:\n");
- while(temp[i].passengersOnFlight){
- printf("%s\n",temp[i].passengersOnFlight->name);
- if(temp[i].passengersOnFlight->next)
- temp[i].passengersOnFlight = flights[i].passengersOnFlight->next;
- }
- while(temp[i].reservesForFlight){
- printf("%s\n",temp[i].reservesForFlight->name);
- if(temp[i].reservesForFlight->next)
- temp[i].reservesForFlight = temp[i].reservesForFlight->next;
- }
- }
- }
- flightRecord* reserve(char name[],int flightNo,struct flightRecord *flights){
- struct flightRecord *temp = flights;
- int i = 0,flag = 0;
- for(i;i < flights->dailyNo;i++){
- if(flightNo == temp[i].flightNo){
- flag++;
- }
- }
- if(flag == 0){
- printf("Flight is not found.");
- }
- else{
- for(i = 0;i < flights->dailyNo;i++){
- if(flightNo == temp[i].flightNo){
- if(temp[i].reservationCount < temp[i].flightCapacity){
- while(temp[i].passengersOnFlight != NULL){
- temp[i].passengersOnFlight= temp[i].passengersOnFlight->next;
- }
- (temp+i)->passengersOnFlight = (struct passenger*)malloc(sizeof(struct passenger));
- strcpy(temp[i].passengersOnFlight->name,name);
- temp[i].passengersOnFlight->next = NULL;
- temp[i].reservationCount++;
- }
- else if(temp[i].reservationCount > temp[i].flightCapacity){
- while(temp[i].reservesForFlight != NULL){
- temp[i].reservesForFlight = temp[i].reservesForFlight->next;
- }
- (temp+i)->reservesForFlight = (struct passenger*)malloc(sizeof(struct passenger));
- strcpy(temp[i].reservesForFlight->name,name);
- temp[i].reservesForFlight->next = NULL;
- }
- printf("%d\n",temp[i].reservationCount);
- }
- }
- }
- for(i = 0;temp[i].passengersOnFlight->next;i++){
- printf("%s ",temp[i].passengersOnFlight->name);
- temp[i].passengersOnFlight = temp[i].passengersOnFlight->next;
- }
- return flights;
- }
- flightRecord* cancel(char name[],int flightNumber,struct flightRecord *flights){
- struct flightRecord *temp = flights;
- int i = 0,flag = 0;
- for(i;i < flights->dailyNo;i++){
- if(flightNumber == temp[i].flightNo){
- flag++;
- }
- }
- if(flag == 0){
- printf("Flight is not found.\n");
- }
- else{
- for(i = 0;i < temp[i].reservationCount;i++){
- if(strcmp(temp[i].passengersOnFlight->name,name) == 0){
- temp[i].passengersOnFlight = temp[i].passengersOnFlight->next;
- temp[i].reservationCount--;
- }
- else if(strcmp(temp[i].passengersOnFlight->next->name,name)== 0){
- temp[i].passengersOnFlight->next = temp[i].passengersOnFlight->next->next;
- temp[i].reservationCount--;
- }
- }
- while(temp[i].reservesForFlight->next){
- if(strcmp(temp[i].reservesForFlight->name,name)==0){
- temp[i].reservesForFlight = temp[i].reservesForFlight->next;
- }
- else if(strcmp(temp[i].reservesForFlight->next->name,name)==0){
- temp[i].reservesForFlight->next = temp[i].reservesForFlight->next->next;
- }
- }
- }
- }
- void inquire(struct flightRecord *flights){
- int i;
- struct flightRecord *temp = flights;
- for(i = 0; i < flights->dailyNo ; i++){
- printf("The flight number is %d.\n",temp[i].flightNo);
- printf("People on the flights are:\n");
- while(temp[i].passengersOnFlight->next){
- printf("%s\n",temp[i].passengersOnFlight->name);
- temp[i].passengersOnFlight = temp[i].passengersOnFlight->next;
- }
- printf("People are waiting for this flight:\n");
- while(temp[i].reservesForFlight->next){
- printf("%s\n",temp[i].reservesForFlight->name);
- temp[i].reservesForFlight = temp[i].reservesForFlight->next;
- }
- printf("\n");
- }
- }
- void processCommands(struct flightRecord *flights){
- printf("\n");
- char command[10];
- char name[100];
- char flightNo[10];
- int totalSpaces = 0;
- int space = 0;
- int i , j , k, l;
- while(strcmp(command,"EXIT")!= 0){
- i = 0, j = 0 , k= 0 , l = 0;
- printf("Enter the command: <COMMAND> <NAME> <FLIGHT NO>: ");
- fflush(stdin);
- char input[200];
- fgets(input, MAX, stdin);
- while(input[i]){
- if(input[i] == ' ')
- totalSpaces++;
- i++;
- }
- i = 0;
- while(input[i] != '\n'){
- if(input[i] == ' '){
- space++;
- }
- if (space == totalSpaces){
- flightNo[l] = input[i];
- l++;
- }
- if(space == 0){
- command[j] = input[i];
- j++;
- }
- else if (space > 0){
- if(space < totalSpaces){
- name[k] = input[i];
- k++;
- }
- }
- i++;
- }
- command[j] = '\0';
- name[k] = '\0';
- flightNo[l] = '\0';
- int flightNumber = atoi(flightNo);
- if (strcmp(command,"STATUS") == 0){
- status(flights);
- }
- else if(strcmp(command,"RESERVE")== 0){
- flights = reserve(name,flightNumber,flights);
- }
- else if(strcmp(command,"CANCEL")== 0){
- flights = cancel(name,flightNumber,flights);
- }
- else if(strcmp(command,"INQUIRE")==0){
- inquire(flights);
- }
- }
- struct flightRecord *temp = flights;
- for(i = 0;i < temp[i].reservationCount;i++){
- printf("%s",temp[i].passengersOnFlight->name);
- }
- printf("Thank you for using reservation system.");
- }
- int main(){
- struct flightRecord *flights = initialise(flights);
- processCommands(flights);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement