![]() Ласло Краус |
Објектно програмирање 1 - колоквијуми Други колоквијум 11. 6. 2011. |
|---|
Написати на језику C++ следеће класе (класе опремити оним конструкторима, деструктором и оператором за доделу вредности који су потребни за безбедно коришћење класа; грешке пријављивати изузецима типа једноставних класа које су опремљене исписивањем поруке):
fun(x)) и да се упише у излазни ток (it<<funS).
*sin(b*x+c), где су a, b и c вредности параметара функције.
poli[ind]). Грешка је ако је индекс изван опсега. У излазни ток се пише у облику p[an,an−1,…,a1,a0](x), где су ai вредности коефицијената полинома.
zbirFun+=fun). Вредност збира функција једнака је збиру вредности садржаних функција. У излазни ток се пише у облику (f)+(f)+…+(f), где је f резултат писања једне садржане функције.
Написати на језику C++ функцију која кроз дијалог прочита једну функцију произвољног типа с главног улаза (резултат може бити и "празна" функција) и главну функцију која прочита једну фукцију, испише је на главном излазу и табелира на главном излазу вредности функције за xmin≤x≤xmax са кораком Δx.
ispitx (x је број радне станице за којим се ради). Лозинка на свакој радној станици је student.Z.
// fun.h
#ifndef _fun_h_
#define _fun_h_
#include <iostream>
using namespace std;
class Fun {
virtual void pisi(ostream& it) const =0;
public:
virtual ~Fun() {}
virtual Fun* kopija() const =0;
virtual double operator()(double x) const =0;
friend ostream& operator<<(ostream& it, const Fun&f )
{ f.pisi(it); return it; }
};
#endif
// sin.h
#ifndef _sin_h_
#define _sin_h_
#include "fun.h"
#include <iostream>
#include <cmath>
using namespace std;
class Sin: public Fun { double a, b, c;
void pisi(ostream& it) const
{ it << a << "*sin(" << b << "*x+" << c << ')'; }
public:
explicit Sin(double aa=1, double bb=1, double cc=0)
{ a = aa; b = bb; c = cc; }
Sin* kopija() const { return new Sin(*this); }
double operator()(double x) const { return a * sin(b*x+c); }
};
#endif
// poli.h
#ifndef _poli_h_
#define _poli_h_
#include "fun.h"
#include <iostream>
using namespace std;
class GInd {};
inline ostream& operator<<(ostream& it, const GInd&)
{ return it << "*** Indeks izvan opsega!"; }
class Poli: public Fun { double* a; int n;
void pisi(ostream& it) const;
void kopiraj(const Poli& p);
void brisi() { delete [] a; }
public:
explicit Poli(int nn=1);
Poli(const Poli& p) { kopiraj(p); }
~Poli() { brisi(); }
Poli& operator=(const Poli& p) {
if (this != &p) { brisi(); kopiraj(p); }
return *this;
}
Poli* kopija() const { return new Poli(*this); }
double operator()(double x) const;
double& operator[](int i) {
if (i<0 || i>n) throw GInd();
return a[i];
}
int red() const { return n; }
const double& operator[](int i) const {
if (i<0 || i>n) throw GInd();
return a[i];
}
};
#endif
// poli.cpp
#include "poli.h"
void Poli::pisi(ostream& it) const {
it << "p[";
for (int i=n; i>=0; i--) { it << a[i]; if (i) it << ','; }
it << "](x)";
}
void Poli::kopiraj(const Poli& p) {
a = new double [(n=p.n) + 1];
for (int i=0; i<=n; i++) a[i] = p.a[i];
}
Poli::Poli(int nn) {
a = new double [(n=nn) + 1];
for (int i=0; i<n; i++) a[i] = 0;
a[n] = 1;
}
double Poli::operator()(double x) const {
double p = a[n];
for (int i=n-1; i>=0; (p*=x)+=a[i--]);
return p;
}
// zbirFun.h
#ifndef _zbirFun_h_
#define _zbirFuz_h_
#include "fun.h"
#include <iostream>
using namespace std;
class ZbirFun: public Fun {
struct Elem { Fun* f; Elem* sled;
Elem(Fun* ff) { f = ff; sled = 0; }
~Elem() { delete f; }
};
Elem *prvi, *posl;
void pisi(ostream& it) const;
void kopiraj(const ZbirFun& zf);
void brisi();
public:
ZbirFun() { prvi = posl = 0; }
ZbirFun(const ZbirFun& zf) { kopiraj(zf); }
~ZbirFun() { brisi(); }
ZbirFun& operator=(const ZbirFun& zf) {
if (this != &zf) { brisi(); kopiraj(zf); }
return *this;
}
ZbirFun* kopija() const {return new ZbirFun(*this);}
double operator()(double x) const;
ZbirFun& operator+=(const Fun& f) {
posl = (!prvi ? prvi : posl->sled) = new Elem(f.kopija());
return *this;
}
};
#endif
// zbirFun.cpp
#include "zbirFun.h"
void ZbirFun::pisi(ostream& it) const {
for( Elem* tek=prvi; tek; tek=tek->sled) {
it<<'('<<*tek->f<<')'; if (tek->sled) it <<'+';
}
}
void ZbirFun::kopiraj(const ZbirFun& zf) {
prvi = posl = 0;
for (Elem* tek=zf.prvi; tek; tek=tek->sled)
*this += *tek->f;
}
void ZbirFun::brisi() {
while (prvi) { Elem* stari=prvi; prvi=prvi->sled; delete stari; }
posl = 0;
}
double ZbirFun::operator()(double x) const {
double zf = 0;
for(Elem* tek=prvi; tek; tek=tek->sled) zf += (*tek->f)(x);
return zf;
}
// program.cpp
#include "sin.h"
#include "poli.h"
#include "zbirFun.h"
#include <iostream>
using namespace std;
Fun* citaj() {
cout << "Vrsta (S,P,Z,.)? ";
char vrs; cin >> vrs;
switch (vrs) {
case 's': case 'S': {
cout << "a,b,c? ";
double a, b, c;
cin >> a >> b >> c;
return new Sin(a, b, c);
}
case 'p': case 'P': {
cout << "n? "; int n; cin >> n;
Poli* p = new Poli(n);
cout << "A? ";
for (int i=n; i>=0; cin>>(*p)[i--]);
return p;
}
case 'z': case 'Z': {
ZbirFun* zf = new ZbirFun();
while (Fun* f = citaj()) { *zf += *f; delete f; }
return zf;
}
default: return 0;
}
}
int main() {
try {
Fun* f = citaj();
if (!f) throw "*** Nema funkcije!";
cout << "f = " << *f << endl;
double xmin, xmax, dx;
cout << "xmin, xmax, dx? ";
cin >> xmin >> xmax >> dx;
for (double x=xmin; x<=xmax; x+=dx)
cout << x << '\t' << (*f)(x) << endl;
} catch (GInd& g) { cout << g << endl; }
catch (const char* g) { cout << g << endl; }
return 0;
}
Vrsta (S, P, Z, .)? z
Vrsta (S, P, Z, .)? s
a,b,c? 2 0.5 0.78
Vrsta (S, P, Z, .)? p
n? 3
A? 1 -2 3 -4
Vrsta (S, P, Z, .)? .
f = (2*sin(0.5*x+0.78))+(p[1,-2,3,-4](x))
xmin, xmax, dx? 0 6.28 0.628
0 -2.59344
0.628 -0.880155
1.256 0.567869
1.884 3.21744
2.512 8.55425
3.14 18.0829
3.768 33.325
4.396 55.816
5.024 87.0998
5.652 128.724
6.28 182.232
(садржај)
Copyright © 2012, Laslo Kraus
Последња ревизија: 6.5.2012.