Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: C++  |  size: 4.35 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Listing 1: Klasse prod
  2. 1 #inc lude <ios t ream>
  3. 2 #inc lude <math . h>
  4. 3 #inc lude <iomanip>
  5. 4 #inc lude <s t r ing>
  6. 5 #d e f i n e N 3
  7. 6
  8. 7 us ing namespace s td ;
  9. 8
  10. 9 c l a s s prod
  11. 10 f
  12. 11 p r i v a t e :
  13. 12 s t r i n g pn ;
  14. 13 double p r e i s ;
  15. 14 uns igned long anz ;
  16. 15 publ i c :
  17. 16 prod ( s t r ing , double , uns igned long ) ;
  18. 17 ~prod ( ) ;
  19. 18 void e in ( ) ;
  20. 19 void out ( ) ;
  21. 20 s t r i n g getPN( ) f r e turn pn ; g
  22. 21 double g e tPr e i s ( ) f r e turn p r e i s ; g
  23. 22 uns igned long getAnz ( ) f r e turn anz ; g
  24. 23 void setPN( s t r i n g pn ) f pn = pn ; g
  25. 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
  26. 25 void setAnz ( uns igned long anz ) f anz = anz ; g
  27. 26 g ;
  28. 27
  29. 28 prod : : prod ( s t r i n g pn="" , double p r e i s =0.0 , uns igned long anz
  30. =0ul ) : pn( pn ) , p r e i s ( p r e i s ) , anz ( anz )
  31. 29 f
  32. 30 cout << "Produktname : " << pn << endl ;
  33. 31 cout << "Pr e i s : " << p r e i s << endl ;
  34. 32 cout << "Anzahl : " << anz << endl ;
  35. 33 g
  36. 34
  37. 35 void prod : : out ( )
  38. 36 f
  39. 37 cout << "Produktname : " << pn << endl ;
  40. 38 cout << "Pr e i s : " << p r e i s << endl ;
  41. 39 cout << "Anzahl : " << anz << endl ;
  42. 40 g
  43. 1
  44. 1 prod : : ~ prod ( )
  45. 2 f
  46. 3 cout << "Produktname : " << pn << endl ;
  47. 4 cout << "Pr e i s : " << p r e i s << endl ;
  48. 5 cout << "Anzahl : " << anz << endl ;
  49. 6 g
  50. 7
  51. 8 void prod : : e in ( )
  52. 9 f
  53. 10 cout << "Eingabe Produktname : " ;
  54. 11 c in >> pn ;
  55. 12 c in . c l e a r ( ) ; c in . i gno r e (INT MAX, ' nn ' ) ;
  56. 13 cout << "Eingabe Pr e i s : " ;
  57. 14 c in >> p r e i s ;
  58. 15 c in . c l e a r ( ) ; c in . i gno r e (INT MAX, ' nn ' ) ;
  59. 16 cout << "Eingabe Anzahl : " ;
  60. 17 c in >> anz ;
  61. 18 c in . c l e a r ( ) ; c in . i gno r e (INT MAX, ' nn ' ) ;
  62. 19 g
  63. Listing 2: Klasse Stack
  64. 1 c l a s s s tack
  65. 2 f
  66. 3 p r i v a t e :
  67. 4
  68. 5 prod s t a p e l [N] ;
  69. 6 uns igned long m;
  70. 7 uns igned long top ;
  71. 8
  72. 9 publ i c :
  73. 10 s tack ( ) ;
  74. 11 ~ s tack ( ) ;
  75. 12 bool i s F u l l ( ) ;
  76. 13 bool isEmpty ( ) ;
  77. 14 void Push ( prod p) ;
  78. 15 void Pop ( ) ;
  79. 16 prod Top ( ) ;
  80. 17 prod PopTop ( ) ;
  81. 18 uns igned long Length ( ) f r e turn top ; g
  82. 19 double gPr e i s ( ) ;
  83. 20 g ;
  84. 21
  85. 22 s tack : : s tack ( ) :m(N) , top (0 ul ) fg
  86. 23
  87. 2
  88. 24 s tack : : ~ s tack ( )
  89. 25 f
  90. 26 top=0ul ;
  91. 27 g
  92. 28
  93. 29 bool s tack : : i s F u l l ( )
  94. 30 f
  95. 31 i f ( top<m)
  96. 32 r e turn f a l s e ;
  97. 33 e l s e
  98. 34 r e turn t rue ;
  99. 35 g
  100. 36
  101. 37 bool s tack : : isEmpty ( )
  102. 38 f
  103. 39 i f ( top==0ul )
  104. 40 r e turn t rue ;
  105. 41 e l s e
  106. 42 r e turn f a l s e ;
  107. 43 g
  108. 44 void s tack : : Push ( prod p)
  109. 45 f
  110. 46 i f ( ! i s F u l l ( ) )
  111. 47 f
  112. 48 s t a p e l [ top]=p ;
  113. 49 top++;
  114. 50 g
  115. 51 g
  116. 52
  117. 53 void s tack : : Pop ( )
  118. 54 f
  119. 55 i f ( ! isEmpty ( ) )
  120. 56 f
  121. 57 top􀀀􀀀;
  122. 58 g
  123. 59 g
  124. 3
  125. 1 prod s tack : : Top ( )
  126. 2 f
  127. 3 i f ( isEmpty ( ) )
  128. 4 f
  129. 5 prod p ( "" , 􀀀1.0 ,0 ul ) ;
  130. 6 r e turn p ;
  131. 7 g
  132. 8 e l s e
  133. 9 f
  134. 10 r e turn s t a p e l [ top 􀀀1] ;
  135. 11 g
  136. 12 g
  137. 13
  138. 14 prod s tack : : PopTop ( )
  139. 15 f
  140. 16 prod p = Top ( ) ;
  141. 17 Pop ( ) ;
  142. 18 r e turn p ;
  143. 19 g
  144. 20
  145. 21 double s tack : : gPr e i s ( )
  146. 22 f
  147. 23 double summe=0.0;
  148. 24
  149. 25 f o r ( i n t i =0; i<m; i++)
  150. 26 f
  151. 27 summe += s t a p e l [ i ] . getAnz ( )  s t a p e l [ i ] . g e tPr e i s ( ) ;
  152. 28 g
  153. 29
  154. 30 r e turn summe ;
  155. 31 g
  156. 4
  157. Listing 3: Klasse Stack
  158. 1 i n t main ( )
  159. 2 f
  160. 3 s tack ps ;
  161. 4 prod pp( "Cu" , 1 2 . 0 , 5) ;
  162. 5 ps . Push (pp) ;
  163. 6 pp . out ( ) ;
  164. 7 pp . setPN( "Pb") ;
  165. 8 pp . s e tPr e i s ( 1 6 . 0 0 ) ;
  166. 9 pp . setAnz (10) ;
  167. 10 ps . Push (pp) ;
  168. 11 cout<< "nn Name = " << pp . getPN( ) << "nn Pr e i s = " << pp .
  169. g e tPr e i s ( ) <<"nnAnzahl = " <<pp . getAnz ( ) <<endl ;
  170. 12 pp . e in ( ) ;
  171. 13 ps . Push (pp) ;
  172. 14 cout<< "Elementezahl= " <<ps . Length ( )<<endl ;
  173. 15 cout . s e t f ( i o s : : boolalpha ) ;
  174. 16 cout<<"ps . I sFu l l ( ) = "<<ps . i s F u l l ( )<<endl ;
  175. 17 cout<<"Gesamtprei s = "<<ps . gPr e i s ( )<<endl ;
  176. 18 pp=ps .Top ( ) ;
  177. 19 ps . Pop ( ) ;
  178. 20 cout<<"Elementezahl= "<<ps . Length ( )<<endl ;
  179. 21 pp . out ( ) ;
  180. 22 pp = ps . PopTop ( ) ; cout<<"Elementezahl= "<<ps . Length ( )<<endl ;
  181. 23 pp . out ( ) ;
  182. 24 pp = ps . PopTop ( ) ; cout<<"Elementezahl= "<<ps . Length ( )<<endl ;
  183. 25 pp . out ( ) ;
  184. 26 cout . s e t f ( i o s : : boolalpha ) ;
  185. 27 cout<<"ps . IsEmpty ( ) = "<<ps . isEmpty ( )<<endl ;
  186. 28 ps . Pop ( ) ;
  187. 29 pp = ps .Top ( ) ;
  188. 30 pp . out ( ) ;
  189. 31 c in . ge t ( ) ;
  190. 32 r e turn 0 ;
  191. 33 g
  192. 5