gpt4 book ai didi

c++ - 使用 C++ 从文件中读取数据并在命中特定行(字符串)后对特定列求和

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:41 25 4
gpt4 key购买 nike

我决定打开一个新线程,即使问题已经部分解决但现在是另一个问题 (Read data from file into 2d array and sum over specific arrays using C++)。不过,这是我想阅读的内容:


计算点数:200 # 原子数:4

点 1:0.00000000 0.00000000 0.00000000 权重 = 0.00500000

能量 1 # 权重为 1.00000000

原子 a b c d1 0.476 0.000 0.000 0.1002 0.476 0.000 0.000 0.1001 0.000 -0.000 -0.000 0.2002 -0.000 -0.000 0.000 0.200

能量 2 # 权重为 1.00000000

原子 a b c d1 0.476 0.000 0.000 0.3002 0.476 0.000 0.000 0.3001 0.000 -0.000 -0.000 0.4002 -0.000 -0.000 0.000 0.400

能量 2 # 权重为 1.00000000

原子 a b c d1 0.476 0.000 0.000 0.5002 0.476 0.000 0.000 0.5001 0.000 -0.000 -0.000 0.6002 -0.000 -0.000 0.000 0.600

....

....

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
int rows = 0;
int columns = 0;
string line;
int firstNumber = 0;
vector<vector<double> > values;
vector<vector<double> > results;
vector<double> rowstotal;
ofstream File;
ifstream in("data.txt");
File.open("Output.txt",ios::app);
File.setf(ios::fixed);
File.setf(ios::showpoint);
File.precision(3);

if(in.fail())
{
cerr << "File can not be opened" << endl;
return -1;
}

File << "\n" << endl;

// Save every double
while(in.good())
{

bool begin_tag = false;
while (getline(in,line))
{
if(line.find("Energy 2 #") != std::string::npos ) {
begin_tag = true;
continue;
}
else if (line == "Energy 1 #")
{
begin_tag = false;

}

istringstream stream(line);
vector<double> tmp;
double x;

while (stream >> x)
tmp.push_back(x);

if (tmp.size() > 0)
values.push_back(tmp);

}
}


columns = values[0].size();
for (unsigned i = 1; i < values.size(); ++i)
{
if (values[i].size() != columns)
{
cerr << "Row with different column number" << endl;
return -1;
}
}

for (unsigned i = 0; i < values.size(); ++i)
{
// If number with 1.0 is encountered, add it to the row
if (values[i][0] == 1.0)
results.push_back(values[i]);

// If number with 2.0 is encountered, add it also to the row
if (values[i][0] == 2.0)
{
for (unsigned j = 0; j < values[i].size(); ++j)
results.back()[j] += values[i][j];
}
}



rows = results.size();

File << "Number of rows # " << rows << endl;
File << "Number of columns # " << columns << endl;
File << " " << endl;

for(int i=0; i < rows; i++)
{
for(int j=4; j < columns; j++)
{
File << results[i][j] << " " << " " << endl;
}
}


for(int i=0; i < rows; i++)
{
rowstotal.push_back(0.0);
for (int j=1; j < columns; j++)
{
rowstotal[i] += results[i][j];
}
}

File.close();
in.close();
return 0;
}

输出是:

行数 # 6列数 # 5

0.200
0.400
0.600
0.800
1.000
1.200

如上所述,我想要实现的是仅对 block “Energy 2 #”求和,而忽略以“Energy 1#”开头的 block 。所以代码应该给出值:

0.600
0.800
1.000
1.200

我试图实现一个 bool 值来完成它,但不知何故我遗漏了一些东西。如果有人能够给我提示或告诉我如何解决它,我将非常感激。

感谢您的帮助和富有成效的提示!

祝你好运,戴夫斯

最佳答案

我找到了解决问题的方法。我意识到我正在设置“begin_tag = false”,但从来没有要求过。再次感谢阅读这篇文章并思考它的任何人!

关于c++ - 使用 C++ 从文件中读取数据并在命中特定行(字符串)后对特定列求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40482626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com