⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 trafficguide.c

📁 模拟的一个交通指南
💻 C
字号:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>
#define n 12
#define INFINITY 9999
#define NULL 0
#define LEN sizeof(struct CellType)
/*------------------输出主界面-------------------*/
menu()
{
    int gdriver, gmode;
    gdriver=DETECT;             /* 屏幕分辨率为640*480 */
    registerbgidriver(EGAVGA_driver);    /* 建立独立图形运行程序 */
    initgraph(&gdriver, &gmode, "");

    setbkcolor(0);        /* 设置背景颜色为淡灰色 */
    setcolor(4);
    rectangle(70,50,480,350);
    setfillstyle(1,6);
    bar(70,50,480,350);
    setfillstyle(1, 15); /* 白以实填充 */
    bar(75,55,475,345);

    setcolor(6);
    line(0,50,640,50);
    line(0,350,640,350);    /* 划出分开屏幕的线 */

    setcolor(11);
    line(100,100,250,100);
    line(100,100,100,200);
    line(100,200,250,100);
    line(250,100,400,150);
    line(250,100,300,160);
    line(400,150,300,160);
    line(400,150,350,200);
    line(400,150,350,300);
    line(400,150,450,250);
    line(100,200,170,200);
    line(100,200,150,300);
    line(300,160,170,200);
    line(300,160,350,200);
    line(300,160,250,280);
    line(170,200,250,280);
    line(170,200,150,300);
    line(450,250,450,300);
    line(250,280,350,300);
    line(350,300,450,300);
    line(100,100,170,200);
    line(250,280,150,300);
    setcolor(2);
    settextstyle(0, 0, 2);
    outtextxy(30,150,"M");
    outtextxy(30,200,"A");
    outtextxy(30,250,"P");
    setcolor(4);
    settextstyle(0, 0, 2);
    outtextxy(150,20,"Traffic Guide System");

    /*---各个旅游点----*/
    setfillstyle(1,10);
    circle(100,100,3);        /* 0 医院:Hospital */
    floodfill(100,100,4);
    circle(100,200,3);        /* 1 火车站:Station */
    floodfill(100,200,4);

    circle(400,150,3);        /* 2 博物馆:Museum */
    floodfill(400,150,4);
    circle(250,100,3);        /* 3 市政府 Government */
    floodfill(250,100,4);

    circle(300,160,3);        /* 4 公园:Park */
    floodfill(300,160,4);
    circle(170,200,3);        /* 5 购物中心:Shopping Centre */
    floodfill(170,200,4);

    circle(350,200,3);        /* 6 大学:University */
    floodfill(350,200,4);
    circle(450,250,3);        /* 7 消防站:Fire Station */
    floodfill(450,250,4);

    circle(250,280,3);        /* 8 警察局:Police Station */
    floodfill(250,280,4);
    circle(150,300,3);        /* 9 国际机场:Aerodrome */
    floodfill(150,300,4);

    circle(350,300,3);        /* 10 码头:Dock */
    floodfill(350,300,4);
    circle(450,300,3);        /* 11 化工厂:Chemical Plant */
    floodfill(450,300,4);

    setcolor(12);
    settextstyle(1, 0, 4);   /* 三重笔划字体, 水平放大8倍 */
    outtextxy(90,100,"0");
    outtextxy(90,200,"1");
    outtextxy(410,150,"2");
    outtextxy(250,90,"3");
    outtextxy(300,150,"4");
    outtextxy(170,190,"5");
    outtextxy(345,205,"6");
    outtextxy(440,250,"7");
    outtextxy(250,290,"8");
    outtextxy(150,310,"9");
    outtextxy(350,310,"10");
    outtextxy(450,310,"11");

    setcolor(2);
    outtextxy(500,70,"0-Hospital");
    outtextxy(500,90,"1-Station ");
    outtextxy(500,110,"2-Museum");
    outtextxy(500,130,"3-Government");
    outtextxy(500,150,"4-Park");
    outtextxy(500,170,"5-Shopping Centre");
    outtextxy(500,190,"6-University");
    outtextxy(500,210,"7-Fire Station");
    outtextxy(500,230,"8-Police Station");
    outtextxy(500,250,"9-Aerodrome");
    outtextxy(500,270,"10-Dock");
    outtextxy(500,290,"11-Chemical Plant");

    setfillstyle(1,8);
    bar(330,420,400,450);
    bar(420,420,490,450);
    bar(510,420,580,450);
    setcolor(11);
    outtextxy(50,370,"Please choose the operation: s--Search r--Renew e--Exit");
    setcolor(15);
    outtextxy(340,432,"Search");
    outtextxy(435,432,"Renew");
    outtextxy(528,432,"Exit");
}

/*------点的结构体并初始化--------*/
struct G
{
    int placeName;
    int x;
    int y;     /* 点的坐标及位置名 */
}G[12]={ {0,100,100},{1,100,200},{2,400,150},{3,250,100},{4,300,160},{5,170,200},
          {6,350,200},{7,450,250},{8,250,280},{9,150,300},{10,350,300},{11,450,300}
       };

/*------图的结构体--------*/
struct graph
{
    struct G G[n];   /* 结点信息 */
    int C[n][n];     /* 边的权(时间) */
}graphic;

/*-------设置全局变量-------*/
int path[n][n],C[n][n]; /*path[n][n]表示路径矩阵,C[n][n]表示权值(时间)*/

/*--------建立一个链表结构体----*/
struct CellType{
    int num;      /*链表中的数*/
    struct CellType *next;/*指向下一个的指针*/
    };

/*--------创建地图的邻接矩阵--------*/
Creatgraph()
{
    int i,j;
    struct graph *graphic=NULL;

/*-------对图的边进行初始化--------*/
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            if(i!=j)graphic->C[i][j]=INFINITY;
    for(i=0;i<n;i++)
        graphic->C[i][i]=0;

/*--------给边的权赋值----------*/
    graphic->C[0][1]=graphic->C[1][0]=4;
    graphic->C[0][3]=graphic->C[3][0]=5;
    graphic->C[0][5]=graphic->C[5][0]=4;
    graphic->C[1][5]=graphic->C[5][1]=3;
    graphic->C[1][3]=graphic->C[3][1]=3;
    graphic->C[1][9]=graphic->C[9][1]=4;
    graphic->C[3][2]=graphic->C[2][3]=4;
    graphic->C[3][4]=graphic->C[4][3]=2;
    graphic->C[5][4]=graphic->C[4][5]=4;
    graphic->C[5][8]=graphic->C[8][5]=2;
    graphic->C[5][9]=graphic->C[9][5]=2;
    graphic->C[9][8]=graphic->C[8][9]=3;
    graphic->C[4][8]=graphic->C[8][4]=3;
    graphic->C[4][2]=graphic->C[2][4]=3;
    graphic->C[4][6]=graphic->C[6][4]=1;
    graphic->C[8][10]=graphic->C[10][8]=6;
    graphic->C[2][6]=graphic->C[6][2]=3;
    graphic->C[2][10]=graphic->C[10][2]=9;
    graphic->C[2][7]=graphic->C[7][2]=4;
    graphic->C[10][11]=graphic->C[11][10]=3;
    graphic->C[7][11]=graphic->C[11][7]=2;
}

/*---------------给出查询结果路径--------------*/
struct CellType *head;
struct CellType *p;      /*设一个指向CellType结构体的指针p*/
route(int i,int j)       /*建一个以i为头指针元素,j为尾的链表*/
{
    int k;
    struct CellType *p1;/*设一个指向CellType结构体的指针p1*/
    k=path[i][j];       /*在路径矩阵中,k为path[i][j]的值*/
    if(k!=0)            /*即i,j之间还有别的点(地点)*/
    {
        route(i,k);     /*递归算法*/
        p->num=k;
        p1=p;
        p=(struct CellType *)malloc(LEN);/*为指针p开辟空间*/
        p1->next=p;
        route(k,j);     /*递归算法*/
    }
    p->num=j;
    p->next=NULL;
}

/*---------------查询描绘路径和节点------------*/
  /*----------  a--起始点 b--终点  -------*/
int a,b;
search(int a,int b)
{

    struct graph *graphic;
    struct CellType *p2;
    struct CellType *p3;

    int suffix1,suffix2,nx1,ny1,nx2,ny2; /*suffix1,suffix2是下标;nx1,ny1,nx2,ny2是坐标*/
    int i,j,k,A[n][n],C[n][n];
    Creatgraph();
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            C[i][j]=graphic->C[i][j];
    for(i=0;i<n;i++)                      /* Floyd算法 */
        for(j=0;j<n;j++) {A[i][j]=C[i][j]; path[i][j]=0;};

    for(k=0;k<n;k++)                     /* A[i][j](最短路径矩阵)的初值 */
       for(i=0;i<n;i++)
          for(j=0;j<n;j++)
            if(A[i][k]+A[k][j]<A[i][j])  /*path[i][j]是路径矩阵*/
               {A[i][j]=A[i][k]+A[k][j];
                 path[i][j]=k;}

    head=(struct CellType *)malloc(LEN);
    head->num=a;
    head->next=NULL;
    p=(struct CellType *)malloc(LEN);
    head->next=p;
    route(a,b);

    p2=p3=head;
    printf("The shortest path from %d to %d is :\n",a,b);
    do
    {   printf("   %d   ",p3->num);
        p3=p3->next;
     }while(p3!=NULL);
    printf("\nThe time spent in the road is %d minutes!",A[a][b]);/*输出最短路径长度*/
    p2=head;
    setcolor(4);
   /*---------------着重标出路径--------------*/
     do
    {
         suffix1=p2->num;
         suffix2=p2->next->num;
         nx1=G[suffix1].x;
         ny1=G[suffix1].y;
         nx2=G[suffix2].x;
         ny2=G[suffix2].y;
         line(nx1,ny1,nx2,ny2);
         p2=p2->next;
    }while(p2->next!=NULL);
   /*---------------着重标出点--------------*/
    setcolor(7);
    setfillstyle(1, 4);
    p2=head;
    do
    {
        suffix1= p2->num;
        nx1=G[suffix1].x;
        ny1=G[suffix1].y;
        p2=p2->next;
        circle(nx1,ny1,3);
        floodfill(nx1,ny1,7);
    }while(p2!=NULL);
    getch();
    main();
    closegraph();
}

/*------------------重新进行使用-------------------*/
renew()
{
    cleardevice();
    menu();
}

/*----------------结束查询,跳出程序----------------*/
end()
{
    int i;
    int gdriver, gmode;
    gdriver=DETECT;                      /* 屏幕分辨率为640*480 */
    registerbgidriver(EGAVGA_driver);    /* 建立独立图形运行程序 */
    initgraph(&gdriver, &gmode, "");
    setcolor(3);
    settextstyle(0,0,8);
    for(i=0;i<=50;i++)
    {
        delay(1000000000000);
        outtextxy(100,200,"Byebye...");
        i++;
    };
    closegraph();
}
checkerror(int e)    /*出错判断*/
{
  if( e<0|| e>11 )
                {
                  if(e==-1)
                  {
                   renew();
                   outtextxy(220,390,"Your input is error.Press any key to exit!");
                   getch();
                   end();
                  }
                   outtextxy(220,390,"Your input is error. You must input from 0 to 11!");
                   getch();
                   main();
                }
 }
main()
{
    int check;
    int a=b=-1;
    char c;
    int gdriver, gmode;
    gdriver=DETECT;                      /* 屏幕分辨率为640*480 */
    registerbgidriver(EGAVGA_driver);    /* 建立独立图形运行程序 */
    initgraph(&gdriver, &gmode, "");
    menu();
    c=getch();
    while(c!='e')                        /*根据输入的字符,判断操作*/
    {
        if(c=='s')
            {
                setcolor(7);
                setfillstyle(1, 4);
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");/* 使文本在屏幕左下角输出 */
                printf("Please enter the locus: ");
                scanf("%d",&a);
                checkerror(a);
                circle(G[a].x,G[a].y,3);
                floodfill(G[a].x,G[a].y,7);

                printf("Please enter the destination:");
                scanf("%d",&b);
                checkerror(b);
                circle(G[b].x,G[b].y,3);
                floodfill(G[b].x,G[b].y,7);

                getch();
                search(a,b);
                exit(0);
            }
        else if(c=='r')
                renew();
        else
            {   setcolor(15);
                outtextxy(220,390,"Your input is error.Renew it then input again!");
            }
        c=getch();
    }
    end();

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -