C++编的车票管理系统《仅供参考!!》

作者在 2010-06-22 10:27:55 发布以下内容
#include<stdio.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>

typedef struct information
{
int numb;
char starttime[10];
char begin[21];
char end[21];
float lasttime;
int canload;
int alreadyload;
struct information *next;
}INFORMATION,*PINF;             //结构体类型的指针

INFORMATION *head;             //建立班次头结点
INFORMATION *rear;             //建立班次尾结点

int main() //主程序
{
system("color 1f");
//system("mode con: cols=140 lines=130");

head=rear=(INFORMATION *)malloc(sizeof(INFORMATION));
head->next=NULL;
//函数声明        
void Read_In();         //文件读入
void Scan();             //浏览全部信息
void Lookup();          //查询信息
void Buyticket();       //预定车票
void Cancelticket();    //退订车票
void Save_In();            //把数据重新保存到文件中

Read_In();     //读入信息

int i=0;
while(i!=5)
{
    printf("\n");
    printf("==========================\n");
    printf("       车票管理系统\n");
    printf("       制作:朱俊峰、郎江涛\n");
    printf("       日期:2010/6/21\n");
    printf("==========================\n\n\n");
    printf("=======车站管理系统=======\n");   //总菜单
    printf(" 1、    浏览信息         \n");
    printf(" 2、    查询信息         \n");
    printf(" 3、    预订车票         \n");
    printf(" 4、    退订车票         \n");
    printf(" 5、    退出系统         \n");
    printf("==========================\n");
    
    printf("请选择你所需要的功能:");
    scanf("%d",&i);
    switch(i)
    {  
      
      case 1:Scan();break;
      case 2:Lookup();break;
      case 3:Buyticket();break;
      case 4:Cancelticket();break;
      case 5:break;
      default:printf("输入错误,请确保你的输入为1--5.\n");break;
    }
}   //while (i!=5)

Save_In();    //保存信息

return 0;
}

void Read_In()       //班次信息文件读入
{
     FILE *fp;
     if((fp=fopen("information.txt","r+"))==0)
     {
        printf("\n打开文件错误!\n");
        exit(0);
     }
     while(!feof(fp))      //读取文件并测试是否抵达文件尾
     {
       INFORMATION *p;
       p=(INFORMATION *)malloc(sizeof(INFORMATION)); //开辟内存空间
      
       fscanf(fp,"%d%s%s%s%f%d%d\n",&p->numb,&p->starttime,&p->begin,&p->end,&p->lasttime,&p->canload,&p->alreadyload);
      
       rear->next=p;      //在INFORMATION单链表中依次插入下一结点
       rear=p;
       rear->next=NULL;    
     }
     fclose(fp);
    
}

void Save_In()     //保存数据
{
     FILE *fp;
     if((fp=fp=fopen("information.txt", "r+"))==0)
     {
        printf("\n打开文件错误!\n");
        exit(0);
     }
     PINF p=head->next;  
     while(p!=NULL)
     {
       fprintf(fp,"%d\t%s\t%s\t%s\t%.2f\t%d\t%d\n",p->numb,p->starttime,p->begin,p->end,p->lasttime,p->canload,p->alreadyload);
       p=p->next;
     }
     fclose(fp);
}

void Scan()
{
     PINF p=head->next;
     printf("\n各班次信息如下:\n");
     printf("\n班次号\t出发时间\t起点\t终点\t行车时间    额定载客量    已订票人数\n");
     printf("____________________________________________________________________________\n");
     while(p!=NULL)
     {
         printf("%d\t%s\t\t%s\t%s\t%.2f\t\t%d\t\t%d\n",p->numb,p->starttime,p->begin,p->end,p->lasttime,p->canload,p->alreadyload);
         p=p->next;
     }
     printf("____________________________________________________________________________\n");
}

void Lookup()       //查询班次信息
{    
     system("color 1f");
     //system("mode con: cols=140 lines=130");
     void SearchbyNum();   //函数声明
     void SearchbyEnd();   //函数声明
     int i,j=0;
     while(j!=1)
     {
        printf("\n");        
        printf("       查询子菜单\n");
        printf("==========================\n");
        printf("     1.按班次号查询\t\n");
        printf("     2.按终点站查询\t\n");
        printf("==========================\n");
        printf("请选择查询方式:");
        scanf("%d", &i);
        switch(i)
        {
           case 1: SearchbyNum(); break;
           case 2: SearchbyEnd(); break;
           default : printf("\n输入错误,请确保你的输入为1--2.\n");
                     break;
        }
        printf("==========================\n");
        printf("\n1.返回主菜单 2.继续查询 : ");
        scanf("%d",&j);
     }
}

void SearchbyNum()   //按班次号查询班次
{  
    system("color 1f");
   // system("mode con: cols=140 lines=130");
    int GetHour(char *s);
    int GetMinute(char *s);
    
    PINF p,q;   //定义两个结点
    int num;   //要查询的班次号
    int j=0;     //判断班次          
    p=head->next;
    printf("\n请输入需要查询的班次号: ");
    scanf("%d",&num);
    while(p!=NULL)
    {
        if(p->numb-num==0)
        {  
            q=p;
            printf("\n");
            printf("班次号: \t");printf("%d\n",q->numb);
            printf("出发时间:\t");printf("%s\n",q->starttime);
            printf("起点: \t\t");printf("%s\n",q->begin);
            printf("终点: \t\t");printf("%s\n",q->end);
            printf("行车时间: \t");printf("%.2f\n",q->lasttime);
            printf("额定载客量: \t");printf("%d\n",q->canload);
            printf("已定票人数: \t");printf("%d\n",q->alreadyload);
            
            time_t t;
            struct tm *timeinfo;
            time(&t);
            timeinfo = localtime(&t);
            
            if(GetHour(q->starttime)<timeinfo->tm_hour)
               printf("\n此班次车已发出!\n");
            else
               if(GetHour(q->starttime) == timeinfo->tm_hour)
               {
                  if(GetMinute(q->starttime)<=timeinfo->tm_min)
                     printf("\n此班次车已发出!\n");
                  else
                     printf("\n此班次车尚未发出!\n");
               }
               else
                  printf("\n此班次车尚未发出!\n");
            j++;
        }
        p=p->next;    
    }     //while(p!=NULL)
    if(j==0)
       printf("\n对不起,不存在此班次!\n");
}

void SearchbyEnd()    //按终点站查询班次
{
     system("color 1f");
     //system("mode con: cols=140 lines=130");
     int GetHour(char *s);
     int GetMinute(char *s);
    
     PINF p,q;   //定义两个结点
     char end[21];   //要查询的终点站名
     int j=0;              
     p=head->next;
     printf("\n请输入需要查询的终点站名: ");
     scanf("%s",end);
     while(p!=NULL)
     {
        if(strcmp(p->end,end)==0)
        {  
            q=p;
            printf("\n");
            printf("班次号: \t");printf("%d\n",q->numb);
            printf("出发时间:\t");printf("%s\n",q->starttime);
            printf("起点: \t\t");printf("%s\n",q->begin);
            printf("终点: \t\t");printf("%s\n",q->end);
            printf("行车时间: \t");printf("%.2f\n",q->lasttime);
            printf("额定载客量: \t");printf("%d\n",q->canload);
            printf("已定票人数: \t");printf("%d\n",q->alreadyload);
            
            time_t t;
            struct tm *timeinfo;
            time(&t);
            timeinfo=localtime(&t);
            
            if(GetHour(q->starttime)<timeinfo->tm_hour)
               printf("\n此班已发出!\n");
            else
               if(GetHour(q->starttime)==timeinfo->tm_hour)
               {
                  if(GetMinute(q->starttime)<=timeinfo->tm_min)
                     printf("\n此班已发出!\n");
                  else
                     printf("\n此班次车尚未发出!\n");
               }
               else
                  printf("\n此班次车尚未发出!\n");
            j++;
        }
        p=p->next;    
     }     //while(p!=NULL)
     if(j==0)
       printf("\n对不起,不存在此班次!\n");
}

void Buyticket()    //预定车票
{
     system("color 1f");
     //system("mode con: cols=140 lines=130");
     int GetHour(char *s);
     int GetMinute(char *s);
    
     int i=0;
     while(i!=1)
     {
        int num,j=0;
        PINF p,q;   //定义两个结点
        p=head->next;
        
        time_t t;
        struct tm *timeinfo;
        time(&t);
        timeinfo=localtime(&t);
        
        printf("\n请输入你要购买的班次号:");
        scanf("%d",&num);
        while(p!=NULL)
        {
            if(p->numb-num==0)
            {
               q=p;
               j++;
               if(q->alreadyload<q->canload)
               {
                  if(GetHour(q->starttime)>timeinfo->tm_hour)
                  {
                     printf("\n你已订票成功,请按时上车,谢谢使用!\n");
                     q->alreadyload++;
                  }
                  else
                     if((GetHour(q->starttime)==timeinfo->tm_hour) && (GetMinute(q->starttime)>timeinfo->tm_min))
                     {
                        printf("\n你已订票成功,请按时上车,谢谢使用!\n");
                        q->alreadyload++;
                     }
                     else
                        printf("\n对不起,该班次车已经发出,请明天再来!谢谢使用!\n");
               }
               else
                  printf("\n对不起,该班次车已经发出,请明天再来!谢谢使用!\n");
            }
            p=p->next;
        }
        if(j==0)
           printf("\n对不起,不存在此班次!谢谢使用!\n");
          
        printf("==========================\n");
        printf("\n1. 返回主菜单 2. 继续订票 : ");
        scanf("%d",&i);
     }//while(i!=1)
}

void Cancelticket()       //退订车票
{
     system("color 1f");
     //system("mode con: cols=140 lines=130");
     int GetHour(char *s);
     int GetMinute(char *s);
    
     int i=0;
     while(i!=1)
     {
         PINF p,q;
         p=head->next;
         int num,j=0;
        
         time_t t;
         struct tm *timeinfo;
         time(&t);
         timeinfo=localtime(&t);
        
         printf("\n请输入你要退票的班次号:");
         scanf("%d",&num);
         while(p!=NULL)
         {
             if(p->numb-num==0)
             {
                q=p;
                j++;
                if(GetHour(q->starttime)>timeinfo->tm_hour)
                {
                   printf("\n你已退票成功,谢谢使用!\n");
                   q->alreadyload--;
                }
                else
                   if((GetHour(q->starttime)==timeinfo->tm_hour) && (GetMinute(q->starttime)>timeinfo->tm_min))
                   {
                      printf("\n你已退票成功,谢谢使用!\n");
                      q->alreadyload--;
                   }
                   else
                      printf("\n对不起,该班次车已经发出,不能退票!谢谢使用!\n");
             }
             p=p->next;
         }
         if(j==0)
            printf("\n对不起,输入错误,不存在此班次车!谢谢使用!\n");
            
         printf("==========================\n");  
         printf("\n1. 返回主菜单 2. 继续退票 : ");
         scanf("%d",&i);
     }
}

int GetHour(char *s)
{
    char *p,*q;
    p=(char *)malloc(10);
    strcpy(p,s);
    q=p;
    while(*q!=':')
    {
       q++;
    }
   *q='\0';
   return atoi(p);                    //字符串到浮点数的转换
}

int GetMinute(char *s)
{
    char *p;
    p=(char *)malloc(10);
    strcpy(p,s);
    while(*p!=':')
    {
       p++;
    }
    p++;
    return atoi(p);
}

默认分类 | 阅读 1639 次
文章评论,共0条
游客请输入验证码