第四周作业

1. 图的表示:给定图数据文件,计算得到图的邻接矩阵,并把邻接矩阵保存到文件(tinyG_matrix.txt)中。

源码:

#include <stdio.h>

#define OK 1

#define ERROR 0

#define MAX_VERTEX_NUM 20

typedef int VrType;

typedef int VertexType;

typedef struct AraCell{

VrType adj;

}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct{

VertexType vexs[MAX_VERTEX_NUM];

AdjMatrix arcs;

int vexnum,arcnum;

}MGraph;



//输出邻接矩阵

void printGraph(MGraph G)

{

	int i,j;
	for(i=0;i<G.vexnum;i++) 
		printf("%2d",i+1);
	printf("\n");

	for(i=0;i<G.vexnum;i++){

		printf("  %2d  ",G.vexs[i]);

		printf("%2d",i+1);

		for(j=0;j<G.vexnum;j++)
			printf("%2d",G.arcs[i][j]);  
		printf("\n");
	}

}

 

//主文件


int main()

{
	int n;
	FILE *in, out;
	in =fopen("e:\\tinyG.txt","r");    
    n=0;    
	
    while (true) {    
        r= fscanf(f,"%lf",&d);    
        if (r == 1) {    
            n++;  
            printf("%d==%g",n,d);  
            a[i]=d;  
            i++;              
        } else if (0==r) {    
            fgetc(f);    
        } else break;    
    }  
    int max = a[0];  
    for (i = 1; i < n; ++i)   
    {  
        if (a[i] > max)  
            max = a[i];  
    }  

    double b[14][14]={0};  
    int t=0,s=0;  
    for(int j=0;j<n;j++)  
    {  
        t=a[j];  
        j++;  
        s=a[j];  
        b[t][s]=1;        
    }  
    fclose(f);   
    out = fopen("E:\\tinyG_matrix.txt","wt");  
        for(t = 0;t < 14;t++)  
        {  
            for(s = 0;s < 14;s++)  
            {  
               fprintf(out,"%8.0lf",b[t][s]);  
            }  
        }  
  
    fclose(out);  
    getchar();  
	MGraph G;
	VertexType u,u1,w1,w2;
	scanf("%d,%d",&G.vexnum,&G.arcnum);
	printGraph(G);

	return 0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值