0% found this document useful (0 votes)
38 views

PDF Metode Programiranja Zadaci - Compress

The document describes creating several classes in C++ including a Rectangle class and Square subclass, a Triangle class and IsoscelesTriangle subclass, a ComplexNumber class for representing complex numbers, a generic Array class, and a generic LinkedList class with a FIFO subclass. Examples of using these classes are provided.

Uploaded by

Jelena Filipovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

PDF Metode Programiranja Zadaci - Compress

The document describes creating several classes in C++ including a Rectangle class and Square subclass, a Triangle class and IsoscelesTriangle subclass, a ComplexNumber class for representing complex numbers, a generic Array class, and a generic LinkedList class with a FIFO subclass. Examples of using these classes are provided.

Uploaded by

Jelena Filipovic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

 

1.Napisati klasu Pravougaonik. Iz klase Pravougaonik izvesti klasu Kvadrat.

class Pravougaonik {
 protected:
double a, b;
 public:
Pravougaonik(double aa=1, double bb=2) { a=aa; b=bb; }
Pravougaonik(const Pravougaonik &p) { a=p.a; b=p.b; }
void setA(double aa) { a=aa; }
void setB(double bb) { b=bb; }
double getA() const { return a; }
double getB() const { return b; }
double getO() const { return 2*a+2*b; }
double getP() const { return a*b; }
};

#include "pravougaonik.hpp
"pravougaonik.hpp""

class Kvadrat : public Pravougaonik {


 public:
Kvadrat(double aa=1) : Pravougaonik(aa,aa) {}
Kvadrat(const Kvadrat &k) : Pravougaonik(k.a, k.b) {}
};

// datoteka: test.cpp
#include "kvadrat.hpp"
#include <iostream>
using namespace std;

int main() {
Pravougaonik p1(2,5);
cout<<"Obim p1: "<<p1.getO()<<endl;
cout<<"Povrsina p1: "<<p1.getP()<<endl;
Kvadrat k1(4);
cout<<"Obim k1: "<<k1.getO()<<endl;
cout<<"Povrsina k1: "<<k1.getP()<<endl;
return 0;
}
 

2. Napisati klasu trougao. Iz klase trougao izvesti klasu jednakokraki trougao. Kreirati
konstruktor bez parametara, konstruktor sa parametrima, konstruktor kopije, get i set metode i
metode za racunanje obima i povrsine.

#ifndef TROUGAO_H
#define TROUGAO_H

class trougao
{
 private:
double a;
double b;
double c;
double hA;
 public:
trougao(){a=b=c=hA=0;}
trougao(double xA, double xB, double xC, double xHA){a=xA,b=xB,c=xC,hA=xHA;}
trougao(trougao &);
~trougao(){}
virtual double obim(){return a+b+c;}
virtual double povrsina(){return (a*hA)/2;}
double getA(){return a;}
double getB(){return b;}
double getC(){return c;}
double getHA(){return hA;}
void setA(double xA){a=xA;}
void setB(double xB){a=xB;}
void setC(double xC){a=xC;}
void setHA(double xHA){hA=xHA;}
};
#include"trougao.h"

trougao::trougao(trougao& xTrougao)
{
a=xTrougao.getA();
 b=xTrougao.getB();
c=xTrougao.getC();
hA=xTrougao.getHA();
}

#ifndef JEDNAKOKRAKI_H
 

#define JEDNAKOKRAKI_H

#include "trougao.h"
class jednakokraki : trougao
{
 public:

virtual double obim(){return


o bim(){return 2*getA()+getB();}
virtual double povrsina(){return
p ovrsina(){return (getA()*getHA())/
(getA()*getHA())/2;}
2;}
 jednakokraki(double xA, double xB, double xHA);
};
#endif // JEDNAKOKRAKI_H

#include "jednakokraki.h"
 jednakokraki::jednakokraki(double xA, double xB, double xHA)
{
setA(xA);
setB(xB);

setHA(xHA);

}
#endif // TROUGAO_H
#include <stdio.h>
#include "trougao.h"
#include "jednakoKraki.h"
int main()
{
trougao a, b(10, 10, 10, 10);
a.setA(12);

a.setB(12);
a.setC(12);
a.setHA(10);
trougao c(a);
 jednakoKraki d(5, 10, 14);
 printf("trougao a ima obim %lf, i povrsinu
povrsinu %lf\n", a.obim(), a.povrsina());
 printf("trougao b ima obim %lf, i povrsinu %lf\n",
%lf\n", b.obim(), b.povrsina());
 printf("trougao c ima obim %lf, i povrsinu
povrsinu %lf\n", c.obim(), c.povrsina());
 printf("trougao d ima obim %lf, i povrsinu %lf\n",
%lf\n", d.obim(), d.povrsina());
return 0;
}
 

3.Napisati klasu koja modeluje kompleksni broj. Preklopiti operatore za sabiranje,


oduzimanje, mnozenje, dodelu vrednosti, proveru jednakosti i ispis na konzolu.

#ifndef COMPLEX_DEF

#define COMPLEX_D
COMPLEX_DEF
EF

#include <iostream>
using namespace std;

class Complex {
 private:
double re;
double im;
 public:
Complex();
Complex(double, double);
Complex(const Complex&);
double getRe() const;
double getIm() const;
void setRe(double);
void setIm(double);

Complex& operator=(const Complex&);


Complex& operator+=(const Complex&);
Complex& operator-=(const Complex&);
Complex& operator*=(const Complex&);
Complex& operator/=(const Complex&);
friend Complex operator+(const Complex&, const Complex&);
friend Complex operator-(const Complex&, const Complex&);
friend Complex operator*(const Complex&, const Complex&);
friend Complex operator/(const Complex&, const Complex&);
friend bool operator==(const Complex&, const Complex&);
friend bool operator!=(const Complex&, const Complex&);
friend ostream& operator<<(ostream&, const Complex&);
};
Preklapanje sabiranja

Complex operator+(const Complex &z1, const Complex &z2) {


Complex w(z1.re+z2.re, z1.im+z2.im);
return w;
}

Preklapanje oduzimanja
 

Complex operator-(const Complex &z1, const Complex &z2) {


Complex w(z1.re-z2.re, z1.im-z2.im);
return w;
}

Preklapanje mnozenja Complex &z1, const Complex &z2) {


Complex operator*(const
double r=z1.re*z2.re - z1.im*z2.im;
double i=z1.re*z2.im + z1.im*z2.re;
Complex w(r, i);
return w;
}

Preklapanje dodele vrednosti

Complex& Complex::operator=(const
Complex::operator=(const Complex &z) {
re=z.re; im=z.im;
return *this;
}

Preklapanje provere jednakosti

 bool operator==(const Complex &z1, const Complex


Complex &z2) {
if(z1.re==z2.re && z1.im==z2.im)
return true;
else
return false;
}

Preklapanje izlaza na konzolu

ostream& operator<<(ostream
operator<<(ostream &out, const Complex &z) {
if(z.re==0 && z.im!=0) out<<z.im<<"i";
if(z.re!=0 && z.im==0) out<<z.re;
if(z.re!=0 && z.im>0) out<<z.re<<"+"<<z.im<<"i";
if(z.re!=0 && z.im<0) out<<z.re<<z.im<<"i";
return out;
}
 
 

4.Napisati sablon klase Niz<class T, int D>. Implementirati konstruktor, destruktor,


operator indeksiranja, operator dodele vrednosti i operator za proveru jednakosti.

#include <iostream>
using namespace std;

template
class Niz <class
{ T, int D>
 private:
T el[D];
int brEl;
 public:
 Niz() { brEl=0; } -konstrukotr 
-konstrukotr 
~Niz() {} -Destruktor 
int getBrEl() const { return brEl; }
T operator[](int i) const { return el[i]; } - Operator 
T& operator[](int i) { return el[i]; } -Indeksiranja
Niz<T,D>& operator=(const Niz<T,D>&);
Niz<T,D>&); - Operator za dodelu vrednosti
bool operator==(const Niz<T,D>&);
Niz<T,D>&); - Operat
Operator
or za proveru jednakosti
jednakosti da li je
 jednoko?
 bool operator!=(const Niz<T,D>&);
Niz<T,D>&); -Operator
-Operator za proveru jednakosti
jednakosti da li nije
 jednako?
void printNiz() const;
 bool insertNiz(const
insertNiz(const T&);
};

template <class T, int D>

 Niz<T,D>& Niz<T,D>::operator=(
Niz<T,D>::operator=(const
const Niz<T,D> &rn) {
for(brEl=0; brEl<rn.brEl; brEl++)

el[brEl]=rn[brEl];
return *this;
}
template <class T, int D>
 bool Niz<T,D>::operator==(const
Niz<T,D>::operator==(const Niz<T,D>
Niz<T,D> &rn) {
if(brEl!=rn.brEl)
return false;
for(int i=0; i<brEl; i++)
if(el[i]!=rn.el[i])
return false;
return true;
}
template <class T, int D>
 

 bool Niz<T,D>::operator!=(const
Niz<T,D>::operator!=(const Niz<T,D>
Niz<T,D> &rn) {
if(brEl!=rn.brEl)
return true;
for(int i=0; i<brEl; i++)
if(el[i]!=rn.el[i])
return true;

return
} false;

template <class T, int D>


void Niz<T,D>::printNiz() const {
cout<<"( ";
for(int i=0; i<brEl-1; i++)
cout<<el[i]<<", ";
cout<<el[brEl-1]<<" )"<<endl;
}
template <class T, int D>
 bool Niz<T,D>::insertNiz(const
Niz<T,D>::insertNiz(const T &t) {

if(brEl<D) {
el[brEl]=t;
 brEl++;
return true;
}
else
return false;
}

#include "niz.hpp"

int main() {
 Niz<int,10> iNiz;
iNiz;
iNiz.insertNiz(1);
iNiz.insertNiz(2);
iNiz.insertNiz(3);
iNiz.insertNiz(4);
iNiz.insertNiz(5);
iNiz.insertNiz(6);
iNiz.printNiz();
return 0;
}
 

5.Napisati genericku klasu List(jednostrukospregnuta lista). Iz klase List izvesti klasu FIFO.
 Napisati kratak test program.

#include <stdlib.h>
#include <iostream>
using namespace std;

template <class T>


class List{
 private:
struct listEl{
T content; //sadrzaj
struct listEl *next;//pokazivac na sledeci elemenat
};
listEl *head;//prvi element liste(glava liste)
listEl *tail;//nije lose za imati, moze povecati upotrebljivost
strukture
int noEl;
 public:
List(){ // konstruktor bez parametara
head=tail=NULL;
noEl=0;
}
List(const List<T> &);//konstruktor kopije
List<T>& operator=(const List<T>&);//prekloplen operator =
virtual ~List();

int size() const {return noEl;}//broj elemenata u liste


 bool empty() const {return head==NULL?1:0;}

 bool add(int, const T&);


 bool remove(int);//brisanje elementa iz liste
liste

 bool read(int,
funkcija T&)const; //ocitani element se smesti
vraca uspelo-neuspelo smesti u T -

void clear();

};

#include "list.hpp"

template <class T>


class FIFO; //prototip genericke klase FIFO

template <class T>


void printOut(const FIFO<T> &); //prototip slobodne friend-funkcije
 

template <class T>


class FIFO: protected List <T> {
 public:
FIFO(){};
 bool readFromQueue(T&)const;

void removeFromQueue() {List<T>::remove(1);}


{List<T>::remove(1);}
void addToQueue(const T &El){add(size()+1,El);}
&El){add(size()+1,El);}
 bool empty() const {return List<T>::empty();}
List<T>::empty();}
int size() const {return List<T>::size();}
friend void printOut<>(const FIFO<T> &);
virtual ~FIFO(){}
};

template <class T>


void printOut(const FIFO<T> &l){
cout<<endl;
cout<<"\tVelicina reda: "<<l.size()<<endl;
cout<<"\tSadrzaj reda je: ";
T retVal;
for(int i=1;i<=l.size();i++)
i=1;i<=l.size();i++){
{
if(i>1) cout<<", ";
l.read(i,retVal);
cout<<retVal;
}

cout<<endl<<endl;
}

template <class T>


 bool LinkedQueue<T>::readFromQueue(T& retVal)const
{
return List<T>::read(1,retVal);
}

#endif 

// datoteka: ilt.cpp
#include "queue_lnk.hpp"
#include <iostream>
using namespace std;

typedef FIFO<int> IntQueue;

int main(){a;
IntQueue
 

a.addToQueue(1);
a.addToQueue(2);
a.addToQueue(3);

cout<<"Queue: ";
 printOut(a);
cout<<endl;

cout<<"--------------------"<<endl;
cout<<"Obavlja se uklanjanje iz reda"<<endl;
a.removeFromQueue();
cout<<"Queue: ";
 printOut(a);
cout<<endl;

cout<<"--------------------"<<endl;
cout<<"Obavlja se uklanjanje iz reda"<<endl;
a.removeFromQueue();
cout<<"Queue: ";
 printOut(a);
cout<<endl;

cout<<"--------------------"<<endl;
cout<<"Dodavanje u red broja 45"<<endl;
a.addToQueue(45);
cout<<"Queue: ";
 printOut(a);
cout<<endl;

cout<<"--------------------"<<endl;
cout<<"Pokusaj samo citanja (ne i uklanjanja) iz reda"<<endl;
int ret;
if(a.readFromQueue(ret))
cout<<"Obavljeno citanje iz reda: "<<ret<<endl<<endl;
else
cout<<"Citanje nije obavljeno - Prazan red!!!"<<endl<<endl;

cout<<"--------------------"<<endl;
cout<<"Obavlja se uklanjanje iz reda"<<endl;
a.removeFromQueue();
cout<<"Queue: ";
 printOut(a);
cout<<endl;

cout<<"--------------------"<<endl;
cout<<"Obavlja se uklanjanje iz reda"<<endl;
 

a.removeFromQueue();
cout<<"Queue: ";
 printOut(a);
cout<<endl;

cout<<"--------------------"<<endl;
cout<<"Pokusaj samo citanja (ne i uklanjanja) iz reda"<<endl;
if(a.readFromQueue(ret))
cout<<"Obavljeno citanje iz reda: "<<ret<<endl<<endl;
else
cout<<"Citanje nije obavljeno - Prazan red!!!"<<endl<<endl;

cout<<"--------------------"<<endl;
cout<<"Dodavanje u red broja 100"<<endl;
a.addToQueue(100);
cout<<"Queue: ";
 printOut(a);

return 0;

You might also like