Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. class Student {
  9.  
  10. string name;
  11. string surname;
  12. int height;
  13. double weight;
  14. public:
  15. Student() {
  16. name = "ImieDomyslne";
  17. surname = "NazwiskoDomyslne";
  18. height = 170;
  19. weight = 85.5;
  20. }
  21.  
  22. Student(string _name, string _surname, int _height, double _weight) {
  23. name = _name;
  24. surname = _surname;
  25. height = _height;
  26. weight = _weight;
  27. }
  28.  
  29. string to_string(int x) {
  30. ostringstream ss;
  31. ss << x;
  32. return ss.str();;
  33. }
  34.  
  35. string toString() {
  36. return name + " " + surname + " " + Student::to_string(height) + " " + Student::to_string(weight);
  37. }
  38.  
  39. int compareTo(Student &student) {
  40. int val = height - student.height;
  41. if (val == 0)
  42. val = weight - student.weight;
  43.  
  44. return val;
  45. }
  46. };
  47.  
  48. int main()
  49. {
  50. Student tabOfStudents[35];
  51. string name;
  52. string surname;
  53. int height;
  54. double wage;
  55. cout << "Podaj dane studentów w kolejnosci : imie nazwisko wzrost waga" << endl;
  56. for (int i = 0; i < 35; i++)
  57. {
  58. cin >> name >> surname >> height >> wage;
  59. tabOfStudents[i] = Student(name, surname, height, wage);
  60. }
  61.  
  62. for (int i = 0; i < 35; i++)
  63. for (int j = i; j < 35; j++)
  64. if (tabOfStudents[i].compareTo(tabOfStudents[j]) < 0)
  65. swap(tabOfStudents[i], tabOfStudents[j]);
  66.  
  67. for (int i = 0; i < 3; i++)
  68. cout << tabOfStudents[i].toString() << endl;
  69.  
  70. cout << "Chcesz zapisać do pliku ? (TAK - tak, cokolwiek innego - nie" << endl;
  71. cin >> name;
  72. if (name == "Tak" || name == "TAK" || name == "tak")
  73. {
  74. fstream plik;
  75. plik.open("studenci.txt", std::ios::in | std::ios::out);
  76. if (plik.good() == true)
  77. {
  78. for (int i = 0; i < 35; i++)
  79. plik << tabOfStudents[i].toString() << endl;;
  80. plik.close();
  81. cout << "Zapisano dane do pliku" << endl;
  82. }
  83. else std::cout << "Dostep do pliku zostal zabroniony!" << std::endl;
  84. }
  85. else {
  86. cout << "Nie zapisano do pliku na prosbe uzytkownika" << endl;
  87. }
  88.  
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement