• 校园导游图的课程设计(一)


    思路:

    1.  使用两个文件 arc 和 vex 分别存放 节点和弧信息

     存放格式为:

    typedef struct data{
    char placename[NAMEMAX];
    char placedata[DATAMAX];
    }Data;
    typedef struct arc{
    int forvex;
    int backves;
    int weight;
    }Arc;//文件存储结构体
    View Code

    2. 为了方便删除和添加,使用邻接表,邻接表的结构体

    typedef struct arcnode{
            int adjvex;
            int weight;
            struct arcnode* next;
    }ArcNode;
    
    typedef struct vertexnode{
            Data vexdata;
            ArcNode * next;
    }VertxNode;
    
    typedef struct {
            VertxNode vertex[NUMMAX];
            int vexnum;
            int arcnum;
    }ListMatrix;
    View Code

    3. 为了方便操做和模块化,将地图的节点和弧的添加分别抽象成模块

    int  AddArc( ListMatrix * G  )
    /*
     * 增加弧
     * 成功返回1
     * 失败返回0
     */
    {
         char forvextemp[NAMEMAX]; 
         char backvextemp[NAMEMAX];
         int forvex;
         int backvex;
         ArcNode * temp;
         ArcNode * p;
         printf("输入起始点:");
         scanf("%s",forvextemp);
        if( (forvex = NametoNum( G, forvextemp )) == 0  )
        {
                return 0;
        }
        printf("输入终点:");
        scanf("%s",backvextemp);
        if( (backvex = NametoNum( G,backvextemp)) == 0 )
        {
                return 0;
        }//读取起点和终点
    
        G->arcnum++;//弧数加一
    
        temp = (ArcNode*)malloc(sizeof(ArcNode));
        printf("输入路径长度:");
        scanf("%d",&temp->weight);
        temp->adjvex = backvex;
        temp->next = NULL;//赋值
    
        p = G->vertex[forvex].next;
        while( p&&p->next )  p = p->next;
        if( !p )  G->vertex[forvex].next = p;
        else      p->next = temp; //挂链
        return 1;
    }
    View Code

    4.整个程序,只有在开始时读 arc vex 文件,建立map,在退出时写文件,将可能被修改过的map,覆盖写入 arc vex 文件

         其他所有操作都建立在内存中的map,不再对文件进行操作

    int  MakeMap( ListMatrix *G )
    /*
     * 利用文件建立地图
     * 输入存放节点的文件 fp1 ,存放弧的文件fp2
     *
    */
    { 
            FILE  * fp1;
            FILE  * fp2;
       int  nodecount = 0;
       int  arccount = 0;
       Data nodedata;
       Arc  arcdata;
       ArcNode * p;
       ArcNode * temp;
            if( (fp1=fopen("vex","ab+")) == NULL )
            {
                 printf("vex打开错误");
                 return 0;
            }
            if( (fp2=fopen("arc","ab+")) == NULL )
            {
                    printf("arc打开错误
    ");
                    return 0;
            }
       
       while( fread(&nodedata,sizeof(Data),1,fp1) == 1 )
       { 
               nodecount++; 
               strcpy(G->vertex[nodecount].vexdata.placename,nodedata.placename);
               strcpy(G->vertex[nodecount].vexdata.placedata,nodedata.placedata);  
               G->vertex[nodecount].next = NULL;
       }//读取节点
    
       if( nodecount != 0 )
       {
            while( fread( &arcdata,sizeof(Arc),1,fp2) == 1 )
            {
                    arccount++;
    
                    temp->next = ( ArcNode *)malloc(sizeof(ArcNode));
                    temp->next->adjvex = arcdata.backves;
                    temp->next->weight = arcdata.weight;
                    temp->next->next = NULL;
     
                    p = G->vertex[arcdata.forvex].next;
                    while( p && p->next )  p = p->next;
                    if( !p )    G->vertex[arcdata.forvex].next = temp;
                    else        p->next =temp;//挂链
            }
       }//读取弧
       G->vexnum = nodecount;
       G->arcnum = arccount;
       fclose(fp1);
       fclose(fp2);
    }
    View Code
  • 相关阅读:
    Web service是什么?
    SQL截取字符串
    SQL Server中使用索引性能的比较
    一个C#中webservice的初级例子(一)
    short s1 = 1; s1 = s1 + 1;有错而short s1 = 1; s1 += 1正确。为何?
    SQL索引
    ORDER BY 子句在子查询和公用表表达式中无效的一种解决办法使用表变量
    创建 索引,
    时间的重叠
    SQLServer Datetime数据类型的转换
  • 原文地址:https://www.cnblogs.com/dilidingzhi/p/4151540.html
Copyright © 2020-2023  润新知