You are on page 1of 2

Objektno orijentirano programiranje

ispit
1. prosinac 2004.

1. (30 bodova)
Zadan je interface:

class filter {
public:
virtual bool read() = 0;
virtual void write() = 0;
virtual float percent() const = 0;
virtual ~filter() = 0;
};

filter::~filter() {}

i funkcija

float loop(filter& f){


while(f.read())
f.write();
return f.percent();
}

Napišite klasu koja implementira filter tako da se unose cijeli brojevi dok se ne unese nula, a na izlaz propuštaju samo
pozitivni brojevi. Napišite program u kojem koristeći tu klasu (sa standardnim ulazom i izlazom) pozivate loop i ispisujete
postotak unešenih negativnih brojeva.

2. (30 bodova)
Napišite klasu array tako da sljedeći program ispisuje sve elemente polja. U konstruktoru se svi elementi postavljaju na
zadanu vrijednost.
#include <iostream>
#include <algorithm>

int main(){
array<double, 16> a(1);
a[2] = 2;

std::ostream_iterator<double> os(std::cout, " ");


std::copy(a.begin(), a.end(), os);
}

3. (40 bodova)
U datoteci osobe.txt se u svakom redu nalazi ime osobe i njezina visina (u cm). Napišite program koji koristeći standardne
algoritme:
a) učita sve osobe u vektor
b) izbaci sve sa visinom manjom od 100 i većom od 200 cm
c) ispiše prosjek visina
d) sortira silazno po visini (u slučaju iste visine, uzlazno po abecedi)
e) ispiše sve osobe u obliku "ime: visina"
Objektno orijentirano programiranje
rješenja ispita
1. prosinac 2004.

1.
class negfilter : public filter {
std::istream& is;
std::ostream& os;
unsigned int tot, neg;
int n;
public:
negfilter(std::istream &is, std::ostream &os)
: is(is), os(os), neg(0), tot(0) {}
bool read() { is >> n; return n != 0; }
void write() {
if(n < 0) ++neg;
else os << n << std::endl;
++tot;
}
float percent() const { return tot ? 100.*neg/tot : 0; }
};

int main() {
negfilter f(std::cin, std::cout);
std::cout << "Postotak negativnih brojeva: " << loop(f) << '%' << std::endl;
}
2.
template <typename T, int N>
class array {
T a[N];
public:
array(const T& v){
for(int i=0; i<N; ++i)
a[i] = v;
}
T& operator[](int i) { return a[i]; }
T* begin() { return a; }
T* end() { return a+N; }
};
3.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric> // accumulate
using namespace std;

struct person {
string name;
unsigned int height; // cm
operator unsigned int() const { return height; } // for accumulate
};

istream& operator>>(istream& is, person& p){


return is >> p.name >> p.height;
}

ostream& operator<<(ostream& os, const person& p){


return os << p.name << ": "<< p.height;
}

bool rem(const person& p){


return p.height < 100 || p.height > 200;
}

bool comp(const person& p1, const person& p2){


return p1.height != p2.height ? p1.height > p2.height : p1.name < p2.name;
}

int main(){
ifstream f("osobe.txt");
istream_iterator<person> is(f), es;
ostream_iterator<person> os(cout, "\n");

vector<person> c;
copy(is, es, back_inserter(c));
c.erase(remove_if(c.begin(), c.end(), rem), c.end());
if(c.size())
cout << "prosjek: " << accumulate(c.begin(), c.end(), 0.)/c.size() << endl;

sort(c.begin(), c.end(), comp);


copy(c.begin(), c.end(), os);
}

You might also like