Train reservation of ten trains

Problem statement
Write a c program to manage reservation list of ten trains, your program should reserve a ticket,delete a ticket,print ticket,print the reservation chart,your program should take the train number and train name as an input from user,

#include<stdio.h>
#include<stdlib.h>
#define TOTAL 10
typedef struct node
{
  int seat;
  char name[15];
  int age;
  char sex;
  struct node* next;
}list,*ptr;
typedef struct tr
{
  char trname[20];
  int trnumber;
  ptr pointer;
}train,*ptrtrain;
void delete(ptr a);
void add(ptr a);
void printseat(ptr temp);
void print(ptr a);
void delete(ptr a);
void book(ptrtrain trainname);
int main()
{
  int i,trainno;
  ptrtrain a[TOTAL];
  ptr temp;
  for(i=0;i<TOTAL;i++)
    {
      a[i]=(ptrtrain)malloc(1*sizeof(train));
      printf("enter number of train %d ",i+1);
      scanf("%d",&a[i]->trnumber);
      printf("enter train name ");
      scanf("%s",a[i]->trname);
      a[i]->pointer=NULL;
    }
  for(i=0;i<TOTAL;i++)
    {
      temp=(ptr)malloc(1*sizeof(list));   /* creating a dummy node in each train */
      temp->seat=0;     
      temp->next=NULL;
      a[i]->pointer=temp;
    }
  for(i=0;i<100;i++)
    {
      printf("enter train number from (1 to 9) and enter 0 to quit the program ");
      scanf("%d",&trainno);
      if(trainno>0&&trainno<10)
    {
      book(a[trainno-1]);
    }
      else if(trainno==0)
    {
      break;
    }
      else
    {
      printf("no such train exist \n");
    }
    }
  printf("thank you \n");
  return 0;
}
void book(ptrtrain trainname)
{
  int input;
  printf("train number=%d \n",trainname->trnumber);    /* here we can add many other information like train name,time  etc.*/
  printf("train name=%s \n",trainname->trname); 
  printf("enter 1 to book a ticket and 2 for deleting a TICKETand 3 to print the chart and 4 to return to train menu \n");
  scanf("%d",&input);
  if(input==1)
    {
      add(trainname->pointer);
    }
  else if(input==2)
    {
      delete(trainname->pointer);
    }
  else if(input==3)
    {
      print(trainname->pointer);
    }
  else if(input==4)
    {
      return ;
    }
  else
    {
      printf("not able to recognise the command \n");
    }
  return ;
}
void add(ptr a)
{
  int input,input2;
  ptr temp=NULL;
  while(a->next!=NULL)
    {
      a=a->next;
    }
  if(a->seat>70)
    {
      printf("train full \n");
      return ;
    }
  else if(a->seat>50)
    {
      printf("%d waiting awailable \n",(a->seat)-50);
      printf("enter 1 to exit 0 to continue ");
      scanf("%d",&input2);
      if(input2==1)
    {
      return ;
    }
    }   
  temp=(ptr)malloc(1*sizeof(list));
  temp->next=NULL;
  printf("enter name ");
  scanf("%s",temp->name);
  printf("enter age ");
  scanf("%d",&temp->age);
  printf("enter sex ");
  scanf("%s",&temp->sex);
  temp->seat=(a->seat)+1;
  printf("enter 1 to print the ticket 0 for Not to print the ticket \n");
  scanf("%d",&input);
  if(input==1)
    { 
      printseat(temp);
    }
  a->next=temp;
  return ;
}
void printseat(ptr temp)
{
  if(temp->seat>50)
    {
      printf("WL=%d",(temp->seat)-50);
    }
  else
    {
      printf("confirm seat=%d",temp->seat);
    }
  printf(" name=%s ",temp->name);
  printf("sex=%c ",temp->sex);
  printf("age=%d \n",temp->age);
  return ;
}
void print(ptr a)
{
  a=a->next;
  while(a!=NULL)
    {
      printf("seat number=%d,name=%s ,age=%d,sex=%c \n",a->seat,a->name,a->age,a->sex);
      a=a->next;
    }
  return ;
}
void delete(ptr a)
{
  int input;
  ptr temp;
  a=a->next;
  printf("enter the seat number ");
  scanf("%d",&input);
  while(a!=NULL&&(a->seat)!=input)
    {
      a=a->next;
    }
  if(a==NULL)
    {
      printf("can not delete \n");
      return ;  
    }
  temp=a;
  a=a->next;
  free(temp);
  while(a!=NULL)
    {
      a->seat=a->seat-1;
      a=a->next;
    }
  printf("seat deleted \n");
  return ;
}

No comments:

Post a Comment