Return to Snippet

Revision: 60480
at November 10, 2012 10:05 by mhudince


Initial Code
#include <iostream>
#include <cstring>

#include "lista_pokazivac.h"
//#include "lista_polje.h"

using namespace std;

void spoji(zivotinje A[], int i, int k, int j){
     int I=i, J=k+1, K=0;
     zivotinje *B = new zivotinje [j-i+1];

     while (I <= k && J <= j)
			if (A[I].cijena<A[J].cijena)
				memcpy(&B[K++], &A[I++], sizeof(zivotinje) );
			else if (A[I].cijena == A[J].cijena) {
				int n = (strlen(A[I].naziv) < strlen(A[J].naziv)) ? strlen(A[I].naziv) : strlen(A[J].naziv);
				for (int in=0; in<n; in++)
					if (A[I].naziv[in]>A[J].naziv[in])
						memcpy(&B[K++], &A[J++], sizeof(zivotinje) );
					else if (A[I].naziv[i]<A[J].naziv[i])
						memcpy(&B[K++], &A[I++], sizeof(zivotinje) );
			}
            else
				memcpy(&B[K++], &A[J++], sizeof(zivotinje) );

     if (I>k)
        while (J <= j)
			memcpy(&B[K++], &A[J++], sizeof(zivotinje) );
     else
          while (I<=k)
			memcpy(&B[K++], &A[I++], sizeof(zivotinje) );
     for (int I = 0; I <=j-i; I++) {
     	memcpy(&A[i+I], &B[I], sizeof(zivotinje) );
     }
     delete []B;
	 }

void MSort (zivotinje *A, int i, int j) {
     if (i<j) {
              int k=(i+j)/2;
              MSort(A,i,k);
              MSort (A, k+1, j);
              spoji (A,i,k,j);
              }
     }

void MSort (zivotinje A[], int N) {
     MSort (A,0,N-1);
     }



int dodaj (LIST *L, int sifra){
     bool u = 0;
     zivotinje ziv;

     ziv.sifra = sifra;
     cout << "Naziv: ";
     cin.ignore();
     cin.getline(ziv.naziv, 20);
     cout << "Vrsta: ";
     cin.getline(ziv.vrsta, 30);
     cout << "Cijena: ";
     cin >> ziv.cijena;

     do {
         cout << "Dan: ";
         cin >> ziv.d;
     } while (ziv.d <= 0 || ziv.d > 31);
     do {
         cout << "Mjesec: ";
         cin >> ziv.m;
     } while (ziv.m <= 0 || ziv.m > 12);
     do {
         cout << "Godina: ";
         cin >> ziv.g;
     } while (ziv.g <= 1900 || ziv.g > 2100);

     if (INSERTL (ziv, ENDL(L), L) != 0) {
        u = 1;
     }
     cout << endl;
     if (u == 1) return 1;
     else return 0;
}

void ispisi (LIST *L) {
     int br = 0;
     zivotinje z[1000];
     element position = FIRSTL(L);

     while (position != ENDL(L)) {
           z[br] = RETRIEVEL (position, L);
           position = NEXTL (position, L);
           br++;
     }

     for(int j = br-1; j >= 0; j--) {
             cout << "Sifra: " << z[j].sifra << endl;
             cout << "Naziv: " << z[j].naziv << endl;
             cout << "Vrsta: " << z[j].vrsta << endl;
             cout << "Datum: " << z[j].d << "." << z[j].m << "." << z[j].g << "." << endl;
             cout << "Cijena: " << z[j].cijena << endl;
             cout << "---------------" << endl;
     }
}

