1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
#include <vector>
// if the dimensions are constant, then you could use std::array instead of std::vector.
using Matrix = std::vector<std::vector<float>>;
void addRow(Matrix& M, const int rowNum, const int id, const float type, const float x, const float y,const float z) {
M[rowNum][0] = id;
M[rowNum][1] = type;
M[rowNum][2] = x;
M[rowNum][3] = y;
M[rowNum][4] = z;
}
int main() {
string line;
float x0, y0, z0, x1, y1, z1, type;
int NUMBER_line=0, id=0,NUMBER_line1=0,NUMBER_line3=0;
ofstream secondoutput; // the output text file
secondoutput.open ("secondoutput.txt"); // the output text file
ifstream myfile("1.lammpstrj");
string namefile;
if (myfile.is_open()) {
for (int lineno = 0; getline(myfile, line) && lineno < 7; lineno++) {
if (lineno == 2) myfile >> NUMBER_line;
}
cout << " NUMBER_line: " << NUMBER_line << endl;
Matrix Input0(NUMBER_line, std::vector<float>(5));
for (float linenoq = 0; getline(myfile, line) && linenoq < NUMBER_line; linenoq++) {
myfile >> id >> type >> x0 >> y0 >> z0;
addRow(Input0, linenoq, id, type, x0, y0, z0);
}
for (int i=0; i <3; i++){
for (int lineno = 0; getline(myfile, line) && lineno < 7; lineno++) {
if (lineno == 2) myfile >> NUMBER_line1;
}
Matrix Input1(NUMBER_line1, std::vector<float>(5));
for (int linenoq = 0; getline(myfile, line) && linenoq < NUMBER_line1; linenoq++) {
myfile >> id >> type >> x1 >> y1 >> z1;
addRow(Input1, linenoq, id, type, x1, y1, z1);
}
Matrix Output(NUMBER_line, std::vector<float>(5));
for (size_t row = 0; row < Output.size(); ++row) {
for (size_t col = 0; col < Output[0].size(); ++col) {
if (col < 2) {
Output[row][col] = Input0[row][col];
}
else {
Output[row][col] = Input1[row][col] - Input0[row][col] ;
}
}
}
std::for_each(Output.cbegin(), Output.cend(),
[](auto const& row) {
std::for_each(row.cbegin(), row.cend(),
[](const float value) {
cout << value << " ";
secondoutput << value << " "; // the output text file
});
cout << endl;
secondoutput << "\n" ; // the output text file
});
}
secondoutput.close();
}
else cout << "Unable to open file";
return 0;
}
|