![]() Ласло Краус |
Објектно програмирање - испити Практични испит 16. 2. 2002 |
---|
Пројектовати на језику C++ класу за грешке недозвољени индекс. Предвидети:
dat<<g
).
Ову класу треба користити за пријављивање одговарајућих изузетака у настаку.
Пројектовати на језику C++ класу за динамичке низове реалних бројева са задатим опсегом индекса. Предвидети:
niz1=niz2
),
niz[ind]
),
+
), и
Пројектовати на језику C++ апстрактну класу за реалне функције са једним реалним аргументом. Предвидети:
f(x)
), и
dat<<f
).
Пројектовати на језику C++ класу за полиноме као изведену класу из класе функција која садржи низ за смештање коефицијената. Поред могућности основне класе редвидети:
p[ind]
).
Исписивање формуле полинома подразумева исписивање низа коефицијената на пригодан начин. На пример, за полином 5x3-2x2+3 може да се испише: p[5,-2,0,3]
.
Саставити на језику C++ главни програм који:
// greske.h
#ifndef _greske_h_
#define _greske_h_
#include <iostream.h>
class GIndeks {
int ind;
public:
GIndeks (int i) { ind = i; }
friend ostream &operator<< (ostream &d, const GIndeks &g)
{ return d << "*** Nedozvoljeni indeks: " << g.ind; }
};
class GOpseg {
int min, max;
public:
GOpseg (int mi, int ma) { min = mi; max = ma; }
friend ostream &operator<< (ostream &d, const GOpseg &g)
{ return d << "*** Nedozvoljeni opseg: " << g.min << " - " << g.max; }
};
#endif
// niz.h
#ifndef _niz_h_
#define _niz_h_
#include "greske.h"
class Niz {
double *a;
int min, max, duz;
void kopiraj (const Niz &n);
void brisi () { delete a; a = 0; }
public:
Niz (int mi=1, int ma=10);
~Niz () { brisi (); }
Niz & operator= (const Niz &n) {
if (&n != this) { brisi (); kopiraj (n); }
return *this;
}
double & operator[] (int i) const {
if (i<min || i>max) throw GIndeks (i);
return a[i-min];
}
int operator+ () const { return duz; }
int minInd () const { return min; }
int maxInd () const { return max; }
};
#endif
// niz.cpp
#include "niz.h"
Niz::Niz (int mi, int ma) {
if ((max=ma)<(min=mi)) throw GOpseg (mi, ma);
a = new double [duz=max-min+1];
for (int i=0; i<duz; a[i++]=0);
}
void Niz::kopiraj (const Niz &n) {
a = new double [duz=n.duz];
min = n.min; max = n.max;
for (int i=0; i<duz; i++) a[i] = n.a[i];
}
// funkcija.h
#ifndef _funkcija_h_
#define _funkcija_h_
class Funkcija {
public:
virtual double operator() (double x) const =0;
virtual ~Funkcija () {}
protected:
virtual void pisi (ostream &d) const =0;
friend ostream & operator<< (ostream &d, const Funkcija &f)
{ f.pisi (d); return d;}
};
#endif
// polinom.h
#ifndef _polinom_h_
#define _polinom_h_
#include "niz.h"
#include "funkcija.h"
class Polinom: public Funkcija {
Niz p;
public:
Polinom (int n=3): p(0,n) {}
double operator() (double x) const;
double & operator[] (int i) { return p[i]; }
int operator+ () const { return p.maxInd (); }
private:
void pisi (ostream &d) const;
};
#endif
// polinom.cpp
#include "polinom.h"
double Polinom::operator() (double x) const {
double s = 0;
for (int i=p.maxInd(); i>=0; (s*=x)+=p[i--]);
return s;
}
void Polinom::pisi (ostream &d) const {
d << "p[";
for (int i=p.maxInd(); i>=0; i--)
{ d << p[i]; if (i > 0) d << ','; }
d << ']';
}
// glavni.cpp
#include "polinom.h"
#include <iostream.h>
int main () {
int n; cout << "Red polinoma? "; cin >> n;
Polinom p(n);
try {
while (1) {
cout << "Indeks i vrednost koeficijenta? ";
int i; double a; cin >> i >> a;
p[i] = a;
}
} catch (GIndeks) {}
cout << "Polinom= " << p << endl;
double xmin, xmax, dx; cout << "xmin, xmax, dx? "; cin >> xmin >> xmax >> dx;
cout << endl;
for (double x=xmin; x<=xmax; x+=dx)
cout << x << ' ' << p(x) << endl;
return 0;
}
(садржај)
Copyright © 2010, Laslo Kraus
Последња ревизија: 4.10.2010.