Created
June 14, 2010 14:50
-
-
Save josip/437782 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
using namespace std; | |
struct diplomant { | |
int id, ocjena; | |
// pi = PrezimeIme | |
char pi[30]; | |
} DIPLOMANTI[100]; | |
int UKUPNO = 0; | |
void quicksort(); | |
int quicksort_particija(int, int); | |
char *ocjena(int); | |
void ispisi_diplomanta(int); | |
void unos_podataka(); | |
void pretrazivanje_podataka(); | |
void sortiranje_podataka(); | |
void ucitaj_podatke(); | |
void zapisi_podatke(); | |
int main(); | |
/*** "Pomocne" funkcije ***/ | |
// Prepisano sa http://24bytes.com/Quick-Sort.html | |
int quicksort_particija(int a, int z) { | |
diplomant d = DIPLOMANTI[z]; | |
int j = a - 1; | |
for(int n = a; n < z; n++) | |
if(d.ocjena <= DIPLOMANTI[n].ocjena) { | |
j++; | |
diplomant tmp = DIPLOMANTI[j]; | |
DIPLOMANTI[j] = DIPLOMANTI[n]; | |
DIPLOMANTI[n] = tmp; | |
} | |
DIPLOMANTI[z] = DIPLOMANTI[j + 1]; | |
DIPLOMANTI[j + 1] = d; | |
return j + 1; | |
} | |
void quicksort(int a, int z) { | |
if(a < z) { | |
int p = quicksort_particija(a, z); | |
quicksort(a, p - 1); | |
quicksort(p + 1, z); | |
} | |
} | |
char *ocjena(int n) { | |
switch(n) { | |
case 5: return "Odlican"; | |
case 4: return "Vrlo dobar"; | |
case 3: return "Dobar"; | |
case 2: return "Dovoljan"; | |
case 1: return "Nedovoljan"; | |
} | |
} | |
void ispisi_diplomanta(int n) { | |
diplomant d = DIPLOMANTI[n]; | |
cout << d.id << "\t\t" | |
<< d.pi << "\t\t\t" | |
<< d.ocjena << " (" | |
<< ocjena(d.ocjena) << ")" | |
<< endl; | |
} | |
void ucitaj_podatke() { | |
ifstream dat("diplomanti", ios::binary); | |
while(!dat.eof()) { | |
diplomant d; | |
dat.read((char *)&d, sizeof(d)); | |
DIPLOMANTI[UKUPNO++] = d; | |
//delete d; | |
} | |
dat.close(); | |
cout << "Podaci o " << UKUPNO << " diplomanta su ucitana." << endl; | |
} | |
void zapisi_podatke() { | |
ofstream dat("diplomanti", ios::binary | ios::trunc); | |
if(!dat.is_open()) { | |
cout << "Nije moguce otvoriti datoteku 'diplomanti'!" << endl; | |
return; | |
} | |
for(int n = 0; n < UKUPNO; n++) | |
dat.write((char*)&DIPLOMANTI[n], sizeof(DIPLOMANTI[n])); | |
dat.close(); | |
cout << "Podaci su zapisani u datoteku 'diplomanti'." << endl; | |
} | |
/*** Funkcije glavnog izbornika ***/ | |
void unos_podataka() { | |
cout << "Vec uneseno: " << UKUPNO << endl; | |
int br_unosa; | |
do { | |
cout << "Broj novih unosa: "; | |
cin >> br_unosa; | |
} while(br_unosa + UKUPNO > 100); | |
for(int n = UKUPNO; n < br_unosa + UKUPNO; n++) { | |
diplomant d; | |
cout << " -- Unos podataka za " << n + 1 << ". diplomanta --" << endl; | |
do { | |
cout << "Sifra: "; | |
cin >> d.id; | |
} while(d.id < 100 || d.id > 999); | |
cout << "Prezime i ime: "; | |
cin.ignore(); | |
cin.getline(d.pi, 30); | |
cout << "Ocjena rada: "; | |
cin >> d.ocjena; | |
DIPLOMANTI[n] = d; | |
cout << endl; | |
} | |
UKUPNO += br_unosa; | |
} | |
void pretrazivanje_podataka() { | |
if(UKUPNO < 3) { | |
cout << "Potrebno je unjeti podatke o najmanje tri diplomanta!" << endl; | |
return; | |
} | |
cout << "Broj unosa: " << UKUPNO << endl; | |
int id; | |
do { | |
cout << "Unesite sifru trazenog diplomanta: "; | |
cin >> id; | |
} while(id < 100 || id > 999); | |
bool pronadjen = false; | |
for(int n = 0; n < UKUPNO; n++) | |
if(DIPLOMANTI[n].id == id) { | |
ispisi_diplomanta(n); | |
pronadjen = true; | |
break; | |
} | |
if(!pronadjen) | |
cout << "Diplomant sa sifrom " << id << " nije pronadjen." << endl; | |
} | |
void sortiranje_podataka() { | |
quicksort(0, UKUPNO); | |
cout << "Sifra\t\tIme i prezime\t\t\tOcjena" << endl; | |
for(int n = 0; n < UKUPNO; n++) | |
ispisi_diplomanta(n); | |
} | |
void rad_s_datotekom() { | |
cout << "--- Odaberite opciju --" << endl | |
<< "1. Ucitavanje podataka iz datoteke" << endl | |
<< "2. Zapisivanje podataka u datoteku" << endl | |
<< "3. Povratak" << endl | |
<< endl | |
<< "Vas odabir: "; | |
int opcija; | |
cin >> opcija; | |
if(opcija == 1) | |
ucitaj_podatke(); | |
else if(opcija == 2) | |
zapisi_podatke(); | |
} | |
int main () { | |
while(true) { | |
cout << "--- Odaberite opciju ---" << endl | |
<< "1. Unos podataka o diplomantima" << endl | |
<< "2. Pretrazivanje podataka" << endl | |
<< "3. Sortiranje diplomanata silazno po ocjeni rada" << endl | |
<< "4. Rad s datotekom" << endl | |
<< "5. Izlaz iz programa" << endl | |
<< endl | |
<< "Vas odabir: "; | |
int opcija; | |
cin >> opcija; | |
switch(opcija) { | |
case 1: unos_podataka(); break; | |
case 2: pretrazivanje_podataka(); break; | |
case 3: sortiranje_podataka(); break; | |
case 4: rad_s_datotekom(); break; | |
case 5: return 0; | |
} | |
cout << endl << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment