Guest User

Untitled

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