Skip to content

Instantly share code, notes, and snippets.

@vlazar-
Created December 16, 2012 22:53
Show Gist options
  • Save vlazar-/4313888 to your computer and use it in GitHub Desktop.
Save vlazar-/4313888 to your computer and use it in GitHub Desktop.
Zadatak 3 Strukture podataka 2012/2013 Implementacija atp reda pomocu polja i pokazivaca
//----------------------polje.h---------------------------------------------------------
struct klijent{
int a, b, c, d, starost;
char prezIme[50], datR[10], prioritet[20], transakcija[15];
float stanjeKN, stanjeEUR;
};
struct qu{
klijent el[10000];
int front, rear;
};
typedef struct qu queue;
queue Q;
int AddOne(int n){
return((n+1)%10000);
}
klijent FrontQ(queue &Q){
return Q.el[Q.front];
}
void EnQueueQ(klijent x, queue &Q){
Q.rear = AddOne(Q.rear);
Q.el[Q.rear] = x;
}
void DeQueueQ(queue &Q){
Q.front = AddOne(Q.front);
}
void InitQ(queue &Q){
Q.front = 0;
Q.rear = 9999;
}
bool IsEmptyQ(queue &Q){
if(AddOne(Q.rear) == Q.front)
return true;
else
return false;
}
//----------------------pokazivac.h---------------------------------------------------------
struct klijent{
int a, b, c, d, starost;
char prezIme[50], datR[10], prioritet[20], transakcija[15];
float stanjeKN, stanjeEUR;
};
struct qu{
klijent value;
struct qu *next;
};
struct que{
struct qu *front, *rear;
};
typedef struct que queue;
typedef struct qu *element;
queue Q;
klijent FrontQ(queue Q){
return ((Q.front)->next)->value;
}
void EnQueueQ(klijent x, queue &Q){
element novi = new qu;
novi->next = NULL;
novi->value = x;
(Q.rear)->next = novi;
Q.rear = novi;
}
void DeQueueQ(queue &Q){
element brisi = Q.front;
Q.front = (Q.front)->next;
delete brisi;
}
void InitQ(queue &Q){
element novi = new qu;
novi->next = NULL;
Q.front = novi;
Q.rear = novi;
}
bool IsEmptyQ(queue Q){
if((int)Q.front == (int)Q.rear)
return true;
else
return false;
}
//----------------------banka.cpp---------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstring>
//#include "polje.h"
#include "pokazivac.h"
using namespace std;
klijent k[10000], *polje;
int n, ind = 0, broj = 0, pom;
bool provjera = false;
void generator(){
srand(time(NULL));
int i, d, m, g, pomg;
char pgr1[10], pgr2[10], pgr3[10], datum[10];
bool p = false;
do{
cout << "\nkoliko klijenata zelite generirati: ";
cin >> n;
}while(n <1||n>10000);
cout << "generiranje u tijeku...\n";
for(i=0; i<n; i++){
k[i].a = rand()%(500-150+1)+150;
k[i].b = rand()%(500-150+1)+150;
k[i].c = rand()%(6-1+1)+1;
k[i].d = rand()%(4-1+1)+1;
pomg = rand()%(85-18+1)+18; //dakle samo punoljetni
g = 2012 - pomg;
k[i].starost = g;
if(g%4 == 0 && 4%400 == 0 && g%100 != 0)
p = true;
m = rand()%(12-1+1)+1;
if(m == 4 || m == 6 || m == 9 || m == 11)
d = rand()%(30-1+1)+1;
else if (m == 2){
if(p) d = rand()%(29-1+1)+1;
else d = rand()%(28-1+1)+1;
}
else
d = rand()%(31-1+1)+1;
itoa(d, pgr1, 10); itoa(m, pgr2, 10); itoa(g, pgr3, 10);
strcpy(k[i].datR, pgr1); strcat(k[i].datR, "-"); strcat(k[i].datR, pgr2); strcat(k[i].datR, "-"); strcat(k[i].datR, pgr3);
char ime[10000];
itoa(i+1, ime, 10);
strcpy(k[i].prezIme, "klijent ");
strcat(k[i].prezIme, ime);
k[i].stanjeKN = ((float(rand()) / float(RAND_MAX)) * (20000 - 4000)) -5000;
k[i].stanjeEUR = ((float(rand()) / float(RAND_MAX)) * (2500+350-1)) -700;
if(k[i].c == 1) strcpy(k[i].prioritet, " umirovljenik ");
else if(k[i].c == 2) strcpy(k[i].prioritet, " invalid ");
else if(k[i].c == 3) strcpy(k[i].prioritet, " trudnica ");
else if(k[i].c == 4) strcpy(k[i].prioritet, " VIP klijent ");
else if(k[i].c == 5) strcpy(k[i].prioritet, " klijent banke ");
else if(k[i].c == 6) strcpy(k[i].prioritet, " nije kljent banke ");
if(k[i].d == 1) strcpy(k[i].transakcija, " uplata ");
else if(k[i].d == 2) strcpy(k[i].transakcija, " isplata ");
else if(k[i].d == 3) strcpy(k[i].transakcija, " placanje racuna ");
else if(k[i].d == 4) strcpy(k[i].transakcija, " krediti ");
}
cout << "zapisi uspjesno generirani!\n";
cout << endl;
}
void glavniRed(){
queue pQ; InitQ(pQ);
InitQ(Q);
bool zamjena;
int br = 0, i,j;
cout << "\nrazmjestam klijente po prioritetu...";
for(i= n-1; i>0 && zamjena; i--){
zamjena = false;
for (j=0; j<i; j++)
if(k[j].c > k[j+1].c){
klijent p = k[j];
k[j] = k[j+1];
k[j+1] = p;
zamjena = true;
}
}
cout << "\nglavni red:\n";
for(int i=0; i<n; i++){
cout << "\n\n"
<< i+1
<< "\nime i prezime: " << k[i].prezIme
<< "\ndatum rodenja: " << k[i].datR
<< "\nstanje na tekucem racunu(KN): " << setiosflags(ios::fixed | ios::showpoint)
<< setprecision(2) << k[i].stanjeKN
<< "\nstanje na deviznom racunu(EUR): " << k[i].stanjeEUR
<< "\nprioritet: " << k[i].c << " - " << k[i].prioritet
<< "\ntransakcija: " << k[i].d << " - " << k[i].transakcija << endl;
EnQueueQ(k[i], Q);
}
cout << "\n________________________________________________________________________________\n"
<< "\nklijenti koji na deviznom racunu imaju\nvise od 10000 kn u eurima:"
<< "\n................................................................................\n\n";
while(!IsEmptyQ(Q)){
EnQueueQ(FrontQ(Q), pQ);
DeQueueQ(Q);
}
InitQ(Q);
while(!IsEmptyQ(pQ)){
klijent k = FrontQ(pQ);
if((k.stanjeEUR*7.5)>=10000)
cout << "\nime i prezime klijenta: " << k.prezIme
<< "\nstanje na deviznom racunu u kn: " << k.stanjeEUR*7.5
<< "\n................................................................................\n";
EnQueueQ(FrontQ(pQ), Q);
DeQueueQ(pQ);
}
cout << endl;
}
void brziRed(){
queue bQ, pQ;
InitQ(bQ);
InitQ(pQ);
klijent k;
if(!n){
cout << "prvo generirajte zapise!\n";
return;
}
while(!IsEmptyQ(Q)){
k = FrontQ(Q);
if(FrontQ(Q).d == 3){
EnQueueQ(k, bQ);
n--;
}
else EnQueueQ(k, pQ);
DeQueueQ(Q);
}
int br = 0;
cout << "\nglavni red nakon formiranja brzog reda:\n ";
while(!IsEmptyQ(pQ)){
cout << br++ << ". kijent:\n";
k = FrontQ(pQ);
cout << "\nime i prezime: " << k.prezIme
<< "\nprioritet: " << k.c
<< "\ntransakcija: " << k.d << "\n\n";
EnQueueQ(k, Q);
DeQueueQ(pQ);
}
cout << "\n________________________________________________________________________________\n"
<< "\nbrzi red\n";
if (IsEmptyQ(bQ))
cout << "je prazan jer nema klijenata s prioritetom 3.\n";
cout << endl;
while(!IsEmptyQ(bQ)){
k = FrontQ(bQ);
cout << "\nime i prezime: " << k.prezIme
<< "\nprioritet: " << k.c
<< "\ntransakcija: " << k.d << "\n\n";
DeQueueQ(bQ);
}
};
void makniGR(){
queue pQ;
InitQ(pQ);
klijent k;
while(!IsEmptyQ(Q)){
k = FrontQ(Q);
EnQueueQ(k, pQ);
DeQueueQ(Q);
}
InitQ(Q);
cout << "\n________________________________________________________________________________\n"
<< "\nglavni red nakon brisanja klijenata s navedenim atributima\n";
while(!IsEmptyQ(pQ)){
k = FrontQ(pQ);
if(k.starost < 30 && k.c == 4 && k.d == 1 && k.stanjeKN < 100 ||
k.c == 1 && k.d == 2 && (k.stanjeKN + (k.stanjeEUR*7.5)) > 5000)
n--;
else
cout << "\nime i prezime: " << k.prezIme
<< "\ndatum rodenja: " << k.datR
<< "\nprioritet: " << k.c
<< "\ntransakcija: " << k.d << "\n\n";
EnQueueQ(k, Q);
DeQueueQ(pQ);
}
};
void noviRed(int prelaze){
pom = prelaze;
polje[ind++] = FrontQ(Q);
broj++;
DeQueueQ(Q);
while(!IsEmptyQ(Q))
noviRed(prelaze);
if(--pom >= 0){
broj--;
cout << "* " << polje[--ind].prezIme << endl;
return;
}
if(--broj > 0)
return;
InitQ(Q);
cout << "\nstanje u glavnom redu:"
<< "\n________________________________________________________________________________\n";
for(int i=0; i<ind; i++){
cout << "* " << polje[i].prezIme << endl;
EnQueueQ(polje[i], Q);
}
broj = 0; ind = 0;
}
int main()
{
int izbor;
float prelaze;
srand(time(0));
rand();
do{
cout << "\n----------------------------------------------------------------------BANKA----\n\n"
<< "\t1. generiranje klijenata s prioritetima posluzivanja\n"
<< "\t2. dodavanje klijenata u red\n"
<< "\t3. simulacija reda brzih transakcija\n"
<< "\t4. makni klijente sa slijedecim atributima iz glavnog reda:\n"
<< "\t >Vip klijenati, ispod 30 godina, vrse uplatu, stanje ispod 100 kn\n"
<< "\t >umirovljenici, transakcija isplate, stanje >5000 kn\n"
<< "\t5. otvaranje novog saltera\n"
<< "\t9. kraj rada\n"
<< "\n--------------------------------------------------------------------------------\n"
<< ">> ";
cin >> izbor;
switch(izbor){
case 1:
generator();
break;
case 2:
if(!n){
cout << "prvo generirajte zapise!\n";
break;
}
glavniRed();
provjera = true;
break;
case 3:
if(!n){
cout << "prvo generirajte zapise!\n";
break;
}
if(!provjera){
cout << "slozite glavni red!\n";
break;
}
brziRed();
break;
case 4:
if(!n){
cout << "prvo generirajte zapise!\n";
break;
}
if(!provjera){
cout << "slozite glavni red!\n";
break;
}
makniGR();
break;
case 5:
if(!n){
cout << "prvo generirajte zapise!\n";
break;
}
if(!provjera){
cout << "slozite glavni red!\n";
break;
}
prelaze = 0.3*n;
polje = new klijent[n];
cout << "\nstanje u novom redu:"
<<"\n________________________________________________________________________________\n";
noviRed((int)prelaze);
break;
}
} while(izbor != 9);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment