Kapitel 9 - Programm 7 - SCHABL2.CPP

zurück…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    // Kapitel 9 - Programm 7 - SCHABL2.CPP
#include <stdio.h>

const int MAXGROESZE = 128;

template<class JEDER_TYP>
class Stapel
{
    JEDER_TYP Array[MAXGROESZE];
    int StapelZeiger;
    public:
    Stapel(void) { StapelZeiger = 0; };
    void Lege(JEDER_TYP EinDat) { Array[StapelZeiger++] = EinDat; };
    JEDER_TYP Nimm(void)    { return Array[--StapelZeiger]; };
    int Leer(void)       { return (StapelZeiger == 0); };
};

char Name[] = "Johann Gottlieb Fichte";

int main(void)
{
    int x = 12, y = -7;
    float fl = 3.1415;

    Stapel<int> intStapel;
    Stapel<float> floatStapel;
    Stapel<char *> charStapel;

    intStapel.Lege(x);
    intStapel.Lege(y);
    intStapel.Lege(77);
    floatStapel.Lege(fl);
    floatStapel.Lege(-12.345);
    floatStapel.Lege(100.01);
    charStapel.Lege("Das ist Zeile 1");
    charStapel.Lege("Das ist die zweite Zeile");
    charStapel.Lege("Das ist die dritte Zeile");
    charStapel.Lege(Name);

    printf("  int-Stapel ---> ");
    printf("%8d ", intStapel.Nimm());
    printf("%8d ", intStapel.Nimm());
    printf("%8d\n", intStapel.Nimm());

    printf("float-Stapel ---> ");
    printf("%8.3f ", floatStapel.Nimm());
    printf("%8.3f ", floatStapel.Nimm());
    printf("%8.3f\n", floatStapel.Nimm());

    printf("\n     Zeichenketten\n");
    do
    {
        printf("%s\n", charStapel.Nimm());
    } while (!charStapel.Leer());

    return 0;
}


// Ergebnis beim Ausführen
//
//   int-Stapel --->       77       -7     12
// float-Stapel --->  100.010  -12.345  3.141
//
//    Zeichenketten
// Johann Gottlieb Fichte
// Das ist die dritte Zeile
// Das ist die zweite Zeile
// Das ist Zeile 1

zurück…