二维阵列的一维读入法
#include <math.h>#include <stdio.h>#include <stdlib.h>#define MAX 50int main() { FILE *f = fopen("new.txt", "r"); if (f == NULL) { printf("no file"); return -1; }
基本的读入判断,利用fopen将位置存入f,并判断档案是否存在
int count=0, top = -1, arr[MAX] = {0}; while (EOF != fscanf(f, "%d", &arr[++top])) { } fclose(f);
持续读档,将读到的资料放入阵列中,直到没有资料传入为止
count = sqrt(top); printf("top=%d\n", top); printf("now=%d\n", count);
此时top为矩阵元素总数量,利用math中的sqrt函式取top的开根号,可得边长(限同边长矩阵)
for (int i = 0; i < count; i++) { for (int j = 0; j < count; j++) { printf("%d ", arr[i * count + j]); } printf("\n"); }}
利用一维的方式印出二维矩阵
其他
待补