Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 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.  
  51.  
  52. Student tabOfStudents[35];
  53. string name;
  54. string surname;
  55. int height;
  56. double wage;
  57. cout << "Podaj dane studentów w kolejnosci : imie nazwisko wzrost waga" << endl;
  58. for (int i = 0; i < 35; i++)
  59. {
  60. cin >> name >> surname >> height >> wage;
  61. tabOfStudents[i] = Student(name, surname, height, wage);
  62. }
  63.  
  64. for (int i = 0; i < 35; i++)
  65. for (int j = i; j < 35; j++)
  66. if (tabOfStudents[i].compareTo(tabOfStudents[j]) < 0)
  67. swap(tabOfStudents[i], tabOfStudents[j]);
  68.  
  69. for (int i = 0; i < 35; i++)
  70. cout << tabOfStudents[i].toString() << endl;
  71.  
  72. // Zapis do pliku bedzie dzialal tylko, gdy bedzie znajdowal sie
  73. // w nim pusty plik tekstowy o nazwie studenci.txt
  74. cout << "Chcesz zapisać do pliku ? (TAK - tak, cokolwiek innego - nie" << endl;
  75. cin >> name;
  76. if (name == "Tak" || name == "TAK" || name == "tak")
  77. {
  78. fstream plik;
  79. plik.open("studenci.txt", std::ios::in | std::ios::out);
  80. if (plik.good() == true)
  81. {
  82. for (int i = 0; i < 35; i++)
  83. plik << tabOfStudents[i].toString() << endl;;
  84. plik.close();
  85. cout << "Zapisano dane do pliku" << endl;
  86. }
  87. else std::cout << "Dostep do pliku zostal zabroniony!" << std::endl;
  88. }
  89. else {
  90. cout << "Nie zapisano do pliku na prosbe uzytkownika" << endl;
  91. }
  92. cin.ignore();
  93.  
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement