Friday, January 9, 2015

Train Information System in C++ using Inheritance

#include<iostream.h>
#include<malloc.h>
#include<string.h>
#include<conio.h>
#include<math.h>
#include<stdio.h>
class station
{
protected:
int password;
char *name;
int trainno;
int scpassword;
struct tinfo
{
char trainname[30];
long int tcode;
int atimeh;
int atimem;
int dtimeh;
int dtimem;
int stimeh;
int stimem;
struct tinfo *next;
struct tinfo *pre;
}*start,*q,*t,*n,*p;
public:
};

class train:public station
{
protected:
char *trainnamed;
int stationno;
struct sinfo
{
char stationame[30];
int atimehd;
int atimemd;
int dtimehd;
int dtimemd;
int stimehd;
int stimemd;
struct Sinfo *nextd;
struct sinfo *pred;
}*startd,*qd,*td,*nd,*pd;
public:
 train()
 {
scpassword=9461;
 }
 void passwd()
 {
int p;
cout<<"enter special security password";
cin>>p;
if(scpassword==p)
{
cout<<"enter the initial password";
cin>>password;
}
 }
 void cpasswd();
 void getbasicdata();
 void timetable();
 void input();
 void del();
 void sort();
 void copy(struct tinfo *a, struct tinfo *b);
};
void train::cpasswd()
{
int o,n,rn;
cout<<"enter the old password";
cin>>o;
if(o==password)
{
cout<<"enter new password";
cin>>n;
cout<<"enter new password again";
cin>>rn;
if(n==rn)
{
password=n;
cout<<"possword successfully changed"<<"\n";
}
else
{
cout<<"you not enter the same password\n";
}
}
else
{
cout<<"you enter wrong password\n";
}
}
void train::getbasicdata()
{
int p;
cout<<"enter password";
cin>>p;
if(p==password)
{
int choice;
cout<<"ENTER ONE FOR ENTER INFORMATION ABOUT STATION AND TWO FOR TRAIN";
cin>>choice;
if(choice==1)
{
name=new char[20];
cout<<"enter the station name";
gets(name);
cout<<"enter the no, of trains going through the station";
cin>>trainno;
start=NULL;
for(int i=0;i<trainno;i++)
{
struct tinfo *g=(struct tinfo*)malloc(sizeof(struct tinfo));
cout<<"ENTER THE TRAIN NAME";
gets(g->trainname);
cout<<"ENTER THE TRAIN ARRIVAL TIME IN HOUR AND MINUTES";
cin>>g->atimeh>>g->atimem;
cout<<"ENTER THE TRAIN DEPARTURE TIME IN HOUR AND MINUTES";
cin>>g->dtimeh>>g->dtimem;
if(g->dtimeh==0)
{
g->stimeh=24-g->atimeh;
}
else
{
g->stimeh=g->dtimeh-g->atimeh;
}
if(g->dtimem>=g->atimem)
{
g->stimem=g->dtimem-g->atimem;
}
else
{
g->stimem=g->dtimem-g->atimem+60;
g->stimeh=g->stimeh-1;
}
if(start==NULL)
{
start=(struct tinfo*)malloc(sizeof(struct tinfo));
start=g;
start->pre=NULL;
start->next=NULL;
}
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
t=(struct tinfo*)malloc(sizeof(struct tinfo));
t=g;
t->next=NULL;
q->next=t;
t->pre=q;
}
}
}
}
sort();

}
void train::input()
{
int p;
cout<<"enter password";
cin>>p;
if(p==password)
{

int a;
cout<<"enter the new train want to add on this station";
cin>>a;
for(int i=trainno;i<(trainno+a);i++)
{
if(start==NULL)
{
start=(struct tinfo*)malloc(sizeof(struct tinfo));
cout<<"ENTER THE TRAIN NAME";
gets(start->trainname);
cout<<"ENTER THE TRAIN ARRIVAL TIME IN HOUR AND MINUTES";
cin>>start->atimeh>>start->atimem;
cout<<"ENTER THE TRAIN DEPARTURE TIME IN HOUR AND MINUTES";
cin>>start->dtimeh>>start->dtimem;
if(start->dtimeh==0)
{
start->stimeh=24-start->atimeh;
}
else
{
start->stimeh=start->dtimeh-start->atimeh;
}
if(start->dtimem>=start->atimem)
{
start->stimem=start->dtimem-start->atimem;
}
else
{
start->stimem=start->dtimem-start->atimem+60;
start->stimeh=start->stimeh-1;
}
start->pre=NULL;
start->next=NULL;
}
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
t=(struct tinfo*)malloc(sizeof(struct tinfo));
cout<<"ENTER THE TRAIN NAME";
gets(t->trainname);
cout<<"ENTER THE TRAIN ARRIVAL TIME IN HOUR AND MINUTES";
cin>>t->atimeh>>t->atimem;
cout<<"ENTER THE TRAIN DEPARTURE TIME IN HOUR AND MINUTES";
cin>>t->dtimeh>>t->dtimem;
if(t->dtimeh==0)
{
t->stimeh=24-t->atimeh;
}
else
{
t->stimeh=t->dtimeh-t->atimeh;
}
if(t->dtimem>=t->atimem)
{
t->stimem=t->dtimem-t->atimem;
}
else
{

t->stimem=t->dtimem-t->atimem+60;
t->stimeh=t->stimeh-1;
}
t->next=NULL;
q->next=t;
t->pre=q;
}
}
trainno=trainno+a;
}
void sort();
}
void train::del()
{
char search[30];
int t;
cout<<"enter password";
cin>>t;
if(t==password)
{
cout<<"enter the train nane want to delete";
cin>>search;
q=start;
p=(struct tinfo*)malloc(sizeof(struct tinfo));
while((strcmp(q->trainname,search))&&q!=NULL)
{
p=q;
q=q->next;
}
if(q==NULL)
{
cout<<"SEARCH IS UNSUCCESSFULL\n";
}
else
{
if(q->pre==NULL)
{
start=start->next;
}
else
{
p->next=q->next;
}
}
}

else
{
cout<<"wrong password\n";
}
}
void train::timetable()
{
//clrscr();
cout<<"___________________________________________________________________\n";
int ww=strlen(name);
for(int qq=0;qq<((65-(18+ww))/2);qq++)
{
cout<<" ";
}
cout<<"THE TIME TABLE OF "<<name<<"\n";
cout<<"-------------------------------------------------------------------\n";
cout<<"NAME            ARRIVAL TIME       DEPARTURE TIME       SPENT TIME\n";
for(q=start;q!=NULL;q=q->next)
{
cout<<q->trainname;
ww=strlen(q->trainname);
for(qq=0;qq<(16-ww);qq++)
{
cout<<" ";
}
if(q->atimeh<10)
{
cout<<"   0";
}
else
{
cout<<"   ";
}
cout<<q->atimeh;
if(q->atimem<10)
{
cout<<":0";
}
else
{
cout<<":";
}
cout<<q->atimem;
if(q->dtimeh<10)
{
cout<<" 0";
}
else
{
cout<<"     ";
}
cout<<q->dtimeh;
if(q->dtimem<10)
{
cout<<":0";
}
else
{
cout<<":";
}
cout<<q->dtimem;
if(q->stimeh<10)
{
cout<<"  0";
}
else
{
cout<<"     ";
}
cout<<q->stimeh;
if(q->stimem<10)
{
cout<<":0";
}
else
{
cout<<":";
}
cout<<q->stimem<<"\n";
}
cout<<"-------------------------------------------------------------------\n";
}
void train::copy(struct tinfo *a, struct tinfo *b)
{
strcpy(a->trainname,b->trainname);
a->atimeh=b->atimeh;
a->atimem=b->atimem;
a->dtimeh=b->dtimeh;
a->dtimem=b->dtimem;
a->pre=a->next=b->pre=b->next=NULL;

}
void train::sort()
{

int i=0,j;
struct tinfo *temp=(struct tinfo*)malloc(sizeof(struct tinfo));
struct tinfo *tempp=(struct tinfo*)malloc(sizeof(struct tinfo));
struct tinfo *tempb=(struct tinfo*)malloc(sizeof(struct tinfo));
struct tinfo *temps=(struct tinfo*)malloc(sizeof(struct tinfo));
q=start;
while(q!=NULL)
{
j=0;
tempp=start;
while(tempp!=NULL)
{
if(q->atimeh>tempp->atimeh)
{
cout<<q->trainname<<" "<<tempp->trainname<<"\n";
copy(tempb,q);
copy(temps,tempp);
temps->next=q->next;
q->next=temps;
temps->pre=q;
q->pre=q->next;
tempb->next=tempp->next;
tempp->next=tempb;
tempb->pre=tempp;
tempp->pre=tempp->next;
cout<<q->trainname<<" "<<tempp->trainname<<"\n";
cout<<"INDIA CHANGED 1\n";
}
else if(q->atimeh==tempp->atimeh)
{
if(q->atimem>tempp->atimem)
{
tempb=q;
temps=tempp;
q=temps;
tempp=tempb;
cout<<"INDIA CHANGED 2\n";
}
}
tempp=tempp->next;
cout<<"J="<<j<<"\n";
j++;
cout<<"j increased\n";

}
q=q->next;
cout<<"I="<<i<<"\n";
i++;
cout<<"j increased\n";
}
cout<<"I="<<i<<"  J="<<j<<"\n";;
q=start;
while(q->next!=NULL)
{
cout<<q->trainname;
q=q->next;
}
}

void main()
{
train a;
a.passwd();
a.cpasswd();
a.getbasicdata();
a.timetable();
a.input();
a.timetable();
a.del();
a.timetable();

}

for more codes you can Visit http://codesofprogramming.blogspot.in/

No comments:

Post a Comment