繁体   English   中英

从 c 中的标准输入 txt 文件读取矩阵

[英]reading matrix from a stdin txt file in c

我有一个任务,我有 4 个小时要做,但我无法继续。

有 3 个不同的文本文件,每个都有不同的矩阵,具有不同的数据类型和不同的行号和列号。

我们必须使用命令行输入 .txt 文件,例如:
./pe6 < input1.txt

输入

input1.txt:
 16,  11,  10,  16,  24,  40,  51,  61,
 12,  12,  14,  19,  26,  58,  60,  55,
 14,  13,  16,  24,  40,  57,  69,  56,
 14,  17,  22,  29,  51,  87,  80,  62,
 18,  22,  37,  56,  68, 109, 103,  77,
 24,  35,  55,  64,  81, 104, 113,  92,
 49,  64,  78,  87, 103, 121, 120, 101,
 72,  92,  95,  98, 112, 100, 103,  99
input3.txt:
19.996,-13.479,6.8042,-0.86667,-0.2625,-3.0167,-7.3708,-0.49583,-4.8,-4.1875,-0.91667,2.3917,2.2125,7.7958,-5.3708,0.36667
-13.479,24.496,-4.7208,2.3333,-10.321,-8.1833,5.4208,-7.7542,9.3333,6.1042,5.5167,0.84167,-2.6958,-13.146,-0.5125,-5.3
6.8042,-4.7208,27.863,3.6667,-3.7375,-7.5833,-0.9625,-8.7042,-4.8,-10.146,9.65,3.675,-1.9458,10.271,-2.2958,2.6333
-0.86667,2.3333,3.6667,21.867,-12.6,-2.2,3.2667,-3.4667,3.4,1.2667,-7,10.867,-4.8,0.13333,4.6,5.8667
-0.2625,-10.321,-3.7375,-12.6,20.929,7.6167,-5.8292,7.0292,-5.6,-2.9458,-5.75,-10.992,7.8542,0.9375,-0.029167,-0.16667
-3.0167,-8.1833,-7.5833,-2.2,7.6167,23.8,6.5833,2.0833,0.33333,0.78333,-2.0667,-9.5,-3.6167,5.45,-2.0833,3.3333
-7.3708,5.4208,-0.9625,3.2667,-5.8292,6.5833,22.996,1.9375,10,3.5125,-4.5833,-1.675,-4.5542,2.0292,-1.8708,5.3
-0.49583,-7.7542,-8.7042,-3.4667,7.0292,2.0833,1.9375,19.929,-1,-0.44583,-11.083,-0.125,3.1542,-3.0958,4.2708,0.5
-4.8,9.3333,-4.8,3.4,-5.6,0.33333,10,-1,19.733,4.7333,0.66667,3.8,1.4667,-4.8,0.2,1.2667
-4.1875,6.1042,-10.146,1.2667,-2.9458,0.78333,3.5125,-0.44583,4.7333,14.896,-4.1167,-0.44167,0.1625,-5.8542,4.3125,5.9667
-0.91667,5.5167,9.65,-7,-5.75,-2.0667,-4.5833,-11.083,0.66667,-4.1167,24.333,-3.1667,-7.85,3.75,-8.45,-5.7333
2.3917,0.84167,3.675,10.867,-10.992,-9.5,-1.675,-0.125,3.8,-0.44167,-3.1667,23.85,2.7583,-0.675,14.525,-1.2
2.2125,-2.6958,-1.9458,-4.8,7.8542,-3.6167,-4.5542,3.1542,1.4667,0.1625,-7.85,2.7583,15.562,-4.1875,8.9792,-3.3667
7.7958,-13.146,10.271,0.13333,0.9375,5.45,2.0292,-3.0958,-4.8,-5.8542,3.75,-0.675,-4.1875,17.462,-6.9708,5.5667
-5.3708,-0.5125,-2.2958,4.6,-0.029167,-2.0833,-1.8708,4.2708,0.2,4.3125,-8.45,14.525,8.9792,-6.9708,24.196,0.23333
0.36667,-5.3,2.6333,5.8667,-0.16667,3.3333,5.3,0.5,1.2667,5.9667,-5.7333,-1.2,-3.3667,5.5667,0.23333,16.267

首先 function 必须读取文件并返回矩阵、行号和列号。

首先,我只是尝试将所有值累积在一维数组中,以查看是否可以全部读取它们。 prog 正在工作,但我做了一些我不知道我做错了什么的事情(可能删除了一些我看不到的行),但它不再在第一个值之后读取。 我已经盯着我的代码很久了,在那之后我必须编写函数来做乘法、转置等,所以我可以使用一些帮助。

  3 int readMatrix(){ // read matrix from txt, return matrix, nrows, ncols
  4     double input[100];
  5     int i;
  6
  7     while( scanf("%lf", &input[i]) != EOF ){ // read untill EOF
  8
  9         scanf("%lf", &input[i]); // read the value
 10         printf("%.2lf \n", input[i]); // check if it's reading
 11
 12         i+=1; // implement loop var
 13
 14         if(i>64){break;} // try with 8x8 
 15     }
 16     return 0;
 17 }

Output:

$ ./pe6 < input1.txt
16.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00
0.00

另外我不知道如何检测具有相同 function 的不同矩阵的列和行变化,而且我不确定 memory 分配给未知行数和列数的矩阵,tbh 我找不到这么多在互联网上。 我对任何想法持开放态度。

首先,您每次阅读两次:一次在 while 条件下,第二次在循环体内。 第二个scanf可以省略。

然后在输入中,数字用逗号分隔,所以scanf格式应该是"%lf,"

最后你应该将你的i索引初始化为 0,你永远不会知道。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM