Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. #include <iomanip>
  7. #define vect vector
  8. using namespace std;
  9.  
  10. ifstream Data("MyData.txt");
  11.  
  12. int getDimension() {
  13. string header = "Dimension=";
  14.  
  15. string input;
  16. getline(Data, input);
  17.  
  18. string::size_type val;
  19.  
  20. val = input.find_first_not_of(header);
  21.  
  22. return atoi(input.substr(val, val + 1).c_str());
  23. }
  24.  
  25. vect<double> getVector() {
  26. string input;
  27. if (getline(Data, input)) {
  28.  
  29. string del = " \t";
  30. string::size_type head, tail;
  31. vect<double> reqVect;
  32. tail = 0;
  33.  
  34. while (1) {
  35. head = input.find_first_not_of(del, tail);
  36. tail = input.find_first_of(del, head);
  37. if (head == string::npos && tail == string::npos) break;
  38.  
  39. reqVect.push_back(atof(input.substr(head, tail - head).c_str()));
  40. }
  41. return reqVect;
  42. }
  43. }
  44.  
  45. int main() {
  46. vect < vect<double> > matrix;
  47. int dim = getDimension();
  48. cout << dim;
  49. while (1) {
  50. if (Data.is_open()) {
  51. if (Data.eof()) break;
  52. matrix.push_back(getVector());
  53. }
  54. }
  55.  
  56. //close "MyData.txt"
  57. Data.close();
  58.  
  59. //open write file
  60. ofstream output("MyOutput.txt");
  61.  
  62. //write to "MyOutput.txt"
  63. //write the dimension
  64. output << "Dimension=" << matrix.size() << endl;
  65.  
  66. //write the data
  67. for (int i = 0; i < dim; i++) {
  68. for (int j = 0; j < matrix.size(); j++) {
  69. output << fixed << setprecision(3) << matrix[j][i] << '\t';
  70. }
  71. output << endl;
  72. }
  73.  
  74. //close the write file
  75. output.close();
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement