如何从具有交替数据的文件中读取输入并存储到两个数组

How to read input from a file with alternating data and store to two arrays

本文关键字:存储 输入 数组 两个 读取 文件 数据      更新时间:2023-10-16

第一位解析: 我正在尝试编写一个程序来计算 2 个数组的点积。数组的数据集将从文件中读入。我目前遇到的麻烦是文件配置为读入为"数据集数量"->"元素数">"第一个数组中的所有元素">"第二个数组中的所有元素"。我目前让程序读取适当数量的数据集,每个数组中适当数量的元素,问题是它是读取数组中元素的输入交替。例如,如果我的文件读取".1, .2, 3.0, 1.0"并且它要求 2 个元素,我希望数组 1 是 .1 和 .2,数组 2 是 3.0 和 1.0。当前它正在读入,因为数组 1 是 .1 和 3.0,数组 2 是 .2 和 1.0。下面是输入文件设置(如果格式显示奇数,则每行一条数据,因此其格式类似于列。有 5 行文件信息):

文件的第一个元素给出文件中数据集的数量(整数) 然后按顺序列出要处理的每个数据集,并包含以下内容: 每个数组中的元素数(整数) 后跟第一个数组中的所有元素(实值) 后跟第二个数组中的所有元素(实数值)

3 6 0.1 0.2 0.1 0.2 0.1 0.2 3.0 1.0 3.0 1.0 3.0 1.0 11 1.0 -.2 .5 .75 .9 -1.1 1.5 1.8 2.25 2.75 -3. 101.0 80.4 -20.5 -30.0 31.2 32.8 34.7 36.1 0.0 38.4 39.12 12 1.05 2.05 3.05 4.05 5.05 6.05 6.05 7.05 8.05 9.05 10.05 11.05 -11.05 -10.05 -9.05 -8.05 -7.05 -6.05 6.05 5.05 4.05 3.05 2.05 1.05

它当前错误地从文件中读取数组的输入。您可以看到我的输入文件在 3(对于数据集)和 6(对于元素)之后显示它有 0.1 0.2 0.1 0.2 0.1 0.2 3.0 1.0 3.0 1.0 1.0 3.0 1.0。它应该将数组 1 打印

为 .1 .2 .1 .2 .1 .2,将数组 2 打印为 3.0 1.0 3.0 1.0 3.0 1.0,但它当前将阵列 1 打印为 .1 .1 .1 3.0 3.0 3.0,将数组 2 打印为 .2 .2 .2 1.0 1.0 1.0编辑现在数据工作正常,我的 dotProduct 计算似乎不正常。我已经更新了代码以反映点积计算。它意味着通过诸如点积 = (x0 * y0) + (x1 * y1) + (x2 * y2) 等公式计算,一直到数组中的所有元素。我试图使用嵌套的 for 循环,但答案并没有像我手动计算的那样出现。我尝试以多种方式编辑嵌套的 for 循环,但仍然没有运气。

#include "pch.h"
#include <iostream> //need this by default for cin
#include <math.h> //includes math functions
#include <cmath> //includes basic math 
#include <cfloat> //includes floating point numbers
#include <iomanip> //includes setprecision for decimal places
#include <cstdlib> //needed for rand and srand functions
#include <ctime> //needed for time function used to seed generator
#include <climits> 
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream inputFile;
string fileinfo1, fileinfo2, fileinfo3, fileinfo4, fileinfo5;
string filename;
float array1[50];
float array2[50];
int count = 0;
int datasets;
int elements;
int a = 0;
int b = 0;
float x;
float y;
cout << "Please enter the input file name: ";
cin >> filename; //allows user to input filename
cout << endl;
cout << "The input file is " << filename << endl << endl;
inputFile.open(filename); //opens file
getline(inputFile, fileinfo1);
getline(inputFile, fileinfo2);
getline(inputFile, fileinfo3);
getline(inputFile, fileinfo4);
getline(inputFile, fileinfo5);
cout << fileinfo1 << endl;
cout << fileinfo2 << endl;
cout << fileinfo3 << endl;
cout << fileinfo4 << endl;
cout << fileinfo5 << endl << endl;
inputFile >> datasets;
while (count != datasets)
{
inputFile >> elements;
cout << "Elementtt" << "Array 1tt" << "Array 2" << endl;
for (int i = 0; i < elements; i++)
{
inputFile >> array1[i];
}
for (int i = 0; i < elements; i++)
{
inputFile >> array2[i];
}
for (int i = 0; i < elements; i++)
{
cout << setprecision(2) << fixed << i << "tt" << array1[i] << 
"tt" << array2[i] << endl;
}
for (int i = 0; i < elements; i++)
{
dotProduct = 0;
for (int j = 0; j < elements; j++)
{
dotProduct += (array1[i] * array2[i]);
}
}
cout << endl;
cout << "The dot product of the two arrays is " << dotProduct << "." << 
endl << endl;
count++;
}
}

程序应按顺序从文件中读取数组值,打印到数组 1、数组 2 等的屏幕值。

目前,您正在交替写入哪个数组。您需要做的是将几个元素写入第一个数组,然后写入第二个数组。然后在读入每个数据集后输出字符串。

此外,代码中的 a 和 b 不会更改,因此它始终写入第 0 个数组元素。

这样的事情应该有效:

for (int i = 0; i < elements; i++)
{
inputFile >> array0[i];
}
for (int i = 0; i < elements; i++)
{
inputFile >> array1[i];
}
for (int i = 0; i < elements; i++)
{
cout << setprecision(2) << fixed << i << 
"tt" << array0[i] << "tt" << array1[i] << endl;
}