/ Published in: C++
Datoteka kod koje se nalazi main funkcija. Glavna datoteka kako bi se pokrenuo program
Expand |
Embed | Plain Text
#include <iostream> #include <string> //#include "lista_polje.h" #include "lista_pokazivac.h" using namespace std; int n = 0; bool AddNewElement(_list *list) { _tempList temp; cin.clear(); cin.ignore(); cout<<"Naziv: "; getline(cin,temp.naziv); cout<<"Vrsta: "; getline(cin,temp.vrsta); cout<<"Datum: "; getline(cin,temp.datum); cout<<"Cijena: "; cin>>temp.cijena; temp.sifra = n; n++; if(InsertL(temp,EndL(list),list)) return true; n--; return false; } bool Print(_list *list) { if (list->next == 0) return false; List last = EndL(list); // zbog typdef svejedno dal _list* ili int _tempList search; while (last != FirstL(list)) { last = PreviousL(last,list); search = RetrieveL(last,list); cout<<"Sifra: "<<search.sifra<<endl; cout<<"Naziv: "<<search.naziv<<endl; cout<<"Vrsta: "<<search.vrsta<<endl; cout<<"Datum: "<<search.datum<<endl; cout<<"Cijena: "<<search.cijena<<endl<<endl; } return true; } bool Print1(_list *list) // format datuma mora biti XX.XX.XXXX { if (list->next == 0) return false; List index = FirstL(list); _tempList search; string buff; string temp; bool print = false; int nPrinted = 0; while (index != EndL(list)) { search.datum.clear(); buff.clear(); temp.clear(); search = RetrieveL(index,list); index = NextL(index,list); // XX.XX.XXXX // 0123456789 for (int i = 6;i<10;i++) { buff = search.datum[i]; temp+=buff; } if (temp>"2012") print = true; else if (temp == "2012") { buff.clear(); temp.clear(); for (int i = 3;i<5;i++) { buff = search.datum[i]; temp+=buff; } if (temp>"09") print = true; else if ( temp == "09") { buff.clear(); temp.clear(); for (int i = 0;i<2;i++) { buff = search.datum[i]; temp+=buff; } if (temp>"23") print = true; } } if (print) { cout<<"Sifra: "<<search.sifra<<endl; cout<<"Naziv: "<<search.naziv<<endl; cout<<"Vrsta: "<<search.vrsta<<endl; cout<<"Datum: "<<search.datum<<endl; cout<<"Cijena: "<<search.cijena<<endl<<endl; nPrinted++; } print = false; } cout<<"Ukupan broj: "<<nPrinted<<endl; return true; } bool Delete(_list *list) { string temp; cout<<"Unesite naziv zivotinje koju zelite izbrisati: "; cin.clear(); cin.ignore(); getline(cin,temp); if(DeleteL(LocateL(temp,list),list)) return true; cout<<"Krivo uneseni naziv ! "<<endl; return false; } bool DeleteVrsta(_list *list) { string temp; cout<<"Unesite naziv vrste koju zelite izbrisati: "; cin>>temp; List first = FirstL(list); _tempList search; bool deleted = false; while (first != EndL(list)) { search = RetrieveL(first,list); if (search.vrsta == temp) { DeleteL(first,list); first = FirstL(list); deleted = true; } else first = NextL(first,list); } if (deleted) return true; cout<<"Krivo unesena vrsta ! "<<endl; return false; } void MergeSort2(_tempList *tempList,int tempN); void MergeSort2(_tempList *tempList,int i,int j); void Merge2(_tempList *tempList,int i,int k,int j); bool Sort2(_list *list) { int tempN = 0; List searchTempN = FirstL(list); while (searchTempN != EndL(list)) { searchTempN = NextL(searchTempN,list); tempN++; } _tempList *tempList = new _tempList[tempN]; List search = FirstL(list); _tempList temp; int i = 0; while (search != EndL(list)) { temp = RetrieveL(search,list); tempList[i] = temp; i++; search = NextL(search,list); } MergeSort2(tempList,tempN); for (int i = 0; i< tempN;i++) { cout<<"Sifra: "<<tempList[i].sifra<<endl; cout<<"Naziv: "<<tempList[i].naziv<<endl; cout<<"Vrsta: "<<tempList[i].vrsta<<endl; cout<<"Datum: "<<tempList[i].datum<<endl; cout<<"Cijena: "<<tempList[i].cijena<<endl<<endl; } return true; } void Merge2(_tempList *tempList,int i,int k,int j) { int I = i, J = k+1, K = 0; _tempList *tempArray = new _tempList [j-i+1]; while ( I <= k && J <= j) { if (tempList[I].cijena > tempList[J].cijena) tempArray[K++] = tempList[I++]; else if (tempList[I].cijena == tempList[J].cijena) { if (tempList[I].naziv > tempList[J].naziv) tempArray[K++] = tempList[I++]; else tempArray[K++] = tempList[J++]; } else tempArray[K++] = tempList[J++]; } if ( I > k) while ( J <= j) tempArray[K++] = tempList[J++]; else while ( I <= k) tempArray[K++] = tempList[I++]; for (int l = 0; l <= j-i; l++) tempList[i+l] = tempArray[l]; delete []tempArray; } void MergeSort2(_tempList *tempList,int i,int j) { if (i<j) { int k = (i+j)/2; MergeSort2(tempList,i,k); MergeSort2(tempList,k+1,j); Merge2(tempList,i,k,j); } } void MergeSort2(_tempList *tempList,int tempN) { MergeSort2(tempList,0,tempN-1); } int main () { _list *list = new _list; InitL(list); int choice = 0; do { cout<<"1. Dodaj novi element\n"; cout<<"2. Ispis liste\n"; cout<<"3. Ispis zivotinja dostavljenih nakon 23.09.2012.\n"; cout<<"4. Brisanje elementa\n"; cout<<"5. Brisanje vrste\n"; cout<<"6. Merge sort\n"; cout<<"9. Kraj programa\n"; cin>>choice; switch (choice) { case 1: if(!AddNewElement(list)) cout<<"Error\n"; break; case 2: if(!Print(list)) cout<<"Error\n"; break; case 3: if(!Print1(list)) cout<<"Error\n"; break; case 4: if (!Delete(list)) cout<<"Error\n"; break; case 5: if (!DeleteVrsta(list)) cout<<"Error\n"; break; case 6: if (!Sort2(list)) cout<<"Error\n"; break; } }while(choice != 9); DeleteAllL(list); system("pause"); return 0; }
You need to login to post a comment.
