Advertisement
Solingen

Untitled

Dec 3rd, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <fstream>
  2. #include <ctime>
  3. #include "string.h"
  4. #include "array.h"
  5.  
  6. using namespace std;
  7.  
  8. bool Mymore(int a, int b)
  9. {
  10.     return a > b;
  11. }
  12.  
  13. bool Myless(int a, int b)
  14. {
  15.     return a < b;
  16. }
  17.  
  18. template <typename T>
  19. void sort(Array<T>& arr, bool asc)
  20. {
  21.     bool (*cmp)(T a, T b) = asc ? Myless : Mymore;
  22.  
  23.     for (int i = 0; i < arr.size() - 1; i++)
  24.     {
  25.         for (int j = i + 1; j < arr.size(); j++)
  26.         {
  27.             if (!cmp(arr[i], arr[j]))
  28.             {
  29.                 T temp = arr[i];
  30.                 arr[i] = arr[j];
  31.                 arr[j] = temp;
  32.             }
  33.         }
  34.     }
  35. }
  36.  
  37. template <typename T>
  38. Array<T> load_array(const String& filename)
  39. {
  40.     Array<T> arr;
  41.     ifstream in(filename.str);
  42.     if (!in)
  43.     {
  44.         throw runtime_error("File not found");
  45.     }
  46.  
  47.     T value;
  48.     while (in >> value)
  49.     {
  50.         arr.append(value);
  51.     }
  52.  
  53.     return arr;
  54. }
  55.  
  56. template <typename T>
  57. void print_array(const Array<T>& arr, ostream& out = cout)
  58. {
  59.     for (int i = 0; i < arr.size(); i++)
  60.     {
  61.         if (i > 0) out << " ";
  62.         out << arr[i];
  63.     }
  64.     out << endl;
  65. }
  66.  
  67. int main(int argc, char* argv[])
  68. {
  69.     if (argc < 2)
  70.     {
  71.         cerr << "Usage: sortf file [asc|desc] [-o outf] [-v]" << endl;
  72.         return 1;
  73.     }
  74.  
  75.     String filename = argv[1];
  76.     bool ascending = true;
  77.     bool verbose = false;
  78.     String outputFile;
  79.  
  80.     for (int i = 2; i < argc; i++)
  81.     {
  82.         if (String(argv[i]) == "desc")
  83.         {
  84.             ascending = false;
  85.         }
  86.         else if (String(argv[i]) == "-o" && i + 1 < argc)
  87.         {
  88.             outputFile = argv[++i];
  89.         }
  90.         else if (String(argv[i]) == "-v")
  91.         {
  92.             verbose = true;
  93.         }
  94.     }
  95.  
  96.     try
  97.     {
  98.         clock_t startTime = clock();
  99.         Array<int> arr = load_array<int>(filename);
  100.         clock_t loadTime = clock() - startTime;
  101.  
  102.         if (verbose)
  103.         {
  104.             cout << "Load time: " << static_cast<double>(loadTime) / CLOCKS_PER_SEC << " sec" << endl;
  105.         }
  106.  
  107.         startTime = clock();
  108.         sort(arr, ascending);
  109.         clock_t sortTime = clock() - startTime;
  110.  
  111.         if (verbose)
  112.         {
  113.             cout << "Sort time: " << static_cast<double>(sortTime) / CLOCKS_PER_SEC << " sec" << endl;
  114.             cout << "Memory used: " << arr.size() * sizeof(int) / 1024.0 << " KB" << endl;
  115.         }
  116.  
  117.         if (outputFile.length() > 0)
  118.         {
  119.             ofstream out(outputFile.str);
  120.             if (!out)
  121.             {
  122.                 throw runtime_error("Failed to open output file");
  123.             }
  124.             print_array(arr, out);
  125.         }
  126.         else
  127.         {
  128.             print_array(arr);
  129.         }
  130.     }
  131.     catch (const exception& ex)
  132.     {
  133.         cerr << "Error: " << ex.what() << endl;
  134.         return 1;
  135.     }
  136.  
  137.     return 0;
  138. }
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement