Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <stdlib.h> // atof
- #include <algorithm> //sort
- using namespace std;
- class element
- {
- public :
- string linia[5];
- double get_x()
- {
- string tmp;
- size_t first, second;
- tmp=linia[1];
- first=tmp.find(",");
- tmp=tmp.substr(first+1,tmp.size()-first-1);
- second=tmp.find(",");
- return atof(linia[1].substr(first+1,second-first).c_str());
- }
- };
- fstream f;
- class param
- {
- public :
- string path;
- bool rev;
- param(int argc, char *argv[])
- {
- path = argv[0];
- rev = argv[1];
- if(argv[1] == "rev_no")
- {
- rev = false;
- }
- else rev = true;
- }
- };
- string get_file_path()
- {
- cout<<"Podaj sciezke do pliku"<<endl;
- string path;
- cin>>path;
- return path;
- };
- bool sort_fnc (element i,element j, param wstecz)
- {
- if(wstecz.rev && i.get_x()<j.get_x())
- return true;
- else
- return false;
- };
- int main(int argc, char *argv[])
- {
- param obiekt2(int argc, char *argv []);
- cout << "Hello world!" << endl;
- string path;
- path=get_file_path();
- ifstream file;
- file.open( path.c_str() );
- if ( !file.is_open() )
- {
- std::cout<<"\n Could not open file. Exiting...\n";
- return 1;
- }
- vector <element> tablica_danych;
- element dana;
- string line;
- for( bool readed = std::getline( file, line ); readed; readed = std::getline( file, line ) )
- {
- if (line.substr(0,5)=="CAERO")
- {
- for(size_t j=0; j<5; j++)
- {
- dana.linia[j]=line;
- if (j<5)
- getline(file,line);
- }
- }
- tablica_danych.push_back(dana);
- }
- file.close();
- sort(tablica_danych.begin(),tablica_danych.end(),sort_fnc);
- path=path+"sorted";
- ofstream file2;
- file2.open( path.c_str() );
- if ( !file2.is_open() )
- {
- std::cout<<"\n Could not save file. Exiting...\n";
- return 2;
- }
- string tmp;
- for (size_t j=0; j<tablica_danych.size(); ++j)
- {
- for(size_t k=0; k<5; k++)
- {
- tmp=tablica_danych[j].linia[k];
- file2<<tmp<<endl;
- }
- file2<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement