标题:[原创]利用VC++6.0制作的一个地铁程序原代码,一起分析
只看楼主
化妆的女人
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2015-6-16
 问题点数:0 回复次数:0 
[原创]利用VC++6.0制作的一个地铁程序原代码,一起分析
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HEADER1 "         --------------------------地铁站点信息-----------------------\n"
#define FORMAT  " %s\n%s\n%s\n\n%s\n%d\n%s\n%s\n"
#define DATA p->data.num,p->data.startstation,p->data.reachstation,p->data.allthestation,p->data.price,p->data.firsttime,p->data.lasttime
int saveflag=0 ;
struct subway
{
    char num[10];/*地铁编号*/
    char startstation[10];/*起点站*/
    char reachstation[10];/*终点站*/
    char allthestation[1000];/*总站点数*/
    int  price;/*总票价*/
    char firsttime[10];/*启运时间*/
    char lasttime[10];/*收班时间*/
   
};
typedef struct node

{

    struct subway data ;

    struct node * next ;

}Node,*Link ;
void subwayinfo(Link linkhead)
{
    struct node *p,*r,*s ;
    char num[10];
    r = linkhead ;
    s = linkhead->next ;
    while(r->next!=NULL)
           r=r->next ;
while(1)
    {  
lab1:printf("请输入此地铁的名称:(0-返回)");
    scanf("%s",num);
    if(strcmp(num,"0")==0)
          break;
s = linkhead;
        while(s)
        {
            if(strcmp(s->data.num,num)==0)
            {
                printf("此地铁线路 '%s'已经存在!\n",num);
                goto lab1;
                return;
            }
            s = s->next ;
        }


p = (struct node*)malloc(sizeof(struct node));
        strcpy(p->data.num,num);
    printf("请输入地铁起始站:");
        scanf("%s",p->data.startstation);
    printf("请输入地铁终点站:");
        scanf("%s",p->data.reachstation);
    printf("请输入此地铁线路所有的站点:");
        scanf("%s",p->data.allthestation);
    printf("请输入此地铁站全程总票价:");
        scanf("%d",&p->data.price);
    printf("请输入此地铁首班车时间:");
        scanf("%s",p->data.firsttime);
    printf("请输入此地铁末班车时间:");
         scanf("%s",p->data.lasttime);
        p->next=NULL ;
        r->next=p ;
        r=p ; }
}





void printheader() /*格式化输出表头*/
{
printf(HEADER1);
}
void printdata(Node *q) /*格式化输出表中数据*/
{
Node *p;
p=q;
printf(FORMAT,DATA);
}


void Modify(Link l)
{
    Node *p ;
    char tnum[10],ch ;
    p=l->next;
    if(!p)
    {
        printf("\n并没有在记录中找到此信息!\n");
        return ;
    }
    else
    {
        printf("\n你想要修改此信息吗?(y/n)\n");
            getchar();
            scanf("%c",&ch);
            if(ch=='y'||ch=='Y')
            {
                printf("\n请输入地铁的线路名称:");
        scanf("%s",tnum);
    while(p!=NULL)
    if(strcmp(p->data.num,tnum)==0)/*查找与输入的名称相匹配的记录*/
        break;
        else
            p=p->next;
                if(p)
                {
                    printf("请输入新的地铁线路名称:");
                    scanf("%s",&p->data.num);
                    printf("请输入新的地铁起始站:");
                    scanf("%s",p->data.startstation);
                    printf("请输入新的地铁终点站:");
                    scanf("%s",p->data.reachstation);
                    printf("请输入新的此地铁所有站:");
                    scanf("%s",p->data.allthestation);
                    printf("请输入新的此地铁的总票价:");
                    scanf("%d",&p->data.price);
                    printf("请输入新的此地铁首班车时间:");
                    scanf("%s",p->data.firsttime);
                    printf("请输入新的此地铁末班车时间:");
                    scanf("%s",p->data.lasttime);
                    printf("\n修改记录成功!\n");
                    saveflag=1 ;
                }
                else
                printf("\t没有找到此记录!");
            }
    }
}

void showsubway(Link l)/*自定义函数显示地铁信息*/
{
Node *p;
p=l->next;
printheader();
if(l->next==NULL)
printf("没有信息!");
else
 while(p!=NULL)
 {  printf("--------------------------------------------------------------------------------\n");
    printf("%s\n",p->data.num);
    printf("%s\n",p->data.startstation);
    printf("%s\n",p->data.reachstation);
    printf("\n%s\n",p->data.allthestation);
    printf("%d\n",p->data.price);
    printf("%s\n",p->data.firsttime);
    printf("%s\n",p->data.lasttime);
    printf("--------------------------------------------------------------------------------\n");
    p=p->next;
}
}

