标题:谁能帮我一下,我的 printf 怎么老是输出零,是软件破解的不对,还是我的程 ...
只看楼主
风车转风车89
Rank: 2
等 级:论坛游民
帖 子:125
专家分:45
注 册:2014-9-15
结帖率:81.82%
 问题点数:0 回复次数:1 
谁能帮我一下,我的 printf 怎么老是输出零,是软件破解的不对,还是我的程序错了
#include<stdio.h>
#include<stdlib.h>


#define MAX_VEX_NUM 20
#define N 30              //...........队的最大长度
#define edge_max 100000    //...........边的最大长度值,为不通路的值

typedef struct shortest_path
{
    int parent;
    float distance;
}shortest_path;

typedef struct Queue
{
    int *base;
    int front,rear,size;
}Queue;

typedef struct arc_node
{
    int adjvex;
    float arc_infor;
    struct arc_node *next_arc;
}arc_node;
typedef struct vex_node
{
    char vex;
    arc_node *first_arc;
}vex_node;
typedef struct graph
{
    int arc_num,vex_num,vex[MAX_VEX_NUM];
    int kind;//kind为0为无向图,1为有向图
    vex_node *adj_list;
}Graph;

FILE *fp;
void main()
{
    Graph* file(Graph *g);
    void creat_graph(Graph *g);
    void DFS(Graph *g,int i);
    void DFS_traverse(Graph *g);
    void init_queue(Queue *q);
    void push_queue(Queue *q,int vex);
    int pop_queue(Queue *q);
    void BFS(Graph *g,int i,Queue *q);
    void BFS_traverse(Graph *g,Queue *q);
    int prime(Graph *g);
    int pop_priority_queue(Queue *q,shortest_path *path);
    shortest_path* dijkstra(Graph *g,int i,Queue *q);

    Graph g1,*g; Queue *q,q1;
    g=&g1;q=&q1;
    g=file(g);
    printf(".............深度优先搜索...............\n");
    DFS_traverse(g);
    printf("\n");
    printf(".............广度优先搜索...............\n");
    BFS_traverse(g,q);
    printf("\n");
    printf("............. prime算法.......................\n");
    prime(g);
    printf("............. dijkstra算法.......................\n");
    dijkstra(g,3,q);
}
//.................................................创建一个图

void creat_graph(Graph *g)//采用临街表寸出土的信息
{

    int i,k,vex1,vex2;
    arc_node *s;
    float arc_infor1;
    printf("输入图的顶点数,边数,和图的类型\n");
    fscanf(fp,"%d%d%d",&g->vex_num,&g->arc_num,&g->kind);
    fgetc(fp);
    printf("   %d   %d  %d",g->vex_num,g->arc_num,g->kind);
    g->adj_list=(vex_node*)malloc(g->vex_num*sizeof(vex_node));
   
    for(i=0;i<g->vex_num;i++)
    {
        printf("输入图的顶点信息\n");
        fscanf(fp,"%c",&g->adj_list[i].vex);
        fgetc(fp);
        g->adj_list[i].first_arc=NULL;
    }//初始化邻接矩阵
    for(k=0;k<g->arc_num;k++)
    {
        if(g->kind==0)
        {
            printf("输入起始顶点vex1和终点vex2的值还有边arc_infor1的值\n");
            fscanf(fp,"%d%d%f",&vex1,&vex2,&arc_infor1);
            fgetc(fp);
            s=(arc_node*)malloc(sizeof(arc_node));
            s->adjvex=vex2;
            s->arc_infor=arc_infor1;
            s->next_arc=g->adj_list[vex1].first_arc;
            g->adj_list[vex1].first_arc=s;
            s=(arc_node*)malloc(sizeof(arc_node));
            s->adjvex=vex1;//边连接的另一定点
            s->arc_infor=arc_infor1;
            s->next_arc=g->adj_list[vex2].first_arc;//由此可知先输入的边在邻接表的后面位置
            g->adj_list[vex2].first_arc=s;
        }
        else             //  当为有向图时
        {
            printf("输入起始顶点vex1和终点vex2的值\n");
            fscanf(fp,"%d%d%f",&vex1,&vex2,&arc_infor1);
            fgetc(fp);
            s=(arc_node*)malloc(sizeof(arc_node));
            s->adjvex=vex2;
            s->arc_infor=arc_infor1;
            s->next_arc=g->adj_list[vex1].first_arc;
            g->adj_list[vex1].first_arc=s;
        }        
    }
}

int prime(Graph *g)
{
    int i,k,a[2][MAX_VEX_NUM-1],m,n;   
    arc_node *p;   
    float edge,sum=0;
    for(i=0;i<g->vex_num;i++)
        g->vex[i]=-1;
    g->vex[0]=1;
    for(k=0;k<g->vex_num-1;k++)
    {
        edge=edge_max;//初始值设置为大于所有边值
        for(i=0;i<g->vex_num;i++)
        {
            if(g->vex[i]==1)//如果顶点已访问,找未访问的最小边值的未访问顶点
            {
                p=g->adj_list[i].first_arc;
                while(p!=NULL)
                {
                    if(edge>p->arc_infor&&g->vex[p->adjvex]!=1)
                    {
                        edge=p->arc_infor;
                        m=i; n=p->adjvex;
                    }
                    p=p->next_arc;
                }
            }
        }//找到未访问顶点中到已访问顶点集中边值最小的那个顶点(未访问)
        sum=sum+edge;
        g->vex[n]=1;
        a[0][k]=m;a[1][k]=n;
    }
    for(i=0;i<g->vex_num-1;i++)
        printf("  %d--%d    ",a[0][i],a[1][i]);
    printf("  %d ",sum);  //............................................再此句前设置一个断点可以看到sum的值但是一经输出就是0为什么会这样
    return a[2][g->vex_num-1];
}
Graph* file(Graph *g)
{
    fp=fopen("E:\\3.txt","r");
    if(fp==NULL)
    {
        printf("  cannt open the file\n");
        exit(0);
    }
    else
        creat_graph(g);
    fclose(fp);
    return g;
}
搜索更多相关主题的帖子: distance include parent 软件 
2014-11-22 21:53
风车转风车89
Rank: 2
等 级:论坛游民
帖 子:125
专家分:45
注 册:2014-9-15
得分:0 
回复 楼主 风车转风车 89
论坛出错了吗  为什么不能加深颜色,代码也贴不到剪切板上了
2014-11-22 21:56



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




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

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