Guest User

Untitled

a guest
Mar 14th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. #include <fstream>
  5. #include <vector>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. int iColumnToCompare;
  11. int iColumnSize[3];
  12.  
  13. struct SDataRecord
  14. {
  15. string Column[3];
  16. };
  17.  
  18. int compare(const void *a, const void *b){
  19. const string first =((SDataRecord *)a)->Column[iColumnToCompare];
  20. const string second =((SDataRecord *)b)->Column[iColumnToCompare];
  21.  
  22. return (first.compare(second));
  23. }
  24.  
  25. void print_one_row(SDataRecord a, int b){
  26. for(int i=0; i<b;i++){
  27. cout<<left<<setw(iColumnSize[i])<<a.Column[i]<<"\t";
  28. }
  29. cout<<endl;
  30. }
  31.  
  32. void print_one_row_to_f(SDataRecord a, int b, fstream* f){
  33. for(int i=0; i<b;i++){
  34. *f<<left<<setw(iColumnSize[i])<<a.Column[i]<<"\t";
  35. }
  36. *f<<endl;
  37. }
  38.  
  39. void display_help(){
  40. cout<<"/? - wyswietla ta pomoc"<<endl;
  41. cout<<"/i[:][patch] - ustawia sciezke pliku wejsciowego na patch"<<endl;
  42. cout<<"/o[:][patch] - ustawia sciezke pliku wyjsiowego na patch"<<endl;
  43. cout<<"/s[:][-][columname] - sortuje po kolumnie o nazwie columnname, jezeli podano '-' to sortowanie odbywa sie w odwrotnej kolejnosci"<<endl;
  44. cout<<"/q - ustawia program w trybie cichym"<<endl;
  45. }
  46.  
  47. string remove_semicolon(string b){
  48. int p=2;
  49. if(b[2]==':') p++;
  50. return b.substr(p);
  51. }
  52.  
  53. int main(int argc, char* argv[])
  54. {
  55. //parametry
  56. string sInputFilePatch;
  57. bool bReverseSort = false;
  58. bool bSilenMode = false;
  59. bool bOutputToFile=false;
  60. string sOutputPatch, sSortBy;
  61.  
  62. //elementy niezbedne do dzialania
  63. vector<SDataRecord> vBase;
  64.  
  65. fstream FInputFile, FOutputFile;
  66. string sInputFileLine;
  67.  
  68. for(int i=1; i<argc; i++){
  69. if(argv[i][0]=='/'){
  70. switch(toupper(argv[i][1]))
  71. {
  72. case 'I': //input filename
  73. {
  74. sInputFilePatch=argv[i];
  75. sInputFilePatch=remove_semicolon(sInputFilePatch);
  76. }
  77. break;
  78. case 'Q': //silent
  79. {
  80. bSilenMode=true;
  81. }
  82. break;
  83. case 'S':
  84. {
  85. sSortBy=argv[i];
  86. sSortBy=remove_semicolon(sSortBy);
  87. if(sSortBy[0]=='-'){
  88. bReverseSort=true;
  89. sSortBy=sSortBy.substr(1);
  90. }
  91. }
  92. break;
  93. case 'O':
  94. {
  95. sOutputPatch=argv[i];
  96. sOutputPatch=remove_semicolon(sOutputPatch);
  97. bOutputToFile=true;
  98. }
  99. break;
  100. case '?':
  101. {
  102. display_help();
  103. cin.get();
  104. return 0;
  105. }
  106. break;
  107. default:
  108. {
  109. cout<<"Nieznany parametr: "<<argv[i][1];
  110. cin.get();
  111. return 0;
  112. }
  113. }} else {
  114. cout<<"Nieznany parametr! Uzyj: /?";
  115. cin.get();
  116. return 0;
  117. }
  118.  
  119. }
  120.  
  121. if(sInputFilePatch == sOutputPatch){
  122. cout<< "Plik Wejsciowy i wyjsciowy jest taki sam!"<<endl;
  123. cin.get();
  124. return 0;
  125. }
  126.  
  127. if(bOutputToFile){
  128. FOutputFile.open(sOutputPatch.c_str(), fstream::out);
  129. if(!FOutputFile.good()) {
  130. cout<<"Nie dam rady otworzyc pliku wyjsciowego";
  131. cin.get();
  132. return 0;
  133. }
  134. }
  135.  
  136. FInputFile.open(sInputFilePatch.c_str(), fstream::in);
  137. if(FInputFile.good()){
  138. while(!FInputFile.eof()){
  139. getline(FInputFile, sInputFileLine);
  140. size_t begin=0, end=0;
  141. sInputFileLine.append("\t");
  142. SDataRecord SRecord;
  143. for(int i=0; i<3; i++){
  144. end = sInputFileLine.find('\t', begin);
  145. if(end ==0) break;
  146. SRecord.Column[i] = sInputFileLine.substr(begin, end-begin);
  147. if(iColumnSize[i]<SRecord.Column[i].size())iColumnSize[i]=SRecord.Column[i].size();
  148. if(!(end >= (sInputFileLine.size()-1))){
  149. begin=end+1;
  150. } else {
  151. break;
  152. }
  153. }
  154. vBase.push_back(SRecord);
  155. }
  156. }else {
  157. cout << "Nie mozna otworzyc pliku wejsciowego" <<endl;
  158. cin.get();
  159. return 0;
  160. }
  161.  
  162. FInputFile.close();
  163.  
  164. //Ustawia wg ktorej kolumny mamy posortowac
  165. for(int i=0; i<3; i++){
  166. if (vBase[0].Column[i] == sSortBy) iColumnToCompare=i;
  167. }
  168.  
  169. //sortowanie
  170. qsort(&vBase[1], (int)vBase.size()-1, sizeof(SDataRecord), compare);
  171.  
  172. //wypisywanie
  173. bSilenMode=(bSilenMode&&bOutputToFile);
  174.  
  175. if(!bSilenMode)print_one_row(vBase[0],3);
  176. if(bOutputToFile)print_one_row_to_f(vBase[0],3,&FOutputFile);
  177. int offset=0;
  178. for(int i=1; i<vBase.size(); i++){
  179. offset=i;
  180. if(bReverseSort) offset=vBase.size()-i;
  181. if(!bSilenMode)print_one_row(vBase[offset], 3);
  182. if(bOutputToFile)print_one_row_to_f(vBase[offset],3,&FOutputFile);
  183. }
  184. if(bOutputToFile)FOutputFile.close();
  185. cin.get();
  186. return 0;
  187.  
  188. system("pause");
  189. return 0;
  190. }
Add Comment
Please, Sign In to add comment