consolidate all repos to one for archive

This commit is contained in:
2025-01-28 13:46:42 +01:00
commit a6610fbc7a
5350 changed files with 2705721 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
.idea/*
cmake-build-debug/*
CMakeLists.txt

View File

@@ -0,0 +1 @@
Navodila v folder navodila

View File

@@ -0,0 +1,172 @@
#include <iostream>
#include <ctime>
using namespace std;
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int partition(int A[], int low, int high) {
int m = (low + high) / 2;
swap(&A[low], &A[m]);
int w = A[low];
int i = low;
int j = high;
while (j > i) {
while (A[j] >= w && j > low) j = j - 1;
while (A[i] <= w && i < high) i = i + 1;
if (i < j) {
swap(&A[i], &A[j]);
}
}
swap(&A[low], &A[j]);
return j;
}
void quickSort(int A[], int low, int high) {
if (low < high) {
int j = partition(A, low, high);
quickSort(A, low, j - 1);
quickSort(A, j + 1, high);
}
}
void bubbleSort(int A[], int length) {
int rep = 0;
for (int i = 0; i < length; i++) {
if (rep == 10000) {
cout << ".";
rep = 0;
}
rep++;
for (int j = 0; j < length - i - 1; j++) {
if (A[j] < A[j + 1]) swap(&A[j], &A[j + 1]);
}
}
cout << "\n";
}
void fillArray(int A[], int length) {
for (int i = 0; i < length; i++) {
A[i] = rand();
}
}
void highToLow(int A[], int length) {
int base = length;
for (int i = 0; i < length; i++) {
A[i] = base;
base--;
}
}
void print(int A[], int length) {
for (int i = 0; i < length; i++) {
cout << A[i] << " ";
}
cout << endl;
}
bool check(const int A[], int length) {
for (int i = 0; i < length - 1; i++) {
if (A[i] > A[i + 1]) {
return false;
}
}
return true;
}
void menu() {
cout << "1 ... Generate random sequence \n"
"2 ... Generate sorted rising sequence \n"
"3 ... Generate sorted falling sequence \n"
"4 ... Print sequence \n"
"5 ... quickSort sequence \n"
"6 ... bobbleSort sequence \n"
"7 ... Check sequence \n"
"0 ... End \n \n"
"Your choice: ";
}
int main() {
int length;
// int A[length];
int *A = NULL;
int low = 0;
int high = length - 1;
srand(time(nullptr));
bool running = true;
int selection;
clock_t start, finish;
double duration;
do {
menu();
cin >> selection;
cout << endl;
switch (selection) {
case 1:
cout << "dolzina";
cin >> length;
high = length - 1;
A = new int[length];
fillArray(A, length);
break;
case 2:
cout << "dolzina";
cin >> length;
high = length - 1;
A = new int[length];
fillArray(A, length);
quickSort(A, low, high);
break;
case 3:
cout << "dolzina";
cin >> length;
high = length - 1;
A = new int[length];
highToLow(A, length);
break;
case 4:
print(A, length);
break;
case 5:
start = clock();
quickSort(A, low, high);
finish = clock();
duration = (double) (finish - start) / CLOCKS_PER_SEC;
cout << "Time to sort " << duration << "\n";
break;
case 6:
start = clock();
bubbleSort(A, length);
finish = clock();
duration = (double) (finish - start) / CLOCKS_PER_SEC;
cout << "Time to sort " << duration << "\n";
break;
case 7:
if (check(A, length)) {
cout << "Sequence is sorted \n";
} else {
cout << "Sequence isn't sorted \n";
}
break;
case 0:
running = false;
break;
default:
cout << "Wrong selection!" << endl;
break;
}
cout << endl;
} while (running);
delete[]A;
return 0;
}

View File

@@ -0,0 +1,3 @@
.idea/*
cmake-build-debug/*
CMakeLists.txt

View File

@@ -0,0 +1 @@
Navodila v folder navodila

View File

@@ -0,0 +1,140 @@
#include <iostream>
#include <stack>
#include <fstream>
#define NEPREGLEDANO 0
#define V_OBDELAVI 1
#define RAZVITO 2
struct Vozlisce {
int predhodnik;
int dolzina;
int status;
int indeks;
};
void ISKANJE_V_GLOBINO(int s, int size, const int G[], Vozlisce vozl[]) {
std::stack<int> stack;
for (int i = 0; i < size; i++) {
vozl[i].status = NEPREGLEDANO;
vozl[i].dolzina = -1;
vozl[i].predhodnik = -1;
vozl[i].indeks = i;
}
vozl[s].status = V_OBDELAVI;
vozl[s].dolzina = 0;
vozl[s].predhodnik = -1;
stack.push(s); // vstavi na vrh sklada
while (!stack.empty()) {
int v = stack.top();//vzemi vozlišče iz vrha sklada
stack.pop();
for (int i = 0; i < size; ++i) {
if (G[size * v + i] == 1) {
if (vozl[i].status == NEPREGLEDANO) {
vozl[i].status = V_OBDELAVI;
vozl[i].dolzina = vozl[v].dolzina + 1;
vozl[i].predhodnik = vozl[v].indeks;
stack.push(i); // vstavi na vrh sklada
}
}
}
vozl[v].status = RAZVITO;
}
}
void IZPIS_POTI(int iskano, Vozlisce vozl[]) {
if (vozl[iskano].predhodnik == -1) {
std::cout << vozl[iskano].indeks << " ";
return;
} else {
IZPIS_POTI(vozl[iskano].predhodnik, vozl);
std::cout << vozl[iskano].indeks << " ";
}
}
void IZPIS_SEZNAMA(Vozlisce vozl[], int size) {
for (int i = 0; i < size; i++) {
std::cout << "indeks: " << vozl[i].indeks << "\n";
std::cout << "predhodnik: " << vozl[i].predhodnik << "\n";
std::cout << "dolzina: " << vozl[i].dolzina << "\n\n";
}
}
void menu() {
std::cout << "Iskanje v globino:\n"
"1) Preberi graf\n"
"2) Pozeni iskanje iz vozlisca s\n"
"3) Izpis seznama vozlisc in njihovih podatkov\n"
"4) Izpis poti med vozliscema s in d\n"
"0) Konec\n"
"Vasa izbira: ";
}
int main() {
std::cout << "Hello, World!" << std::endl;
Vozlisce *vozl;
int *G;
int stevilo_vozlisc;
int stevilo_povezav;
std::ifstream f("graf_big.txt");
f >> stevilo_vozlisc;
f >> stevilo_povezav;
vozl = new Vozlisce[stevilo_vozlisc];
G = new int[stevilo_vozlisc * stevilo_vozlisc];
for (int i = 0; i < stevilo_povezav; ++i) {
int v1, v2, cena;
f >> v1 >> v2 >> cena;
v1 = v1 - 1;
v2 = v2 - 1;
G[stevilo_vozlisc * v1 + v2] = 1;
G[stevilo_vozlisc * v2 + v1] = 1;
}
bool running = true;
int selection;
int izbira;
do {
menu();
std::cin >> selection;
std::cout << std::endl;
switch (selection) {
case 1:
std::cout << "Prebrano\n"
"St.vozlisc: " << stevilo_vozlisc << " St.povezav: " << stevilo_povezav << "\n";
break;
case 2:
std::cout << "Izberi vozlisce: ";
std::cin >> izbira;
ISKANJE_V_GLOBINO(izbira, stevilo_vozlisc, G, vozl);
break;
case 3:
IZPIS_SEZNAMA(vozl, stevilo_vozlisc);
break;
case 4:
std::cout << "Izberi vozlisce: ";
std::cin >> izbira;
IZPIS_POTI(izbira, vozl);
break;
case 0:
running = false;
break;
default:
std::cout << "Wrong selection!\n";
break;
}
std::cout << std::endl;
} while (running);
delete[] vozl;
delete[] G;
return 0;
}

View File

@@ -0,0 +1,19 @@
#include <fstream>
using namespace std;
int main(){
....
ifstream f("graf.txt"); // odpremo datoteko (v isti mapi, kot je projekt oziroma exe datoteka)
// če so težave z branjem, zapišite absolutno pot, npr: ifstream f("c:/graf.txt");
f >> stevilo_vozlisc; // v prvi vrstici datoteke imamo stevilo vozlisc
f >> stevilo_povezav; // v drugi vrstici datoteke imamo stevilo povezav oziroma vrstic v nadaljevnaju datoteke
ustvari matriko sosednosti C (2D polje); // priporočamo dinamično alokacijo
ustvari polje V in dodaj vozlišča v V, kjer je ime=indeks;
for(i to stevilo_povezav....){
f >> v1 >> v2 >> cena; // v vsaki vrstici beremo 3 podatke (stevilke)
v matriki C označi vozlišči v1 in v2 kot soseda
}
...
}

View File

@@ -0,0 +1,3 @@
.idea/*
cmake-build-debug/*
CMakeLists.txt

View File

@@ -0,0 +1,41 @@
//
// Created by Nik on 16/04/2022.
//
#include "QuickSort.h"
void QuickSort::swap(Povezava *a, Povezava *b) {
Povezava t = *a;
*a = *b;
*b = t;
}
int QuickSort::partition(Povezava *A, int low, int high) {
int m = (low + high) / 2;
swap(&A[low], &A[m]);
int w = A[low].cost;
int i = low;
int j = high;
while (j > i) {
while (A[j].cost >= w && j > low) j = j - 1;
while (A[i].cost <= w && i < high) i = i + 1;
if (i < j) {
swap(&A[i], &A[j]);
}
}
swap(&A[low], &A[j]);
return j;
}
void QuickSort::quickSort(Povezava *A, int low, int high) {
if (low < high) {
int j = partition(A, low, high);
quickSort(A, low, j - 1);
quickSort(A, j + 1, high);
}
}

View File

@@ -0,0 +1,24 @@
//
// Created by Nik on 16/04/2022.
//
#ifndef NALOGA_3_QUICKSORT_H
#define NALOGA_3_QUICKSORT_H
struct Povezava{
int p, q, cost;
};
class QuickSort {
private:
QuickSort() = default;
public:
static void swap(Povezava *a, Povezava *b);
static int partition(Povezava A[], int low, int high);
static void quickSort(Povezava A[], int low, int high);
};
#endif //NALOGA_3_QUICKSORT_H

View File

@@ -0,0 +1 @@
Navodila v folder navodila

View File

@@ -0,0 +1,169 @@
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "QuickSort.h"
bool pogoj1(const int *S, const Povezava pov) {
if (S[pov.p] == 0 or S[pov.q] == 0) return false;
return (S[pov.p] == S[pov.q]);
}
bool pogoj2(const int *S, const Povezava pov) {
if (S[pov.p] == 0 or S[pov.q] == 0) return false;
return (S[pov.p] != S[pov.q]);
}
bool pogoj3(const int *S, const Povezava pov) {
if (S[pov.p] != 0 and S[pov.q] == 0) return true;
if (S[pov.q] != 0 and S[pov.p] == 0) return true;
return false;
}
bool pogoj4(const int *S, const Povezava pov) {
return (S[pov.p] == 0 and S[pov.q] == 0);
}
void Kruskal(Povezava *P, Povezava *R, int st_vozlisc, int st_povezav) {
QuickSort::quickSort(P, 0, st_povezav - 1);
int S[st_vozlisc + 1];
for (int i = 0; i < st_vozlisc + 1; i++) S[i] = 0;
bool koncano = false;
int i = 0;
int st_sprej_pov = 0;
int stevilo_mnozic = 1;
while (!koncano) {
if (!pogoj1(S, P[i])) {
if (pogoj2(S, P[i])) {
R[st_sprej_pov] = P[i];
st_sprej_pov = st_sprej_pov + 1;
int pridruzujem = S[P[i].p];
int odstranjujem = S[P[i].q];
for (int l = 0; l < st_vozlisc; l++) {
if (S[l] == odstranjujem) S[l] = pridruzujem;
}
} else if (pogoj3(S, P[i])) {
R[st_sprej_pov] = P[i];
st_sprej_pov = st_sprej_pov + 1;
if (S[P[i].p] != 0 and S[P[i].q] == 0) S[P[i].q] = S[P[i].p];
if (S[P[i].q] != 0 and S[P[i].p] == 0) S[P[i].p] = S[P[i].q];
} else if (pogoj4(S, P[i])) {
R[st_sprej_pov] = P[i];
st_sprej_pov = st_sprej_pov + 1;
S[P[i].p] = stevilo_mnozic;
S[P[i].q] = stevilo_mnozic;
stevilo_mnozic += 1;
}
}
if (st_sprej_pov == st_vozlisc - 1) {
koncano = true;
} else {
i = i + 1;
}
}
}
void nafilajRand(Povezava *&P, Povezava *&R, int st_vozlisc, int &st_povezav){
st_povezav = (st_vozlisc * (st_vozlisc - 1)) / 2;
P = new Povezava[st_povezav];
R = new Povezava[st_vozlisc - 1];
int k = 0;
for(int i = 1; i <= st_vozlisc; i++){
for(int j = 1 + i; j <= st_vozlisc; j++){
if(i != j) {
P[k].p = i;
P[k].q = j;
P[k].cost = rand();
k++;
}
}
}
std::cout << "Vozlisca: " << st_vozlisc << " Povezave: " << st_povezav << "\n";
}
void beri(Povezava *&P, Povezava *&R, int &st_vozlisc, int &st_povezav){
std::ifstream f("graf2.txt");
f >> st_vozlisc;
f >> st_povezav;
P = new Povezava[st_povezav];
R = new Povezava[st_vozlisc - 1];
for (int i = 0; i < st_povezav; ++i) {
int v1, v2, cena;
f >> v1 >> v2 >> cena;
P[i].p = v1;
P[i].q = v2;
P[i].cost = cena;
}
f.close();
std::cout << "Vozlisca: " << st_vozlisc << " Povezave: " << st_povezav << "\n";
}
void menu() {
std::cout << "Kruskalov algoritem - izbira: \n"
"1 Preberi graf iz datoteke \n"
"2 Generiraj nakljucni graf \n"
"3 Pozeni \n"
"4 Izpis sprejetih povezav \n"
"0 Konec \n\n"
"Vasa izbira: ";
}
int main() {
std::cout << "Hello, World!" << std::endl;
int st_povezav, st_vozlisc;
clock_t start,finish;
double duration;
Povezava *R;
Povezava *P;
bool running = true;
while (running) {
menu();
int coich;
std::cin >> coich;
switch (coich) {
case 1:
beri(P,R,st_vozlisc,st_povezav);
break;
case 2:
do{
std::cout << "stevilo vozlisc: ";
std::cin >> st_vozlisc;
}while(st_vozlisc > 1500);
nafilajRand(P, R, st_vozlisc, st_povezav);
break;
case 3:
start = clock();
Kruskal(P, R, st_vozlisc, st_povezav);
finish = clock();
duration = (double) (finish - start) / CLOCKS_PER_SEC;
std::cout << "Time to sort " << duration << "\n";
break;
case 4:
for (int i = 0; i < st_vozlisc - 1; i++) {
std::cout << R[i].p << " " << R[i].q << " " << R[i].cost << "\n";
}
break;
case 0:
running = false;
break;
default:
std::cout << " Napaka\n";
break;
}
}
return 0;
}

View File

@@ -0,0 +1,15 @@
8
13
1 2 1
2 3 2
4 6 2
3 5 3
7 8 3
1 3 4
5 6 4
2 5 5
3 4 7
4 5 10
6 7 13
6 8 15
4 7 20

View File

@@ -0,0 +1,21 @@
10
19
1 2 33
1 3 10
1 4 56
2 4 13
2 5 21
3 4 23
3 6 24
3 7 65
4 5 51
4 7 20
5 7 17
5 8 35
6 7 40
6 9 72
7 8 99
7 9 45
7 10 42
8 10 38
9 10 83

View File

@@ -0,0 +1,15 @@
8
13
7 4 20
8 6 15
7 6 13
5 4 10
4 3 7
5 2 5
3 1 4
6 5 4
5 3 3
8 7 3
3 2 2
6 4 2
2 1 1

View File

@@ -0,0 +1,29 @@
#include <fstream>
using namespace std;
int main(){
....
ifstream f("graf.txt"); // odpremo datoteko (v isti mapi, kot je projekt oziroma exe datoteka)
// če so težave z branjem, zapišite absolutno pot, npr: ifstream f("c:/graf.txt");
f >> stevilo_vozlisc; // v prvi vrstici datoteke imamo stevilo vozlisc
f >> stevilo_povezav; // v drugi vrstici datoteke imamo stevilo povezav oziroma vrstic v nadaljevnaju datoteke
ustvari matriko sosednosti C (2D polje); // priporočamo dinamično alokacijo
for(i to stevilo_povezav....){
f >> v1 >> v2 >> cena; // v vsaki vrstici beremo 3 podatke (stevilke)
v matriki C označi vozlišči v1 in v2 kot soseda in vpiši ceno povezave
}
ustvari polje povezav P; // priporočamo dinamično alokacijo
for(p to stevilo_vozlisc....) { // preiščemo zgornji trikotni del matrike C
for (q=p+1 to stevilo_vozlisc....) {
if (C[p][q] != infinity) {
cena = C[p][q];
ustvari objekt tipa Povezava in mu določi atribute p, q in cena;
shrani objekt v polje P;
}
}
}
...
}

View File

@@ -0,0 +1,3 @@
.idea/*
cmake-build-debug/*
CMakeLists.txt

View File

@@ -0,0 +1 @@
Navodila v folder navodila

View File

@@ -0,0 +1,145 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
struct vozlisce {
int vez, predhodnik, cena;
};
int izlociNajmanjsega(std::vector<int> &voz, const std::vector<vozlisce> &rez) {
int tmp = voz[0];
int rem_voz = 0;
for (int i = 0; i < voz.size(); ++i) {
if (rez[tmp].cena > rez[voz[i]].cena) {
tmp = rez[voz[i]].vez;
rem_voz = i;
}
}
if(rem_voz == 0){
voz.erase(voz.begin());
}else {
voz.erase(voz.begin() + rem_voz);
}
return tmp;
}
void dijkstrovAlgoritem(int **&G, int s, const int st_vozlisc, std::vector<vozlisce> &rez) {
std::vector<int> Q;
for (int i = 0; i < st_vozlisc; ++i) {
vozlisce tmp;
tmp.vez = i;
tmp.predhodnik = -1;
tmp.cena = INT32_MAX;
Q.push_back(i);
rez.push_back(tmp);
}
rez[s].cena = 0;
int preg_voz = 0;
while (!Q.empty()) {
preg_voz = izlociNajmanjsega(Q, rez);
for (int i = 0; i < st_vozlisc; i++) {
if (G[preg_voz][i] > 0) {
if(rez[i].cena > rez[preg_voz].cena + G[preg_voz][i]){
rez[i].cena = rez[preg_voz].cena + G[preg_voz][i];
rez[i].predhodnik = preg_voz;
}
}
}
}
}
void izpisPoti(const std::vector<vozlisce> &rez, const int s, int v){
if(s == rez[v].vez){
std::cout << rez[v].vez << " cena: " << rez[v].cena << "\n";
return;
}else{
izpisPoti(rez,s,rez[v].predhodnik);
std::cout << rez[v].vez << " cena: " << rez[v].cena << "\n";
return;
}
}
void beri(int **&G, int &st_vozlisc) {
int st_povezav;
std::ifstream f("graf.txt");
f >> st_vozlisc;
f >> st_povezav;
int v1, v2, cena;
G = new int *[st_vozlisc];
for (int i = 0; i < st_vozlisc; i++) {
G[i] = new int[st_vozlisc];
}
for (int i = 0; i < st_vozlisc; i++) {
for (int j = 0; j < st_vozlisc; j++) {
G[i][j] = 0;
}
}
for (int i = 0; i < st_povezav; i++) {
f >> v1 >> v2 >> cena;
G[v1 - 1][v2 - 1] = cena;
G[v2 - 1][v1 - 1] = cena;
}
std::cout << "Vozlisca: " << st_vozlisc << " Povezave: " << st_povezav << "\n";
// for (int i = 0; i < st_vozlisc; ++i) {
// for (int j = 0; j < st_vozlisc; ++j) {
// std::cout << G[j][i] << " ";
// }
// std::cout << "\n";
// }
}
void menu(){
std::cout << "Dijkstrov algoritem - izbira:\n"
"1 Nalozi graf\n"
"2 Zagon algoritma\n"
"3 Izpis najkrajse poti\n"
"0 Konec\n\n"
"Vasa izbira: ";
}
int main() {
std::cout << "Hello, World!" << std::endl;
int **G;
int st_vozlisc = 0;
std::vector<vozlisce> rez;
bool runing = true;
int condition;
int tmp, iskanoVozlisce;
while(runing){
menu();
std::cin >> condition;
switch (condition) {
case 1:
beri(G, st_vozlisc);
break;
case 2:
std::cout << "Iskano vozlisce: ";
std::cin >> iskanoVozlisce;
dijkstrovAlgoritem(G, iskanoVozlisce, st_vozlisc, rez);
break;
case 3:
// for (int i = 0; i < rez.size(); ++i) {
// std::cout << rez[i].vez << " " << rez[i].predhodnik << " " << rez[i].cena << "\n";
// }
std::cout << "Izberi vozlisce: ";
std::cin >> tmp;
izpisPoti(rez,iskanoVozlisce, tmp);
break;
case 0:
runing = false;
break;
}
}
return 0;
}

View File

@@ -0,0 +1,14 @@
8
12
1 2 3
1 3 6
1 8 1
2 3 2
2 8 2
3 4 1
4 5 6
4 6 3
4 7 4
5 6 3
6 7 2
7 8 10

View File

@@ -0,0 +1,3 @@
.idea/*
cmake-build-debug/*
CMakeLists.txt

View File

@@ -0,0 +1 @@
Navodila v folder navodila

View File

@@ -0,0 +1,139 @@
#include <iostream>
#include <fstream>
void FLOYD_WARSHALL(int **&C, int **&D, int **&PI, int st_vozlisc) {
D = new int *[st_vozlisc];
PI = new int *[st_vozlisc];
for (int i = 0; i < st_vozlisc; i++) {
PI[i] = new int[st_vozlisc];
D[i] = new int[st_vozlisc];
}
for (int i = 0; i < st_vozlisc; i++) {
for (int j = 0; j < st_vozlisc; j++) {
D[i][j] = C[i][j];
if (i != j && C[i][j] != INT16_MAX) { //inf
PI[i][j] = i;
} else {
PI[i][j] = INT16_MAX; //nill
}
}
}
for (int k = 0; k < st_vozlisc; k++) {
for (int i = 0; i < st_vozlisc; i++) {
for (int j = 0; j < st_vozlisc; j++) {
if (D[i][j] > D[i][k] + D[k][j]) {
D[i][j] = D[i][k] + D[k][j];
PI[i][j] = PI[k][j];
}
}
}
}
}
void beri(int **&C, int &st_vozlisc) {
int st_povezav;
std::ifstream f("graf3.txt");
f >> st_vozlisc;
f >> st_povezav;
int v1, v2, cena;
C = new int *[st_vozlisc];
for (int i = 0; i < st_vozlisc; i++) {
C[i] = new int[st_vozlisc];
}
for (int i = 0; i < st_vozlisc; i++) {
for (int j = 0; j < st_vozlisc; j++) {
if (i == j) {
C[i][j] = 0;
} else {
C[i][j] = INT16_MAX; //inf
}
}
}
for (int i = 0; i < st_povezav; i++) {
f >> v1 >> v2 >> cena;
C[v1 - 1][v2 - 1] = cena;
}
std::cout << "Vozlisca: " << st_vozlisc << " Povezave: " << st_povezav << "\n";
}
void izpisPoti(int **&PI, int s, int g) {
if (s == g) {
std::cout << s << " ";
} else {
if (PI[s][g] == INT16_MAX) { //inf
std::cout << "med " << s << " in " << g << " ni poti";
} else {
izpisPoti(PI, s, PI[s][g]);
std::cout << g << " ";
}
}
}
void menu() {
std::cout << "Dijkstrov algoritem - izbira:\n"
"1 Nalozi graf\n"
"2 Zagon algoritma\n"
"3 Izpis najkrajse poti\n"
"0 Konec\n\n"
"Vasa izbira: ";
}
int main() {
std::cout << "Hello, World!" << std::endl;
int **C;
int **D;
int **PI;
int st_vozlisc = 0;
bool runing = true;
int condition;
int s, g;
bool func = false;
while (runing) {
menu();
std::cin >> condition;
switch (condition) {
case 1:
beri(C, st_vozlisc);
break;
case 2:
FLOYD_WARSHALL(C, D, PI, st_vozlisc);
func = true;
break;
case 3:
if (func) {
do {
std::cout << "Izberi s:";
std::cin >> s;
} while (s > st_vozlisc);
do {
std::cout << "Izberi g:";
std::cin >> g;
}while(g > st_vozlisc);
std::cout << "Pot: ";
izpisPoti(PI, s, g);
std::cout << "\n Cena: " << D[s][g];
std::cout << "\n";
}
break;
case 0:
runing = false;
break;
}
}
return 0;
}

View File

@@ -0,0 +1,8 @@
3
6
1 2 10
1 3 4
2 3 3
2 1 5
3 1 8
3 2 2

View File

@@ -0,0 +1,8 @@
4
6
1 2 8
1 4 1
2 3 1
3 1 4
4 2 2
4 3 9

View File

@@ -0,0 +1,11 @@
5
9
1 2 3
1 3 8
1 5 -4
2 4 1
2 5 7
3 2 4
4 1 2
4 3 -5
5 4 6

View File

@@ -0,0 +1,3 @@
.idea/*
cmake-build-debug/*
CMakeLists.txt

View File

@@ -0,0 +1 @@
Navodila v folder navodila

View File

@@ -0,0 +1,159 @@
#include <iostream>
#include <fstream>
#include <vector>
const int inf = 9999;
struct struktura {
std::vector<int> vozlisca;
int dolzina;
bool isInPot(const int p){
for (int i = 0; i < vozlisca.size(); ++i) {
if(vozlisca[i] == p) return true;
}
return false;
}
};
struktura vzemiIzVrste(std::vector<struktura> &Q) {
struktura tmp;
int index = 0;
for (int i = 0; i < Q.size(); ++i) {
if(Q[index].dolzina > Q[i].dolzina){
index = i;
}
}
auto it = Q.begin();
it += index;
tmp = Q[index];
Q.erase(it);
return tmp;
}
void razvejiInOmeji(int **&G, const int s, const int g, std::vector<std::vector<int>>& R, const int stVozlisc , int& dolzina) {
std::vector<struktura> Q;
struktura pot;
pot.vozlisca.push_back(s);
pot.dolzina = 0;
dolzina = inf;
Q.push_back(pot);
struktura p;
struktura p1;
int u;
while (!Q.empty()) {
p = vzemiIzVrste(Q);
u = p.vozlisca.back();
for (int v = 0; v < stVozlisc; ++v) {
if (G[u][v] != inf) {
if(!p.isInPot(v)){
p1 = p;
p1.vozlisca.push_back(v);
p1.dolzina += G[u][v];
if(v == g){
if(p1.dolzina < dolzina){
R.clear();
R.push_back(p1.vozlisca);
dolzina = p1.dolzina;
}else if(p1.dolzina == dolzina){
R.push_back(p1.vozlisca);
}
}else if(p.dolzina < dolzina){
Q.push_back(p1);
}
}
}
}
}
}
void izpis(const std::vector<std::vector<int>>& R, int dolzina){
for (int i = 0; i < R.size(); ++i) {
for (int j = 0; j < R[i].size(); ++j) {
std::cout << R[i][j] << "->";
}
}
std::cout << ", cena = " << dolzina << "\n";
}
void beri(int**& C, int& stVozlisc) {
int stPovezav;
std::ifstream f("graf2.txt");
f >> stVozlisc;
f >> stPovezav;
int v1, v2, cena;
C = new int *[stVozlisc];
for (int i = 0; i < stVozlisc; i++) {
C[i] = new int[stVozlisc];
}
for (int i = 0; i < stVozlisc; ++i) {
for (int j = 0; j < stVozlisc; ++j) {
C[i][j] = inf;
}
}
for (int i = 0; i < stPovezav; i++) {
f >> v1 >> v2 >> cena;
C[v1 - 1][v2 - 1] = cena;
}
std::cout << "Vozlisca: " << stVozlisc << " Povezave: " << stPovezav << "\n";
// for (int i = 0; i < stVozlisc; ++i)
// {
// for (int j = 0; j < stVozlisc; ++j)
// {
// std::cout << C[i][j] << " ";
// }
// std::cout << "\n";
// }
}
void menu(){
std::cout << "Razveji in omeji - izbira:\n"
"1 Preberi podatke iz datoteke\n"
"2 Iskanje poti med vozliscema s in g\n"
"3 Konec\n"
"Vasa izbira:";
}
int main() {
int **C;
int stVozlisc;
std::vector<std::vector<int>> R;
int dolzina;
int izbira, start, end;
bool running = true;
while (running){
menu();
std::cin >> izbira;
switch (izbira) {
case 1:
beri(C, stVozlisc);
break;
case 2:
std::cout << "Izberi s: ";
std::cin >> start;
std::cout << "Izberi end: ";
std::cin >> end;
razvejiInOmeji(C,start,end,R,stVozlisc, dolzina);
izpis(R, dolzina);
break;
default:
running = false;
break;
}
}
return 0;
}

View File

@@ -0,0 +1,24 @@
8
22
1 2 3
1 8 4
2 1 3
2 8 5
2 3 9
3 2 9
3 4 4
3 7 5
4 3 4
4 5 2
5 4 2
5 6 3
5 7 8
6 5 3
6 7 4
7 3 5
7 5 8
7 6 4
7 8 2
8 1 4
8 2 5
8 7 2

View File

@@ -0,0 +1,58 @@
8
56
1 2 18
1 3 18
1 4 38
1 5 6
1 6 49
1 7 7
1 8 48
2 1 20
2 3 38
2 4 28
2 5 43
2 6 28
2 7 25
2 8 42
3 1 40
3 2 7
3 4 21
3 5 48
3 6 24
3 7 23
3 8 15
4 1 27
4 2 5
4 3 39
4 5 27
4 6 50
4 7 44
4 8 35
5 1 7
5 2 25
5 3 11
5 4 30
5 6 16
5 7 24
5 8 48
6 1 19
6 2 5
6 3 49
6 4 43
6 5 1
6 7 24
6 8 4
7 1 10
7 2 25
7 3 41
7 4 34
7 5 42
7 6 33
7 7 7
8 1 17
8 2 23
8 3 47
8 4 4
8 5 44
8 6 42
8 7 24

View File

@@ -0,0 +1,3 @@
.idea/*
cmake-build-debug/*
CMakeLists.txt

View File

@@ -0,0 +1 @@
Navodila v folder navodila

View File

@@ -0,0 +1,131 @@
#include <iostream>
#include <string>
#include <list>
using namespace std;
struct Student {
string Ime;
string Priimek;
string IDUM; };
// prototipi
void izpisi_seznam(list<Student>);
void izpisi_studenta(Student);
void vstavi(list<Student> &, int, Student);
Student vrni(list<Student>, int);
void vrini(list<Student> &, int, Student);
Student odstrani(list<Student> &, int);
void izpisi_seznam(list<Student> seznam)
{
for (list<Student>::iterator it = seznam.begin(); it != seznam.end(); ++it)
{
Student st = *it;
izpisi_studenta(st);
}
}
void izpisi_studenta(Student st)
{
cout << "Ime: " << st.Ime << endl << "Priimek: " << st.Priimek << endl << "IDUM: " << st.IDUM << endl << endl;
}
void vstavi(list<Student> &seznam, int indeks, Student podatek)
{
if (indeks < 0 || indeks >= seznam.size() || seznam.empty()) {
return;
}
auto index = seznam.begin();
advance(index, indeks);
*index = podatek;
}
Student vrni(const list<Student> seznam, int indeks)
{
Student student;
if (indeks < 0 || indeks >= seznam.size() || seznam.empty()) return student;
auto index = seznam.begin();
advance(index, indeks);
student = *index;
return student;
return Student();
}
void vrini(list<Student> &seznam, int indeks, Student podatek)
{
if (indeks < 0) {
return;
}
else if(indeks >= seznam.size() || seznam.empty()){
seznam.push_back(podatek);
return;
}
auto index = seznam.begin();
advance(index, indeks);
seznam.insert(index, podatek);
}
Student odstrani(list<Student> &seznam, int indeks)
{
Student student;
if (indeks < 0 || indeks >= seznam.size() || seznam.empty()) return student;
auto index = seznam.begin();
advance(index, indeks);
student = *index;
seznam.erase(index);
return student;
return Student();
}
int main()
{
int tmp;
list<Student> students;
Student st1 = {"jan", "bel", "243"};
Student st2 = {"pet" , "jsad", "324"};
Student st3 = {"asndlk", "ndsia", "345"};
Student st4 = {"nilp", "dsag", "365"};
Student st5 = {"dsag", "sadgn", "268"};
Student tmpStud;
students.push_back(st1);
students.push_back(st2);
students.push_back(st3);
izpisi_seznam(students);
cout<<"Naloga 1:"<<endl;
cin >> tmp;
vstavi(students, tmp, st4);
izpisi_seznam(students);
cout<<"Naloga 2:"<<endl<<endl;
cin >> tmp;
tmpStud=vrni(students, tmp);
izpisi_studenta(tmpStud);
cout<<"Naloga 3:"<<endl<<endl;
cin >> tmp;
vrini(students, tmp, st5);
izpisi_seznam(students);
cout<<"Naloga 4:"<<endl<<endl;
cin >> tmp;
tmpStud=odstrani(students, tmp);
izpisi_studenta(tmpStud);
izpisi_seznam(students);
return 0;
}

View File

@@ -0,0 +1,3 @@
.idea/*
cmake-build-debug/*
CMakeLists.txt

View File

@@ -0,0 +1 @@
Navodila v folder navodila

View File

@@ -0,0 +1,198 @@
#include <iostream>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
using namespace std;
// prototipi
bool jeOperand(char);
bool jeOperator(char);
int prioritetaOperatorja(char);
queue<char> pretvori_niz_v_vrsto(string);
queue<char> pretvorba_infiks_v_postfiks(queue<char>);
double izracunaj_skladovni_stroj(queue<char>);
double izracunaj_izraze(string);
bool jeOperand(char c) {
if (c >= '0' && c <= '9')
return true;
return false;
}
bool jeOperator(char c) {
if (c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c == '(' || c == ')')
return true;
return false;
}
int prioritetaOperatorja(char c) {
switch (c) {
case '(':
return 4;
case ')':
return 4;
case '^':
return 3;
case '*':
return 2;
case '/':
return 2;
case '+':
return 1;
case '-':
return 1;
default:
return -1;
}
}
queue<char> pretvori_niz_v_vrsto(string izraz) {
queue<char> infiksni_izraz;
// TODO
for (int i = 0; i < izraz.size(); i++) {
if (jeOperand(izraz[i]) || jeOperator(izraz[i])) {
infiksni_izraz.push(izraz[i]);
}
}
return infiksni_izraz;
}
queue<char> pretvorba_infiks_v_postfiks(queue<char> infiksni_izraz) {
queue<char> postfiksni_izraz;
stack<char> pomozni_sklad;
char operator1;
char precitaniPodatek;
while (!infiksni_izraz.empty()) {
precitaniPodatek = infiksni_izraz.front();
infiksni_izraz.pop();
if (precitaniPodatek != ')') {
if (jeOperand(precitaniPodatek)) {
postfiksni_izraz.push(precitaniPodatek);
} else {
if (precitaniPodatek == '(') {
pomozni_sklad.push(precitaniPodatek);
} else {
while (!pomozni_sklad.empty() && pomozni_sklad.top() != '(' && prioritetaOperatorja(precitaniPodatek) <= prioritetaOperatorja(pomozni_sklad.top())) {
operator1 = pomozni_sklad.top();
pomozni_sklad.pop();
postfiksni_izraz.push(operator1);
}
pomozni_sklad.push(precitaniPodatek);
}
}
} else {
operator1 = pomozni_sklad.top();
pomozni_sklad.pop();
while (operator1 != '(') {
postfiksni_izraz.push(operator1);
operator1 = pomozni_sklad.top();
pomozni_sklad.pop();
}
}
}
while (!pomozni_sklad.empty()) {
operator1 = pomozni_sklad.top();
pomozni_sklad.pop();
postfiksni_izraz.push(operator1);
}
return postfiksni_izraz;
}
double izracunaj_skladovni_stroj(queue<char> postfiksni_izraz) {
stack<double> skladovni_stroj;
char precitaniPodatek;
double rezultat, num1, num2;
while (!postfiksni_izraz.empty()) {
precitaniPodatek = postfiksni_izraz.front();
postfiksni_izraz.pop();
if (jeOperand(precitaniPodatek)) {
string tmp_string(1, precitaniPodatek);
skladovni_stroj.push(stod(tmp_string));
} else {
num1 = skladovni_stroj.top();
skladovni_stroj.pop();
num2 = skladovni_stroj.top();
skladovni_stroj.pop();
switch (precitaniPodatek) {
case '^':
rezultat = pow(num2, num1);
break;
case '*':
rezultat = num2 * num1;
break;
case '/':
rezultat = num2 / num1;
break;
case '+':
rezultat = num1 + num2;
break;
case '-':
rezultat = num2 - num1;
break;
}
skladovni_stroj.push(rezultat);
}
}
// na skladu mora ostat samo rezultat
return (skladovni_stroj.size() != 0) ? skladovni_stroj.top() : INT32_MIN;
}
double izracunaj_izraz(string izraz) {
// podprogram pretvori_niz_v_vrsto()
queue<char> infiksni_izraz = pretvori_niz_v_vrsto(izraz);
// podprogram pretvorba_infiks_v_postfiks()
queue<char> postfiksni_izraz = pretvorba_infiks_v_postfiks(infiksni_izraz);
// podprogram izracunaj_skladovni_stroj()
double rezultat = izracunaj_skladovni_stroj(postfiksni_izraz);
return rezultat;
}
int main() {
string izrazi[] = {
"2^(2-2)+1",
"4*( 9 - 5 )",
"2-2*2+2",
"2*6/3-2+2"
};
double rezultati[] = {
2.0,
16.0,
0.0,
4.0
};
int N = sizeof(izrazi) / sizeof(string);
for (int i = 0; i < N; i++) {
double rezultat = izracunaj_izraz(izrazi[i]);
if (rezultati[i] == rezultat)
cout << "OK" << endl;
else
cout << "Napacen rezultat za izraz " << izrazi[i] << ": " << rezultat << " (pricakovan rezultat: " << rezultati[i] << ")." << endl;
}
return 0;
}

View File

@@ -0,0 +1,3 @@
.idea/*
cmake-build-debug/*
CMakeLists.txt

View File

@@ -0,0 +1 @@
Navodila v folder navodila

View File

@@ -0,0 +1,304 @@
#include <iostream>
#include <queue>
#include <limits>
using namespace std;
// razred IskalnoDrevo
class IskalnoDrevo
{
// razred Vozlisce
class Vozlisce
{
double podatek;
Vozlisce *levo;
Vozlisce *desno;
public:
Vozlisce(double podatek)
{
this->podatek = podatek;
this->levo = NULL;
this->desno = NULL;
}
void vstavi(double podatek)
{
if (podatek < this->podatek)
{
if (levo == NULL)
levo = new Vozlisce(podatek);
else
levo->vstavi(podatek);
}
else
{
if (desno == NULL)
desno = new Vozlisce(podatek);
else
desno->vstavi(podatek);
}
}
int stevilo_vozlisc()
{
int stevilo = 1;
if(desno != NULL)
stevilo += desno->stevilo_vozlisc();
if(levo != NULL)
stevilo += levo->stevilo_vozlisc();
return stevilo;
}
int visina_drevesa(int globina)
{
int desnoVisina = 0;
int levoVisina = 0;
if(desno != NULL)
desnoVisina += desno->visina_drevesa(globina + 1);
if(levo != NULL)
levoVisina += levo->visina_drevesa(globina + 1);
if(levoVisina == 0 and desnoVisina == 0)
return globina;
else if(levoVisina < desnoVisina)
return desnoVisina;
else
return levoVisina;
}
double najvecji()
{
double najvec = this->podatek;
if(desno != NULL)
najvec = desno->najvecji();
return najvec;
}
double najmanjsi()
{
double najmanj = this->podatek;
if(levo != NULL)
najmanj = levo->najmanjsi();
return najmanj;
}
void premi_pregled(queue<double> &vrsta)
{
vrsta.push(podatek);
if(levo != NULL)
levo->premi_pregled(vrsta);
if(desno != NULL)
desno->premi_pregled(vrsta);
}
void vmesni_pregled(queue<double> &vrsta)
{
if(levo != NULL)
levo->vmesni_pregled(vrsta);
vrsta.push(podatek);
if(desno != NULL)
desno->vmesni_pregled(vrsta);
}
void obratni_pregled(queue<double> &vrsta)
{
if(levo != NULL)
levo->obratni_pregled(vrsta);
if(desno != NULL)
desno->obratni_pregled(vrsta);
vrsta.push(podatek);
}
bool iskanje(double stevilo)
{
if(stevilo == podatek)
return true;
if (stevilo < podatek)
{
if (levo == NULL)
return false;
else
return levo->iskanje(stevilo);
}
else
{
if (desno == NULL)
return false;
else
return desno->iskanje(stevilo);
}
}
double vsota()
{
double stevilo = podatek;
if(desno != NULL)
stevilo += desno->vsota();
if(levo != NULL)
stevilo += levo->vsota();
return stevilo;
}
};
// nadaljevanje razreda IskalnoDrevo
Vozlisce *koren;
public:
IskalnoDrevo()
{
koren = NULL;
}
void vstavi(double podatek)
{
if (koren == NULL)
koren = new Vozlisce(podatek);
else
koren->vstavi(podatek);
}
int stevilo_vozlisc() const
{
if (koren == NULL)
return 0;
else
return koren->stevilo_vozlisc();
}
int visina_drevesa() const
{
if (koren == NULL)
return 0;
else
return koren->visina_drevesa(1);
}
double najvecji() const
{
if (koren == NULL)
return numeric_limits<double>::quiet_NaN();
return koren->najvecji();
}
double najmanjsi() const
{
if (koren == NULL)
return numeric_limits<double>::quiet_NaN();
return koren->najmanjsi();
}
queue<double> premi_pregled() const
{
queue<double> prefiks;
if (koren != NULL)
koren->premi_pregled(prefiks);
return prefiks;
}
queue<double> vmesni_pregled() const
{
queue<double> infiks;
if (koren != NULL)
koren->vmesni_pregled(infiks);
return infiks;
}
queue<double> obratni_pregled() const
{
queue<double> postfiks;
if (koren != NULL)
koren->obratni_pregled(postfiks);
return postfiks;
}
bool iskanje(double stevilo) const
{
if (koren != NULL)
return koren->iskanje(stevilo);
return false;
}
double vsota() const
{
if (koren != NULL)
return koren->vsota();
return numeric_limits<double>::quiet_NaN();
}
};
int main(int argn, char **args)
{
double zaporedje[] = {5,2,8,10,4,6,1,12,-5,-3,-6,-8,7 };
int dolzina = 13;
// ustvarimo nov objekt
IskalnoDrevo id;
// napolnimo drevo
for (int i = 0; i < dolzina; i++)
id.vstavi((zaporedje[i]));
// pričakovan izhod
double pricakovan_izhod = 7;
// dobljen izhod
double dobljen_izhod = id.najvecji();
// preverimo, če dobimo pravilen izhod
if (pricakovan_izhod == dobljen_izhod)
cout << "OK" << endl;
else
cout << "Napaka! Pricakovan izhod: " << pricakovan_izhod << ", dobljen izhod: " << dobljen_izhod << endl;
cout << "Stevilo vozlisc: " << id.stevilo_vozlisc() << "\n";
cout << "Najdi stevilo 2: " << (id.iskanje(2) ? "true" : "false") << "\n";
cout << "Najdi stevilo 1: " << (id.iskanje(1) ? "true" : "false") << "\n";
cout << id.vsota() << endl;
cout << id.visina_drevesa() << endl;
queue<double> ha = id.premi_pregled();
for (int i = 0; i < dolzina; ++i) {
cout << ha.front() << " ";
ha.pop();
}
cout << "\n";
ha = id.vmesni_pregled();
for (int i = 0; i < dolzina; ++i) {
cout << ha.front() << " ";
ha.pop();
}
cout << "\n";
ha=id.obratni_pregled();
for (int i = 0; i < dolzina; ++i) {
cout << ha.front() << " ";
ha.pop();
}
return 0;
}

View File

@@ -0,0 +1,3 @@
.idea/*
cmake-build-debug/*
CMakeLists.txt

View File

@@ -0,0 +1 @@
Navodila v folder navodila

View File

@@ -0,0 +1,98 @@
#include <iostream>
#include <string>
#include <map>
#include <assert.h> /* assert */
using namespace std;
struct Oseba
{
string Ime;
string Priimek;
int IDUM; // 9-mestna unikatna številka
void print(){
cout << Ime << " " << Priimek << " " << to_string(IDUM) << "\n";
}
};
// prototipi
void dobi_prvo_crko(Oseba, char &);
void dobi_slovar_s_crko(map<char, map<int, Oseba>>, char, map<int, Oseba> &);
void vstavi_osebo_v_slovar_oseb(map<int, Oseba> &, Oseba);
void posodobi_slovar_oseb_v_slovarju(map<char, map<int, Oseba>> &, char, map<int, Oseba>);
void napolni_slovar(Oseba [], int, map<char, map<int, Oseba>> &);
void dobi_prvo_crko(Oseba oseba, char &prva_crka)
{
// TODO
prva_crka = oseba.Priimek[0];
}
void dobi_slovar_s_crko(map<char, map<int, Oseba>> slovar, char prva_crka, map<int, Oseba> &slovar_oseb)
{
// TODO
slovar_oseb = slovar[prva_crka];
}
void vstavi_osebo_v_slovar_oseb(map<int, Oseba> &slovar_oseb, Oseba os)
{
// TODO
slovar_oseb.insert(std::pair<int,Oseba>(os.IDUM, os));
}
void posodobi_slovar_oseb_v_slovarju(map<char, map<int, Oseba>> &slovar, char prva_crka, map<int, Oseba> slovar_oseb)
{
// TODO
slovar[prva_crka].swap(slovar_oseb);
}
void napolni_slovar(Oseba seznam[], int N, map<char, map<int, Oseba>> &slovar)
{
for (int i = 0; i < N; i++)
{
// pridobimo prvo črko
char prva_crka;
dobi_prvo_crko(seznam[i], prva_crka);
// pridobimo slovar oseb s prvo črko
map<int, Oseba> slovar_oseb;
dobi_slovar_s_crko(slovar, prva_crka, slovar_oseb);
// vstavimo osebo v slovar
vstavi_osebo_v_slovar_oseb(slovar_oseb, seznam[i]);
// posodobi slovar
posodobi_slovar_oseb_v_slovarju(slovar, prva_crka, slovar_oseb);
}
}
int main()
{
Oseba seznam[] = {
Oseba {"Jani", "Dugonik", 123456789 },
Oseba{"Nikola", "Petrov", 1002525225},
Oseba{"Nikollll", "Petttr", 1223625123},
Oseba{"Ozan", "Zef",1001234567},
Oseba{"Zan", "Burger", 1002345678},
Oseba{"Marko", "Novak", 1003456789}
};
int N = sizeof(seznam) / sizeof(Oseba);
map<char, map<int, Oseba>> slovar;
napolni_slovar(seznam, N, slovar);
map<int,Oseba> slovarOseb;
Oseba tmp;
for (auto it=slovar.begin(); it!=slovar.end(); ++it) {
slovarOseb = it->second;
cout << it->first << endl;
for (auto at=slovarOseb.begin(); at!=slovarOseb.end(); ++at) {
at->second.print();
}
}
return 0;
}

View File

@@ -0,0 +1,31 @@
//
// Created by Nik on 07/04/2022.
//
#include "Article.h"
Article::Article(std::string name, std::string barcode, double price) : name(name), barcode(barcode), price(price), quantity(1) {}
bool Article::hasSameCode(Article *a) const {
return (barcode == a->barcode);
}
double Article::getTotalPrice() const{
return price * quantity;
}
std::string Article::toSting() const{
return name + " " + std::to_string(quantity) + " " + std::to_string(getTotalPrice()) + " E";
}
void Article::increseQuantity() {
quantity += 1;
}
void Article::setQuantity(double q) {
quantity = q;
}
void Article::setPrice(double p) {
price = p;
}

View File

@@ -0,0 +1,23 @@
//
// Created by Nik on 07/04/2022.
//
#ifndef PRIRAVA_1_ARTICLE_H
#define PRIRAVA_1_ARTICLE_H
#include <string>
class Article {
protected:
std::string name, barcode;
double price, quantity;
public:
Article(std::string name, std::string barcode, double price);
bool hasSameCode(Article* a) const;
double getTotalPrice() const;
virtual std::string toSting() const;
void increseQuantity();
void setQuantity(double q);
void setPrice(double p);
};
#endif //PRIRAVA_1_ARTICLE_H

View File

@@ -0,0 +1,38 @@
//
// Created by Nik on 07/04/2022.
//
#include "Invoice.h"
//int Invoice::countId = 0;
Invoice::Invoice(std::string seller) : seller(seller){
countId += 1;
id = countId;
}
Invoice::~Invoice() {
countId -= 1;
}
void Invoice::addArticle(Article *a) {
for(int i = 0; i < articels.size(); i++){
if(articels[i]->hasSameCode(a)){
articels[i]->increseQuantity();
return;
}
}
articels.push_back(a);
}
void Invoice::print() const {
double skupaj = 0.0;
std::cout << seller << " " << std::to_string(id) << "\n";
for(int i = 0; i < articels.size(); i++){
std::cout << articels[i]->toSting() << "\n";
skupaj += articels[i]->getTotalPrice();
}
std::cout << "skupna cena " << std::to_string(skupaj) << " E \n\n";
}

View File

@@ -0,0 +1,27 @@
//
// Created by Nik on 07/04/2022.
//
#ifndef PRIRAVA_1_INVOICE_H
#define PRIRAVA_1_INVOICE_H
#include "Article.h"
#include "WeighableArticle.h"
#include <vector>
#include <iostream>
class Invoice {
private:
std::string seller;
int id;
static int countId;
std::vector<Article*> articels;
public:
Invoice(std::string seller);
~Invoice();
void addArticle(Article* a);
void print() const;
};
#endif //PRIRAVA_1_INVOICE_H

View File

@@ -0,0 +1,13 @@
//
// Created by Nik on 07/04/2022.
//
#include "WeighableArticle.h"
WeighableArticle::WeighableArticle(std::string name, std::string barcode, double price, double quantity) : Article(name, barcode, price) {
this->quantity = quantity;
}
std::string WeighableArticle::toString() {
return name + " " + std::to_string(quantity) + " " + std::to_string(getTotalPrice()) + " E \n";
}

View File

@@ -0,0 +1,18 @@
//
// Created by Nik on 07/04/2022.
//
#ifndef PRIRAVA_1_WEIGHABLEARTICLE_H
#define PRIRAVA_1_WEIGHABLEARTICLE_H
#include "Article.h"
class WeighableArticle : public Article{
private:
public:
WeighableArticle(std::string name, std::string barcode, double price, double quantity);
std::string toString();
};
#endif //PRIRAVA_1_WEIGHABLEARTICLE_H

View File

@@ -0,0 +1,18 @@
#include "Invoice.h"
int main() {
Article *artikel1 = new Article("Habotov tic", "1223", 25.69);
Article *artikel2 = new WeighableArticle("Golobov tic", "2233", 2.33, 0.9);
Invoice *racun1 = new Invoice("Tici d.o.o");
artikel1->setQuantity(7);
racun1->addArticle(artikel1);
racun1->addArticle(artikel2);
racun1->print();
Article *artikel3 = new Article("Ficotov tic", "1223", 50000.12);
Article *artikel4 = new WeighableArticle("rajzmanov tic", "2234", 98.8, 15);
Invoice *racun2 = new Invoice("Novi Tici d.o.o");
racun2->addArticle(artikel3);
racun2->addArticle(artikel4);
racun2->print();
return 0;
}

