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 =(struct 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;
- flights[i].passengersOnFlight = NULL;
- flights[i].reservesForFlight = NULL;
- 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 != NULL){
- printf("%s\n",temp[i].passengersOnFlight->name);
- 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;
- struct passenger *temp1 =(struct passenger *)malloc(sizeof(struct passenger));
- temp1->name =(char*)malloc((strlen(name)+1)*(sizeof(char)));
- 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.\n");
- }
- else{
- for(i = 0;i < flights->dailyNo;i++){
- if(flightNo == temp[i].flightNo){
- printf("%d",flightNo);
- if(temp[i].reservationCount < temp[i].flightCapacity){
- struct passenger *tempPas = temp[i].passengersOnFlight;
- strcpy(temp1->name,name);
- temp1->next = NULL;
- if(tempPas == NULL){
- tempPas = temp1;
- temp[i].reservationCount++;
- printf("%d",temp[i].reservationCount);
- }
- else{
- while (tempPas->next){
- tempPas = tempPas->next;
- }
- tempPas->next = temp1;
- temp[i].reservationCount++;
- printf("%d",temp[i].reservationCount);
- }
- }
- else{
- struct passenger *tempRes = temp[i].reservesForFlight;
- strcpy(temp1->name,name);
- temp1->next = NULL;
- if(tempRes == NULL){
- tempRes = temp1;
- }
- else{
- while(tempRes->next){
- tempRes = tempRes->next;
- }
- tempRes->next = temp1;
- }
- }
- }
- }
- }
- for(i = 0; i<temp[i].dailyNo;i++){
- while(temp[i].passengersOnFlight){
- printf("%s\n",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].dailyNo ; i++){
- if(flightNumber == temp[i].flightNo){
- if(temp[i].reservationCount < temp[i].flightCapacity){
- struct passenger *cur = temp[i].passengersOnFlight;
- struct passenger *temp1;
- while(strcpy(cur->name,name)!= 0){
- cur = cur->next;
- }
- if (strcmp(cur->name,name)==0){
- temp1 = cur;
- cur = cur->next;
- }
- free(temp);
- temp[i].reservationCount--;
- cur = temp[i].passengersOnFlight;
- if (temp[i].reservesForFlight != NULL){
- if(temp[i].reservationCount < temp[i].flightCapacity){
- while(cur){
- cur = cur->next;
- }
- cur->next = temp[i].reservesForFlight;
- temp[i].reservesForFlight = temp[i].reservesForFlight->next;
- }
- }
- }
- else{
- struct passenger *cur = temp[i].reservesForFlight;
- struct passenger *temp1;
- while(strcpy(cur->name,name)){
- cur = cur->next;
- }
- if (strcmp(cur->name,name)==0){
- temp1 = cur;
- cur = cur->next;
- }
- free(temp);
- }
- }
- }
- }
- return flights;
- }
- void inquire(struct flightRecord *flights,char name[]){
- int i;
- struct flightRecord *temp = flights;
- for(i = 0; i < flights->dailyNo ; i++){
- while(temp[i].passengersOnFlight != NULL){
- if(strcmp(temp[i].passengersOnFlight->name,name)==0){
- printf("%s reserved on flight %d.",name,temp[i].flightNo);
- }
- temp[i].passengersOnFlight = flights[i].passengersOnFlight->next;
- }
- while(temp[i].reservesForFlight != NULL){
- if(strcmp(temp[i].reservesForFlight->name,name)==0){
- printf("%s in reserve list for flight %d.",name,temp[i].flightNo);
- }
- 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;
- totalSpaces = 0;
- space = 0;
- memset(command,'\0',10);
- memset(name,'\0',100);
- memset(flightNo,'\0',10);
- printf("Enter the command: <COMMAND> <NAME> <FLIGHT NO>:");
- char input[200];
- fgets(input, MAX, stdin);
- fflush(stdin);
- while(input[i] != '\0'){
- 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){
- if(input[i] == ' ' && space == 1 ){
- name[k] = input[i+1];
- }
- else{
- name[k] = input[i];
- k++;
- }
- }
- }
- i++;
- }
- printf( "%s-%s-\n",name,flightNo);
- 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,name);
- }
- }
- struct flightRecord *temp = flights;
- printf("Thank you for using reservation system.");
- }
- int main(){
- struct flightRecord *flights;
- flights = initialise(flights);
- processCommands(flights);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement