Listing 1: Klasse prod
1 #inc lude <ios t ream>
2 #inc lude <math . h>
3 #inc lude <iomanip>
4 #inc lude <s t r ing>
5 #d e f i n e N 3
6
7 us ing namespace s td ;
8
9 c l a s s prod
10 f
11 p r i v a t e :
12 s t r i n g pn ;
13 double p r e i s ;
14 uns igned long anz ;
15 publ i c :
16 prod ( s t r ing , double , uns igned long ) ;
17 ~prod ( ) ;
18 void e in ( ) ;
19 void out ( ) ;
20 s t r i n g getPN( ) f r e turn pn ; g
21 double g e tPr e i s ( ) f r e turn p r e i s ; g
22 uns igned long getAnz ( ) f r e turn anz ; g
23 void setPN( s t r i n g pn ) f pn = pn ; g
24 void s e tPr e i s ( double p r e i s ) f p r e i s = p r e i s ; g
25 void setAnz ( uns igned long anz ) f anz = anz ; g
26 g ;
27
28 prod : : prod ( s t r i n g pn="" , double p r e i s =0.0 , uns igned long anz
=0ul ) : pn( pn ) , p r e i s ( p r e i s ) , anz ( anz )
29 f
30 cout << "Produktname : " << pn << endl ;
31 cout << "Pr e i s : " << p r e i s << endl ;
32 cout << "Anzahl : " << anz << endl ;
33 g
34
35 void prod : : out ( )
36 f
37 cout << "Produktname : " << pn << endl ;
38 cout << "Pr e i s : " << p r e i s << endl ;
39 cout << "Anzahl : " << anz << endl ;
40 g
1
1 prod : : ~ prod ( )
2 f
3 cout << "Produktname : " << pn << endl ;
4 cout << "Pr e i s : " << p r e i s << endl ;
5 cout << "Anzahl : " << anz << endl ;
6 g
7
8 void prod : : e in ( )
9 f
10 cout << "Eingabe Produktname : " ;
11 c in >> pn ;
12 c in . c l e a r ( ) ; c in . i gno r e (INT MAX, ' nn ' ) ;
13 cout << "Eingabe Pr e i s : " ;
14 c in >> p r e i s ;
15 c in . c l e a r ( ) ; c in . i gno r e (INT MAX, ' nn ' ) ;
16 cout << "Eingabe Anzahl : " ;
17 c in >> anz ;
18 c in . c l e a r ( ) ; c in . i gno r e (INT MAX, ' nn ' ) ;
19 g
Listing 2: Klasse Stack
1 c l a s s s tack
2 f
3 p r i v a t e :
4
5 prod s t a p e l [N] ;
6 uns igned long m;
7 uns igned long top ;
8
9 publ i c :
10 s tack ( ) ;
11 ~ s tack ( ) ;
12 bool i s F u l l ( ) ;
13 bool isEmpty ( ) ;
14 void Push ( prod p) ;
15 void Pop ( ) ;
16 prod Top ( ) ;
17 prod PopTop ( ) ;
18 uns igned long Length ( ) f r e turn top ; g
19 double gPr e i s ( ) ;
20 g ;
21
22 s tack : : s tack ( ) :m(N) , top (0 ul ) fg
23
2
24 s tack : : ~ s tack ( )
25 f
26 top=0ul ;
27 g
28
29 bool s tack : : i s F u l l ( )
30 f
31 i f ( top<m)
32 r e turn f a l s e ;
33 e l s e
34 r e turn t rue ;
35 g
36
37 bool s tack : : isEmpty ( )
38 f
39 i f ( top==0ul )
40 r e turn t rue ;
41 e l s e
42 r e turn f a l s e ;
43 g
44 void s tack : : Push ( prod p)
45 f
46 i f ( ! i s F u l l ( ) )
47 f
48 s t a p e l [ top]=p ;
49 top++;
50 g
51 g
52
53 void s tack : : Pop ( )
54 f
55 i f ( ! isEmpty ( ) )
56 f
57 top;
58 g
59 g
3
1 prod s tack : : Top ( )
2 f
3 i f ( isEmpty ( ) )
4 f
5 prod p ( "" , 1.0 ,0 ul ) ;
6 r e turn p ;
7 g
8 e l s e
9 f
10 r e turn s t a p e l [ top 1] ;
11 g
12 g
13
14 prod s tack : : PopTop ( )
15 f
16 prod p = Top ( ) ;
17 Pop ( ) ;
18 r e turn p ;
19 g
20
21 double s tack : : gPr e i s ( )
22 f
23 double summe=0.0;
24
25 f o r ( i n t i =0; i<m; i++)
26 f
27 summe += s t a p e l [ i ] . getAnz ( ) s t a p e l [ i ] . g e tPr e i s ( ) ;
28 g
29
30 r e turn summe ;
31 g
4
Listing 3: Klasse Stack
1 i n t main ( )
2 f
3 s tack ps ;
4 prod pp( "Cu" , 1 2 . 0 , 5) ;
5 ps . Push (pp) ;
6 pp . out ( ) ;
7 pp . setPN( "Pb") ;
8 pp . s e tPr e i s ( 1 6 . 0 0 ) ;
9 pp . setAnz (10) ;
10 ps . Push (pp) ;
11 cout<< "nn Name = " << pp . getPN( ) << "nn Pr e i s = " << pp .
g e tPr e i s ( ) <<"nnAnzahl = " <<pp . getAnz ( ) <<endl ;
12 pp . e in ( ) ;
13 ps . Push (pp) ;
14 cout<< "Elementezahl= " <<ps . Length ( )<<endl ;
15 cout . s e t f ( i o s : : boolalpha ) ;
16 cout<<"ps . I sFu l l ( ) = "<<ps . i s F u l l ( )<<endl ;
17 cout<<"Gesamtprei s = "<<ps . gPr e i s ( )<<endl ;
18 pp=ps .Top ( ) ;
19 ps . Pop ( ) ;
20 cout<<"Elementezahl= "<<ps . Length ( )<<endl ;
21 pp . out ( ) ;
22 pp = ps . PopTop ( ) ; cout<<"Elementezahl= "<<ps . Length ( )<<endl ;
23 pp . out ( ) ;
24 pp = ps . PopTop ( ) ; cout<<"Elementezahl= "<<ps . Length ( )<<endl ;
25 pp . out ( ) ;
26 cout . s e t f ( i o s : : boolalpha ) ;
27 cout<<"ps . IsEmpty ( ) = "<<ps . isEmpty ( )<<endl ;
28 ps . Pop ( ) ;
29 pp = ps .Top ( ) ;
30 pp . out ( ) ;
31 c in . ge t ( ) ;
32 r e turn 0 ;
33 g
5