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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <limits>
using ROW = std::vector<double>;
using SU = std::vector<ROW>;
using CAP = std::vector<double>;
bool doOneFile(size_t& T, size_t& P, SU& su, CAP& cap) {
const auto getA { [](std::istream& in, const std::string& att) {
if (std::string line; (in >> line >> std::ws) && line.substr(0, att.size()) != att) {
std::cout << "Invalid file format - missing " << att << '\n';
return false;
}
return !!in;
} };
const auto getVal { [getA](size_t& val, std::istream& in, const std::string& att) {
if (!getA(in, att))
return false;
return !!((in >> val) && in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'));
} };
const auto getAttr { [getA](std::istream& in, const std::string& att) {
if (!getA(in, att))
return false;
if (in.peek() == '[') {
std::string line;
in >> line;
}
return !!in;
} };
const std::string filename { "9.dat" };
std::fstream in { filename };
if (!in) {
std::cerr << "ERROR: could not open file '" << filename << "' for reading\n";
return false;
}
su.clear();
cap.clear();
if (!(getVal(T, in, "T=") && getVal(P, in, "P=") && getAttr(in, "cap=")))
return false;
for (double value {}; in >> value; cap.push_back(value));
in.clear();
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (!getAttr(in, "su="))
return false;
char t1 {}, t2 {};
do {
ROW row;
for (double e {}; in >> e; row.push_back(e));
su.push_back(row);
in.clear();
} while ((in >> t1 >> t2) && (t1 != ']' || t2 != ']'));
return true;
}
double DoMath(const std::vector<std::vector<double>>& su, const std::vector<double>& cap) {
//...some mathematical operations...
//.....................
for (size_t kk {}; kk < su.size(); ++kk) {
for (size_t t {}; t < su[kk].size(); ++t)
std::cout << su[kk][t] << " ";
std::cout << '\n';
}
return 0;
}
int main() {
SU su;
CAP cap;
size_t T {}, P {};
if (doOneFile(T, P, su, cap)) {
std::cout << "T= " << T << ", P= " << P << '\n';
for (const auto& c : cap)
std::cout << c << ' ';
std::cout << "\n\n";
DoMath(su, cap);
}
}
|