?? trainlist.cpp
字號:
/*================================================================*\
|******************************************************************|
|* Train's Time List Manage(列車時刻表管理) ********|
|** ****|
|**** By Snowing.NK@Frozenworld **|
|******** From: 2007.07.09 Date: 2007.07.11 *|
|******************************************************************|
\*================================================================*/
/*================================================================*\
|******************************************************************|
|** **|
|* 請使用命令提示符(CMD.exe,9X為command.com)運行本程序 *|
|** **|
|******************************************************************|
\*================================================================*/
/******************************************************************\
|***************************code_start*****************************|
\******************************************************************/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*\
\*++++++++++++++++++++++include+start++++++++++++++++++++++++++++*/
#include "math.h"
#include "stdio.h"
#include "conio.h"
#include "string.h"
#include "malloc.h"
//#include "windows.h"//not use
/*+++++++++++++++++++++++include+end+++++++++++++++++++++++++++++*\
\*+++++++++++++++++++++++define+start++++++++++++++++++++++++++++*/
//constant's define(常量的定義)
#define MaxLen 20 //the max of string
#define lenid 10 //the max of train's coach number's string
#define lentm 12 //the max of time's string
//time type
typedef struct{
int hour; //hour
int minute; //minute
}Times;
//data saved type
typedef struct{
char ID[lenid]; //train's coach number
Times lvtime; //leave time
Times avtime; //arrive time
char start[MaxLen]; //start station
char end[MaxLen]; //end station
}datatype;
//creat a Link List
typedef struct LinkList{
datatype *train; //train infomation
LinkList *next; //next point
}LinkList;
/*+++++++++++++++++++++++define+end++++++++++++++++++++++++++++++*\
\*+++++++++++++++++++++Function+start++++++++++++++++++++++++++++*/
//creat a linklist
LinkList* CrtList()
{
LinkList* L;
L=(LinkList *)malloc(sizeof(LinkList));
L->next=NULL;
return L;
}
//get the lenth of the list
int LenList(LinkList *L)
{
int n=0;
while(L->next!=NULL)
{
L=L->next;
n++;
}
return n;
}
//insert a data into the list
int InsList(LinkList* L,datatype *Item)
{
int i=0;
LinkList *temp,*H=NULL;
datatype *tmp;
temp=CrtList();
tmp=(datatype*)malloc(sizeof(datatype));
//to memory
strcpy(tmp->ID,Item->ID);
tmp->avtime=Item->avtime;
tmp->lvtime=Item->lvtime;
strcpy(tmp->start,Item->start);
strcpy(tmp->end,Item->end);
if(temp==NULL)return 3;//creat link list failure
//get seat from small to large
for(;;)
{
H=L->next;
if(L->next==NULL)break;
if(strcmp(H->train->ID,tmp->ID)>0)break;
if(!strcmp(H->train->ID,tmp->ID))return 0;
L=L->next;
}
//insert data
temp->train=tmp;
temp->next=L->next;
L->next=temp;
return 0;
}
//delete a data from the list
int DelList(LinkList* L,char *trID)
{
int i=0,n=0;
LinkList *temp=L,*tmp=NULL;
n=LenList(L);
if(!n)return 1;//return error's code(Empty)
for(;i<n;i++)
{
if(!strcmp(temp->next->train->ID,trID))
break;
temp=temp->next;
}
if(i==n)return 2;//no found
//delete data
tmp=temp->next;
temp->next=tmp->next;
printf("Delete completed!");
return 0;
}
//sort from small to large
void SortList(LinkList *L,char *str)
{
int n,exchange;
LinkList *T=NULL,*H=L,*R=NULL;
n=LenList(L);
printf("Sort from small to large:\n");
//sort start from small to large
if(!strcmp(str,"cn"))
{
//by coach number
printf("Sort by coach number:\n");
for(;H->next!=NULL;)
{
exchange=0;
for(;H->next->next!=NULL;)
{
T=H->next;
if(strcmp(T->train->ID,T->next->train->ID)>0)
{
R=T->next;
H->next=R;
T->next=R->next;
R->next=T;
exchange=1;
}
H=H->next;T=T->next;
}
if(!exchange)break;
H=L;
}
}
else if(!strcmp(str,"at"))
{
//by arrive time
printf("Sort by arrive time:\n");
for(;H->next!=NULL;)
{
exchange=0;
for(;H->next->next!=NULL;)
{
T=H->next;
if(T->train->avtime.hour>T->next->train->avtime.hour)
{
R=T->next;
H->next=R;
T->next=R->next;
R->next=T;
exchange=1;
}
else if((T->train->avtime.hour==T->next->train->avtime.hour)&&(T->train->avtime.minute>T->next->train->avtime.minute))
{
R=T->next;
H->next=R;
T->next=R->next;
R->next=T;
exchange=1;
}
H=H->next;T=T->next;
}
if(!exchange)break;
H=L;
}
}
else if(!strcmp(str,"lt"))
{
//by leave time
printf("Sort by leave time:\n");
for(;H->next!=NULL;)
{
exchange=0;
for(;H->next->next!=NULL;)
{
T=H->next;
if(T->train->lvtime.hour>T->next->train->lvtime.hour)
{
R=T->next;
H->next=R;
T->next=R->next;
R->next=T;
exchange=1;
}
else if((T->train->lvtime.hour==T->next->train->lvtime.hour)&&(T->train->lvtime.minute>T->next->train->lvtime.minute))
{
R=T->next;
H->next=R;
T->next=R->next;
R->next=T;
exchange=1;
}
H=H->next;T=T->next;
}
if(!exchange)break;
H=L;
}
}
else if(!strcmp(str,"ss"))
{
//by start station
printf("Sort by start station:\n");
for(;H->next!=NULL;)
{
exchange=0;
for(;H->next->next!=NULL;)
{
T=H->next;
if(strcmp(T->train->start,T->next->train->start)>0)
{
R=T->next;
H->next=R;
T->next=R->next;
R->next=T;
exchange=1;
}
H=H->next;T=T->next;
}
if(!exchange)break;
H=L;
}
}
else
{
//by end station
printf("Sort by end station:\n");
for(;H->next!=NULL;)
{
exchange=0;
for(;H->next->next!=NULL;)
{
T=H->next;
if(strcmp(T->train->end,T->next->train->end)>0)
{
R=T->next;
H->next=R;
T->next=R->next;
R->next=T;
exchange=1;
}
H=H->next;T=T->next;
}
if(!exchange)break;
H=L;
}
}
//sort end
printf("Sort completed!\n\n");
}
//write files
int WriteFiles(LinkList* L)
{
int n,i;
FILE *fl;
n=LenList(L);
L=L->next;
//open a file
if((fl=fopen("List.dat","wb"))==NULL)return 11;
//write to this file
for(i=0;i<n;i++)
{
fwrite(L->train,sizeof(datatype),1,fl);
L=L->next;
}
//close file
fclose(fl);
printf("The Database has been update!\n");
return 0;
}
//read files
int ReadFiles(LinkList *L)
{
FILE *fl;
datatype *data;
data=(datatype*)malloc(sizeof(datatype));
//open a file
if((fl=fopen("List.dat","rb"))==NULL)return 11;
//point the file heat
while(!feof(fl))
{
fread(data,sizeof(datatype),1,fl); //read the data
if(strlen(data->ID)>10)continue; //jump the error data
InsList(L,data);
}
//close file and free temp memory
fclose(fl);
free(data);
return 0;
}
//change string to number
int ToNUM(char *str)
{
int num=0,n,m,i;
n=strlen(str);
//change to number
for(i=0;i<n;i++)
{
switch(*str)
{
case '1':
{
m=1;break;
}
case '2':
{
m=2;break;
}
case '3':
{
m=3;break;
}
case '4':
{
m=4;break;
}
case '5':
{
m=5;break;
}
case '6':
{
m=6;break;
}
case '7':
{
m=7;break;
}
case '8':
{
m=8;break;
}
case '9':
{
m=9;break;
}
case '0':
{
m=0;break;
}
default:return -1;//return error's code(illegal char)
}
//creat number
num=num+m*(int)pow((float)10,(float)(n-i-1));
(int)i;str++;
}
return num;
}
//change string to Times type
Times ToTIM(char *str)
{
int i;
Times tm;
char tph[3],tpm[3];
//divide string useing ':'
for(i=0;*str!=':';str++,i++)
tph[i]=*str;
tph[i]='\0';
str++;
for(i=0;*str!='\0';str++,i++)
tpm[i]=*str;
tpm[i]='\0';
//change to number
tm.hour=ToNUM(tph);
tm.minute=ToNUM(tpm);
//deal with error
if(tm.hour==-1||tm.minute==-1)
{
return tm;
}
//hour must be in 24
if(tm.hour>23)
{
tm.hour-=24;
}
//minute must be in 60
if(tm.minute>59)
{
tm.minute-=60;
}
return tm;
}
//check and change time array
int chktime(datatype *Item,char *str)
{
int i;
Times tm[2];
char tp[2][lentm/2];
//check string format
if(str[2]!=':'||str[5]!='-'||str[8]!=':')
{
return 5;
}
//divide string useing ':'
for(i=0;*str!='-';str++,i++)
tp[0][i]=*str;
tp[0][i]='\0';
str++;
for(i=0;*str!='\0';str++,i++)
tp[1][i]=*str;
tp[1][i]='\0';
//change to Times type
tm[0]=ToTIM(tp[0]);
tm[1]=ToTIM(tp[1]);
//deal with error
if(tm[0].hour==-1&&tm[0].minute==-1)
return 5;
if(tm[0].hour>tm[1].hour)
{
return 5;
}
else if(tm[0].hour==tm[1].hour&&tm[0].minute>=tm[1].minute)
{
return 5;
}
//return result
Item->avtime.hour=tm[0].hour;
Item->avtime.minute=tm[0].minute;
Item->lvtime.hour=tm[1].hour;
Item->lvtime.minute=tm[1].minute;
return 0;
}
//check and change time type
Times chktm(char *str)
{
Times tpm;
tpm.hour=-1;
tpm.minute=-1;
//check string format
if(str[2]!=':')
{
return tpm;
}
//change to Times type
tpm=ToTIM(str);
//deal with error
if(tpm.hour==-1||tpm.minute==-1)
{
return tpm;
}
//hour must be in 24
if(tpm.hour>23)
{
tpm.hour-=24;
}
//minute must be in 60
if(tpm.minute>59)
{
tpm.minute-=60;
}
return tpm;
}
//check Illegal char
int strchk(char *str)
{
for(;*str!='\0';)
{
if((*str<'0'||*str>'9')&&(*str<'A'||*str>'Z')&&(*str<'a'||*str>'z')&&(*str!='.')&&(*str!=':')&&(*str!='-')&&(*str!=' '))
return 6;
str++;
}
return 0;
}
//input data into linklist
int Inputdata(LinkList *L)
{
int i,j,m=0,n,err=0;
char tpt[lentm],s[lenid],str[MaxLen];
datatype *Item;
Item=(datatype*)malloc(sizeof(datatype));
//get train's number
printf("Please Input the train's number: ");
fflush(stdin);
scanf("%s",&s);
s[9]='\0';//prevent spill
m=ToNUM(s);
if(m==-1)return 6;//Illegal input data
//get data
for(i=0;i<m;i++)
{
printf("\nPlease Input the coach number[Format: T111]: ");//coach number(車次)
fflush(stdin);
scanf("%s",&s);
s[9]='\0';//prevent spill
if(err=strchk(s))return err;//Illegal input data
strcpy(Item->ID,s);
//check ID, decide ID is only one
n=LenList(L);
for(j=0;j<n;j++)
{
L=L->next;
if(!strcmp(L->train->ID,Item->ID))
{
return 4;
}
}
//get start station
printf("Please Input the train's start station: ");
fflush(stdin);
scanf("%s",&str);
str[19]='\0';//prevent spill
if(err=strchk(str))return err;//Illegal input data
strcpy(Item->start,str);
//get end station
printf("Please Input the train's end station: ");
fflush(stdin);
scanf("%s",&str);
str[19]='\0';//prevent spill
if(err=strchk(str))return err;//Illegal input data
strcpy(Item->end,str);
//get arrive time and leave time
printf("Please Input the train's arrive time and leave time [Fromat: 09:05-10:30]: \nInput: ");
fflush(stdin);
scanf("%s",&tpt);
tpt[11]='\0';//prevent spill
if(err=strchk(str))return err;//Illegal input data
if(err=chktime(Item,tpt))return err;//not zero return error's code
//insert to list
if(err=InsList(L,Item))return err;//not zero return error's code
printf("Completed a record!");
getch();
printf("\n");
}
return err;
}
//
int editdata(LinkList *L,char *tid)
{
int n,i=0,err=0;
char ttm[lentm],str[MaxLen];
n=LenList(L);
if(!n)return 1;//return error's code(Empty)
for(;i<n;i++)
{
L=L->next;
if(!strcmp(L->train->ID,tid))break;
}
if(i==n)return 2;//return error's code(no found)
//show the record
printf("Train's coach number: %s\n",tid);
printf("| Coach No.| Start Station | End Station |Arrive T| Leave T|\n");
printf("| %s\t |%20s|%20s|%5d:%2d|%5d:%2d|\n",L->train->ID,L->train->start,L->train->end,L->train->avtime,L->train->lvtime);
//--------------------edit infomation-------------------------------
//edit start station
printf("Please input new start station [Input \".\" to pass]: ");
fflush(stdin);
scanf("%s",&str);
str[19]='\0';//prevent spill
if(err=strchk(str))return err;//Illegal input data
if(*str!='.')strcpy(L->train->start,str);
//edit end station
printf("Please input new end station [Input \".\" to pass]: ");
fflush(stdin);
scanf("%s",&str);
str[19]='\0';//prevent spill
if(err=strchk(str))return err;//Illegal input data
if(*str!='.')strcpy(L->train->end,str);
//edit arrive time and leave time
printf("Please input new arrive time and new leave time [Input \".\" to pass, Format: 09:05-10:30]:\n");
printf("Input: ");
fflush(stdin);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -