/* autor: Krzysztof 'frimer' Zmijewski
 *
 * prog: " stosik.c " - Program zapisuje/zdejmuje liczby na stos z uzyciem tablicy
 *
 * Kompilator: gcc 4.3.3
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 */

#include <stdio.h>
#include <ctype.h>

const int rozmiarStosu = 10;  	/* rozmiar stosu */
int liczbaElementow = 0; 		/* poczatkowa ilosc elemetow */

/* dodaj element na stos */
int push(int stos[], int nowy) {  
	
	if(liczbaElementow < rozmiarStosu) {	
		stos[liczbaElementow] = nowy;		/* dodaj na stos */
		liczbaElementow++;
	} else if (full(stos)==0) {	/* stos jest juz pełny */
			printf("przepelnienie stosu!!! \n");
	} 
}

/* zdejmij element ze stosu */
int pop(int stos[]) {
	
	if(liczbaElementow != 0) {		/* zdejmij ze stosu */
		stos[liczbaElementow-1] =0;  /* zdjety element zerujemy */
		liczbaElementow--;
	} else if (empty(stos)==1) {	/* stos jest juz pusty */
			printf("stos jest pusty!!! \n");
	}
}

int empty(int stos[]) {

	if(liczbaElementow == 0) {  /* jesli elementy na stosie sa rowne 0 to: */
		return 1; /* pusty */
	} else {	
		return 0; /* pelny */
	}
}

int full(int stos[]) {

	if(liczbaElementow == 0) { /* jesli caly stos jest pelny to: */
		return 1; /* pusty */
	} else {	
		return 0; /* pelny */
	}
}

/* wyswietlamy elementy znajdujace sie na stosie */
int wyswietl(int stos[]) {
int j;

	for(j=0; j<liczbaElementow; j++) { /* wyswietlamy kazdy element tablicy */
		printf("%d ", stos[j]);
	}
	puts("\n");
return;
}

int main(void) {
int err, i, ch, stos[rozmiarStosu];
char opcja;

system("clear");	/* instrukcja systemowa czyszczaca ekran */

	while(1) {	
		printf("Co chcesz zrobic? = lub - : ");
			scanf("%s", &opcja);	
		switch(opcja) {
	case '=':	/* opcja dodawania elementu */
		printf("podaj liczbe: ");
			err=scanf("%d", &ch);
		if (err==1) {	/* warunek sprawdza czy scanf zwrocil wartosc 1 czyli ze pobral liczbe */
			push(stos, ch);
			break;
		} else if (err==0) {	/* jezeli scanf pobral znak nastapi warunkowe zakonczenie programu */
				printf("Wpisano znak... Nastapilo wyjscie z programu !!!\n\n");
			return 0;	
		}
	case '-':		/* opcja zdejmowania elementu */
		pop(stos);
			break;
	case 'w':	/* opcja wyswietlania stosu */
		wyswietl(stos);
			break;
	case 'q':	/* opcja wyjscia z programu */
		printf("\n\tBye...\n\n");
		return 0;
		}
	}

	for(i=0; i<rozmiarStosu; i++) /* tworzymy stos o rozmiarze "rozmiarStosu" o stalym rozmiarze */
		stos[i] =0;	/* zerujemy kazdy element tablicy (stosu) */
	
return 0;
}

