如何用fscanf_s调取txt中二维数组

qq_36510645 2017-10-18 10:54:50
我想用fscanf_s函数,调用txt里面的二维数组,但是不知道怎么用,求教
//读权矩阵
int** readWeight(int &n,char* filename)
{
FILE *fp;
fp = fopen(filename,"r");
if(fp == NULL)
{
printf("文件%s打开时出错\n",filename);
exit(1);
}

int i,j,**w;
fscanf(fp,"%d",&n);
w = (int**)calloc(n,sizeof(int*));
for(i = 0;i < n;i++)
{
w[i] = (int*)calloc(n,sizeof(int));
}

for(i = 0;i < n;i++)
{
for(j = 0;j < n;j++)
{
fscanf(fp,"%d",&w[i][j]);
}
}
fclose(fp);
return w;
}
不知道怎么改,可能是因为我的权重文件写的不对,权重信息如下
0,2,7,2,5
2,0,3,5,1
7,3,0,4,6
2,5,4,0,3
5,1,6,3,0
...全文
1150 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_36510645 2017-10-18
  • 打赏
  • 举报
回复
试了一下,是没问题的,但是还是报错,不知道您了不了解tsp问题,我把代码附上,结果就是跑不出来,距离矩阵本来是 0 2 7 2 5 2 0 3 5 1 7 3 0 4 6 2 5 4 0 3 5 1 6 3 0 但是出现了中断,所以就把它改为 65535 2 7 2 5 2 65535 3 5 1 7 3 65535 4 6 2 5 4 65535 3 5 1 6 3 65535 改成这个的目的是为了让每个城市不经过自身 源代码附上,望大神指点: #include"stdafx.h" #include<stdio.h> #include<stdlib.h> #include<math.h> #include<windows.h> typedef struct tf* cnode; struct tf { int currentNode;//当前的节点 int set; //当前的状态集合,用二进制表示 int pathLength;//当前的路径值 cnode last;//记录上一个节点,以保存上一个的信息 cnode next;//记录下一个节点 }; struct tsp { int count;//当前的城市的个数 cnode* ltf;//记录struct tf数组 }; void output1(int *a, int n) { int i; for (i = 0; i < n; i++) printf("%d ", a[i]); printf("\n"); } void output2(int **a, int n) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%d ", a[i][j]); } printf("\n"); } } //自定义的幂函数<用于之后的集合的表示> int mypow(int x, int y) { return (int)pow((double)x, (double)y); } //判断元素i是否在set集合中 int inSet(int set, int i) { if ((set&mypow(2, i - 1)) > 0) return 1; else return 0; } //将元素i插入集合set中 void insertSet(int &set, int i) { int p = mypow(2, i - 1); set = set | p; } //删除set中的i void deleteSet(int &set, int i) { set = ~(mypow(2, i - 1))&set; } //读权矩阵 int** readWeight(int &n, char* filename) { FILE *fp; errno_t err; if ((err = fopen_s(&fp, filename, "r")) != 0) { printf("文件%s打开时出错\n", filename); exit(1); } int i, j, **w; fscanf_s(fp, "%d", &n); w = (int**)calloc(n, sizeof(int*)); for (i = 0; i < n; i++) { w[i] = (int*)calloc(n, sizeof(int)); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { fscanf_s(fp, "%d", &w[i][j]); } } fclose(fp); return w; } //返回组合数C(n,r)值 int ZuHeShu(int n, int r) { int i, c, k, *a; a = (int*)calloc(n, sizeof(int)); for (i = 0; i < n; i++) { a[i] = i + 1; } c = 0; do { i = -1; for (k = 0; k < r; k++) if (a[k] < n - r + k + 1) i = k; if (i != -1) { a[i]++; for (k = i + 1; k < r; k++) a[k] = a[k - 1] + 1; c++; } } while (i != -1); free(a); return c + 1; } //指定组合c(n,r)中所有可能组合,返回其转化后的01格式的int数组 int* ZuHeArray(int *a0, int n, int r, int sn) { int i, c, k, *set, *a; c = 0; set = (int*)calloc(sn, sizeof(int)); for (i = 0; i < sn; i++) { set[i] = 0; } a = (int*)calloc(n, sizeof(int)); for (i = 0; i < n; i++) { a[i] = i + 1; } int t; for (i = 0; i < r; i++) { t = a[i] - 1; insertSet(set[c], a0[t]); } do { i = -1; for (k = 0; k < r; k++) if (a[k] < n - r + k + 1) i = k; if (i != -1) { a[i]++; for (k = i + 1; k < r; k++) a[k] = a[k - 1] + 1; c++; for (k = 0; k < r; k++) { t = a[k] - 1; insertSet(set[c], a0[t]); } } } while (i != -1); free(a); return set; } //设置最后一个集合 int setLastSet(int n) { int i, set; set = 0; for (i = 0; i < n; i++) { set = set | mypow(2, i); } return set; } //获得第row行的set中所有元素集合 ,总元素的个数为n<非set集合中的元素个数> int* getSetNum(int set, int row, int n) { int i, j, *a; a = (int*)calloc(row, sizeof(int)); j = 0; for (i = 1; i < n; i++) { if (inSet(set, i) == 1) { a[j] = i; j++; } if (j == row) { break; } } return a; } //将集合a转为二进制的数 int setNumSet(int *a, int n) { int set, i, t; set = 0; for (i = 0; i < n; i++) { t = a[i] - 1; set = set | mypow(2, t); } return set; } //将set[n]中的第i个点删除 int* dropOneNode(int*set, int i, int n) { int * s, j, k; s = (int*)calloc(n - 1, sizeof(int)); k = 0; for (j = 0; j < n; j++) { if (j != i) { s[k] = set[j]; k++; } } return s; } //查找上一节点 cnode findNode(int row, cnode p, struct tsp circle) { cnode phead; phead = (cnode)malloc(sizeof(*phead));//必须分配空间 phead = circle.ltf[row]; cnode q; q = (cnode)malloc(sizeof(*q)); q = phead; while (phead != NULL) { q = phead; if ((q->currentNode == p->currentNode) && (p->set == q->set)) { break; } phead = phead->next; } return q; } //设置上一节点 void setLastNode(cnode p, int row, struct tsp circle, int **w) { int start = p->currentNode; cnode* clast; int i, n; n = circle.count; int *setNum; //将节点p中的set转为相应的int型数组 setNum = getSetNum(p->set, row, n); clast = (cnode*)calloc(row, sizeof(cnode)); //设置所有关联的上一级的接点 for (i = 0; i < row; i++) { int *dSet = dropOneNode(setNum, i, row); int sDNum; if (row == 1) { sDNum = 0; } else { sDNum = setNumSet(dSet, row - 1); } clast[i] = (cnode)malloc(sizeof(*clast[i]));//必须有的语句 clast[i]->currentNode = setNum[i]; clast[i]->last = NULL; clast[i]->next = NULL; clast[i]->set = sDNum; clast[i] = findNode(row - 1, clast[i], circle);//之后测试这个函数 } int minlength, minlabel; cnode q; q = clast[0]; minlength = w[start][q->currentNode] + q->pathLength; minlabel = 0; for (i = 1; i < row; i++) { q = clast[i]; int len = w[start][q->currentNode] + q->pathLength; if (minlength > len) { minlength = len; minlabel = i; } } p->last = clast[minlabel]; p->pathLength = minlength; } //设置集合,即已知起点,列举所有可能状态集合 int* setSet(int row, int start, int n, int &sn) { int *set, i, j, *a; sn = ZuHeShu(n - 2, row); //集合a中存放V中所用可能取得的点的下标 a = (int*)calloc(n - 2, sizeof(int)); i = 0; for (j = 1; j < n; j++) { if (j != start) { a[i] = j; i++; } } set = ZuHeArray(a, n - 2, row, sn); free(a); return set; } void tsp(char *filename) { int **w, n; //从文件中读取路径权值矩阵信息 w = readWeight(n, filename); //数据结构:使用的是链接结构 //每一层对应一行,每一层中所有的节点依次排列 struct tsp circle; circle.count = n; circle.ltf = (cnode*)calloc(n - 1, sizeof(cnode)); int i, j; for (i = 0; i < n - 1; i++) { circle.ltf[i] = NULL; } cnode p, q; q = (cnode)malloc(sizeof(*q)); //添加第一行 for (i = 1; i < n; i++) { p = (cnode)malloc(sizeof(*p)); p->currentNode = i; p->last = NULL; p->next = NULL; p->pathLength = w[i][0]; p->set = 0; if (circle.ltf[0] == NULL) { circle.ltf[0] = p; } else { q->next = p; } q = p; } int *sSet, sn, ts; //添加其余几行 for (i = 1; i < n - 1; i++) { for (j = 1; j < n; j++) { sSet = setSet(i, j, n, sn); for (ts = 0; ts < sn; ts++) { p = (cnode)malloc(sizeof(*p)); p->currentNode = j; p->set = sSet[ts]; setLastNode(p, i, circle, w);//是i不是j if (circle.ltf[i] == NULL) { circle.ltf[i] = p; } else q->next = p; q = p; free(sSet); } } } //求解最后的f0(v0,{v1,v2,...vn-1}); cnode last; last = (cnode)malloc(sizeof(*last)); last->currentNode = 0; last->set = setLastSet(n - 1); setLastNode(last, n - 1, circle, w); int *path; path = (int*)calloc(n, sizeof(int)); i = 0; p = last; while (p != NULL) { cnode p1 = p; path[i] = p->currentNode + 1; i++; p = p->last; } printf("最短路的路径为: "); for (i = 0; i < n; i++) { printf("%d -> ", path[i]); } printf("%d \n", 1); printf("最短路径的长度为:%d\n", last->pathLength); } void main() { tsp("da.txt"); system("pause"); }
ooolinux 2017-10-18
  • 打赏
  • 举报
回复
权重文件中的,逗号全部替换成空格试试。

1,222

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