Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- using namespace std;
- class Student {
- string name;
- string surname;
- int height;
- double weight;
- public:
- Student() {
- name = "ImieDomyslne";
- surname = "NazwiskoDomyslne";
- height = 170;
- weight = 85.5;
- }
- Student(string _name, string _surname, int _height, double _weight) {
- name = _name;
- surname = _surname;
- height = _height;
- weight = _weight;
- }
- string to_string(int x) {
- ostringstream ss;
- ss << x;
- return ss.str();;
- }
- string toString() {
- return name + " " + surname + " " + Student::to_string(height) + " " + Student::to_string(weight);
- }
- int compareTo(Student &student) {
- int val = height - student.height;
- if (val == 0)
- val = weight - student.weight;
- return val;
- }
- };
- int main()
- {
- Student tabOfStudents[35];
- string name;
- string surname;
- int height;
- double wage;
- cout << "Podaj dane studentów w kolejnosci : imie nazwisko wzrost waga" << endl;
- for (int i = 0; i < 35; i++)
- {
- cin >> name >> surname >> height >> wage;
- tabOfStudents[i] = Student(name, surname, height, wage);
- }
- for (int i = 0; i < 35; i++)
- for (int j = i; j < 35; j++)
- if (tabOfStudents[i].compareTo(tabOfStudents[j]) < 0)
- swap(tabOfStudents[i], tabOfStudents[j]);
- for (int i = 0; i < 35; i++)
- cout << tabOfStudents[i].toString() << endl;
- // Zapis do pliku bedzie dzialal tylko, gdy bedzie znajdowal sie
- // w nim pusty plik tekstowy o nazwie studenci.txt
- cout << "Chcesz zapisać do pliku ? (TAK - tak, cokolwiek innego - nie" << endl;
- cin >> name;
- if (name == "Tak" || name == "TAK" || name == "tak")
- {
- fstream plik;
- plik.open("studenci.txt", std::ios::in | std::ios::out);
- if (plik.good() == true)
- {
- for (int i = 0; i < 35; i++)
- plik << tabOfStudents[i].toString() << endl;;
- plik.close();
- cout << "Zapisano dane do pliku" << endl;
- }
- else std::cout << "Dostep do pliku zostal zabroniony!" << std::endl;
- }
- else {
- cout << "Nie zapisano do pliku na prosbe uzytkownika" << endl;
- }
- cin.ignore();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement