Untitled
unknown
plain_text
8 months ago
21 kB
10
Indexable
Napišite klasu Complex Number koja predstavlja kompleksne brojeve. Klasa treba sadržavati:
• Konstruktor s realnim i imaginarnim dijelom.
• Preopterećene operatore +, - i *.
• Metodu modulus() koja vraća apsolutnu vrijednost kompleksnog broja.
/****************************************************************************************************************************/
#include <iostream>
#include <cmath>
#include <complex>
class ComplexNumber {
private:
double real;
double imag;
public:
ComplexNumber(double r = 0, double i = 0) : real(r), imag(i) {}
ComplexNumber operator+(ComplexNumber& other) {
return ComplexNumber(real + other.real, imag + other.imag);
}
ComplexNumber operator-(ComplexNumber& other) {
return ComplexNumber(real - other.real, imag - other.imag);
}
ComplexNumber operator*(ComplexNumber& other) {
return ComplexNumber(real * other.real - imag * other.imag, real* other.imag - imag * other.real);
}
double modulus() {
return std::sqrt(real * real + imag * imag);
}
void print() {
std::cout << real << " + " << imag << "i" << '\n';
}
};
int main() {
ComplexNumber c1(3, 4);
ComplexNumber c2(5, 6);
ComplexNumber sum = c1 + c2;
sum.print();
ComplexNumber diff = c2 - c1;
diff.print();
ComplexNumber prod = c1 * c2;
prod.print();
std::cout << c1.modulus();
return 0;
}
/****************************************************************************************************************************/
Napišite program koji koristi apstraktnu klasu BankAccount s barem dvije izvedene klase Savings Account (štednja) i CheckingAccount (tekući račun). Klasa BankAccount treba sadržavati:
• Čisto virtualnu metodu void deposit (double amount).
• Čisto virtualnu metodu void withdraw (double amount).
Tekući račun ima dozvoljeni minus, a štednja ima kamatu čije se vrijednosti postavljaju u konstruk- toru. Osim toga, oba računa imaju inicijalno stanje.
/****************************************************************************************************************************/
#include <iostream>
class BankAccount {
protected:
double balance;
public:
BankAccount(double initBalance) : balance(initBalance) {}
virtual ~BankAccount() {}
virtual void deposit(double amount) = 0;
virtual void withdraw(double amount) = 0;
void display() {
std::cout << "Stanje: " << balance << "." << '\n';
}
};
class SavingsAccount : public BankAccount {
private:
double kamate;
public:
SavingsAccount(double initBalance, double kamate) : BankAccount(initBalance), kamate(kamate) {}
virtual void deposit(double amount) override {
balance += amount + (amount * kamate / 100);
}
virtual void withdraw(double amount) override {
if (balance >= amount) {
balance -= amount;
}
else std::cout << "Nemate dovoljno novaca.\n";
}
};
class ChekingAccount : public BankAccount {
private:
double dozvoljeniMinus;
public:
ChekingAccount(double initBalance, double dozvoljeniMinus) : BankAccount(initBalance), dozvoljeniMinus(dozvoljeniMinus) {}
virtual void deposit(double amount) override {
balance += amount;
}
virtual void withdraw(double amount) override {
if (balance - amount >= dozvoljeniMinus) {
balance -= amount;
}
else std::cout << "Prekorecenje dozvoljenog minusa.\n";
}
};
int main() {
SavingsAccount savings(1000, 5);
ChekingAccount cheking(500, 200);
std::cout << "Štenja: \n";
savings.display();
savings.deposit(100);
savings.display();
savings.withdraw(200);
savings.display();
savings.withdraw(5000);
std::cout << '\n';
std::cout << "Tekuci : \n";
cheking.display();
cheking.deposit(200);
cheking.display();
cheking.withdraw(500);
cheking.display();
cheking.withdraw(1000);
return 0;
}
/****************************************************************************************************************************/
) Napišite template klasu Stack koja omogućava rad sa stogom. Klasa treba sadržavati:
• Metode push, pop i top.
Bacanje iznimke ako se pokušava izvaditi element iz praznog stoga.
/****************************************************************************************************************************/
#include <iostream>
#include <vector>
#include <stdexcept>
template <typename T>
class Stack {
private:
std::vector<T> elements;
public:
void push(const T& item) {
elements.push_back(item);
}
void pop() {
if (elements.empty()) {
throw std::out_of_range("Prazan stog");
}
elements.pop_back();
}
T top() {
if (elements.empty()) {
throw std::out_of_range("Prazan stog");
}
return elements.back();
}
};
int main() {
Stack<int> stog;
try {
stog.push(10);
stog.push(20);
stog.push(30);
std::cout << "vrh stoga: " << stog.top() << '\n';
stog.pop();
stog.pop();
std::cout << "vrh stoga: " << stog.top() << '\n';
stog.pop();
stog.pop();
}
catch (std::exception& e) {
std::cerr << "Greška: " << e.what() << std::endl;
}
return 0;
}
/****************************************************************************************************************************/
Napišite program koji koristi std::unique_ptr za dinamičku alokaciju objekata klase Document. Klasa Document treba sadržavati:
• Metodu void print() koja ispisuje sadržaj dokumenta.
• Konstruktor koji prima string i sprema ga kao sadržaj dokumenta.
U main funkciji napravite vektor dinamički stvorenih objekata, te ih iteracijom kroz vektor ispišite.
/****************************************************************************************************************************/
#include <iostream>
#include <string>
#include <vector>
class Document {
private:
std::string text;
public:
Document(std::string text) : text(text) {}
void print() {
std::cout << "Sadržaj: " << text << '\n';
}
};
int main() {
std::vector<std::unique_ptr<Document>> docs;
docs.push_back(std::make_unique<Document>("Prvi."));
docs.push_back(std::make_unique<Document>("Drugi."));
docs.push_back(std::make_unique<Document>("Treci."));
for (auto& doc : docs) {
doc->print();
}
return 0;
}
/****************************************************************************************************************************/
Napišite klasu Currency koja predstavlja valutu. Klasa treba sadržavati:
• Konstruktor koji prima iznos i oznaku valute.
Preopterećene operatore + i za rad s valutama.
• Metodu convert() koja konvertira valutu u drugu valutu koristeći tečaj.
/****************************************************************************************************************************/
class Currency {
double amount;
std::string valuta;
public:
Currency(double amount, std::string valuta) : amount(amount), valuta(valuta) {}
Currency operator+(Currency& other) {
if (valuta != other.valuta) {
throw std::invalid_argument("Pogrešna valuta.");
}
return Currency(amount + other.amount, valuta);
}
Currency operator-(Currency& other) {
if (valuta != other.valuta) {
throw std::invalid_argument("Pogrešna valuta.");
}
return Currency(amount - other.amount, valuta);
}
Currency convert(const std::string& trazenaValuta, double tecaj) {
return Currency(amount * tecaj, trazenaValuta);
}
void print() {
std::cout << "Imate: " << amount << " " << valuta << '\n';
}
};
int main() {
try {
Currency c1(500, "eura");
Currency c2(200, "dolara");
c1.print();
c2.print();
Currency sum1 = c1 + c2;
Currency diff1 = c1 - c2;
diff1.print();
}
catch (std::exception& e) {
std::cerr << "Greška! " << e.what() << '\n';
}
try {
Currency c1(500, "eura");
Currency c2(200, "kuna");
c1.print();
c2.print();
c2 = c2.convert("eura", 0.13);
c2.print();
Currency sum1 = c1 + c2;
Currency diff1 = c1 - c2;
diff1.print();
}
catch (std::exception& e) {
std::cerr << "Greška! " << e.what() << '\n';
}
return 0;
}
/****************************************************************************************************************************/
Napišite program koji koristi apstraktnu klasu Employee s barem dvije izvedene klase Engineer i Manager. Klasa Employee treba sadržavati:
• Čisto virtualnu metodu double getSalary() koja vraća plaću zaposlenika.
• Metodu void print() koja ispisuje informacije o zaposleniku.
• Engineer ima fiksnu plaću, dok Manager dobiva bonus na osnovnu plaću (postotak).
U main funkciji napravite vektor zaposlenika Employee, pa koristeći upcasting i for each ispišite sve zaposlenike koji imaju plaću veću od nekog iznosa.
/****************************************************************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
class Employ {
protected:
std::string name;
double pay;
public:
Employ(double p, std::string name) : pay(p), name(name) {}
virtual ~Employ() {}
virtual double getSalary() = 0;
void print() {
std::cout << "zaposlenik " << name << '\n';
}
};
class Engineer : public Employ {
public:
Engineer(double p, std::string name) : Employ(p, name) {}
virtual double getSalary() override {
return pay;
}
};
class Manager : public Employ {
private:
double postotak;
public:
Manager(double p, double pos, std::string name) : Employ(p, name), postotak(pos) {}
virtual double getSalary() override {
return pay + pay * postotak /100;
}
};
int main() {
std::vector<Employ*> employes;
employes.push_back(new Engineer(201, "luka"));
employes.push_back(new Manager(200, 15, "marko"));
employes.push_back(new Engineer(100, "tino"));
int iznos = 200;
std::for_each(employes.begin(), employes.end(), [iznos](Employ* e) {
if (e->getSalary() > iznos) {
e->print();
}
});
for (Employ* e : employes) {
delete e;
}
return 0;
}
/****************************************************************************************************************************/
Napišite template klasu Queue koja omogućava rad s redom. Klasa treba sadržavati:
• Metode enqueue, dequeue i front.
• Bacanje iznimke ako se pokušava dohvatiti element iz praznog reda.
/****************************************************************************************************************************/
#include <iostream>
#include <vector>
#include <stdexcept>
template <typename T>
class Queue {
private:
std::vector<T> red;
public:
void enqueue(const T& item) {
red.push_back(item);
}
void dequeue() {
if (red.empty()) {
throw std::out_of_range("Red je prazan.");
}
red.pop_back();
}
T& front() {
if (red.empty()) {
throw std::out_of_range("Red je prazan.");
}
return red.front();
}
void print() {
for (auto r : red) {
std::cout << r;
}
std::cout << '\n';
}
};
int main() {
Queue<int> red;
try {
red.enqueue(1);
red.enqueue(2);
red.enqueue(3);
red.enqueue(4);
red.enqueue(5);
red.print();
red.dequeue();
red.dequeue();
red.print();
std::cout << red.front() << '\n';
red.print();
}
catch (std::exception& e) {
std::cerr << "Greška: " << e.what() << std::endl;
}
}
/****************************************************************************************************************************/
Napišite program koji koristi STL algoritme za učitavanje popisa proizvoda iz datoteke, filtriranje proizvoda s cijenom većom
od određene vrijednosti i spremanje rezultata u novu datoteku koristeći std::copy.
/****************************************************************************************************************************/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
struct Product {
std::string name;
double price;
Product(std::string& name, double price) : name(name), price(price) {}
void print() {
std::cout << "Proizvod: " << name << " cijena " << price << '\n';
}
};
void izDatoteke(std::string& filename, std::vector<Product>& products) {
std::ifstream file(filename);
std::string line;
if (!file.is_open()) {
throw std::ios_base::failure("Datoteka se nije otvorila.");
}
while (std::getline(file, line)) {
std::stringstream ss(line);
std::string name;
double price;
std::getline(ss, name, ',');
ss >> price;
products.push_back(Product(name, price));
}
}
bool filterCijene(Product& product, double priceThresh) {
return product.price > priceThresh;
}
int main() {
std::string filename = "prozvodi.txt";
std::string outputfile = "filter_proizvodi.txt";
std::vector<Product> products;
double priceThresh = 50.0;
try {
izDatoteke(filename, products);
std::vector<Product> filtrirani;
for (auto& prod : products) {
if (prod.price > priceThresh) {
filtrirani.push_back(prod);
}
}
std::ofstream outputfile(outputfile);
if (!outputfile.is_open())
{
throw std::ios_base::failure("Datoteka se nije otvorila.");
}
for (auto prod : filtrirani) {
outputfile << prod.name << "," << prod.price << std::endl;
}
std::cout << "Proizvodi su spremljeni." << std::endl;
}
catch(std::exception& e){
std::cerr << "Greška! : " << e.what() << std::endl;
}
return 0;
}
/****************************************************************************************************************************/
Napišite klasu Fraction koja predstavlja razlomak. Klasa treba sadržavati:
• Konstruktor s parametrima.
• operator+ kao funkciju član.
• operator<< kao globalnu funkciju za ispis razlomka u obliku brojnik/nazivnik.
• Metodu za skraćivanje razlomka.
/****************************************************************************************************************************/
#include <iostream>
#include <cmath>
class Fraction {
private:
int brojnik;
int nazivnik;
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
void skrati() {
int djelitelj = gcd(abs(brojnik), abs(nazivnik));
brojnik /= djelitelj;
nazivnik /= djelitelj;
if (nazivnik < 0) {
brojnik = -brojnik;
nazivnik = -nazivnik;
}
}
public:
Fraction(int broj, int naz) : brojnik(broj), nazivnik(naz) {
if (nazivnik == 0) {
throw std::invalid_argument("Nazivnik nemože biti nula.");
}
skrati();
}
Fraction operator+(Fraction& other) {
int zajdNaz = nazivnik * other.nazivnik;
int noviBroj = brojnik * other.nazivnik + other.brojnik * nazivnik;
Fraction result(noviBroj, zajdNaz);
result.skrati();
return result;
}
friend std::ostream& operator<<(std::ostream& os, Fraction& f) {
os << f.brojnik << "/" << f.nazivnik;
return os;
}
};
int main() {
try {
Fraction f1(4, 6);
Fraction f2(2, 3);
Fraction sum = f1 + f2;
std::cout << "Zbroj razlomaka: " << sum << '\n';
}
catch (std::exception& e) {
std::cerr << "Greska!: " << e.what() << '\n';
}
return 0;
}
/****************************************************************************************************************************/
Koristeći funkcije standardne biblioteke, implementirajte sljedeće funkcije:
Funkciju koja koristi std::copy za učitavanje stringova iz tekstualne datoteke u vektor.
Funkciju koja koristi std::remove_if za izbacivanje riječi kraćih od 3 znaka.
Funkciju koja koristi std:: transform za konverziju svih slova u mala.
Funkciju koja koristi std::copy za spremanje preostalih riječi u novu datoteku.
/****************************************************************************************************************************/
// datoteka -> Hello world this is a test example
with various words in it.
//output datoteka -> hello
world
this
test
example
with
various
words
it.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
void load(const std::string& filename, std::vector<std::string>& words) {
std::ifstream file(filename);
std::istream_iterator<std::string> start(file), end;
std::copy(start, end, std::back_inserter(words));
}
void remove(std::vector<std::string>& words) {
words.erase(std::remove_if(words.begin(), words.end(), [](std::string& word) {
return word.size() < 3;
}), words.end());
}
void convert(std::vector<std::string>& words) {
std::transform(words.begin(), words.end(), words.begin(), [](std::string& word) {
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
return word;
});
}
void save(const std::string& filename, std::vector<std::string>& words) {
std::ofstream file(filename);
std::copy(words.begin(), words.end(), std::ostream_iterator<std::string>(file, "\n"));
}
int main() {
std::vector<std::string> words;
load("text.txt", words);
remove(words);
convert(words);
save("output.txt", words);
return 0;
}
/****************************************************************************************************************************/
Napišite apstraktnu klasu Measurement koja predstavlja mjerenje. Izvedite dvije klase Temperature i Distance koje omogućuju:
Konverziju između različitih jedinica (npr. Celzijus-Fahrenheit, kilometar-metar).
• Ispis mjerenja pomoću operator<<.
• Virtualnu metodu convertToBaseUnit() koja vraća vrijednost u osnovnoj jedinici.
/****************************************************************************************************************************/
#include <iostream>
#include <cmath>
class Measurment {
protected:
double value;
public:
Measurment(double value) : value(value) {}
virtual ~Measurment() {}
virtual double convertToBaseUnit() = 0;
friend std::ostream& operator<<(std::ostream& os, Measurment& m) {
m.print(os);
return os;
}
protected:
virtual void print(std::ostream& os) = 0;
};
class Temperatue : public Measurment {
public:
enum Unit {Clesius, Fahrenheit};
private:
Unit unit;
public:
Temperatue(double v, Unit u) : Measurment(v), unit(u) {}
double convertToBaseUnit() override {
return (unit == Fahrenheit) ? (value - 32) * 5.0 / 9.0 : value;
}
protected:
void print(std::ostream& os) override {
os << value << (unit == Clesius ? "°C" : "°F");
}
};
class Distance : public Measurment {
public:
enum Unit {Kilometar, Metar};
private:
Unit unit;
public:
Distance(double v, Unit u) : Measurment(v), unit(u) {}
double convertToBaseUnit() override {
return (unit == Kilometar) ? value * 1000 : value;
}
protected:
void print(std::ostream& os) override{
os << value << (unit == Metar ? "m" : "km");
}
};
int main() {
Temperatue t1(15, Temperatue::Clesius);
Temperatue t2(212, Temperatue::Fahrenheit);
Distance d1(5, Distance::Kilometar);
Distance d2(500, Distance::Metar);
std::cout << "Temperature 1: " << t1 << " (Osnovna jedinica: " << t1.convertToBaseUnit() << " °C)\n";
std::cout << "Temperature 2: " << t2 << " (Osnovna jedinica: " << t2.convertToBaseUnit() << " °C)\n";
std::cout << "Distance 1: " << d1 << " (Osnovna jedinica: " << d1.convertToBaseUnit() << " m)\n";
std::cout << "Distance 2: " << d2 << " (Osnovna jedinica: " << d2.convertToBaseUnit() << " m)\n";
return 0;
}
/****************************************************************************************************************************/
Napišite template klasu Calculator koja omogućava izvođenje osnovnih matematičkih operacija (+, -, *, /) nad različitim tipovima podataka. Klasa treba sadržavati:
• Metode add, subtract, multiply i divide.
• Metoda divide treba baciti iznimku ako je dijeljenje s nulom
/****************************************************************************************************************************/
#include <iostream>
#include <cmath>
template <typename T>
class Calculator {
public:
T add(T a, T b) { return a + b; }
T divide(T a, T b) {
if (b == 0) {
throw std::invalid_argument("Nemože se dijeliti s nulom.");
}
return a / b;
}
T subtract(T a, T b) { return a - b; }
T multiply(T a, T b) { return a * b; }
};
int main() {
try {
Calculator<int> calc;
std::cout << "Sum: " << calc.add(5, 5) << '\n';
std::cout << "Diff: " << calc.subtract(12, 6) << '\n';
std::cout << "Prod: " << calc.multiply(2, 3) << '\n';
std::cout << "Quot: " << calc.divide(6, 2) << '\n';
calc.divide(6, 0);
}
catch (std::exception& e) {
std::cerr << "Greska! " << e.what() << '\n';
}
return 0;
}
/****************************************************************************************************************************/Editor is loading...
Leave a Comment