void searchsubway(Link l)

{
    Node *s[10],*r;
    int sel,k,i=0 ;
    char str1[10],str2[10];
    if(!l->next)
    {
        printf("没有任何记录 !");
        return ;
    }
    printf("选择一下方式:\n1:根据地铁线路;\n2:根据地铁站点:\n");
    scanf("%d",&sel);/*输入选择地铁线*/
    if(sel==1)
    {
        printf("请输入想查找的地铁线路:");
        scanf("%s",str1);
        r=l->next;
    while(r!=NULL)
        if(strcmp(r->data.num,str1)==0)/*检索是否有与输入的地铁线路相匹配的*/
        {
            s[i]=r;
        i++;
        break;
        }
        else
            r=r->next;
    }
    else if(sel==2)
    {
        printf("请输入你想去的车站:");
        scanf("%s",str2);
        r=l->next;
        while(r!=NULL)
        if(strcmp(r->data.allthestation,str2)==0)/*检索是否有与输入的车站相同*/
        {
            s[i]=r;
        i++;
        r=r->next;
        }
        else
            r=r->next;
    }
        if(i==0)
        printf("没有找到!");
    else
    {
        printheader();
        for(k=0;k<i;k++)
        printdata(s[k]);
    }
}




void SaveSubway(Node *l)
{
    FILE *fp;
    Node *p;
    int count=0,flag=1 ;
    fp=fopen("h:\\subway.txt","wb");
    if(fp==NULL)
    {
        printf("此文件不能打开哦!");
        return ;
    }
    p=l->next;
    while(p)
    {
        if(fwrite(p,sizeof(Node),1,fp)==1)
        {
            p=p->next ;
            count++;
        }
        else
        {
            flag=0 ;
            break ;
        }
    }
    if(flag)
    {
        printf(" 保存 %d 地铁并记录\n",count);
        saveflag=0 ;
    }
    fclose(fp);
}





void menu()
{
    puts("\n\n");
    puts("\t\t|-------------------------------------------------------|");
    puts("\t\t|                   地铁查询系统                        |");
    puts("\t\t|-------------------------------------------------------|");
    puts("\t\t|       0:返回主界面                                    |");
    puts("\t\t|       1:输入地铁信息                                  |");
    puts("\t\t|       2:查询地铁站点信息                              |");
    puts("\t\t|       3:换乘信息查询                                  |");
    puts("\t\t|       4:计价查询系统                                  |");
    puts("\t\t|       5:修改地铁信息                                  |");
    puts("\t\t|       6:显示所有地铁信息                              |");
    puts("\t\t|       7:保存地铁信息                                  |");
    puts("\t\t|-------------------------------------------------------|");
}
main()
{  int num;
   FILE*fp1;
   Node *r,*p;
  //char ch1;
   Link l;
   l=(Node *)malloc(sizeof (Node));
   l->next=NULL;
   r=l;
   fp1=fopen("h:\\subway.txt","ab+");
    if((fp1==NULL))
    {
        printf("打开不了文件!");
        return 0 ;
    }
    while(!feof(fp1))
    {
        p=(Node*)malloc(sizeof(Node));
        if(fread(p,sizeof(Node),1,fp1)==1)
        {
            p->next=NULL ;
            r->next=p;
            r=p ;

        }
    }
   fclose(fp1);
   while(1)
   {   
        menu();
        printf("请选择输入 0~7:\n");
        scanf("%d",&num);
        /*if(num==0)
        {
           if(saveflag==1)//当退出时判断信息是否保存
           {
                getchar();
                printf("\nthe file have been changed!do you want to save it(y/n)?\n");
                scanf("%c",&ch1);
                if(ch1=='y'||ch1=='Y')
                {
                    SaveSubway(l);
                }
           }
            printf("\nThank you!!You are welcome too\n");
               break;
        }*/
        switch(num)
        {

        case 1:  
            {subwayinfo(l);
                break;}
        case 2:
            searchsubway(l);   
            break;
        case 3:
            printf("换乘信息查询");    break;
        case 4:
            printf("计价查询系统");    break;
        case 5:
            Modify (l);   
            break;
        case 6:
            showsubway(l);   
            break;
        case 7:
            SaveSubway(l);
            break;
        case 0:
            num=0;
        }
        printf("\n请按任意健继续......");
        getchar();
    }
}
我想请教下用除去图的方法做换乘该如何做,还有就是计费系统用什么方法做比较好,查询系统那,如何只输入一个站点就能够得到此站点所有线路的所有信息??、
搜索更多相关主题的帖子: include subway 终点站 起点 
2015-06-16 19:38



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-446664-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.529456 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved