Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // zad1_Hawryl.cpp : Defines the entry point for the console application.
- //
- /*
- Dany jest plik z ocenami studentów. Każdy student opisany jest za pomocą nazwiska i ocen z LICZBA_OCEN przedmiotów.
- Dane studenta są wczytywane z pliku. Napisz program, który obliczy dla każdego studenta średnią ocenę i wyświetli wykaz
- tych ocen posortowanych według nazwisk. Opracuj potrzebną do tego zadania klasę StudentInfo.
- Program zgłasza wyjątki, gdy plik nie istnieje lub ocena wykracza poza zakres (0-6).
- */
- #include "stdafx.h"
- #include "string"
- #include <iostream>
- #include <cstdlib>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <algorithm> // std::sort
- #define LICZBA_OCEN 5
- using namespace std;
- class Student{
- private:
- string nazwisko;
- int oceny[LICZBA_OCEN];
- public:
- void pobierzDaneStudenta(string studentLinia)
- {
- istringstream is(studentLinia);
- is >> nazwisko >> oceny[0] >> oceny[1] >> oceny[2] >> oceny[3] >> oceny[4] >> oceny[5];
- for(int i = 0; i < LICZBA_OCEN; i++)
- {
- if( !(oceny[i] > 0 && oceny[i] < 7) )
- {
- throw 0;
- }
- }
- }
- double obliczSrednia()
- {
- double srednia = 0;
- for(int i = 0; i < LICZBA_OCEN; i++)
- {
- srednia += oceny[i];
- }
- srednia/=LICZBA_OCEN; // srednia/= LICZBA_OCEN to to samo co srednia = srednia/ LICZBA_OCEN
- return srednia;
- }
- void pokazStudenta()
- {
- cout << "Nazwisko: " << nazwisko << " " << ", Oceny: ";
- for(int i = 0 ; i < LICZBA_OCEN; i++)
- {
- cout << oceny[i]<< " ";
- }
- }
- friend bool operator < (const Student s1, const Student s2)
- {
- return (s1.nazwisko < s2.nazwisko);
- }
- };
- bool mojaFunkcja(Student s1, Student s2)
- {
- return (s1<s2);
- }
- class studentInfo
- {
- private:
- vector<Student> studentVec;
- public:
- studentInfo(fstream& file){
- while(!file.eof())
- {
- string linia;
- getline(file, linia);
- Student sTmp;
- try
- {
- sTmp.pobierzDaneStudenta(linia);
- }
- catch(int e)
- {
- cerr << "Zly Zakres Oceny" << endl;
- }
- studentVec.push_back(sTmp);
- }
- sort(studentVec.begin(), studentVec.end(), mojaFunkcja);
- }
- void pokazStudentow()
- {
- if(!studentVec.empty())
- {
- for(int i = 0; i < studentVec.size(); i++)
- {
- studentVec[i].pokazStudenta();
- cout << ",srednia: " << studentVec[i].obliczSrednia() << endl;
- }
- }
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- string nazwaPliku = "text.txt";
- fstream file;
- file.open( nazwaPliku.c_str() , std::ios::in);
- try
- {
- if( !file.good() )
- {
- string wBrakPliku;
- throw 0;
- }
- }
- catch(int e)
- {
- cout<< "Brak pliku!!!" << endl;
- }
- studentInfo sInfo(file);
- sInfo.pokazStudentow();
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment