Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[C++] Wywołanie metody zwracającej średnią z vectora.

Ostatnio zmodyfikowano 2014-05-25 14:39
Autor Wiadomość
AngrySkarpeta
Temat założony przez niniejszego użytkownika
[C++] Wywołanie metody zwracającej średnią z vectora.
» 2014-05-25 11:16:15
Witam,
mam problem z metodą mającą zwracać średnią średnich ocen studentów zapisanych w wektorze (z zapisem i odczytem z pliku też ale jeden problem na raz ;p - chyba że wie ktoś jak zrobić żeby działało i chciałby się podzielić ;-) ), metodę jako taką napisałem (być może nawet dobrze), ale próbuje ją wywołać na całą masę sposobów, a ona ani myśli ruszyć ; / ( na nowym obiekcie, na pierwszym elemencie wektora, jak z zmienna statyczna x.x ) obecny zapis może być debilny, ale to ostatnie czego próbowałem. Będę wdzięczny za pomoc ;-)
C/C++
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;

class Adres
{
private:
    string Miasto;
    string Ulica;
    string Numer;
public:
    Adres( string M, string U, string N )
    {
        Miasto = M;
        Ulica = U;
        Numer = N;
    };
    string get_Miasto()
    {
        return this->Miasto;
    }
    string get_Ulica()
    {
        return this->Ulica;
    }
    string get_Numer()
    {
        return this->Numer;
    }
    void set_Miasto( string M )
    {
        this->Miasto = M;
    }
    void set_Ulica( string U )
    {
        this->Ulica = U;
    }
    void set_Numer( string N )
    {
        this->Numer = N;
    }
};
class Osoba
{
private:
    string Imie;
    string Nazwisko;
    int Wiek;
    Adres * Adres_Zamieszkania;
    Adres * Adres_Korespondencyjny;
    Adres * Adres_Dodatkowy;
protected:
    Osoba( string I, string N, int W )
    {
        Imie = I;
        Nazwisko = N;
        Wiek = W;
        Adres_Zamieszkania = NULL;
        Adres_Korespondencyjny = NULL;
        Adres_Dodatkowy = NULL;
        ++ile_O;
    };
public:
    static int ile_O;
    ~Osoba()
    {
        --ile_O;
    }
    string get_Imie()
    {
        return this->Imie;
    }
    string get_Nazwisko()
    {
        return this->Nazwisko;
    }
    int get_Wiek()
    {
        return this->Wiek;
    }
    string get_Adres_Zamieszkania()
    {
        return Adres_Zamieszkania->get_Miasto() + " " + Adres_Zamieszkania->get_Ulica() + " " + Adres_Zamieszkania->get_Numer();
    }
    string get_Adres_Korespondencyjny()
    {
        return Adres_Korespondencyjny->get_Miasto() + " " + Adres_Korespondencyjny->get_Ulica() + " " + Adres_Korespondencyjny->get_Numer();
    }
    string get_Adres_Dodatkowy()
    {
        return Adres_Dodatkowy->get_Miasto() + " " + Adres_Dodatkowy->get_Ulica() + " " + Adres_Dodatkowy->get_Numer();
    }
    void set_Imie( string I )
    {
        this->Imie = I;
    }
    void set_Nazwisko( string N )
    {
        this->Nazwisko = N;
    }
    void set_Wiek( int W )
    {
        this->Wiek = W;
    }
    void set_Adres_Zamieszkania( string M, string U, string N )
    {
        if( Adres_Zamieszkania == NULL )
        {
            Adres_Zamieszkania = new Adres( M, U, N );
        }
        else
        {
            this->Adres_Zamieszkania->set_Miasto( M );
            this->Adres_Zamieszkania->set_Ulica( U );
            this->Adres_Zamieszkania->set_Numer( N );
        }
    }
    void set_Adres_Korespondencyjny( string M, string U, string N )
    {
        if( Adres_Korespondencyjny == NULL )
        {
            Adres_Korespondencyjny = new Adres( M, U, N );
        }
        else
        {
            this->Adres_Korespondencyjny->set_Miasto( M );
            this->Adres_Korespondencyjny->set_Ulica( U );
            this->Adres_Korespondencyjny->set_Numer( N );
        }
    }
    void set_Adres_Dodatkowy( string M, string U, string N )
    {
        if( Adres_Dodatkowy == NULL )
        {
            Adres_Dodatkowy = new Adres( M, U, N );
        }
        else
        {
            this->Adres_Dodatkowy->set_Miasto( M );
            this->Adres_Dodatkowy->set_Ulica( U );
            this->Adres_Dodatkowy->set_Numer( N );
        }
    }
    static int ile_Osob()
    {
        return ile_O;
    }
   
};
class Pracownik
    : public Osoba
{
private:
    string Kod_Pracownika;
    double Pensja;
public:
    static int ile_P;
    Pracownik( string I, string N, int W, string K, double P )
        : Osoba( I, N, W )
    {
        Kod_Pracownika = K;
        Pensja = P;
        ++ile_P;
    };
    ~Pracownik()
    {
        --ile_P;
    }
    string get_Kod_Pracownika()
    {
        return this->Kod_Pracownika;
    }
    double get_Pensja()
    {
        return this->Pensja;
    }
    void set_Kod_Pracownika( string K )
    {
        this->Kod_Pracownika = K;
    }
    void set_Pensja( double P )
    {
        this->Pensja = P;
    }
    static int ile_Pracownikow()
    {
        return ile_P;
    }
};
class Student
    : public Osoba
{
private:
    string Numer_Albumu;
    double Srednia;
public:
    static int ile_S;
    Student( string I, string N, int W, string NA, double S )
        : Osoba( I, N, W )
    {
        Numer_Albumu = NA;
        Srednia = S;
        ++ile_S;
    };
    ~Student()
    {
        --ile_S;
    }
    string get_Numer_Albumu()
    {
        return this->Numer_Albumu;
    }
    double get_Srednia()
    {
        return this->Srednia;
    }
    void set_Numer_Albumu( string NA )
    {
        this->Numer_Albumu = NA;
    }
    void set_Srednia( double S )
    {
        this->Srednia = S;
    }
    static int ile_Studentow()
    {
        return ile_S;
    }
    void srednia_Studenci();
};
vector < Student *> Vec_Student;
vector < Pracownik *> Vec_Pracownik;
void dodaj_pracownika();
void wyswietl_pracownikow();
void wykasuj_Adres_Pracownika();
void dodaj_studenta();
void wykasuj_Adres_Studenta();
void wyswietl_studentow();
void wyswietl_adresy_pracownika();
void wyswietl_adresy_studenta();
void zapisz_pracownikow();
int Pracownik::ile_P = 0;
int Student::ile_S = 0;
int Osoba::ile_O = 0;
int main()
{
    for(;; )
    {
       
        cout << "Menu" << endl
        << "1.Dodaj Pracownika" << endl
        << "2.Dodaj Studenta" << endl
        << "3.Wyswietl Pracownikow" << endl
        << "4.Wyswietl Studentow" << endl
        << "5.Wykasuj adres Pracownika" << endl
        << "6.Wykasuj adres Studenta" << endl
        << "7.Pokaz Adres Pracownika" << endl
        << "8.Pokaz Adres Studenta" << endl
        << "9.Ile pracownikow?" << endl
        << "10.Ile studentow?" << endl
        << "11.Ile osob?" << endl
        << "12.Srednia pensja" << endl
        << "13.Srednia ocen" << endl
        << "14.Zapis do pliku" << endl
        << "15.Odczyt z pliku" << endl
        << "k.Wyjscie z programu" << endl;
        string buf;
        getline( cin, buf );
        cout << endl;
        if( buf == "1" )
        {
            dodaj_pracownika();
            cin.ignore();
            system( "PAUSE" );
        }
        else if( buf == "2" )
        {
            dodaj_studenta();
            cin.ignore();
            system( "PAUSE" );
        }
        else if( buf == "3" )
        {
            wyswietl_pracownikow();
            cin.ignore();
            system( "PAUSE" );
        }
        else if( buf == "4" )
        {
            wyswietl_studentow();
            cin.ignore();
            system( "PAUSE" );
        }
        else if( buf == "5" )
        {
            wykasuj_Adres_Pracownika();
            cin.ignore();
            system( "PAUSE" );
        }
        else if( buf == "6" )
        {
            wykasuj_Adres_Studenta();
            cin.ignore();
            system( "PAUSE" );
        }
        else if( buf == "7" )
        {
            wyswietl_adresy_pracownika();
            cin.ignore();
            system( "PAUSE" );
        }
        else if( buf == "8" )
        {
            wyswietl_adresy_studenta();
            cin.ignore();
            system( "PAUSE" );
        }
        else if( buf == "9" )
        {
            cout << "Pracownikow w bazie : " << Pracownik::ile_Pracownikow() << endl;
            system( "PAUSE" );
        }
        else if( buf == "10" )
        {
            cout << "Studentow w bazie : " << Student::ile_Studentow() << endl;
            system( "PAUSE" );
        }
        else if( buf == "11" )
        {
            cout << "Osob w bazie : " << Osoba::ile_Osob() << endl;
            system( "PAUSE" );
        }
        else if( buf == "13" )
        {
            Student::srednia_Studenci();
            system( "PAUSE" );
        }
        else if( buf == "14" )
        {
            zapisz_pracownikow();
            system( "PAUSE" );
        }
        else if( buf == "k" )
        {
            cout << "Nastapi wyjscie z programu" << endl;
            break; //wyjscie
        }
        else {
            cout << "Wybrano nieprawidlowa opcje" << endl;
        }
    }
}
void dodaj_pracownika()
{
    string I, N, K, M, U;
    int W;
    double P;
    char pytanie;
    cout << "Podaj Imie" << endl;
    cin >> I;
    cout << "Podaj Nazwisko" << endl;
    cin >> N;
    cout << "Podaj Wiek" << endl;
    cin >> W;
    cout << "Podaj Kod Pracownika" << endl;
    cin >> K;
    cout << "Podaj pensje" << endl;
    cin >> P;
    Vec_Pracownik.push_back( new Pracownik( I, N, W, K, P ) );
    cout << "Czy chcesz dodac Adres Zamieszkania?(t-Tak)" << endl;
    cin >> pytanie;
    if( pytanie == 't' )
    {
        cout << "Miasto zamieszkania" << endl;
        cin >> M;
        cout << "Ulica zamieszkania" << endl;
        cin >> U;
        cout << "Numer zamieszkania" << endl;
        cin >> N;
        Vec_Pracownik.back()->set_Adres_Zamieszkania( M, U, N );
    }
    cout << "Czy chcesz dodac Adres Korespondencyjny(t-Tak)" << endl;
    cin >> pytanie;
    if( pytanie == 't' )
    {
        cout << "Miasto zamieszkania" << endl;
        cin >> M;
        cout << "Ulica zamieszkania" << endl;
        cin >> U;
        cout << "Numer zamieszkania" << endl;
        cin >> N;
        Vec_Pracownik.back()->set_Adres_Korespondencyjny( M, U, N );
    }
    cout << "Czy chcesz dodac Adres Dodatkowy(t-Tak)" << endl;
    cin >> pytanie;
    if( pytanie == 't' )
    {
        cout << "Miasto zamieszkania" << endl;
        cin >> M;
        cout << "Ulica zamieszkania" << endl;
        cin >> U;
        cout << "Numer zamieszkania" << endl;
        cin >> N;
        Vec_Pracownik.back()->set_Adres_Dodatkowy( M, U, N );
    }
}
void wyswietl_pracownikow()
{
    cout << "Rekord / Imie / Nazwisko / Wiek / Kod Pracownika / Pensja" << endl;
    for( int i = 0; i < Vec_Pracownik.size(); i++ )
    {
        cout << i << " / " << Vec_Pracownik[ i ]->get_Imie() << " / " << Vec_Pracownik[ i ]->get_Nazwisko() << " / " << Vec_Pracownik[ i ]->get_Wiek() << " / " << Vec_Pracownik[ i ]->get_Kod_Pracownika() << " / " << Vec_Pracownik[ i ]->get_Pensja() << endl;
    }
}
void wykasuj_Adres_Pracownika()
{
    int i;
    char pytanie;
    cout << "Ktorego Pracownika adres chcesz wykasowac?" << endl;
    cin >> i;
    cout << "Ktory Adres chcesz usunac? z-zamieszkania,k-korespondencyjny,d-dodatkowy" << endl;
    cin >> pytanie;
    if( pytanie == 'z' )
         Vec_Pracownik[ i ]->set_Adres_Zamieszkania( NULL, NULL, NULL );
    else if( pytanie == 'k' )
         Vec_Pracownik[ i ]->set_Adres_Korespondencyjny( NULL, NULL, NULL );
    else if( pytanie == 'd' )
         Vec_Pracownik[ i ]->set_Adres_Dodatkowy( NULL, NULL, NULL );
    else
         cout << "Wybrales zla opcje" << endl;
   
}
void dodaj_studenta()
{
    string I, N, K, M, U;
    int W;
    double P;
    char pytanie;
    cout << "Podaj Imie" << endl;
    cin >> I;
    cout << "Podaj Nazwisko" << endl;
    cin >> N;
    cout << "Podaj Wiek" << endl;
    cin >> W;
    cout << "Podaj Numer Albumu" << endl;
    cin >> K;
    cout << "Podaj Srednia" << endl;
    cin >> P;
    Vec_Student.push_back( new Student( I, N, W, K, P ) );
    cout << "Czy chcesz dodac Adres Zamieszkania?(t-Tak)" << endl;
    cin >> pytanie;
    if( pytanie == 't' )
    {
        cout << "Miasto zamieszkania" << endl;
        cin >> M;
        cout << "Ulica zamieszkania" << endl;
        cin >> U;
        cout << "Numer zamieszkania" << endl;
        cin >> N;
        Vec_Student.back()->set_Adres_Zamieszkania( M, U, N );
    }
    cout << "Czy chcesz dodac Adres Korespondencyjny(t-Tak)" << endl;
    cin >> pytanie;
    if( pytanie == 't' )
    {
        cout << "Miasto zamieszkania" << endl;
        cin >> M;
        cout << "Ulica zamieszkania" << endl;
        cin >> U;
        cout << "Numer zamieszkania" << endl;
        cin >> N;
        Vec_Student.back()->set_Adres_Korespondencyjny( M, U, N );
    }
    cout << "Czy chcesz dodac Adres Dodatkowy(t-Tak)" << endl;
    cin >> pytanie;
    if( pytanie == 't' )
    {
        cout << "Miasto zamieszkania" << endl;
        cin >> M;
        cout << "Ulica zamieszkania" << endl;
        cin >> U;
        cout << "Numer zamieszkania" << endl;
        cin >> N;
        Vec_Student.back()->set_Adres_Dodatkowy( M, U, N );
    }
}
void wyswietl_studentow()
{
    cout << "Rekord / Imie / Nazwisko / Wiek / Numer Albumu / Srednia" << endl;
    for( int i = 0; i < Vec_Student.size(); i++ )
    {
        cout << i << " / " << Vec_Student[ i ]->get_Imie() << " / " << Vec_Student[ i ]->get_Nazwisko() << " / " << Vec_Student[ i ]->get_Wiek() << " / " << Vec_Student[ i ]->get_Numer_Albumu() << " / " << Vec_Student[ i ]->get_Srednia() << endl;
    }
}
void wykasuj_Adres_Studenta()
{
    int i;
    char pytanie;
    cout << "Ktorego Pracownika adres chcesz wykasowac?" << endl;
    cin >> i;
    cout << "Ktory Adres chcesz usunac? z-zamieszkania,k-korespondencyjny,d-dodatkowy" << endl;
    cin >> pytanie;
    if( pytanie == 'z' )
         Vec_Student[ i ]->set_Adres_Zamieszkania( NULL, NULL, NULL );
    else if( pytanie == 'k' )
         Vec_Student[ i ]->set_Adres_Korespondencyjny( NULL, NULL, NULL );
    else if( pytanie == 'd' )
         Vec_Student[ i ]->set_Adres_Dodatkowy( NULL, NULL, NULL );
    else
         cout << "Wybrales zla opcje" << endl;
   
}
void wyswietl_adresy_pracownika()
{
    int i;
    cout << "Ktorego Pracownika adresy Cie interesuja?" << endl;
    cin >> i;
    cout << "Imie : " << Vec_Pracownik[ i ]->get_Imie() << endl;
    cout << "Nazwisko : " << Vec_Pracownik[ i ]->get_Nazwisko() << endl;
    cout << "Wiek : " << Vec_Pracownik[ i ]->get_Wiek() << endl;
    if( Vec_Pracownik[ i ]->get_Adres_Zamieszkania().empty() == false )
         cout << "Adres Zamieszkania : " << Vec_Pracownik[ i ]->get_Adres_Zamieszkania() << endl;
   
    if( Vec_Pracownik[ i ]->get_Adres_Korespondencyjny().empty() == false )
         cout << "Adres Korespondencyjny : " << Vec_Pracownik[ i ]->get_Adres_Korespondencyjny() << endl;
   
    if( Vec_Pracownik[ i ]->get_Adres_Dodatkowy().empty() == false )
         cout << "Adres Dodatkowy : " << Vec_Pracownik[ i ]->get_Adres_Dodatkowy() << endl;
   
}
void wyswietl_adresy_studenta()
{
    int i;
    cout << "Ktorego Studenta adresy Cie interesuja?" << endl;
    cin >> i;
    cout << "Imie : " << Vec_Student[ i ]->get_Imie() << endl;
    cout << "Nazwisko : " << Vec_Student[ i ]->get_Nazwisko() << endl;
    cout << "Wiek : " << Vec_Student[ i ]->get_Wiek() << endl;
    cout << "Adres Zamieszkania : " << Vec_Student[ i ]->get_Adres_Zamieszkania() << endl;
    cout << "Adres Korespondencyjny : " << Vec_Student[ i ]->get_Adres_Korespondencyjny() << endl;
    cout << "Adres Dodatkowy : " << Vec_Student[ i ]->get_Adres_Dodatkowy() << endl;
}
void zapisz_pracownikow()
{
    ofstream zapis;
    zapis.open( "PRACOWNICY.txt" );
    for( int i = 0; i < Pracownik::ile_P; i++ )
    {
        zapis << Vec_Pracownik[ i ]->get_Imie() << " " << Vec_Pracownik[ i ]->get_Nazwisko() << " " << Vec_Pracownik[ i ]->get_Wiek() << " " << Vec_Pracownik[ i ]->get_Kod_Pracownika() << " " << Vec_Pracownik[ i ]->get_Pensja() << " ";
        if( Vec_Pracownik[ i ]->get_Adres_Zamieszkania().empty() == false )
        {
            zapis << Vec_Pracownik[ i ]->get_Adres_Zamieszkania() << " ";
        }
        else
        {
            zapis << "NULL" << " " << "NULL" << " " << "NULL" << " ";
        }
        if( Vec_Pracownik[ i ]->get_Adres_Korespondencyjny().empty() == false )
        {
            zapis << Vec_Pracownik[ i ]->get_Adres_Korespondencyjny() << " ";
        }
        else
        {
            zapis << "NULL" << " " << "NULL" << " " << "NULL" << " ";
        }
        if( Vec_Pracownik[ i ]->get_Adres_Dodatkowy().empty() == false )
        {
            zapis << Vec_Pracownik[ i ]->get_Adres_Dodatkowy() << " ";
        }
        else
        {
            zapis << "NULL" << " " << "NULL" << " " << "NULL" << " ";
        }
        zapis << endl;
    }
}
void srednia_Studenci()
{
    float srednia_ocen = 0;
    for( int i = 0; i <= Student::ile_S; i++ )
    {
        srednia_ocen = srednia_ocen + Vec_Student[ i ]->get_Srednia();
    }
    srednia_ocen = srednia_ocen / Student::ile_S;
    cout << "Srednia ocen wszystkich studentow wynosi: " << srednia_ocen << endl;
}
P-110814
MrPoxipol
» 2014-05-25 11:31:38
Ale co nie działa?
P-110815
AngrySkarpeta
Temat założony przez niniejszego użytkownika
» 2014-05-25 11:41:54
Program się obecnie nie kompiluje nawet, nie działa wywołanie funkcji w 334 linijce dokładnie w tym miejscu
C/C++
else if( buf == "13" )
{
    Student::srednia_Studenci();
    system( "PAUSE" );
}
P-110817
MrPoxipol
» 2014-05-25 11:54:25
Zamień na srednia_Studenci(), albo utwórz statyczną metodę klasy Studenci.
P-110819
MrPoxipol
» 2014-05-25 11:55:10
Zamień na srednia_Studenci(), albo utwórz statyczną metodę Student::srednia_Studenci.
P-110820
AngrySkarpeta
Temat założony przez niniejszego użytkownika
» 2014-05-25 12:09:19
Jeżeli wstawię samo "srednia_Studenci();" wywala mi że jest nie zadeklarowane (bo w sumie nie jest, bo jest składową klasy- dobrze kombinuje?) A co do statycznej, to próbowałem dopisać przed deklaracje static ( static void srednia_Studenta() ) Ale wtedy trzeba coś dopisać przed samym mainem prawda? tak jak tu
int Student::ile_S = 0;
 Czy void może być static? czy zmienić na float, i zwracać returnem? Nie jestem jeszcze zbyt obeznany z obiektowością ;c
P-110821
Moorfox
» 2014-05-25 12:46:35
C/C++
void Student::srednia_Studenci() // <-- tu byl blad
{
    float srednia_ocen = 0;
    for( int i = 0; i <= Student::ile_S; i++ )
    {
        srednia_ocen = srednia_ocen + Vec_Student[ i ]->get_Srednia();
    }
    srednia_ocen = srednia_ocen / Student::ile_S;
    cout << "Srednia ocen wszystkich studentow wynosi: " << srednia_ocen << endl;
}
i jesli wywolujesz ta metode to na rzecz obiektu lub uczyn ja static

C/C++
for( int i = 0; i <= Student::ile_S; i++ )
{
    srednia_ocen = srednia_ocen + Vec_Student[ i ]->get_Srednia();
}
 To mozna zastapic lepsza wersja
C/C++
vector < Student >::iterator i = Vec_Student.began();
while( i != Vec_Student.end() )
{
    srednia_ocen = srednia_ocen +( * i )->get_Srednia();
    i++;
}
Jakos tak to bylo ;/
P-110824
AngrySkarpeta
Temat założony przez niniejszego użytkownika
» 2014-05-25 14:09:46
Już działa ;-) Dziękuje bardzo, w pętli był dodatkowo błąd z tym Student::ile_S bo powinno być < a nie <= ;-). Dzięki Wielkie ;-)
P-110827
« 1 » 2
  Strona 1 z 2 Następna strona