Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. 2 3 There Will Be Times That You Come To Class & I Dump A Whole Bunch Of New Stuff On You & You Leave Confused! TODAY MAY BE ONE OF THOSE DAYS! You.

Similar presentations


Presentation on theme: "1. 2 3 There Will Be Times That You Come To Class & I Dump A Whole Bunch Of New Stuff On You & You Leave Confused! TODAY MAY BE ONE OF THOSE DAYS! You."— Presentation transcript:

1 1

2 2

3 3 There Will Be Times That You Come To Class & I Dump A Whole Bunch Of New Stuff On You & You Leave Confused! TODAY MAY BE ONE OF THOSE DAYS! You Leave - Review Your Notes/Slides/etc. Do Your Homework. Study! And If You Do Nothing In Between Classes, You Will Probably Stay Confused - Get Behind - Fail

4 4

5 How many of you have ever written a program that had a stack? What do we ever use a stack for? LIFO is an acronym for __________________ Last In First Out FIFO is an acronym for __________________ First In First Out A Stack Is A {LIFO/FIFO} Structure ? __________________ LIFO Stack Applet 5

6 Stack Applet http://www.sci.brooklyn.cuny.edu/~cis22/animations/tsang/html/STACK/stack800.html http://www.sci.brooklyn.cuny.edu/~cis22/animations/tsang/html/STACK/stack800.html 3 Required Primitive Operations For Stack Push – Add To Stack Pop – Remove From Stack Empty – Is Stack Empty Optional Primitive Operations For Stack StackTop – Peek To See What’s On Top [Peek] Resize – Resize A Dynamic Memory Stack Full – Is Stack Full 6

7 7

8 class Stack1 { public: // Constructor // Destructor // Push // Pop // Empty private: }; Class Design Should Often Start With The Data! What Type Of Info Goes Into The Container? Data Structures Are Often Data Driven long int What Is The Capacity Of The Container? 6 Need The Info Container? long int Info[MAX+1], Info 0 1 2 3 4 5 Need Some Reference To Top? # define MAX 5 MAX Top; Top 8

9 # define MAX 5 class Stack1 { public: // Constructor // Destructor // Push // Pop // Empty private: long int Info[MAX + 1]. Top; }; Stack1 S1, S2; Design Really Poor Thus Far? Info 0 1 2 3 4 5 MAX Top Info 0 1 2 3 4 5 MAX Top S1 S2 All Stacks Have To Be The Same Exact Size! We Really Don’t Want To Have To Create One Class For 6 integers, another for 10 integers, another for 100 integers, etc. 9

10 StackInt::StackInt(void) { Max = MAX; Top = -1; // Empty }; StackInt S1; The StackInt Constructor Info Max Top S1 Not the best constructor, but it works! 5 &1000 0 1 2 3 4 5 ? ? ? ? ? ? We Could Write Push, Pop, Empty, etc. 10

11 11

12 class StackInt { public: StackInt (long int NewMax = 10); // Destructor // Push // Pop // Empty private: long int *Info, Max, Top; }; StackInt S1, S2(5), S3(100); What If We Used Dynamic Memory & Passed The Size? Info Max Top S1 StackInt Design is Better Than Stack1 Design? 12 The Dynamic Stack Allows For Stacks To Be Of Different Size!

13 StackInt::StackInt(long int NewMax) { Max = NewMax; Info = new long int [Max + 1]; if (Info == NULL) { puts ("Out Of Memory In StackInt()"); Max = -1; } Top = -1; // Empty }; StackInt S1(5); The StackInt Constructor Info Max Top S1 Not the best constructor, but it works! 5 &1000 0 1 2 3 4 5 ? ? ? ? ? ? 13 We Could Write Push, Pop, Empty, etc.

14 14

15 class StackInt { public: StackInt (long int NewMax = 10); ~StackInt (void); bool Empty (void); bool Full (void); bool Pop (long int & OldInfo); bool Push (long int NewInfo); bool StackTop (long int & OldInfo); # ifdef STACK_DIAGNOSTIC_LEVEL //----------------------------------- void Display(char Message[]="", long int NoToDisplay = 0); # endif // STACK_DIAGNOSTIC_LEVEL --------------------------- private: long int *Info; long int Max, Top; }; # ifdef STACK_DIAGNOSTIC_LEVEL //----------------------------------- void TestStackInt (void); # endif // STACK_DIAGNOSTIC_LEVEL ---------------------------------- # endif //STACK_CLASS ============================================== Putting It All Together! Good But Not Great! Why Not Great? What If We Needed A Stack For float, char, int, short int, double, Athlete, Part, Employee, etc.? 15

16 16

17 class StackFloat { public: StackFloat (long int NewMax = 10); ~StackFloat (void); bool Empty (void); bool Full (void); bool Pop ( float & OldInfo); bool Push ( float NewInfo); bool StackTop ( float & OldInfo); # ifdef STACK_DIAGNOSTIC_LEVEL //----------------------------------- void Display(char Message[]="", long int NoToDisplay = 0); # endif // STACK_DIAGNOSTIC_LEVEL --------------------------- private: float *Info; long int Max, Top; }; # ifdef STACK_DIAGNOSTIC_LEVEL //----------------------------------- void TestStackFloat (void); # endif // STACK_DIAGNOSTIC_LEVEL ---------------------------------- # endif //STACK_CLASS ============================================== Change long int to float Rename Class, Constructor, Destructor 17

18 18

19 class StackChar { public: StackChar (long int NewMax = 10); ~StackChar (void); bool Empty (void); bool Full (void); bool Pop ( char & OldInfo); bool Push ( char NewInfo); bool StackTop ( char & OldInfo); # ifdef STACK_DIAGNOSTIC_LEVEL //----------------------------------- void Display(char Message[]="", long int NoToDisplay = 0); # endif // STACK_DIAGNOSTIC_LEVEL --------------------------- private: char *Info; long int Max, Top; }; # ifdef STACK_DIAGNOSTIC_LEVEL //----------------------------------- void TestStackFloat (void); # endif // STACK_DIAGNOSTIC_LEVEL ---------------------------------- # endif //STACK_CLASS ============================================== Change long int to char Rename Class, Constructor, Destructor Could Do Likewise for Employee, short int, double, Athlete, etc. 19

20 20

21 21

22 22

23 typedef long int InfoType; class Stack2 { public: StackInt (long int NewMax = 10); ~StackInt (void); bool Empty (void); bool Full (void); bool Pop (InfoType & OldInfo); bool Push (InfoType NewInfo); bool StackTop (InfoType & OldInfo); # ifdef STACK_DIAGNOSTIC_LEVEL //----------------------------------- void Display(char Message[]="", long int NoToDisplay = 0); # endif // STACK_DIAGNOSTIC_LEVEL --------------------------- private: InfoType *Info; long int Max, Top; }; # ifdef STACK_DIAGNOSTIC_LEVEL //----------------------------------- void TestStackInt (void); # endif // STACK_DIAGNOSTIC_LEVEL ---------------------------------- # endif //STACK_CLASS ============================================== This is even better; only one change is required to transfer the stack from one datatype to another. Many Languages have some type of TypeDef Including C & C++ Problem? What if a single program has two stacks of different datatypes? 23

24 24

25 25

26 26

27 27

28 Extract The File Into C:\Temp Name The Folder TomH-Stack  (Use Your Name) 28

29 Enter Your Name Twice Update The Rest Of The Documentation Later 29

30 The Program Should Compile 30

31 Comment Out The define STACK_DIAGNOSTIC_LEVEL Those Includes Necessary For Testing No Longer Need ToBe Included In The Project 31

32 Comment Out The define STACK_DIAGNOSTIC_LEVEL All Test Code Goes Away With One Comment! // When The Class Is Changed Later, All Testing Will Return By Deleting The Comment // Delete The Comment // 32

33 33

34 Info Max Top S1 5 &1000 0 1 2 3 4 5 ? ? ? ? ? ? 34 Write The Code Necessary To Set Up The Template Stack With A Dynamic Array That Matches The Model To The Right  template Stack :: Stack (long int NewMax) { }

35 When You Get The Constructor & Destructor Compiling, Uncomment Out The Test Code For Diagnostic Level 1  Execute  Examine Output We Need Some Way To See That The Constructor Worked! 35

36 36

37 Display Function template void Stack ::Display(char Message[], long int NoToDisplay) { long int Postition;; if (NoToDisplay == -1) NoToDisplay = Max; else NoToDisplay = Top; if(strlen(Message) > 0) puts(Message); for (Postition = NoToDisplay; Postition >= 0; Postition--) { puts(" |----------------------------------------------------|"); fflush(stdout); cout << setw(5) << right << Postition << " | "; cout << setw(50) << Info[Postition]; cout << " |\n"; cout.flush(); } if (Top == -1) { puts(" |----------------------------------------------------|"); puts(" | Stack Is Empty |"); puts(" |----------------------------------------------------|"); printf(" Max = %3ld Top = %3ld &=%ld &=%X\n\n", Max, Top, &Info[0], &Info[0]); } else { puts(" |----------------------------------------------------|"); printf(" Max = %3ld Top = %3ld &=%ld &=%X\n\n", Max, Top, &Info[0], &Info[0]); } 37

38 Set The Level 2  Execute  Examine Output Look At The Various Ways You Can Call The Function & What Each Does Display With Data 38

39 Stack IntStack1; IntStack1.Display("Contents Of IntStack1", -1); IntStack1 shall contain 11 Part elements – the default. Since the built in int class has no initialization in the constructor, the contents of this container is initialized to garbage in memory. 39

40 Stack FloatStack4(4); FloatStack5.Display("Contents Of FloatStack4", -1); FloatStack4 shall contain 5 float elements – the default. Since the built in Float class has no initialization in the constructor, the contents of this container is initialized to garbage in memory. 40

41 Stack CharStack5(5); CharStack5.Display("Contents Of CharStack5", -1); CharStack5 shall contain 6 char elements. Since the built in char class has no initialization in the constructor, the contents of this container is initialized to garbage in memory. 41

42 Stack ClassStack8(8); ClassStack8.Display("Contents Of ClassStack8", -1); ClassStack8 shall contain 9 Student elements. Since the Student class has a constructor to initialize the data, the contents of this container is initialized accordingly! 42

43 Stack GarageStack9(9); GarageStack9.Display("Contents Of GarageStack9", -1); GarageStack9 shall contain 10 Auto elements. Since the Auto class has a constructor to initialize the data, the contents of this container is initialized accordingly! 43

44 44

45 Info Max Top S1 5 &1000 0 1 2 3 4 5 ? ? ? ? ? ? 45 Write The Code For Empty S1 is Empty!  template bool Stack ::Empty (void) { } Empty=True! Info Max Top S2 0 5 &1500 0 1 2 3 4 5 5 ? ? ? ? ? 45 Empty=False!

46 When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 3  Execute  Examine Output 46

47 47

48 Write The Code For Push - 1 template bool Stack ::Push (InfoType NewInfo) { return (?); } if (IntStack1.Push (10)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); Watch Before You Begin To Code 48

49 Write The Code For Push - 2 template bool Stack ::Push (InfoType NewInfo) { return (?); } Watch Before You Begin To Code if (IntStack1.Push (20)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 49

50 Write The Code For Push - 3 Watch Before You Begin To Code if (IntStack1.Push (30)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 50

51 Write The Code For Push - 4 Watch Before You Begin To Code if (IntStack1.Push (40)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 51

52 Write The Code For Push - 5 Watch Before You Begin To Code if (IntStack1.Push (50)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 52

53 Write The Code For Push - 6 Watch Before You Begin To Code if (IntStack1.Push (50)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 53

54 Write The Code For Push - 7 if (IntStack1.Push (60)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 54

55 When You Get The PushCompiling, Uncomment Out The Test Code For Diagnostic Level 4  Execute  Examine Output 55

56 56

57 Info Max Top S1 5 &1000 0 1 2 3 4 5 ? ? ? ? ? ? 57 Write The Code For Empty S1 is Empty!  template bool Stack ::Empty (void) { } Empty=True! Info Max Top S2 0 5 &1500 0 1 2 3 4 5 5 ? ? ? ? ? 57 Empty=False!

58 When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 5  Execute  Examine Output 58

59 59

60 Info Max Top S1 5 5 &1000 0 1 2 3 4 5 2 4 3 1 8 6 60 Write The Code For Full S1 is Full!  template bool Stack ::Full(void) { } Full=True! Info Max Top S2 0 5 &1500 0 1 2 3 4 5 5 ? ? ? ? ? 60 Full=False!

61 When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 6  Execute  Examine Output 61

62 62

63 Write The Code For Pop - 1 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); Watch Before You Begin To Code 50 OldInfo 63

64 rite The Code For Pop - 2 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); Watch Before You Begin To Code 40 OldInfo 64

65 Write The Code For Pop - 3 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); Watch Before You Begin To Code 30 OldInfo 65

66 Write The Code For Pop - 4 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); Watch Before You Begin To Code 20 OldInfo 66

67 Write The Code For Pop - 5 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); Watch Before You Begin To Code 10 OldInfo 67

68 Write The Code For Pop - 6 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); 68

69 When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 7  Execute  Examine Output 69

70 70

71 Write The Code For StackTop - 1 template bool Stack ::StackTop (InfoType &OldInfo) { return (?); } if (IntStack1.StackTop(TopInfo)) printf("TopInfo = %ld\n\n", OldInfo); else puts ("Stack Must Be Empty!\n"); Watch Before You Begin To Code 50 TopInfo 71

72 Write The Code For StackTop - template bool Stack ::StackTop (InfoType &OldInfo) { return (?); } if (IntStack1.StackTop(TopInfo)) printf("TopInfo = %ld\n\n", OldInfo); else puts ("Stack Must Be Empty!\n"); Ben Guest Record TopInfo Watch Before You Begin To Code 72

73 Write The Code For StackTop - 3 template bool Stack ::StackTop (InfoType &OldInfo) { return (?); } if (IntStack1.StackTop(TopInfo)) printf("TopInfo = %ld\n\n", OldInfo); else puts ("Stack Must Be Empty!\n"); ? TopInfo Stack Must Be Empty 73

74 When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 8  Execute  Examine Output 74

75 75

76 template bool Stack :: Resize (long int SizeChange) { return (?); } Write The Code For Resize - 1 if (CSCI_2320.Push (Hunter)) CSCI_2320.Display(); else puts(" Attempted Stack Overflow"); Change Push  Watch Before You Begin To Code 76

77 bool Resize (long int SizeChange = 5) Write The Code For Resize - 2 Info = &1774732 Attempt To Create NewInfo With (Max + SizeChange) + 1 Records  No Room For Hunter NewInfo = &1809260 if (NewInfo = NULL) return (UNSUCCESSFUL)! Copy Records 0 - Top From Info To NewInfo Watch Before You Begin To Code 77

78 bool Resize (long int SizeChange = 5) Write The Code For Resize - 3 Info = &1774732 Delete/Return The Memory Associated With Info Point Info To The Memory Associated With NewInfo NewInfo = &1809260 Info = &1809260 Watch Before You Begin To Code 78

79 bool Resize (long int SizeChange = 5) Write The Code For Resize - 4 Things Look Like This, But We Are Not Done Yet. What Is Wrong With This Picture? We Need To Update The Max! Max = Max + SizeChange 10 Watch Before You Begin To Code 79

80 Write The Code For Resize - 5 if (CSCI_2320.Push (Hunter)) CSCI_2320.Display(); else puts(" Attempted Stack Overflow"); 80

81 When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 9  Execute  Examine Output 81

82 82

83 template void Stack :: operator = (const Stack & S) { } Write The Code For Operator = -1 If we do not overload the = operator, then we would have the following Shallow Copy 83

84 template void Stack :: operator = (const Stack & S) { } Write The Code For Operator = -2 CSCI3343 = CSCI2320; You Will Use Many Of The Commands & Concepts That We Used In Resize! Slightly Different Order! 84

85 When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 10  Execute  Examine Output 85

86 86

87 Remember The Microsoft Office Example Structured Language Developed Windows Version First Mac Version Cost 88% of Windows Version C++ Developed Windows Version First Wrote Parallel Versions Of A Few Short Utilities To Open/Close File, Move Cursor To Row/Col, Minimize Window, etc. using the basic operating system calls. Mac Version Ran In One Day – had to tweek screen because of different pixel resolution – alter images on user manual. Function OverloadsOperator Overloads.hpp &.cpp SeparationStream overloads Templates 87

88 88

89 89

90 90


Download ppt "1. 2 3 There Will Be Times That You Come To Class & I Dump A Whole Bunch Of New Stuff On You & You Leave Confused! TODAY MAY BE ONE OF THOSE DAYS! You."

Similar presentations


Ads by Google