Advertisement
MariusPure

sorting by two or more attributes

Jun 7th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <string>
  7. #include <iomanip>
  8. using namespace std;
  9.  
  10. struct salota{
  11. string pav;
  12. double a = 0;
  13. double b = 0;
  14. double r = 0;
  15. double h2o = 0;
  16. int nr = 0;
  17. };
  18. salota m[222];
  19.  
  20. int n = 0;
  21. void skaitymas(int &n, salota m[]);
  22. void rikiavimas(int n, salota m[]);
  23. void spausdinimas(int n, salota m[]);
  24.  
  25. int main()
  26. {
  27. skaitymas(n, m);
  28. rikiavimas(n, m);
  29. spausdinimas(n, m);
  30. }
  31.  
  32. void skaitymas(int &n, salota m[])
  33. {
  34. char x[15];
  35. ifstream d("duom.txt");
  36.  
  37. d >> n;
  38. d.ignore();
  39. for (int i = 0; i < n; i++)
  40. {
  41. m[i].nr = i;
  42. d.get(x, 15);
  43. m[i].pav = x;
  44. d >> m[i].a >> m[i].b >> m[i].r;
  45. d.ignore();
  46. m[i].h2o = 100-(m[i].a + m[i].b + m[i].r);
  47. }
  48. d.ignore();
  49. d.close();
  50. }
  51. void rikiavimas(int n, salota m[])
  52. {
  53. for (int i = 0; i < n; i++)
  54. {
  55. for (int j = 0; j < n; j++)
  56. {
  57. if ((m[i].b < m[j].b) || ((m[i].b == m[j].b) && (m[i].h2o > m[j].h2o)))
  58. {
  59. swap(m[i].b, m[j].b);
  60. swap(m[i].h2o, m[j].h2o);
  61. swap(m[i].pav, m[j].pav);
  62. swap(m[i].r, m[j].r);
  63. swap(m[i].a, m[j].a);
  64. }
  65. }
  66. }
  67.  
  68. }
  69. void spausdinimas(int n, salota m[])
  70. {
  71. for (int i = 0; i < n; i++)
  72. {
  73.  
  74. cout << fixed << setprecision(1) << m[i].pav << " " << m[i].a << " " << " " << m[i].b << " " << m[i].r << endl;
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement