added more code
This commit is contained in:
212
CPE212/Project_03/main.cpp
Normal file
212
CPE212/Project_03/main.cpp
Normal file
@ -0,0 +1,212 @@
|
||||
//
|
||||
// main.cpp -- 2010 Fall -- Project03 -- Stacks
|
||||
//
|
||||
// Driver program for Stack ADT -- The text files (read by this code) contain a series of commands
|
||||
// that will help you test your Stack ADT code by triggering various Stack class methods.
|
||||
//
|
||||
// DO NOT MODIFY OR SUBMIT THIS FILE
|
||||
//
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <new>
|
||||
#include <cstddef>
|
||||
#include "stack.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
ifstream inputs; // Input file for commands
|
||||
char op; // Hold operation and optional char input
|
||||
int value; // Value input from file
|
||||
string comment; // Holds comment from file
|
||||
Stack* sPtr = NULL; // Will point to TemplateQ object
|
||||
|
||||
// Output usage message if one input file name is not provided
|
||||
if (argc != 2)
|
||||
{
|
||||
cout << "Usage:\n project03 <inputfile>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Attempt to open input file -- terminate if file does not open
|
||||
inputs.open(argv[1]);
|
||||
if (!inputs)
|
||||
{
|
||||
cout << "Error - unable to open input file" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Input and echo header comment from file
|
||||
getline(inputs, comment);
|
||||
cout << endl << '#' << comment << endl;
|
||||
|
||||
// Process commands from input file
|
||||
inputs >> op; // Attempt to input first command
|
||||
while (inputs)
|
||||
{
|
||||
switch (op) // Process operation input from file
|
||||
{
|
||||
case '#': // Test file comment
|
||||
getline(inputs, comment); // Input and echo the comment appearing in the test file
|
||||
cout << '#' << comment << endl;
|
||||
break;
|
||||
|
||||
case 'c': // Parameterized Constructor
|
||||
inputs >> value;
|
||||
cout << endl << "Stack(" << value << ")";
|
||||
try
|
||||
{
|
||||
sPtr = new Stack(value); // Attempt to create a stack object with array size value
|
||||
cout << " -- Successful" << endl;
|
||||
}
|
||||
catch ( std::bad_alloc )
|
||||
{
|
||||
cout << "Failed : Terminating now..." << endl;
|
||||
return 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case '+': // Push
|
||||
inputs >> value;
|
||||
cout << "Push(" << value << ")";
|
||||
try
|
||||
{
|
||||
sPtr->Push(value);
|
||||
cout << " -- successful";
|
||||
}
|
||||
catch (StackFull)
|
||||
{
|
||||
cout << " -- Failed Full Stack";
|
||||
}
|
||||
cout << endl;
|
||||
break;
|
||||
|
||||
case '-': // Pop
|
||||
cout << "Pop() -- ";
|
||||
try
|
||||
{
|
||||
sPtr->Pop();
|
||||
cout << "successful";
|
||||
}
|
||||
catch (StackEmpty)
|
||||
{
|
||||
cout << "Failed Empty Stack";
|
||||
}
|
||||
cout << endl;
|
||||
break;
|
||||
|
||||
case 'f': // IsFull
|
||||
cout << "IsFull() -- ";
|
||||
try
|
||||
{
|
||||
if (sPtr->IsFull())
|
||||
cout << "true";
|
||||
else
|
||||
cout << "false";
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
cout << "operation failed";
|
||||
}
|
||||
cout << endl;
|
||||
break;
|
||||
|
||||
case 'e': // IsEmpty
|
||||
cout << "IsEmpty() -- ";
|
||||
try
|
||||
{
|
||||
if (sPtr->IsEmpty())
|
||||
cout << "true";
|
||||
else
|
||||
cout << "false";
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
cout << "operation failed";
|
||||
}
|
||||
cout << endl;
|
||||
break;
|
||||
|
||||
case 'm': // Make Empty
|
||||
sPtr->MakeEmpty();
|
||||
cout << "MakeEmpty()" << endl;
|
||||
break;
|
||||
|
||||
case 'p': // Print Stack
|
||||
cout << "Print() -- ";
|
||||
sPtr->Print();
|
||||
break;
|
||||
|
||||
case 't': // Top of Stack
|
||||
try
|
||||
{
|
||||
cout << "Top() -- " << sPtr->Top() << endl;
|
||||
}
|
||||
catch (StackEmpty)
|
||||
{
|
||||
cout << "Top() -- Failed Empty Stack" << endl;
|
||||
}
|
||||
break;
|
||||
|
||||
case '>': // Max value within Stack
|
||||
try
|
||||
{
|
||||
cout << "Max() -- " << sPtr->Max() << endl;
|
||||
}
|
||||
catch (StackEmpty)
|
||||
{
|
||||
cout << "Max() -- Failed Empty Stack" << endl;
|
||||
}
|
||||
break;
|
||||
|
||||
case '<': // Min value within Stack
|
||||
try
|
||||
{
|
||||
cout << "Min() -- " << sPtr->Min() << endl;
|
||||
}
|
||||
catch (StackEmpty)
|
||||
{
|
||||
cout << "Min() -- Failed Empty Stack" << endl;
|
||||
}
|
||||
break;
|
||||
|
||||
case '?': // Peek(n) Stack
|
||||
inputs >> value;
|
||||
try
|
||||
{
|
||||
cout << "Peek(" << value << ") -- " << sPtr->Peek(value) << endl;
|
||||
}
|
||||
catch (StackInvalidPeek)
|
||||
{
|
||||
cout << "Peek(" << value << ") -- Failed Invalid Peek" << endl;
|
||||
}
|
||||
break;
|
||||
|
||||
case 's': // Size of Stack
|
||||
cout << "Size() -- " << sPtr->Size() << endl;
|
||||
break;
|
||||
|
||||
case 'z': // Capacity of Stack
|
||||
cout << "Capacity() -- " << sPtr->Capacity() << endl;
|
||||
break;
|
||||
|
||||
case 'd': // Destructor
|
||||
delete sPtr;
|
||||
sPtr = NULL;
|
||||
cout << "~Stack()" << endl << endl;
|
||||
break;
|
||||
|
||||
default: // Error
|
||||
cout << "Error - unrecognized operation '" << op << "'" << endl;
|
||||
cout << "Terminating now..." << endl;
|
||||
return 1;
|
||||
break;
|
||||
}
|
||||
|
||||
inputs >> op; // Attempt to input next command
|
||||
}
|
||||
|
||||
return 0;
|
||||
} // End main()
|
||||
|
BIN
CPE212/Project_03/main.o
Normal file
BIN
CPE212/Project_03/main.o
Normal file
Binary file not shown.
15
CPE212/Project_03/makefile
Normal file
15
CPE212/Project_03/makefile
Normal file
@ -0,0 +1,15 @@
|
||||
# Project03 makefile
|
||||
|
||||
project03: stack.o main.o
|
||||
g++ stack.o main.o -o project03
|
||||
|
||||
stack.o: stack.h stack.cpp
|
||||
g++ -c stack.cpp
|
||||
|
||||
main.o: stack.h main.cpp
|
||||
g++ -c main.cpp
|
||||
|
||||
clean:
|
||||
rm *.o project03
|
||||
|
||||
|
43
CPE212/Project_03/p03input1.txt
Normal file
43
CPE212/Project_03/p03input1.txt
Normal file
@ -0,0 +1,43 @@
|
||||
# p03input1.txt -- Test: Stack(), Push(), Pop(), and Top()
|
||||
|
||||
c 8
|
||||
p
|
||||
t
|
||||
+ 5
|
||||
+ 10
|
||||
+ 15
|
||||
p
|
||||
t
|
||||
+ 20
|
||||
+ 25
|
||||
+ 30
|
||||
p
|
||||
t
|
||||
-
|
||||
-
|
||||
p
|
||||
+ 35
|
||||
+ 40
|
||||
t
|
||||
+ 45
|
||||
t
|
||||
p
|
||||
-
|
||||
-
|
||||
-
|
||||
-
|
||||
-
|
||||
-
|
||||
-
|
||||
-
|
||||
p
|
||||
t
|
||||
d
|
||||
|
||||
|
||||
c 4
|
||||
p
|
||||
-
|
||||
t
|
||||
d
|
||||
|
38
CPE212/Project_03/p03input2.txt
Normal file
38
CPE212/Project_03/p03input2.txt
Normal file
@ -0,0 +1,38 @@
|
||||
# p03input2.txt -- Test IsFull(), IsEmpty(), and MakeEmpty()
|
||||
|
||||
c 8
|
||||
e
|
||||
f
|
||||
+ 5
|
||||
+ 10
|
||||
+ 15
|
||||
+ 20
|
||||
+ 25
|
||||
+ 30
|
||||
p
|
||||
e
|
||||
f
|
||||
m
|
||||
p
|
||||
e
|
||||
f
|
||||
+ 35
|
||||
+ 40
|
||||
+ 45
|
||||
+ 50
|
||||
p
|
||||
e
|
||||
f
|
||||
-
|
||||
-
|
||||
-
|
||||
p
|
||||
e
|
||||
f
|
||||
m
|
||||
p
|
||||
e
|
||||
f
|
||||
d
|
||||
|
||||
|
58
CPE212/Project_03/p03input3.txt
Normal file
58
CPE212/Project_03/p03input3.txt
Normal file
@ -0,0 +1,58 @@
|
||||
# p03input3.txt -- Test Size(), Capacity()
|
||||
|
||||
# Test Size()
|
||||
c 6
|
||||
p
|
||||
s
|
||||
+ 5
|
||||
+ 10
|
||||
p
|
||||
s
|
||||
+ 15
|
||||
+ 20
|
||||
p
|
||||
s
|
||||
+ 25
|
||||
+ 30
|
||||
p
|
||||
s
|
||||
-
|
||||
-
|
||||
-
|
||||
p
|
||||
s
|
||||
-
|
||||
-
|
||||
-
|
||||
p
|
||||
s
|
||||
d
|
||||
|
||||
# Test Capacity()
|
||||
c 2
|
||||
z
|
||||
+ 5
|
||||
z
|
||||
+ 10
|
||||
z
|
||||
+ 15
|
||||
z
|
||||
+ 20
|
||||
z
|
||||
+ 25
|
||||
z
|
||||
+ 30
|
||||
z
|
||||
p
|
||||
-
|
||||
-
|
||||
-
|
||||
z
|
||||
p
|
||||
-
|
||||
-
|
||||
-
|
||||
z
|
||||
p
|
||||
d
|
||||
|
34
CPE212/Project_03/p03input4.txt
Normal file
34
CPE212/Project_03/p03input4.txt
Normal file
@ -0,0 +1,34 @@
|
||||
# p03input4.txt -- Test Min() and Max()
|
||||
|
||||
c 8
|
||||
<
|
||||
>
|
||||
+ 5
|
||||
+ 30
|
||||
+ 15
|
||||
+ 20
|
||||
+ 50
|
||||
+ 10
|
||||
p
|
||||
<
|
||||
>
|
||||
+ 20
|
||||
+ 50
|
||||
+ 5
|
||||
p
|
||||
<
|
||||
>
|
||||
-
|
||||
-
|
||||
-
|
||||
p
|
||||
<
|
||||
>
|
||||
-
|
||||
-
|
||||
-
|
||||
p
|
||||
<
|
||||
>
|
||||
d
|
||||
|
51
CPE212/Project_03/p03input5.txt
Normal file
51
CPE212/Project_03/p03input5.txt
Normal file
@ -0,0 +1,51 @@
|
||||
# p03input5.txt -- Test Peek(n)
|
||||
|
||||
c 8
|
||||
? 0
|
||||
+ 5
|
||||
+ 30
|
||||
+ 15
|
||||
+ 20
|
||||
+ 50
|
||||
+ 10
|
||||
p
|
||||
? 0
|
||||
? 1
|
||||
? 2
|
||||
? 3
|
||||
? 4
|
||||
? 5
|
||||
? 6
|
||||
p
|
||||
-
|
||||
-
|
||||
-
|
||||
p
|
||||
? 0
|
||||
? 1
|
||||
? 2
|
||||
? 5
|
||||
p
|
||||
-
|
||||
-
|
||||
-
|
||||
p
|
||||
? 0
|
||||
? 1
|
||||
? 2
|
||||
? 3
|
||||
? 4
|
||||
? 5
|
||||
+ 25
|
||||
+ 9
|
||||
+ 8
|
||||
p
|
||||
? 0
|
||||
? 1
|
||||
? 2
|
||||
? 3
|
||||
? 4
|
||||
? 5
|
||||
d
|
||||
|
||||
|
BIN
CPE212/Project_03/project03
Normal file
BIN
CPE212/Project_03/project03
Normal file
Binary file not shown.
157
CPE212/Project_03/stack.cpp
Normal file
157
CPE212/Project_03/stack.cpp
Normal file
@ -0,0 +1,157 @@
|
||||
#include <iostream>
|
||||
#include <new>
|
||||
#include "stack.h"
|
||||
|
||||
|
||||
int Max_Stack; // Global variable for stack size
|
||||
Stack::Stack(int n) // Parameterized constructor dynamically allocates an empty stack array
|
||||
{ // that may hold no more than n elements and initializes other private variables
|
||||
Max_Stack=n;
|
||||
array = new int[n];
|
||||
top=-1;
|
||||
num=n;
|
||||
}
|
||||
|
||||
void Stack::Resize(int n) // Attempts to increase size of stack array to 2*num and then push n onto stack
|
||||
{ // If unable to resize, throw StackFull exception
|
||||
try
|
||||
{
|
||||
int largerArry[2*Max_Stack];
|
||||
for (int i = 0; i < num; i++)
|
||||
{
|
||||
largerArry[i]=array[i];
|
||||
}
|
||||
delete [] array;
|
||||
num= num * 2; // num to twice original size
|
||||
Max_Stack=num;
|
||||
array = largerArry;
|
||||
array[top]=n;
|
||||
//top++;
|
||||
}
|
||||
catch( ... )
|
||||
{
|
||||
throw StackFull();
|
||||
//return;
|
||||
}
|
||||
}
|
||||
|
||||
Stack::~Stack()
|
||||
{
|
||||
delete [] array;
|
||||
}
|
||||
|
||||
void Stack::Push(int n)
|
||||
{
|
||||
bool full = IsFull();
|
||||
if (full)
|
||||
{
|
||||
Resize(n);
|
||||
array[top]=n;
|
||||
}
|
||||
//array[top]=n;
|
||||
top++;
|
||||
array[top]=n;
|
||||
}
|
||||
|
||||
void Stack::Pop()
|
||||
{
|
||||
bool empty=IsEmpty();
|
||||
if (empty)
|
||||
{
|
||||
throw StackEmpty();
|
||||
}
|
||||
top--;
|
||||
}
|
||||
|
||||
bool Stack::IsEmpty() const
|
||||
{
|
||||
return (top == -1);
|
||||
}
|
||||
|
||||
bool Stack::IsFull() const
|
||||
{
|
||||
return (top == Max_Stack);
|
||||
}
|
||||
|
||||
void Stack::MakeEmpty()
|
||||
{
|
||||
top = -1;
|
||||
}
|
||||
|
||||
int Stack::Top() const
|
||||
{
|
||||
bool empty=IsEmpty();
|
||||
if (empty)
|
||||
{
|
||||
throw StackEmpty();
|
||||
//return;
|
||||
}
|
||||
return array[top];
|
||||
}
|
||||
|
||||
int Stack::Size() const // Returns number of items on stack WITHOUT modifying the stack
|
||||
{
|
||||
return top;
|
||||
}
|
||||
|
||||
int Stack::Max() const // Returns value of largest integer on stack WITHOUT modifying the stack
|
||||
{ // If stack is empty, throws StackEmpty
|
||||
bool empty=IsEmpty();
|
||||
if (empty)
|
||||
{
|
||||
throw StackEmpty();
|
||||
}
|
||||
|
||||
int i;
|
||||
|
||||
// Initialize maximum element
|
||||
int max = array[0];
|
||||
|
||||
// Traverse array elements
|
||||
// from second and compare
|
||||
// every element with current max
|
||||
for (i = 1; i < top; i++)
|
||||
{
|
||||
if (array[i] > max)
|
||||
max = array[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
int Stack::Min() const // Returns value of smallest integer on stack WITHOUT modifying the stack
|
||||
{ // If stack is empty, throws StackEmpty
|
||||
bool empty=IsEmpty();
|
||||
if (empty)
|
||||
{
|
||||
throw StackEmpty();
|
||||
}
|
||||
|
||||
int i;
|
||||
|
||||
// Initialize minimum element
|
||||
int min = array[0];
|
||||
|
||||
// Traverse array elements
|
||||
// from second and compare
|
||||
// every element with current min
|
||||
for (i = 1; i < top; i++)
|
||||
{
|
||||
if (array[i] < min)
|
||||
min = array[i];
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
int Stack::Peek(unsigned int n) const // Returns stack value n levels down from top of stack. Peek(0) = Top()
|
||||
{ // If position n does not exist, throws StackInvalidPeek
|
||||
|
||||
if(!array[top-n])
|
||||
throw StackInvalidPeek();
|
||||
int st=array[top-n];
|
||||
return st;
|
||||
}
|
||||
|
||||
int Stack::Capacity() const // Returns total num of elements that maybe stored in stack array
|
||||
{
|
||||
return Max_Stack;
|
||||
}
|
103
CPE212/Project_03/stack.h
Normal file
103
CPE212/Project_03/stack.h
Normal file
@ -0,0 +1,103 @@
|
||||
//
|
||||
// stack.h 2010 Fall CPE 212 -- Project03 -- Stacks
|
||||
//
|
||||
// The comments have been updated, but there have been no changes to the code.
|
||||
//
|
||||
// Specification file for Stack class, a stack of integers implemented
|
||||
// as a linked list of nodes.
|
||||
//
|
||||
// ***** DO NOT MODIFY OR SUBMIT THIS FILE *****
|
||||
//
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
#ifndef STACK_H
|
||||
#define STACK_H
|
||||
|
||||
class StackEmpty
|
||||
{
|
||||
// Exception class - throw an object of this type when stack is empty
|
||||
// Hint: there is no code for exception classes
|
||||
};
|
||||
|
||||
|
||||
class StackFull
|
||||
{
|
||||
// Exception class - throw an object of this type when stack is full
|
||||
};
|
||||
|
||||
class StackInvalidPeek
|
||||
{
|
||||
// Exception class - throw an object of this type when invalid peek position is used
|
||||
};
|
||||
|
||||
|
||||
class Stack // Models stack of integers ADT implemented as a dynamically allocated array
|
||||
{
|
||||
private:
|
||||
int* array; // Points to the stack array
|
||||
int num; // Holds max number of elements that may be stored in stack array
|
||||
int top; // Holds the index of the top data value stored on the stack
|
||||
void Resize(int n); // Attempts to increase size of stack array to 2*num and then push n onto stack
|
||||
// If unable to resize, throw StackFull exception
|
||||
|
||||
public:
|
||||
Stack(int n); // Parameterized constructor dynamically allocates an empty stack array
|
||||
// that may hold no more than n elements and initializes other private variables
|
||||
|
||||
|
||||
~Stack(); // Destructor deallocates all dynamically-allocated memory
|
||||
// associated with the object
|
||||
|
||||
void Push(int n); // Pushes integer n onto top of stack. If stack is full, attempts to
|
||||
// resize stack and then push n. If unable to resize, throws StackFull exception.
|
||||
|
||||
void Pop(); // Removes top integer from stack
|
||||
// If stack is empty, throws StackEmpty exception
|
||||
|
||||
bool IsEmpty() const; // Returns true if stack is empty; false otherwise
|
||||
|
||||
bool IsFull() const; // Returns true if stack is full; false otherwise
|
||||
|
||||
void MakeEmpty(); // Removes all items from stack leaving an empty, but usable stack with capacity num
|
||||
// If stack is already empty, MakeEmpty() does nothing
|
||||
|
||||
int Top() const; // Returns value of top integer on stack WITHOUT modifying the stack
|
||||
// If stack is empty, throws StackEmpty exception
|
||||
|
||||
int Size() const; // Returns number of items on stack WITHOUT modifying the stack
|
||||
|
||||
|
||||
int Max() const; // Returns value of largest integer on stack WITHOUT modifying the stack
|
||||
// If stack is empty, throws StackEmpty
|
||||
|
||||
int Min() const; // Returns value of smallest integer on stack WITHOUT modifying the stack
|
||||
// If stack is empty, throws StackEmpty
|
||||
|
||||
int Peek(unsigned int n) const; // Returns stack value n levels down from top of stack. Peek(0) = Top()
|
||||
// If position n does not exist, throws StackInvalidPeek
|
||||
|
||||
int Capacity() const; // Returns total num of elements that maybe stored in stack array
|
||||
|
||||
/******* DO NOT MODIFY ANY OF THE CODE FOR PRINT() *******/
|
||||
/****** DO NOT PLACE A COPY OF PRINT() CODE IN STACK.CPP!!! *******/
|
||||
|
||||
void Print() const // Writes stack contents to stdout, separated by a space, followed by endl
|
||||
{
|
||||
int index = top;
|
||||
cout << "Top { ";
|
||||
|
||||
while (index != -1) // Loop to output any values stored on stack
|
||||
{
|
||||
cout << array[index] << " ";
|
||||
index--;
|
||||
}
|
||||
cout << "} Bottom" << endl;
|
||||
} // End Print()
|
||||
|
||||
}; // End Class Stack
|
||||
|
||||
#endif
|
||||
|
||||
|
BIN
CPE212/Project_03/stack.o
Normal file
BIN
CPE212/Project_03/stack.o
Normal file
Binary file not shown.
Reference in New Issue
Block a user