Commit 88c368b2 by Patryk Czarnik

typedef aby w jednym miejscu określić typ

parent 010aee55
......@@ -10,7 +10,7 @@ int main() {
w.push_back(12);
std::cout << "Element nr 0: " << w.get(0) << '\n';
std::cout << "Element nr 2: " << w.get(2) << '\n';
w.set(2, 222);
w.set(2, 22);
std::cout << "Element nr 2: " << w.get(2) << '\n';
std::cout <<"size: " << w.size() << '\n';
std::cout << '\n';
......
#include <iostream>
#include "wektor.h"
const int startowy_rozmiar_tablicy = 8;
const TypIndeksu startowy_rozmiar_tablicy = 8;
Wektor::Wektor()
: rozmiar_tablicy{startowy_rozmiar_tablicy},
liczba_elementow{0}
{
t = new int[rozmiar_tablicy];
t = new TypWartosci[rozmiar_tablicy];
}
Wektor::~Wektor() {
delete[] t;
}
int Wektor::size() const {
TypIndeksu Wektor::size() const {
return liczba_elementow;
}
void Wektor::push_back(int e) {
void Wektor::push_back(TypWartosci e) {
if(liczba_elementow == rozmiar_tablicy) {
// przerzuć elementy do większej tablicy
rozmiar_tablicy = 2*rozmiar_tablicy;
int *nt = new int[rozmiar_tablicy];
for(int i=0; i<liczba_elementow; i++) {
TypWartosci *nt = new TypWartosci[rozmiar_tablicy];
for(TypIndeksu i=0; i<liczba_elementow; i++) {
nt[i] = t[i];
}
delete[] t;
......@@ -32,14 +32,14 @@ void Wektor::push_back(int e) {
t[liczba_elementow++] = e;
}
void Wektor::set(int idx, int e) {
void Wektor::set(TypIndeksu idx, TypWartosci e) {
if(idx < 0 || idx >= liczba_elementow) {
throw "indeks poza zakresem";
}
t[idx] = e;
}
int Wektor::get(int idx) {
TypWartosci Wektor::get(TypIndeksu idx) {
if(idx < 0 || idx >= liczba_elementow) {
throw "indeks poza zakresem";
}
......@@ -48,7 +48,7 @@ int Wektor::get(int idx) {
std::ostream& operator<<(std::ostream& out, const Wektor& w) {
out << '[';
for(int i=0; i<w.liczba_elementow; i++) {
for(TypIndeksu i=0; i<w.liczba_elementow; i++) {
out << w.t[i] << ", ";
}
out << ']';
......
......@@ -3,31 +3,35 @@
#include <iostream>
typedef int TypWartosci;
typedef unsigned int TypIndeksu;
/** Tablica zmiennej długości zawierająca wartości int. */
class Wektor {
int *t;
TypWartosci *t;
// fizyczny rozmiar wewnętrznej tablicy
int rozmiar_tablicy;
TypIndeksu rozmiar_tablicy;
// logiczny rozmiar wektora = ile pozycji w tablicy jest zajętych = indeks pierwszej wolnej pozycji
int liczba_elementow;
TypIndeksu liczba_elementow;
// powinno być liczba_elementow <= rozmiar_tablicy
public:
Wektor();
~Wektor();
int size() const;
TypIndeksu size() const;
/** Dodanie nowego elementu na końcu. */
void push_back(int e);
void push_back(TypWartosci e);
/** Przypisanie nowej wartości na podanej pozycji. */
void set(int idx, int e);
void set(TypIndeksu idx, TypWartosci e);
/** Odczyt wartości spod podanej pozycji. */
int get(int idx);
TypWartosci get(TypIndeksu idx);
friend std::ostream& operator<<(std::ostream&, const Wektor&);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment