Advertisement
Guest User

Untitled

a guest
Mar 13th, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <stdlib.h> // atof
  6. #include <algorithm> //sort
  7. using namespace std;
  8. class element
  9. {
  10.     public :
  11. string linia[5];
  12. double get_x()
  13. {
  14. string tmp;
  15. size_t first, second;
  16. tmp=linia[1];
  17. first=tmp.find(",");
  18. tmp=tmp.substr(first+1,tmp.size()-first-1);
  19. second=tmp.find(",");
  20. return atof(linia[1].substr(first+1,second-first).c_str());
  21. }
  22. };
  23. fstream f;
  24. class param
  25. {
  26.     public :
  27.         string path;
  28.         bool rev;
  29.         param(int argc, char *argv[])
  30.         {
  31.             path = argv[0];
  32.             rev = argv[1];
  33.             if(argv[1] == "rev_no")
  34.             {
  35.                 rev = false;
  36.             }
  37.             else rev = true;
  38.     }
  39.        
  40.     };
  41.            
  42.  
  43.  
  44. string get_file_path()
  45. {
  46. cout<<"Podaj sciezke do pliku"<<endl;
  47. string path;
  48. cin>>path;
  49. return path;
  50. };
  51. bool sort_fnc (element i,element j, param wstecz)
  52. {
  53. if(wstecz.rev && i.get_x()<j.get_x())
  54. return true;
  55. else
  56. return false;
  57. };
  58. int main(int argc, char *argv[])
  59. {
  60.     param obiekt2(int argc, char *argv []);
  61. cout << "Hello world!" << endl;
  62. string path;
  63. path=get_file_path();
  64. ifstream file;
  65. file.open( path.c_str() );
  66. if ( !file.is_open() )
  67. {
  68. std::cout<<"\n Could not open file. Exiting...\n";
  69. return 1;
  70. }
  71. vector <element> tablica_danych;
  72. element dana;
  73. string line;
  74. for( bool readed = std::getline( file, line ); readed; readed = std::getline( file, line ) )
  75. {
  76. if (line.substr(0,5)=="CAERO")
  77. {
  78. for(size_t j=0; j<5; j++)
  79. {
  80. dana.linia[j]=line;
  81. if (j<5)
  82. getline(file,line);
  83. }
  84. }
  85. tablica_danych.push_back(dana);
  86. }
  87. file.close();
  88. sort(tablica_danych.begin(),tablica_danych.end(),sort_fnc);
  89. path=path+"sorted";
  90. ofstream file2;
  91. file2.open( path.c_str() );
  92. if ( !file2.is_open() )
  93. {
  94. std::cout<<"\n Could not save file. Exiting...\n";
  95. return 2;
  96. }
  97. string tmp;
  98. for (size_t j=0; j<tablica_danych.size(); ++j)
  99. {
  100. for(size_t k=0; k<5; k++)
  101. {
  102. tmp=tablica_danych[j].linia[k];
  103. file2<<tmp<<endl;
  104. }
  105. file2<<endl;
  106. }
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement