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

标签 c++

我决定打开一个新线程,即使问题已经部分解决但现在是另一个问题 (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 d 1 0.476 0.000 0.000 0.100 2 0.476 0.000 0.000 0.100 1 0.000 -0.000 -0.000 0.200 2 -0.000 -0.000 0.000 0.200

能量 2 # 权重为 1.00000000

原子 a b c d 1 0.476 0.000 0.000 0.300 2 0.476 0.000 0.000 0.300 1 0.000 -0.000 -0.000 0.400 2 -0.000 -0.000 0.000 0.400

能量 2 # 权重为 1.00000000

原子 a b c d 1 0.476 0.000 0.000 0.500 2 0.476 0.000 0.000 0.500 1 0.000 -0.000 -0.000 0.600 2 -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/

相关文章:

c++ - 文本编辑器 API。用于实验性 IDE 的 Scintilla。你用别的东西吗?

c++ - 使用指针将代码翻译成 Pascal 中的程序集 - Delphi

c++ - 没有调用 ‘myclass::myclass()’ 的匹配函数

c++ - 每个数字最大的数字最多只交换 K 次并且只交换相邻的数字

c++ - C++ Concepts TS 会启用多参数包吗?

c++ - 监控一个线程的状态

c++ - 为什么值放在 fortran 函数的数组中而不是在调用 c++ 函数中?

c++ - WINAPI 按钮背景

c++ - 如何在 Derived 的 Ctor 中使用模板基类中的 typedef?

c++ - MFC C++ - 在没有命令的情况下在 ProcessShellCommand() 上崩溃