Duman Temat założony przez niniejszego użytkownika |
Dynamicznie alkowana tablica liczb rzeczywistych » 2017-09-08 19:28:15 Witam , muszę zdefiniować klasę "Oceny" przechowującą informacje o ocenach w dynamicznie alokowanej tablicy liczb rzeczywistych. Alokacja tablicy następuje w konstruktorze ogólnym , otrzymującym jako parametr liczbę ocen. Zdefiniuj funkcję dodajOcene (double ocena) , która dopisuje do tablicy ocene(tylko liczby od 2.0 do 5.0) przekazaną parametrem ocena. Dopisać można ocene jedynie wtedy , gdy w tablicy jest jeszcze miejsce. Należy zdefiniować funkcję najnizszaOcena(), której rezultatem jest najniższa ocena zapisana w tablicy ocen. Jako przykład jak ma to wyglądać , udało mi się tak to zobrazować: Wiem ze chodzi tutaj o wykorzystanie wskaznikow , ale nie wiem w jaki sposob. Oceny listaOcen( 2 ); listaOcen.dodajOcene( 5 ); listaOcen.dodajOcene( 3 ); listaOcen.dodajOcene( 4 );
Oceny drugaLista( ListaOcen ); cout << listaOcen.najnizszaOcena() << " " drugaLista.najnizszaOcena();
|
|
latajacaryba |
» 2017-09-08 19:47:20 Wiem ze chodzi tutaj o wykorzystanie wskaznikow , ale nie wiem w jaki sposob. |
A jak chcesz dynamicznie alokować bez wskaźników? Przeczytaj |
|
f651144 |
» 2017-09-09 12:33:17 Chodzi o coś takiego? #include <memory>
class Oceny { public: Oceny() = delete; Oceny( size_t rozmiar ); Oceny( const Oceny & o ); Oceny( Oceny && o ); Oceny & operator =( const Oceny & o ); Oceny & operator =( Oceny && o ); ~Oceny() = default; bool dodajOcene( double ocena ); double & najnizszaOcena(); const double & najnizszaOcena() const; private: std::unique_ptr < double > begin_ = nullptr; double * end_ = nullptr; double * alloc_end_ = nullptr; void init_( size_t rozmiar ); };
#include <Oceny.hpp>
#include <algorithm> #include <iterator> #include <memory> #include <stdexcept>
Oceny::Oceny( size_t rozmiar ) { this->init_( rozmiar ); };
Oceny::Oceny( const Oceny & o ) : Oceny( std::distance( o.begin_.get(), o.alloc_end_ ) ) { std::copy( o.begin_.get(), o.end_, this->begin_.get() ); this->end_ += std::distance( o.begin_.get(), o.end_ ); };
Oceny::Oceny( Oceny && o ) { this->begin_ = std::move( o.begin_ ); this->end_ = o.end_; this->alloc_end_ = o.alloc_end_; };
Oceny & Oceny::operator =( const Oceny & o ) { this->init_( std::distance( o.begin_.get(), o.alloc_end_ ) ); std::copy( o.begin_.get(), o.end_, this->begin_.get() ); this->end_ += std::distance( o.begin_.get(), o.end_ ); return * this; };
Oceny & Oceny::operator =( Oceny && o ) { this->begin_ = std::move( o.begin_ ); this->end_ = o.end_; this->alloc_end_ = o.alloc_end_; return * this; };
bool Oceny::dodajOcene( double ocena ) { if( ocena > 5 || ocena < 2 ) return false; if( this->end_ != this->alloc_end_ ) { * this->end_ = ocena; this->end_ += 1; return true; } return false; };
double & Oceny::najnizszaOcena() { double * min_ptr = std::min_element( this->begin_.get(), this->end_ ); if( min_ptr == this->end_ ) throw std::runtime_error( "Oceny::empty_check" ); return * min_ptr; };
const double & Oceny::najnizszaOcena() const { const double * min_ptr = std::min_element( this->begin_.get(), this->end_ ); if( min_ptr == this->end_ ) throw std::runtime_error( "Oceny::empty_check" ); return * min_ptr; };
void Oceny::init_( size_t rozmiar ) { this->begin_ = std::make_unique < double >( rozmiar ); this->end_ = this->begin_.get(); this->alloc_end_ = this->begin_.get() + rozmiar; };
#include <iostream>
int main() { Oceny listaOcen( 2 ); listaOcen.dodajOcene( 5 ); listaOcen.dodajOcene( 3 ); listaOcen.dodajOcene( 4 ); Oceny drugaLista( listaOcen ); std::cout << listaOcen.najnizszaOcena() << " " << drugaLista.najnizszaOcena(); }
Jeżeli masz za stary kompilator, to możesz przetestować rozwiązanie tutaj. PS: Tak, zupełnie na marginesie: nie zalecam zaczynania wiadomości od 'Witam', a zwłaszcza zwracając się o pomoc. Nie bierz tego do siebie - ten błąd popełniają tu wszyscy. |
|
Luq |
» 2017-09-09 15:02:48 this->begin_ = std::make_unique < double >( rozmiar ); |
To jest błędne. Skoro to ma być tablica, to powinno być this->begin_ = std::make_unique < double[] >( rozmiar ); Podobnie begin_ powinno być typu std::unique_ptr < double[] > |
|
mateczek |
» 2017-09-09 16:51:56 auto inteligentnyWskaznik = std::make_unique < double >( rozmiar )
|
|
karambaHZP |
» 2017-09-09 19:04:28 |
|
Kinexity |
» 2017-09-09 21:34:22 Nie wiem po co tak kombinujecie - @Duman wcale nie napisał, że nie chce używać wskaźników. #include <iostream> #include <limits> using namespace std;
class Oceny { private: const unsigned short maksymalna_ocena = short( 5 ), minimalna_ocena = short( 2 ); unsigned int rozmiar_tablicy = int(), liczba_ocen = int(); unsigned short * tablica_ocen = nullptr, najnizsza_ocena = numeric_limits < unsigned short >::max(), najwyzsza_ocena = numeric_limits < unsigned short >::min(); public: Oceny( unsigned int liczba_ocen ); Oceny( const Oceny & obiekt ); ~Oceny(); void dodaj_ocene( unsigned short ocena ), operator =( const Oceny & obiekt ); unsigned short najwyzsza(), najnizsza(), operator []( unsigned int indeks_oceny ); };
Oceny::Oceny( unsigned int liczba_ocen ) : rozmiar_tablicy( liczba_ocen ) { tablica_ocen = new unsigned short[ rozmiar_tablicy ]; }
Oceny::Oceny( const Oceny & obiekt ) { * this = obiekt; }
Oceny::~Oceny() { delete[] tablica_ocen; }
void Oceny::dodaj_ocene( unsigned short ocena ) { if( ocena < minimalna_ocena || ocena > maksymalna_ocena ) { cout << "Nie ma takiej oceny!" << endl; } else if( liczba_ocen == rozmiar_tablicy ) { cout << "Brak miejsca na oceny!" << endl; } else { tablica_ocen[ liczba_ocen++ ] = ocena; najnizsza_ocena =( ocena < najnizsza_ocena ? ocena: najnizsza_ocena ); najwyzsza_ocena =( ocena > najwyzsza_ocena ? ocena: najwyzsza_ocena ); } }
void Oceny::operator =( const Oceny & obiekt ) { rozmiar_tablicy = obiekt.rozmiar_tablicy; liczba_ocen = obiekt.liczba_ocen; tablica_ocen = obiekt.tablica_ocen; najnizsza_ocena = obiekt.najnizsza_ocena; najwyzsza_ocena = obiekt.najwyzsza_ocena; }
unsigned short Oceny::najnizsza() { return najwyzsza_ocena; }
unsigned short Oceny::najwyzsza() { return najnizsza_ocena; }
unsigned short Oceny::operator []( unsigned int indeks_oceny ) { if( indeks_oceny >= rozmiar_tablicy ) { cout << "Nie ma takiego indeksu!" << endl; return unsigned short( 0 ); } else if( indeks_oceny >= liczba_ocen ) { cout << "Pod tym indeksem nie ma jeszcze oceny!" << endl; return unsigned short( 0 ); } else { return tablica_ocen[ indeks_oceny ]; } } |
|
f651144 |
» 2017-09-09 23:20:03 @Luq i @mateczek macie rację, mój błąd - powinno być this->begin_ = std::make_unique < double[] >( rozmiar ); . |
|
« 1 » |