Dr. A.P.J. Abdul Kalam Technical University, Lucknow
KCS101 / KCS201 Programming for Problem Solving - Using C Language
Lab Exercises
29. Define a structure data type TRAIN_INFO. The type contains Train No.: integer type, Train name: string, Departure Time: aggregate type TIME, Arrival Time: aggregate type TIME, Start station: string, End station: string. The structure type TIME contains two integer members: hour and minute. Maintain a train timetable and implement the following operations:
(i) List all the trains (stored according to train number) that depart from a particular station.
(ii) List all the trains that depart from a particular station at a particular time.
(iii) List all the trains that depart from a particular station within the next one hour of a given time.
(iv) List all the trains between a pair of start station and end station.
/*
File: Prgrm29.c
Author: Aditya Saini
Date: Sept 30, 2019
Description: Program to maintain the train schedule.
*/
#include<stdio.h>
#include<string.h>
typedef struct TIME{
int hour;
int minute;
}TIME;
typedef struct TRAIN_INFO{
int train_no;
char train_name[35];
char start_st[35];
char end_st[35];
TIME dept_time;
TIME arr_time;
} TRAIN;
void train_edit (TRAIN *, int *);
int main (void)
{
int no_of_trains = 0;
int i;
char choice;
char dept_st[35];
char arr_st[35];
TIME train_time;
TRAIN train[50];
train_edit(train, &no_of_trains);
//Display Menu
while (1)
{
printf("\t\t\t****MENU****\n");
printf("1. List all the trains departed from %s.\n", dept_st);
printf("2. List all the trains departed from %s at %d:%d.\n", dept_st, train_time.hour,
train_time.minute);
train_time.minute);
printf("3. List all the trains departed from %s within the next one hour from %d:%d.\n",
dept_st, train_time.hour, train_time.minute);
dept_st, train_time.hour, train_time.minute);
printf("4. List all the trains between %s and %s.\n", dept_st, arr_st);
printf("5. Edit train details.\n");
printf("Your choice: ");
fflush(stdin);
choice = getchar();
switch(choice)
{
case '1':
printf("\t\t****INPUT DETAILS****\n");
printf("Depart Station: ");
fflush(stdin);
gets(dept_st);
printf("Train no\tName\tFrom\tTo\tD_Time\tA_Time\n");
for (i = 0; i <= no_of_trains - 1; i++)
{
if (!strcmp(train[i].start_st, dept_st))
{
printf("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n", train[i].train_no, train[i].train_name,
train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
}
}
break;
case '2':
printf("\t\t****INPUT DETAILS****\n");
printf("Depart Station: ");
fflush(stdin);
gets(dept_st);
printf("Train Time: \n");
printf("Hour: ");
scanf("%d", &train_time.hour);
printf("Minute: ");
scanf("%d", &train_time.minute);
printf("Train no\tName\tFrom\tTo\tD_Time\tA_Time\n");
for (i = 0; i <= no_of_trains - 1; i++)
{
if(!strcmp(train[i].start_st, dept_st) && train[i].dept_time.hour == train_time.hour
&& train[i].dept_time.minute == train_time.minute)
&& train[i].dept_time.minute == train_time.minute)
{
printf("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n", train[i].train_no,
train[i].train_name, train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
train[i].train_name, train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
}
}
break;
case '3':
printf("\t\t****INPUT DETAILS****\n");
printf("Depart Station: ");
fflush(stdin);
gets(dept_st);
printf("Time: \n");
printf("Hour: ");
scanf("%d", &train_time.hour);
printf("Minute: ");
scanf("%d", &train_time.minute);
printf("Train no\tName\tFrom\tTo\tD_Time\tA_Time\n");
for (i = 0; i <= no_of_trains - 1; i++)
{
if(!strcmp(train[i].start_st, dept_st) && ((train[i].dept_time.hour == train_time.hour
&& train[i].dept_time.minute >= train_time.minute)
|| (train[i].dept_time.hour == train_time.hour + 1
&& train[i].dept_time.minute <= train_time.minute)))
&& train[i].dept_time.minute >= train_time.minute)
|| (train[i].dept_time.hour == train_time.hour + 1
&& train[i].dept_time.minute <= train_time.minute)))
{
printf("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n", train[i].train_no,
train[i].train_name, train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
train[i].train_name, train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
}
}
break;
case '4':
printf("\t\t****INPUT DETAILS****\n");
printf("Depart Station: ");
fflush(stdin);
gets(dept_st);
printf("Arrival Station: ");
fflush(stdin);
gets(arr_st);
for (i = 0; i <= no_of_trains - 1; i++)
{
if ((!strcmp(train[i].start_st, dept_st)) && (!strcmp(train[i].end_st, arr_st)))
{
printf("%d\t\t%s\t%s\t%s\t%d:%d\t%d:%d\n", train[i].train_no,
train[i].train_name, train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
train[i].train_name, train[i].start_st, train[i].end_st, train[i].dept_time.hour,
train[i].dept_time.minute, train[i].arr_time.hour, train[i].arr_time.minute);
}
}
break;
case '5':
train_edit (train, &no_of_trains);
break;
default:
printf("Error! Wrong Choice.\n");
}
printf("Press any key to continue. 'X' to Exit.\n");
printf("Your choice: ");
fflush(stdin);
choice = getchar();
if (choice == 'x' || choice == 'X')
break;
}
return 0;
}
void train_edit (TRAIN *train, int *no_of_trains)
{
char choice;
int i, train_num;
TRAIN temp;
//Edit Menu
while (1)
{
printf("\t\t****TRAIN EDIT MENU****\n");
printf("1. Add Train.\n");
printf("2. Delete Train.\n");
printf("Your Choice: ");
fflush(stdin);
choice = getchar();
switch (choice)
{
case '1':
if(*no_of_trains >= 50)
{
printf("Error! There are already 50 trains.\n");
break;
}
printf("Input Train Number: ");
scanf("%d", &temp.train_no);
printf("Input Train Name: ");
fflush(stdin);
gets(temp.train_name);
printf("Input Start Station: ");
fflush(stdin);
gets(temp.start_st);
printf("Input End Station: ");
fflush(stdin);
gets(temp.end_st);
printf("Input Departure Time: \n");
printf("Hour: ");
scanf("%d", &temp.dept_time.hour);
printf("Minute: ");
scanf("%d", &temp.dept_time.minute);
printf("Input Arrival Time: \n");
printf("Hour: ");
scanf("%d", &temp.arr_time.hour);
printf("Minute: ");
scanf("%d", &temp.arr_time.minute);
train[*no_of_trains] = temp;
for(i = *no_of_trains; i >= 1; i--)
{
if(train[i - 1].train_no >= train[i].train_no)
{
temp = train[i - 1];
train[i - 1] = train[i];
train[i] = temp;
}
else
break;
}
*no_of_trains = *no_of_trains + 1;
break;
case '2':
if(*no_of_trains == 0)
{
printf("Error! No Train Available.\n");
break;
}
printf("Input Train Number: ");
scanf("%d", &train_num);
for(i = 0; i <= *no_of_trains - 1; i++)
{
if(train[i].train_no == train_num)
{
while(i <= *no_of_trains - 1)
{
train[i] = train[i + 1];
i++;
}
}
}
*no_of_trains = *no_of_trains - 1;
break;
default:
printf("Error! Wrong Choice.\n");
}
printf("Press any key to continue. 'X' to exit TRAIN EDIT MENU.\n");
printf("Your choice: ");
fflush(stdin);
choice = getchar();
if (choice == 'x' || choice == 'X')
break;
}
}
Output
Input
No comments:
Post a Comment
Please do not post spam links.