1 /* autor: Krzysztof Zmijewski
2 *
3 * prog: " stosik.c " - Program zapisuje/zdejmuje liczby na stos z uzyciem tablicy
4 *
5 * Kompilator: gcc 4.3.3
6 */
7
8 /*
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301, USA.
23 *
24 */
25
26 #include <stdio.h>
27 #include <ctype.h>
28
29 const int rozmiarStosu = 10; /* rozmiar stosu */
30 int liczbaElementow = 0; /* poczatkowa ilosc elemetow */
31
32 /* dodaj element na stos */
33 int push(int stos[], int nowy) {
34
35 if(liczbaElementow < rozmiarStosu) {
36 stos[liczbaElementow] = nowy; /* dodaj na stos */
37 liczbaElementow++;
38 } else if (full(stos)==0) { /* stos jest juz pełny */
39 printf("przepelnienie stosu!!! \n");
40 }
41 }
42
43 /* zdejmij element ze stosu */
44 int pop(int stos[]) {
45
46 if(liczbaElementow != 0) { /* zdejmij ze stosu */
47 stos[liczbaElementow-1] =0; /* zdjety element zerujemy */
48 liczbaElementow--;
49 } else if (empty(stos)==1) { /* stos jest juz pusty */
50 printf("stos jest pusty!!! \n");
51 }
52 }
53
54 int empty(int stos[]) {
55
56 if(liczbaElementow == 0) { /* jesli elementy na stosie sa rowne 0 to: */
57 return 1; /* pusty */
58 } else {
59 return 0; /* pelny */
60 }
61 }
62
63 int full(int stos[]) {
64
65 if(liczbaElementow == 0) { /* jesli caly stos jest pelny to: */
66 return 1; /* pusty */
67 } else {
68 return 0; /* pelny */
69 }
70 }
71
72 /* wyswietlamy elementy znajdujace sie na stosie */
73 int wyswietl(int stos[]) {
74 int j;
75
76 for(j=0; j<liczbaElementow; j++) { /* wyswietlamy kazdy element tablicy */
77 printf("%d ", stos[j]);
78 }
79 puts("\n");
80 return;
81 }
82
83 int main(void) {
84 int err, i, ch, stos[rozmiarStosu];
85 char opcja;
86
87 system("clear"); /* instrukcja systemowa czyszczaca ekran */
88
89 while(1) {
90 printf("Co chcesz zrobic? = lub - : ");
91 scanf("%s", &opcja);
92 switch(opcja) {
93 case '=': /* opcja dodawania elementu */
94 printf("podaj liczbe: ");
95 err=scanf("%d", &ch);
96 if (err==1) { /* warunek sprawdza czy scanf zwrocil wartosc 1 czyli ze pobral liczbe */
97 push(stos, ch);
98 break;
99 } else if (err==0) { /* jezeli scanf pobral znak nastapi warunkowe zakonczenie programu */
100 printf("Wpisano znak... Nastapilo wyjscie z programu !!!\n\n");
101 return 0;
102 }
103 case '-': /* opcja zdejmowania elementu */
104 pop(stos);
105 break;
106 case 'w': /* opcja wyswietlania stosu */
107 wyswietl(stos);
108 break;
109 case 'q': /* opcja wyjscia z programu */
110 printf("\n\tBye...\n\n");
111 return 0;
112 }
113 }
114
115 for(i=0; i<rozmiarStosu; i++) /* tworzymy stos o rozmiarze "rozmiarStosu" o stalym rozmiarze */
116 stos[i] =0; /* zerujemy kazdy element tablicy (stosu) */
117
118 return 0;
119 }
syntax highlighted by Code2HTML, v. 0.9.1