您的位置:首页 > 其它

邻接矩阵输出程序(问题)

2014-03-02 16:38 155 查看
#ifndef MAIN_HEAD
#define MAIN_HEAD
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <time.h>
#include <math.h>
#include <algorithm>
#include <iostream>

#define N 25
#define INF 999

using namespace std;

int f_read_file1(int topo

);

#endif

上面是头文件

#include "main.h"
#define TURE 1
#define FALSE 0
#define ARRIVING 1
#define LEAVING 0
#define NOEVENT -1
FILE *fp_1=fopen("test.txt","w");
int main()
{
int i,j;

int topo

;
int link_sum=0;
////读取邻接表文件到数组
if (f_read_file1(topo)==0)
{
printf("文件打开失败!\n");
return 0;
}
////显示邻接数组
printf("\n/*****************原始网络的带权临接矩阵如下:***************************/\n");
for (i=0;i<N;i++)
{
for (j=0;j<N;j++)
{
if (topo[i][j]!=INF) link_sum++;
printf("%d ",topo[i][j]);
}
printf("\n");
}
link_sum=link_sum/2;
printf("链路数总和为:%d \n",link_sum);
}
int f_read_file1(int topo

)
{
char ch_read;
int i,j;
char str[]={"0"};
char ch_qz[]={"0"},ch_qz_ini[]={""};///ch_qz用于存储某个节点权值,ch_qz_ini用于初使化
FILE *fp=NULL;
//if((fp=fopen("e:\\topology.txt", "r"))==NULL) ///打开存储权值的文件
if((fp=fopen("topology1.txt", "r"))==NULL) ///打开存储权值的文件
{
//printf("文件打开失败!\n");
return 0;
}
//printf("文件打开成功!\n");
////读文件,从文件中每次读取一个字符
ch_read=fgetc(fp);
ch_qz[0]=ch_read;
i=0;////数组行下标
j=0;///数组列下标
while(ch_read!=EOF)
{
//if (ch==10) printf("\n放行回车");
//if (ch==32) printf("\n这是空格");

ch_read=fgetc(fp);
str[0]=ch_read;
if ((ch_read==32) || (ch_read==10))
{
topo[i][j]=atoi(ch_qz);
j++;
if (ch_read==10) ///新行开始
{
i++;
j=0;

}
ch_qz[0]=ch_qz_ini[0];
// ch_qz[1]=ch_qz_ini[1];
// ch_qz[2]=ch_qz_ini[2];
// ch_qz[3]=ch_qz_ini[3];
}
else
{
strcat(ch_qz,str); ///合并
}
}
fclose(fp);
return 1;
}

用visual C++ 2008运行时出现如下问题:



用VC6.0运行时,拓扑打印有误(最后一个数字打印错误)



原始矩阵为

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