View File

@@ -0,0 +1,23 @@
//
// Created by Nik on 09/06/2022.
//
#ifndef PRIRAVA_2_MESSAGE_H
#define PRIRAVA_2_MESSAGE_H
#include <string>
class Message{
protected:
int time;
std::string text;
public:
Message(int time, std::string text): text(text) {
if (time < 1 or time > 90) throw WrongTimeException();
};
virtual std::string toString() const{
return std::to_string(time) + text + "\n";
};
};
#endif //PRIRAVA_2_MESSAGE_H

View File

@@ -0,0 +1,22 @@
//
// Created by Nik on 09/06/2022.
//
#ifndef PRIRAVA_2_PLAYER_H
#define PRIRAVA_2_PLAYER_H
#include <string>
class Player {
private:
std::string name;
int number;
public:
Player(std::string name, int number) : name(name), number(number){} ;
std::string toString(){
return "Player: " + name + " " + std::to_string(number) + "\n";
};
};
#endif //PRIRAVA_2_PLAYER_H

View File

@@ -0,0 +1,6 @@
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}

View File

@@ -0,0 +1 @@
# Programiranje_2

View File

@@ -0,0 +1,25 @@
Napisali boste program, ki bo omogočal vnos poljubnega števila. Program bo moral izračunati vsoto vseh cifer v podanem številu in preveriti ali je vsota popolno število.
Kaj je popolno število? Popolno število je v matematiki pozitivno celo število n, za katerega je vsota pozitivnih pravih deliteljev enaka številu n. Več o popolnem številu.
https://en.wikipedia.org/wiki/Perfect_number
Zahteve programa:
Napišite program, ki od uporabnika zahteva število tako dolgo, dokler podano število ni pozitivno ( uporabite do while zanko).
Izračunajte in izpišite vsoto cifer v podanem številu.
Na koncu preverite in izpišite ali je vsota števil popolno število.
Primer programa:
> Vnesite stevilo:-50
> Vnesite stevilo:-29
> Vnesite stevilo:10325
> Vsota cifer v stevilu je 11
> Stevilo 11 ni popolno stevilo
Kako preverimo ali je število popolno?
Primer 1: Število 11 ni popolno, ker je njeni edini pravi delitelj 1. Vsota je torej 1, kar ni enako številu samemu.
Primer 2: Število 28 je popolno, ker so njeni pravi delitelji 1, 2, 4, 7, 14. Vsota številj je 28, kar je enako številu samemu.

View File

@@ -0,0 +1,43 @@
#include <iostream>
using namespace std;
int add(int number) {
int sum = 0;
while (number > 0) {
sum = sum + number % 10;
number = number / 10;
}
return sum;
}
void perfect_number(int number) {
int sum = 0;
for (int i = 1; i < number; i++) {
if (number % i == 0) {
sum = sum + i;
}
}
if (sum == number) {
cout << number << " is a perfect number";
} else {
cout << number << " is not a perfect number";
}
}
int main() {
int input_number;
for (;;) {
cout << "Input number: ";
cin >> input_number;
if (input_number > 0) break;
}
int sum = add(input_number);
cout << "Sum is " << sum << endl;
perfect_number(sum);
return 0;
}
// namesto while use for

View File

@@ -0,0 +1,19 @@
1.Preučite in uporabite zgled.(zgled.cpp)
2.Spremenite program tako, da se v polje vnese 30 naključnih na eno decimalno mesto natančnih števil med 0,5 in 3,5. Vrednosti predstavljajo količino popite vode (v litrih) na dan.
3.Dopolnite program z izpisom povprečne količine popite vode.
4.Dopolnite program z izpisom števila dni, ko je bila količina popite vode pod povprečjem.
5.Dopolnite program z izpisom največje popite količine vode.
6.Izpišite število dni, ko je bila popita količina vode v mejah priporočenih količin, in sicer med 2 in 2,5 litra.
Končana naloga mora imeti v meniju 6 izbir + izbira za izhod iz programa.
Pomoč pri generiranju naključnih števil:
https://en.cppreference.com/w/cpp/numeric/random/rand
https://en.cppreference.com/w/cpp/numeric/random/random_device

View File

@@ -0,0 +1,146 @@
#include <iostream>
#include <ctime>
#include <cmath>
using namespace std;
void menu() {
cout << "============================" << endl;
cout << "=========== MENU ===========" << endl;
cout << "============================" << endl;
cout << "1 ... GENERATE WATER INTAKES" << endl;
cout << "2 ... PRINT WATER INTAKES" << endl;
cout << "3 ... AVERAGE" << endl;
cout << "4 ... DAYS UNDER AVERAGE" << endl;
cout << "5 ... THE MOST WATER" << endl;
cout << "6 ... IN LIMIT" << endl;
cout << "7 ... ALL THE WATER" << endl;
cout << "0 ... EXIT" << endl;
cout << "============================" << endl;
cout << "Select: ";
}
float map(float value, float start1, float stop1, float start2, float stop2) {
return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
}
void fillArray(float *array, const unsigned int size) {
float rand_number;
for (unsigned int i = 0; i < size; i++) {
rand_number = map(rand(), 0, RAND_MAX, 0.5, 3.5);
rand_number = round(10 * rand_number) / 10;
array[i] = rand_number;
}
}
void printArray(const float *array, const unsigned int size) {
for (unsigned int i = 0; i < size; i++) {
cout << "Day: " << i << " --> " << array[i] << "L\n";
}
}
float sum(const float *array, const unsigned int size) {
float liters = 0;
for (unsigned int i = 0; i < size; i++) {
liters += array[i];
}
return liters;
}
float average(const float *array, const unsigned int size) {
float liters = sum(array, size);
liters = liters / size;
liters = round(10 * liters) / 10;
return liters;
}
void under_average(const float *array, const unsigned int size) {
float avg = average(array, size);
unsigned int dni = 0;
for (unsigned int i = 0; i < size; i++) {
if (avg > array[i]) {
cout << "Day: " << i << " --> " << array[i] << "L\n";
dni++;
}
}
cout << dni << " days are under average \n";
}
void max_day(const float *array, const unsigned int size) {
float max_drank = array[0];
unsigned int day;
for (unsigned int i = 0; i < size; i++) {
if (max_drank < array[i]) {
max_drank = array[i];
day = i;
}
}
cout << "On day " << day << " you drank the most water " << max_drank << "L";
}
void in_limit(const float *array, const unsigned int size) {
unsigned int day = 0;
for (unsigned int i = 0; i < size; i++) {
if (2.0 <= array[i] && array[i] <= 2.5) {
cout << "Day: " << i << " --> " << array[i] << "L\n";
day++;
}
}
cout << day << endl;
}
int main() {
const unsigned int days = 30;
float *waterIntakes = new float[days];
float tmp;
srand(time(nullptr));
fillArray(waterIntakes, days);
bool running = true;
int selection;
do {
menu();
cin >> selection;
cout << endl;
switch (selection) {
case 1:
fillArray(waterIntakes, days);
break;
case 2:
printArray(waterIntakes, days);
break;
case 3:
tmp = average(waterIntakes, days);
cout << "On average you drank " << tmp << "L a day";
break;
case 4:
under_average(waterIntakes, days);
break;
case 5:
max_day(waterIntakes, days);
break;
case 6:
in_limit(waterIntakes, days);
break;
case 7:
tmp = sum(waterIntakes, days);
cout << "Vsa voda popita" << tmp;
break;
case 0:
running = false;
break;
default:
cout << "Wrong selection!" << endl;
break;
}
cout << endl;
} while (running);
delete[] waterIntakes;
return 0;
}
// izpis celotne kolicine vode

View File

@@ -0,0 +1,61 @@
#include <iostream>
#include <ctime>
using namespace std;
void menu() {
cout << "============================" << endl;
cout << "=========== MENU ===========" << endl;
cout << "============================" << endl;
cout << "1 ... GENERATE WATER INTAKES" << endl;
cout << "2 ... PRINT WATER INTAKES" << endl;
cout << "0 ... EXIT" << endl;
cout << "============================" << endl;
cout << "Select: ";
}
void fillArray(float* array, const unsigned int size) {
for (unsigned int i = 0; i < size; i++) {
array[i] = 0.5f + i * 0.3f;
}
}
void printArray(const float* array, const unsigned int size) {
for (unsigned int i = 0; i < size; i++) {
cout << ((i > 0) ? ", " : "") << array[i];
}
}
int main() {
const unsigned int days = 10;
float* waterIntakes = new float[days];
srand(time(nullptr));
bool running = true;
int selection;
do {
menu();
cin >> selection;
switch (selection) {
case 1:
fillArray(waterIntakes, days);
break;
case 2:
printArray(waterIntakes, days);
break;
case 0:
running = false;
break;
default:
cout << "Wrong selection!" << endl;
break;
}
cout << endl;
} while (running);
delete[] waterIntakes;
return 0;
}

View File

@@ -0,0 +1,39 @@
#include "Artwork.h"
#include <iostream>
#include <string>
Artwork::Artwork() : author(""), title(""), description(""), price(0), year(0) {}
Artwork::Artwork(std::string author, std::string title, std::string description, int price, int year) : author(author), title(title), description(description), price(price), year(year) {}
Artwork::~Artwork() {};
void Artwork::setAuthor(std::string author) {this->author = author;}
void Artwork::setTitle(std::string title) {this ->title = title;}
void Artwork::setDescription(std::string description) {this->description = description;}
void Artwork::setPrice(int price) {this->price = price;}
void Artwork::setYear(int year) { this->year = year;}
std::string Artwork::getAuthor() { return author; }
std::string Artwork::getTitle() { return title; }
std::string Artwork::getDescription() { return description; }
int Artwork::getPrice() { return price; }
int Artwork::getYear() { return year; }
std::string Artwork::toString() {
return "Author: " + author + "\nTitle: " + title + "\nDescription: " + description + "\nPrice: " +
std::to_string(price) + " EUR\nYear: " + std::to_string(year) + "\n\n";
}
void Artwork::print() {
std::cout << "Author: " << author << "\nTitle: " << title << "\nDescription: " << description << "\nPrice: "
<< price << " EUR\nYear: " << year << "\n\n";
}

View File

@@ -0,0 +1,37 @@
#ifndef NALOGA0201_ARTWORK_H
#define NALOGA0201_ARTWORK_H
#include <string>
class Artwork {
private:
std::string author, title, description;
int price, year;
public:
Artwork(); //constructor
Artwork(std::string author, std::string title, std::string description, int price, int year);
~Artwork(); //destructor
//Methods
void setAuthor(std::string);
void setTitle(std::string);
void setDescription(std::string);
void setPrice(int);
void setYear(int);
std::string getAuthor();
std::string getTitle();
std::string getDescription();
int getPrice();
int getYear();
std::string toString();
void print();
};
#endif //NALOGA0201_ARTWORK_H

View File

@@ -0,0 +1,15 @@
Preučite primer Point na prosojnicah iz predavanj in po enakem vzoru napišite razred Artwork (zapišite Artwork.h in Artwork.cpp). Vse metode (tudi krajše) implementirajte v datoteki Artwork.cpp.
Razred Artwork mora imeti naslednje instančne spremenljivke:
title,
description,
price in
year.
Razred Artwork mora imeti konstruktor s 4 parametri.
Napišite javne metode: get (vrne podatek) za vsako lastnost posebej.
Napišite javne metode: set (nastavi podatek) pri izbranih lastnostih, kjer je smiselno.
Napišite metodo toString(), ki vrne string, ki vsebuje vse podatke o umetnini.
Napišite javno metodo print(), ki izpiše vse podatke.
V glavnem programu demonstrirajte uporabo konstruktorjev in metod iz razreda Artwork. Ustvarite vsaj 5 objektov, kjer demonstrirate:
statično in dinamično alokacijo objektov (razreda Artwork),
uporabo vseh metod iz razreda Artwork.

View File

@@ -0,0 +1,52 @@
#include <iostream>
#include "Artwork.h"
#include <string>
int main() {
std::cout << "Hello, World!" << std::endl;
Artwork a("nikola", "nik", "pet", 20, 15);
Artwork b;
std::string name = "lag";
std::string tit = "dsla";
std::string des = "pet";
int pri = 33;
int ye = 66;
b.setAuthor(name);
b.setTitle(tit);
b.setDescription(des);
b.setDescription(des);
b.setPrice(pri);
b.setYear(ye);
std::cout << a.toString();
b.print();
Artwork f(a);
f.print();
Artwork *c = new Artwork;
Artwork *d = new Artwork("jan", "andz", "novak", 34, 75);
c->setAuthor(name);
c->setTitle(tit);
c->setDescription(des);
c->setDescription(des);
c->setPrice(pri);
c->setYear(ye);
d->print();
std::cout << c->toString();
std::cout << f.getAuthor() << "\n";
std::cout << a.getTitle() << "\n";
std::cout << b.getDescription() << "\n";
std::cout << c->getPrice() << "\n";
std::cout << d->getYear() << "\n";
delete c;
delete d;
return 0;
}
//string Avto

View File

@@ -0,0 +1,12 @@
Vzemite vašo rešitev naloge 1.2.
Preučite in uporabite zgled ter predelajte nalogo 1.2 tako, da uporabite (https://en.cppreference.com/w/cpp/numeric/random/random_device) naključnih števil za generiranje objektov popite vode (mesec naj bo 3, leto 2022, dnevi pa od 1 do 30). Namesto polja uporabite (https://en.cppreference.com/w/cpp/container/vector).
Napišite razred WaterIntake, ki bo imel naslednje instančne privatne lastnosti:
day: unsigned int,
month: unsigned int,
year: unsigned int,
quantity: float.
Razred naj vsebuje konstruktor s 4 argumenti.
Napišite javne metode: get (vrne podatek) za vsako lastnost posebej in set (nastavi podatek) samo za lastnost quantity.
Razredu dodajte metodo toString, ki vrne vse podatke objekta.
Razredu dodajte metodo addQuantity, ki prejme realno vrednost, za katero poveča quantity.
Preuredite glavni program iz naloge 1.2 tako, da bo vector vseboval kazalce na objekte razreda WaterIntake (zgled.cpp).

View File

@@ -0,0 +1,37 @@
#include "WaterIntake.h"
#include <iostream>
WaterIntake::WaterIntake() : day(0), month(0), year(0), quantity(0.0) {}
WaterIntake::WaterIntake(unsigned int day, unsigned int month, unsigned int year, float quantity) : day(day), month(month), year(year), quantity(quantity) {
}
WaterIntake::~WaterIntake() {}
unsigned int WaterIntake::getDay() {
return day;
}
unsigned int WaterIntake::getMonth() {
return month;
}
unsigned int WaterIntake::getYear() {
return year;
}
float WaterIntake::getQuantity() {
return quantity;
}
std::string WaterIntake::toString() {
return "day: " + std::to_string(day) + "\n month: " + std::to_string(month) + "\n year: " + std::to_string(year) + "\n quantity: " + std::to_string(quantity) + "\n";
}
void WaterIntake::addQuantity(float addQuantity) {
quantity = quantity + addQuantity;
}
bool WaterIntake::isNormal() {
return 2.0 <= quantity && quantity <= 2.5;
}

View File

@@ -0,0 +1,33 @@
#ifndef NALOGA0102_WATERINTAKE_H
#define NALOGA0102_WATERINTAKE_H
#include <string>
class WaterIntake {
private:
unsigned int day, month, year;
float quantity;
public:
WaterIntake();
WaterIntake(unsigned int, unsigned int, unsigned int, float);
~WaterIntake();
unsigned int getDay();
unsigned int getMonth();
unsigned int getYear();
float getQuantity();
std::string toString();
void addQuantity(float);
bool isNormal();
};
#endif //NALOGA0102_WATERINTAKE_H

View File

@@ -0,0 +1,140 @@
#include <iostream>
#include <ctime>
#include <cmath>
#include <vector>
#include "WaterIntake.h"
using namespace std;
void menu() {
cout << "============================" << endl;
cout << "=========== MENU ===========" << endl;
cout << "============================" << endl;
cout << "1 ... GENERATE WATER INTAKES" << endl;
cout << "2 ... PRINT WATER INTAKES" << endl;
cout << "3 ... AVERAGE" << endl;
cout << "4 ... DAYS UNDER AVERAGE" << endl;
cout << "5 ... THE MOST WATER" << endl;
cout << "6 ... IN LIMIT" << endl;
cout << "7 ... ALL THE WATER" << endl;
cout << "0 ... EXIT" << endl;
cout << "============================" << endl;
cout << "Select: ";
}
void fillArray(vector<WaterIntake*> &waterIntakes, const unsigned int size) {
float randNumber;
for (unsigned int i = 0; i < size; i++) {
randNumber = (rand() / (RAND_MAX + 0.0)) * 3 + 0.5;
randNumber = round(10 * randNumber) / 10;
waterIntakes.push_back(new WaterIntake(i,2,2022,randNumber));
}
}
void printArray(const vector<WaterIntake*> &waterIntakes) {
for (unsigned int i = 0; i < waterIntakes.size(); i++)
cout << waterIntakes[i]->toString() << std::endl;
}
float sum(const vector<WaterIntake*> &waterIntakes) {
float liters = 0;
for (unsigned int i = 0; i < waterIntakes.size(); i++) {
liters += waterIntakes[i]->getQuantity();
}
return liters;
}
float average(const vector<WaterIntake*> &waterIntakes) {
float liters = sum(waterIntakes);
liters = liters / waterIntakes.size();
liters = round(10 * liters) / 10;
return liters;
}
void under_average(const vector<WaterIntake*> &waterIntakes) {
float avg = average(waterIntakes);
unsigned int dni = 0;
for (unsigned int i = 0; i < waterIntakes.size(); i++) {
if (avg > waterIntakes[i]->getQuantity()) {
cout << waterIntakes[i]->toString();
dni++;
}
}
cout << dni << " days are under average \n";
}
void max_day(const vector<WaterIntake*> &waterIntakes) {
float max_drank = waterIntakes[0]->getQuantity();
unsigned int day;
for (unsigned int i = 0; i < waterIntakes.size(); i++) {
if (max_drank < waterIntakes[i]->getQuantity()) {;
day = i;
}
}
cout << "On day \n" << waterIntakes[day]->toString();
}
void in_limit(const vector<WaterIntake*> &waterIntakes) {
unsigned int day = 0;
for (unsigned int i = 0; i < waterIntakes.size(); i++) {
if (waterIntakes[i]->isNormal()) {
cout << waterIntakes[i]->toString();
day++;
}
}
cout << day << endl;
}
int main() {
const unsigned int days = 30;
vector<WaterIntake*> waterIntakes;
float tmp;
srand(time(nullptr));
fillArray(waterIntakes, days);
bool running = true;
int selection;
do {
menu();
cin >> selection;
cout << endl;
switch (selection) {
case 1:
fillArray(waterIntakes, days);
break;
case 2:
printArray(waterIntakes);
break;
case 3:
tmp = average(waterIntakes);
cout << "On average you drank " << tmp << "L a day";
break;
case 4:
under_average(waterIntakes);
break;
case 5:
max_day(waterIntakes);
break;
case 6:
in_limit(waterIntakes);
break;
case 7:
tmp = sum(waterIntakes);
cout << "Vsa voda popita" << tmp;
break;
case 0:
running = false;
break;
default:
cout << "Wrong selection!" << endl;
break;
}
cout << endl;
} while (running);
return 0;
}
//isNormal bool med

View File

@@ -0,0 +1,59 @@
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
void menu() {
cout << "============================" << endl;
cout << "=========== MENU ===========" << endl;
cout << "============================" << endl;
cout << "1 ... GENERATE WATER INTAKES" << endl;
cout << "2 ... PRINT WATER INTAKES" << endl;
cout << "0 ... EXIT" << endl;
cout << "============================" << endl;
cout << "Select: ";
}
void fillVector(vector<WaterIntake*> &waterIntakes, const unsigned int size) {
for (unsigned int i = 0; i < size; i++) {
waterIntakes.push_back(new WaterIntake(3, 3, 2022, (float)(rand() % 31 + 5)/10.f));
}
}
void printVector(const vector<WaterIntake*> &waterIntakes) {
for (unsigned int i = 0; i < waterIntakes.size(); i++)
cout << waterIntakes[i]->toString() << ((i < waterIntakes.size() - 1) ? ", " : ".") << std::endl;
}
int main() {
const unsigned int days = 30;
vector<WaterIntake*> waterIntakes;
srand(time(nullptr));
bool running = true;
int selection;
do {
menu();
cin >> selection;
switch (selection) {
case 1:
fillVector(waterIntakes, days);
break;
case 2:
printVector(waterIntakes);
break;
case 0:
running = false;
break;
default:
cout << "Wrong selection!" << endl;
break;
}
cout << endl;
} while (running);
return 0;
}

View File

@@ -0,0 +1,21 @@
Napišite razred Time, ki naj ima:
privatne instančne spremenljivke:
hour,
minute in
second.
razredne spremenljivke:
maxHour, ki je konstanta in predstavlja maksimalno vrednost, in sicer 24
noonHour, ki je konstanta in predstvalja opoldne, in sicer ima vrednost 12
javni konstruktor s 3 parametri (pri tem pazite, da nastavite vrednosti le, če je čas veljaven, sicer nastavite vse vrednosti na 0),
javno metodo toString, ki vrne čas kot string, in sicer v formatu hh:mm:ss (npr. 14:05:30),
javno metodo toString12HourFormat, ki vrne čas kot string, vendar je čas prilagojen za 12-urni časovni sistem. Torej je potrebno ustrezno spremeniti čas in na koncu dodati še "AM"/"PM". Format pri tem zapisu je hh:mm:ss XM (npr. 18:03:01 --> 06:03:01 PM),
javno razredno metodo isTimeValid(unsigned int hour, unsigned int minute, unsigned int second), ki preveri, ali je možen čas glede na podane podatke. Potrebno je upoštevati maksimalne vrednosti, in sicer 23:59:59. Primer: isTimeValid(14, 3, 43) vrne true, isTimeValid(16, 67, 91) vrne false.
javno razredno metodo parse(std::string time), ki prejme čas v obiki niza (v formatu hh:mm:ss), razbere posamezne vrednosti in vrne čas tipa Time.
V glavnem programu prikažite delovanje vseh metod. Ustvarite vsaj 5 različnih objektov razreda Time in na ta način preverite, ali metode pokrivajo vse možne scenarije.
Ostali napotki pri reševanju naloge:
Če potrebujete kako metodo get ali set (za posamezno instančno spremenljivko), si jo zapišite.
V nalogi uporabite kazalec this na vseh mestih, kjer je smiselno.
V nalogi uporabite določilo const pri vseh metodah, kjer je smiselno.
substr: https://cplusplus.com/reference/string/string/substr/

View File

@@ -0,0 +1,62 @@
#include "Time.h"
#include <string>
Time::Time() : hour(0), minute(0), second(0) {}
Time::Time(unsigned int hour) : Time(hour, 0, 0) {}
Time::Time(unsigned int hour, unsigned int minute, unsigned int second) : hour(hour), minute(minute), second(second) {
if (hour >= MAX_HOUR || minute >= 60 || second >= 60) {
this->hour = 0;
this->minute = 0;
this->second = 0;
}
}
std::string Time::toString() const {
return std::to_string(hour) + ":" + std::to_string(minute) + ":" + std::to_string(second);
}
std::string Time::toString12HourFormat() const {
std::string ret;
if (hour > NOON_HOUR) ret = std::to_string(hour - 12); //" PM";
if (hour == NOON_HOUR) ret = std::to_string(hour); //" PM";
if (hour < NOON_HOUR) ret = std::to_string(hour); // " AM";
if (hour == 0) ret = std::to_string(12); //AM
ret = ret + ":" + std::to_string(minute) + ":" + std::to_string(second);
return ret;
}
bool Time::isTimeValid(unsigned int hour, unsigned int minute, unsigned int second) {
return !(hour >= MAX_HOUR || minute >= 60 || second >= 60);
}
Time Time::parse(const std::string &time) {
unsigned int h = std::stoi(time.substr(0, 2));
unsigned int m = std::stoi(time.substr(3, 2));
unsigned int s = std::stoi(time.substr(6, 2));
return {h, m, s};
}
const Time* Time::maxTime(const Time *time1, const Time *time2) {
unsigned int timeSec1 = time1->second + time1->minute * 60 + time1->hour * 3600;
unsigned int timeSec2 = time2->second + time2->minute * 60 + time2->hour * 3600;
return (timeSec1 > timeSec2) ? time1 : time2;
}
unsigned int Time::getHour() const {
return hour;
}
unsigned int Time::getMinute() const {
return minute;
}
unsigned int Time::getSecond() const {
return second;
}

View File

@@ -0,0 +1,39 @@
#ifndef NALOGA0301_TIME_H
#define NALOGA0301_TIME_H
#include <string>
/*#define MAX_HOUR 24
#define NOON_HOUR 12*/
class Time {
private:
unsigned int hour, minute, second;
public:
const static unsigned int MAX_HOUR = 24;
const static unsigned int NOON_HOUR = 12;
Time();
explicit Time(unsigned int hour);//time(hour,0,0);
Time(unsigned int hour, unsigned int minute, unsigned int second);
std::string toString() const;
std::string toString12HourFormat() const;
static bool isTimeValid(unsigned int hour, unsigned int minute, unsigned int second);
static Time parse(const std::string &time);
static const Time *maxTime(const Time *time1, const Time *time2);
unsigned int getHour() const;
unsigned int getMinute() const;
unsigned int getSecond() const;
};
#endif //NALOGA0301_TIME_H

View File

@@ -0,0 +1,38 @@
#include <iostream>
#include "Time.h"
#include <string>
int main() {
std::cout << "Hello, World!" << std::endl;
Time a;
Time b(1, 45, 55);
Time c(20);
Time e(1, 65, 55);
Time f(25);
std::cout << a.toString() << "\n";
std::cout << b.toString() << "\n";
std::cout << c.toString() << "\n";
std::cout << e.toString() << "\n";
std::cout << f.toString() << "\n\n";
std::cout << a.toString12HourFormat() << "\n";
std::cout << b.toString12HourFormat() << "\n";
std::cout << c.toString12HourFormat() << "\n";
std::cout << e.toString12HourFormat() << "\n";
std::cout << f.toString12HourFormat() << "\n\n";
std::string tim1 = "01:42:48";
a = Time::parse(tim1);
tim1 = "01:62:48";
e = Time::parse(tim1);
std::cout << a.toString() << "\n";
std::cout << e.toString() << "\n\n";
std::cout << Time::isTimeValid(50, 22, 65)<< "\n";
const Time *g = new Time(20,56,5);
const Time *h = new Time(18,4,6);
std::cout << Time::maxTime(g,h)->toString();
return 0;
}

View File

@@ -0,0 +1,14 @@
Napišite razred TextUtility, ki naj ima privatni privzeti konstruktor.
Razredu dodajte javne razredne metode:
capitalize(const std::string &str), ki kot argument prejme niz in vrne niz, ki ima za vsakim končnim ločilom (. ,! in ?) veliko začetnico. V primeru, da ugotovimo, da se velika začetnica že nahaja, na tistem mestu ne spremenimo nič. Pazite tudi na to, da je prva črka v nizu z veliko. Primer: lorem ipsum dolor, adipiscing magna? facil isi 2.5 morbi tempus urna id. Gravida non tellus orci! molestieac sed lectus. --> Lorem ipsum dolor, adipiscing magna? Facil isi 2.5 morbi tempus urna id. Gravida non tellus orci! Molestieac sed lectus.
toUpperCase(const std::string &str), ki kot argument prejme niz in vrne niz, v katerem so vse črke velike tiskane. Primer: Lorem ipsum dolor 12, adipiscing Magna. --> LOREM IPSUM DOLOR 12, ADIPISCING MAGNA.
isNumeric(const std::string &str), ki kot argument prejme niz in preveri, ali se v tem nizu nahajo le števke (0-9) . Primer: "432423" --> true, "4234 234" --> false, "4453asd" --> false
contains(const std::string &str, const std::string &substr), ki kot argument prejme niz in iskani niz. Metoda vrne prvi indeks, kje se iskani niz pojavi v nizu. V primeru, da se iskani niz ne nahaja znotraj niza, potem vrnemo -1. Algoritem napišite sami in ni dovoljena uporaba knjižnice. Primer: contains("Lorem ipsum dolor.", "sum") vrne 8, contains("Neobvezna naloga.", "goft") vrne -1
Razredu dodajte še eno razredno metodo po lastni izbiri (naj bo vezana na tekst)!
V glavnem programu prikažite delovanje vseh metod. Pri tem pa kličite metode z različnimi argumenti in na ta način preverite, ali metode pokrivajo vse možne scenarije.
Pomoč za delo z nizi v C++. https://en.cppreference.com/w/cpp/string/basic_string

View File

@@ -0,0 +1,74 @@
#include "TextUtility.h"
#include <string>
#include <cctype>
std::string TextUtility::capitalize(const std::string &str) {
std::string ret;
bool nex = false;
auto it = str.begin();
ret.push_back(std::toupper(*it));
it++;
while (it != str.end()) {
if (nex) {
ret.push_back(std::toupper(*it));
nex = false;
} else {
ret.push_back(*it);
}
if ('.' == *it || '!' == *it || '?' == *it) nex = true;
it++;
}
return ret;
}
std::string TextUtility::toUpperCase(const std::string &str) {
std::string ret;
auto it = str.begin();
while (it != str.end()) {
ret.push_back(std::toupper(*it));
it++;
}
return ret;
}
bool TextUtility::isNumeric(const std::string &str) {
auto it = str.begin();
while (it != str.end() && std::isdigit(*it)) ++it;
return !str.empty() && it == str.end();
}
int TextUtility::contains(const std::string &str, const std::string &substr) {
std::size_t found = str.find(substr);
if (found != std::string::npos) return found;
return -1;
}
std::string TextUtility::addSpaces(const std::string &str) {
std::string ret;
auto it = str.begin();
while (it != str.end()) {
ret.push_back(*it);
ret.push_back(' ');
it++;
}
return ret;
}
std::string TextUtility::removeDuplicatedSpaces(const std::string &str) {
std::string ret;
bool next = false;
auto it = str.begin();
while (it != str.end()) {
if (next) {
if(*it != ' ') {
ret.push_back(*it);
}
next = false;
} else {
ret.push_back(*it);
}
if (*it == ' ') next = true;
it++;
}
return ret;
}

View File

@@ -0,0 +1,24 @@
#ifndef NALOGA0302_TEXTUTILITY_H
#define NALOGA0302_TEXTUTILITY_H
#include <string>
class TextUtility {
private:
TextUtility() = default;
public:
static std::string capitalize(const std::string &str);
static std::string toUpperCase(const std::string &str);
static bool isNumeric(const std::string &str);
static int contains(const std::string &str, const std::string &substr);
static std::string addSpaces(const std::string &str);
static std::string removeDuplicatedSpaces(const std::string &str);
};
#endif //NALOGA0302_TEXTUTILITY_H

View File

@@ -0,0 +1,19 @@
#include <iostream>
#include "TextUtility.h"
int main() {
std::cout << "capitalize \n";
std::cout << "Ni.kola!na: " << TextUtility::capitalize("Ni.kola!na") << "\n";
std::cout << "toUpperCase \n";
std::cout << "nikola!: " << TextUtility::toUpperCase("nikola!") << "\n";
std::cout << "isNumeric \n";
std::cout << "59 " << TextUtility::isNumeric("59") << "\n";
std::cout << "nik " << TextUtility::isNumeric("nik") << "\n";
std::cout << "contains \n";
std::cout << "pa in nikola: " << TextUtility::contains("nikola", "pa") << "\n";
std::cout << "ol in nikola: " << TextUtility::contains("nikola", "ol") << "\n";
std::cout << "addSpaces \n";
std::cout << "nikola: " << TextUtility::addSpaces("nikola") << "\n";
std::cout << "nikola hi how are you: " << TextUtility::removeDuplicatedSpaces("nikola hi how are you") << "\n";
return 0;
}

View File

@@ -0,0 +1,7 @@
#include "Artist.h"
Artist::Artist(std::string name, std::string biography, unsigned int day, unsigned int month, unsigned int year) : name(name), biography(biography), dateOfBirth(day, month, year){}
std::string Artist::toString() const {
return name + ", " + biography + ", " + dateOfBirth.toString();
}

View File

@@ -0,0 +1,20 @@
#ifndef NALOGA0201_ARTIST_H
#define NALOGA0201_ARTIST_H
#include "Date.h"
class Artist {
private:
std::string name, biography;
Date dateOfBirth;
public:
Artist() = default;
Artist(std::string name, std::string biography, unsigned int day, unsigned int month, unsigned int year);
~Artist() = default;
std::string toString() const;
};
#endif //NALOGA0201_ARTIST_H

View File

@@ -0,0 +1,13 @@
#include "Artwork.h"
Artwork::Artwork(std::string title, std::string description, int price, int year, Artist *artist, double width, double height, double depth) :
title(title), description(description), price(price), year(year), artist(artist), dimension(width, height, depth){}
std::string Artwork::toString() const {
return "Title: " + title +
"\nDescription: " + description +
"\nPrice: " + std::to_string(price) +
" EUR\nYear: " + std::to_string(year) +
"\nArtist: " + artist->toString() +
"\nDimension: " + dimension.toString() + "\n\n";
}

View File

@@ -0,0 +1,23 @@
#ifndef NALOGA0201_ARTWORK_H
#define NALOGA0201_ARTWORK_H
#include "Artist.h"
#include "Dimension.h"
class Artwork {
private:
std::string title, description;
int price, year;
Artist *artist;
Dimension dimension;
public:
Artwork() = default;
Artwork(std::string title, std::string description, int price, int year, Artist *artist, double width, double height, double depth);
~Artwork() = default;
std::string toString() const;
};
#endif //NALOGA0201_ARTWORK_H

View File

@@ -0,0 +1,7 @@
#include "Date.h"
Date::Date(unsigned int day, unsigned int month, unsigned int year) : day(day), month(month), year(year) {}
std::string Date::toString() const {
return std::to_string(day) + ", " + std::to_string(month) + ", " + std::to_string(year);
}

View File

@@ -0,0 +1,19 @@
#ifndef NALOGA0201_DATE_H
#define NALOGA0201_DATE_H
#include <string>
class Date {
private:
unsigned int day, month, year;
public:
Date() = default;
Date(unsigned int day, unsigned int month, unsigned int year);
~Date() = default;
std::string toString() const;
};
#endif //NALOGA0201_DATE_H

View File

@@ -0,0 +1,9 @@
#include "Dimension.h"
Dimension::Dimension(double width, double height, double depth) : width(width), height(height), depth(depth) {
}
std::string Dimension::toString() const {
return std::to_string(width) + " " + std::to_string(height) + " " + std::to_string(depth);
}

Some files were not shown because too many files have changed in this diff Show More