Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. struct Operation {
  9.  
  10. public:
  11.     int amount;
  12.     string title;
  13.     string from;
  14.     string to;
  15.     int ID;
  16.  
  17. };
  18.  
  19. void heap(int arr[][], int size, int index)
  20. {
  21.     int largest = index;
  22.     int left = 2 * index + 1;
  23.     int right = 2 * index + 2;
  24.  
  25.     if (left<size && arr[left][1]>arr[largest][1])
  26.         largest = left;
  27.  
  28.     if (right<size && arr[right][1]>arr[largest][1])
  29.         largest = right;
  30.  
  31.     if (largest != index)
  32.     {
  33.         swap(arr[index][1], arr[largest][1]);
  34.         swap(arr[index][2], arr[largest][2]);
  35.         heap(arr, size, largest);
  36.     }
  37. }
  38.  
  39. void heapsort2(int arr[][], int size)
  40. {
  41.  
  42.     for (int i = size / 2 - 1; i >= 0; i--)
  43.     {
  44.         heap(arr, size, i);
  45.     }
  46.  
  47.     for (int j = size - 1; j >= 0; j--)
  48.     {
  49.         swap(arr[0][1], arr[j][1]);
  50.         swap(arr[0][2], arr[j][2]);
  51.         heap(arr, j, 0);
  52.     }
  53. }
  54.  
  55.  
  56. int main() {
  57.  
  58.     Operation dane[10000];
  59.     Operation enddane[1000];
  60.     int amount;
  61.     int id = 0;
  62.     string title, from, to,temp;
  63.  
  64.     ifstream file("data3.txt");
  65.     if (file.is_open())
  66.     {
  67.         while (file.good())
  68.         {
  69.             getline(file, temp, ',');
  70.             getline(file, title, ',');
  71.             getline(file, from, ',');
  72.             getline(file, to);
  73.            
  74.             stringstream qwe(temp);
  75.             qwe >> amount;
  76.  
  77.             dane[id].amount = amount;
  78.             dane[id].title = title;
  79.             dane[id].from = from;
  80.             dane[id].to = to;
  81.             dane[id].ID = id;
  82.  
  83.             id++;
  84.  
  85.         }
  86.         file.close();
  87.     }
  88.     else cout << "Unable to open file";
  89.  
  90.  
  91.     int tab[10000][2];
  92.  
  93.     for (int i = 0; i < 10000; i++)
  94.     {
  95.         tab[i][1] = dane[i].amount;
  96.         tab[i][2] = i;
  97.     }
  98.  
  99.     heapsort2(tab, 10000);
  100.  
  101.     for (int j = 0; j < 1000; j++)
  102.     {
  103.         enddane[j] = dane[tab[j][2]];
  104.     }
  105.  
  106.     system("pause");
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement