我真的很困惑如何解决这个问题。我有一个像这样的文本文件:
STEP
10
NUMBER line
2
cc
aa
ab
ac
ad
1 1 81 91 101
2 1 82 92 102
STEP
20
NUMBER line
2
cc
aa
ab
ac
ad
1 1 51 61 71
2 1 52 62 72
带有这段代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
double x0, y0, z0, x1, y1, z1, type ;
int NUMBER_line,id ;
ifstream myfile ("1.txt");
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;
for (int linenoq = 0; getline (myfile,line) && linenoq < NUMBER_line; linenoq++){
myfile>>id>>type>>x0>>y0>>z0;
cout<<id<<" "<<type<<" "<<x0<<" "<<y0<<" "<<z0 <<endl;
}
for (int lineno = 0; getline (myfile,line) && lineno < 7; lineno++)
{
}
for (int linenoq = 0; getline (myfile,line) && linenoq < NUMBER_line; linenoq++){
myfile>>id>>type>>x1>>y1>>z1;
cout<<id<<" "<<type<<" "<<x1<<" "<<y1<<" "<<z1 <<endl;
}
}
else cout << "Unable to open file";
return 0;
}
我可以显示具有编号的行,我的输出是:
NUMBER_line: 2
1 1 81 91 101
2 1 82 92 102
NUMBER_line: 2
1 1 51 61 71
2 1 52 62 72
但是我不知道如何在81和51或82和52等之间进行减法运算。我想我必须将第四个循环放在第二个循环中的某个位置,但是我不知道如何。另一件事是关于第三循环,我不需要。但是我必须把它放进去,因为如果我不把它放在那里,第四个循环的起始行就会出错。我的意思是第四个循环从第12行而不是第21行开始。
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
// if the dimensions are constant, then you could use std::array instead of std::vector.
using Matrix = std::vector<std::vector<int>>;
void addRow(Matrix& M, const int rowNum, const int id, const int type, const int x, const int y, const int 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;
int x0, y0, z0, x1, y1, z1, type;
int NUMBER_line, id;
ifstream myfile("1.txt");
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(2, std::vector<int>(5));
for (int linenoq = 0; getline(myfile, line) && linenoq < NUMBER_line; linenoq++) {
myfile >> id >> type >> x0 >> y0 >> z0;
addRow(Input0, linenoq, id, type, x0, y0, z0);
cout << id << " " << type << " " << x0 << " " << y0 << " " << z0 << endl;
}
for (int lineno = 0; getline(myfile, line) && lineno < 7; lineno++) {
}
Matrix Input1(2, std::vector<int>(5));
for (int linenoq = 0; getline(myfile, line) && linenoq < NUMBER_line; linenoq++) {
myfile >> id >> type >> x1 >> y1 >> z1;
addRow(Input1, linenoq, id, type, x1, y1, z1);
cout << id << " " << type << " " << x1 << " " << y1 << " " << z1 << endl;
}
Matrix Output(2, std::vector<int>(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] = Input0[row][col] - Input1[row][col];
}
}
}
std::for_each(Output.cbegin(), Output.cend(),
[](auto const& row) {
std::for_each(row.cbegin(), row.cend(),
[](const int value) { cout << value << " "; });
cout << endl;
});
}
else cout << "Unable to open file";
system("pause");
return 0;
}