void pretrazi (LIST *L) {
     int br = 0;
     int brojac = 0;
     zivotinje z[1000];
     element position = FIRSTL(L);

     while (position != ENDL(L)) {
           z[br] = RETRIEVEL (position, L);
           position = NEXTL (position, L);
           br++;
     }

     zivotinje polje[br];

     for (int i = 0; i <= br; i++) {
         if(z[i].g > 2012){
                   polje[brojac] = z[i];
                   brojac ++;
         }
         else if(z[i].g == 2012){
              if(z[i].m > 9){
                     polje[brojac] = z[i];
                     brojac ++;
              }
         }
         else if(z[i].g == 2012){
        	 if(z[i].m == 9){
        		 if(z[i].d > 23){

        			 polje[brojac] = z[i];
        			 brojac ++;}
        	 	 }
         	 }
     	 }
     for (int j = 0; j < brojac; j++) {
         cout << "Sifra: " << polje[j].sifra << endl;
         cout << "Naziv: " << polje[j].naziv << endl;
         cout << "Vrsta: " << polje[j].vrsta << endl;
         cout << "Datum: " << polje[j].d << "." << polje[j].m << "." << polje[j].g << "." << endl;
         cout << "Cijena: " << polje[j].cijena << endl;
         cout << "---------------" << endl;
     }
     cout << "---------------" << endl;
     cout << "Broj: " << brojac << endl;
     cout << "---------------" << endl;
}

int brisi_naziv (LIST *L){
    char naz[20];
    cout << "Unesi naziv zivotinje: ";
    cin.ignore();
    cin.getline (naz, 20);

    element position = FIRSTL(L);

    zivotinje ziv;

    while (position != ENDL(L)) {
          ziv = RETRIEVEL (position, L);
          if (strcmp(ziv.naziv, naz) != 0) {
             //position = position->sljedeci; // position = NEXTL (position, L); -> zbog polja
             position = NEXTL (position, L);
          }
          else {
               DELETEL (position, L);
               return 1;
          }
    }
    return 0;
}

int brisi_vrstu (LIST *L) {
    char v[20];
    cout << "Unesi vrstu zivotinje: ";
    cin.ignore();
    cin.getline (v, 20);

    element position = FIRSTL(L);

    zivotinje ziv;

    while (position != ENDL(L)) {
          ziv = RETRIEVEL (position, L);
          if (strcmp(ziv.vrsta, v) != 0) {
             //position = position->sljedeci; // position = NEXTL (position, L); -> zbog polja
             position = NEXTL (position, L);
          }
          else {
               DELETEL (position, L);
          }
    }
    return 0;
}

void sortiraj (LIST *L) {
     zivotinje z[1000];
     element position = FIRSTL(L);
     int b=0;
     while (position != ENDL(L)) {
           z[b] = RETRIEVEL (position, L);
           position = NEXTL (position, L);
           b++;
     }

     MSort (z, b);


     // ispisuje sortirano
     for(int j=0; j < b; j++) {
             cout << "Sifra: " << z[j].sifra << endl;
             cout << "Naziv: " << z[j].naziv << endl;
             cout << "Vrsta: " << z[j].vrsta << endl;
             cout << "Datum: " << z[j].d << "." << z[j].m << "." << z[j].g << "." << endl;
             cout << "Cijena: " << z[j].cijena << endl;
             cout << "---------------" << endl;
     }
}

int main () {

    int izbor, sifra = 100;
    LIST *L = INITL(L);

    do {
       cout << "IZBORNIK" << endl;
       cout << "======================================================" << endl;
       cout << "1. Dodaj zapis." << endl;
       cout << "2. Ispisi sadrzaj." << endl;
       cout << "3. Pretrazi nakon 23.9.2012." << endl;
       cout << "4. Brisi po nazivu." << endl;
       cout << "5. Brisi vrstu." << endl;
       cout << "6. Sortiraj po cijeni i nazivu." << endl;
       cout << "0. IZLAZ" << endl;
       cout << "------------------------------------------------------" << endl;
       do {
          cout << "IZBOR: ";
          cin >> izbor;
          } while (izbor < 0 || izbor > 6);

       cout << endl;
       switch (izbor) {

              case 0: break;

              case 1:
                   dodaj(L, sifra);
                   sifra ++;
                   break;

              case 2:
                   ispisi(L);
                   break;

              case 3:
                   pretrazi(L);
                   break;

              case 4:
                   brisi_naziv(L);
                   break;
              case 5:
                   brisi_vrstu(L);
                   break;
              case 6:
                   sortiraj(L);
                   break;
              }

       } while (izbor != 0);

    return 0;
}

Initial URL
mhudince1

Initial Description
zadatak 1 za SP

Initial Title
Strukture_podataka_zad_1 (zivotinje.cpp)

Initial Tags
c++

Initial Language
C++