Visa elektrotehnicka skola, Beograd
Ласло Краус
Објектно програмирање 1 - колоквијуми
Други колоквијум
11. 6. 2011.

Задатак | Напомене | Решење

Поставка задатка (решење | садржај)

Написати на језику C++ следеће класе (класе опремити оним конструкторима, деструктором и оператором за доделу вредности који су потребни за безбедно коришћење класа; грешке пријављивати изузецима типа једноставних класа које су опремљене исписивањем поруке):

Написати на језику C++ функцију која кроз дијалог прочита једну функцију произвољног типа с главног улаза (резултат може бити и "празна" функција) и главну функцију која прочита једну фукцију, испише је на главном излазу и табелира на главном излазу вредности функције за xminxxmax са кораком Δx.


Напомене (садржај)


Решење задатка (поставка | садржај)

// 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
(садржај)
         
Аутор: Ласло Краус
Е-пошта: kraus@etf.rs

Copyright © 2012, Laslo Kraus
Последња ревизија: 6.5.2012.