Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <sstream>
- #include <string>
- #include <iomanip>
- #define vect vector
- using namespace std;
- ifstream Data("MyData.txt");
- int getDimension() {
- string header = "Dimension=";
- string input;
- getline(Data, input);
- string::size_type val;
- val = input.find_first_not_of(header);
- return atoi(input.substr(val, val + 1).c_str());
- }
- vect<double> getVector() {
- string input;
- if (getline(Data, input)) {
- string del = " \t";
- string::size_type head, tail;
- vect<double> reqVect;
- tail = 0;
- while (1) {
- head = input.find_first_not_of(del, tail);
- tail = input.find_first_of(del, head);
- if (head == string::npos && tail == string::npos) break;
- reqVect.push_back(atof(input.substr(head, tail - head).c_str()));
- }
- return reqVect;
- }
- }
- int main() {
- vect < vect<double> > matrix;
- int dim = getDimension();
- cout << dim;
- while (1) {
- if (Data.is_open()) {
- if (Data.eof()) break;
- matrix.push_back(getVector());
- }
- }
- //close "MyData.txt"
- Data.close();
- //open write file
- ofstream output("MyOutput.txt");
- //write to "MyOutput.txt"
- //write the dimension
- output << "Dimension=" << matrix.size() << endl;
- //write the data
- for (int i = 0; i < dim; i++) {
- for (int j = 0; j < matrix.size(); j++) {
- output << fixed << setprecision(3) << matrix[j][i] << '\t';
- }
- output << endl;
- }
- //close the write file
- output.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement