Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

simpleStack.cc #include simpleStack.h void Stack::push(const StackType &d) throw (ContainerFullException) { if (count == STACK_SIZE) throw ContainerFullException(SimpleStack); data[count] = d; count++; // data[count++] = d; }

simpleStack.cc
#include "simpleStack.h"
void Stack::push(const StackType &d) throw (ContainerFullException) {
if (count == STACK_SIZE)
throw ContainerFullException("SimpleStack");
data[count] = d;
count++;
// data[count++] = d;
}
StackType Stack::pop(void) throw (ContainerEmptyException) {
if (!count)
throw ContainerEmptyException("SimpleStack");
count--;
return data[count];
// return data[--count];
}
StackType Stack::peek(void) throw (ContainerEmptyException) {
if (!count)
throw ContainerEmptyException("SimpleStack");
return data[count-1];
}
*********************************************************************************************************************************************************************************************************************
// simpleStack.h
#ifndef _SIMPLESTACK_H
#define _SIMPLESTACK_H
#include
const int STACK_SIZE = 10;
typedef int StackType;
class Stack {
public:
Stack(void) { count = 0; }
~Stack(void) { }
void clear(void) { count = 0; }
int size(void) { return count; }
bool isEmpty(void) { return count == 0; }
void push(const StackType &) throw (ContainerFullException);
StackType pop(void) throw (ContainerEmptyException);
StackType peek(void) throw (ContainerEmptyException);
private:
StackType
data[STACK_SIZE];
int
count;
};
#endif
*********************************************************************************************************************************************************************************************************************
// zenStack.cc
#include "zenStack.h"
void ZenStack::push(void *p) throw (ContainerFullException) {
if (count == STACK_SIZE)
throw ContainerFullException("ZenStack");
memcpy(data+count*dSize,p,dSize);
count++;
}
void ZenStack::pop(void *p) throw (ContainerEmptyException) {
if (count == 0)
throw ContainerEmptyException("ZenStack");
count--;
memcpy(p,data+count*dSize,dSize);
}
void ZenStack::peek(void *p) throw (ContainerEmptyException) {
if (count == 0)
throw ContainerEmptyException("ZenStack");
memcpy(p,data+(count-1)*dSize,dSize);
}
*********************************************************************************************************************************************************************************************************************
// zenStack.h
#ifndef _ZENSTACK_H
#define _ZENSTACK_H
#include
#include
#include
using namespace std;
const int STACK_SIZE = 1024;
class ZenStack {
public:
ZenStack(int s) { count = 0; dSize = s; }
~ZenStack(void) { }
void clear(void) { count = 0; }
int size(void) { return count; }
bool isEmpty(void) { return count == 0; }
void push(void *) throw (ContainerFullException);
void pop(void *) throw (ContainerEmptyException);
void peek(void *) throw (ContainerEmptyException);
private:
uint8_t
data[STACK_SIZE];
uint32_t
count,
dSize;
};
#endif
*********************************************************************************************************************************************************************************************************************
// stackTest1.cc
#include
#include "simpleStack.h"
using namespace std;
int main(void) {
int x,d;
Stack myStack;
cout
cin >> x;
while (x != 0) {
d = x % 10;
x /= 10;
myStack.push(d);
}
while (!myStack.isEmpty()) {
d = myStack.pop();
cout
}
return 0;
}
*********************************************************************************************************************************************************************************************************************
// stackTest2.cc
#include
#include "zenStack.h"
using namespace std;
int main(void) {
int x,d;
Stack myStack;
cout
cin >> x;
while (x != 0) {
d = x % 10;
x /= 10;
myStack.push(d);
}
while (!myStack.isEmpty()) {
d = myStack.pop();
cout
}
return 0;
}
*********************************************************************************************************************************************************************************************************************
// stackTest3.cc
#include
#include "stack.h"
using namespace std;
int main(void) {
int x,d;
Stack myStack;
cout
cin >> x;
while (x != 0) {
d = x % 10;
x /= 10;
myStack.push(d);
}
while (!myStack.isEmpty()) {
d = myStack.pop();
cout
}
return 0;
}
*********************************************************************************************************************************************************************************************************************
// stack.h
#ifndef _STACK_H
#define _STACK_H
#include
const int STACK_SIZE = 10;
typedef int StackType;
class Stack {
public:
Stack(void) { count = 0; }
~Stack(void) { }
void clear(void) { count = 0; }
int size(void) { return count; }
bool isEmpty(void) { return count == 0; }
void push(const StackType &d) throw (ContainerFullException) {
if (count == STACK_SIZE)
throw ContainerFullException("Stack");
data[count] = d;
count++;
}
StackType pop(void) throw (ContainerEmptyException) {
if (!count)
throw ContainerEmptyException("Stack");
count--;
return data[count];
}
StackType peek(void) throw (ContainerEmptyException) {
if (!count)
throw ContainerEmptyException("Stack");
return data[count-1];
}
private:
StackType
data[STACK_SIZE];
int
count;
};
#endif
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Goal Experiment with various methods for creating containers that can hold multiple types of data within the same program 2 3 Method 1 - Unions So far, you have seen a class -- a collection of data and the code that manipulates the data. You may have also seen a struct, which is just the collection of data without the code. A union is similar to a struct. The difference is, while a struct can hold many values inside of itself simultaneously, a union can only hold one value at a time. Consider the following struct: 1 struct { int foo; double bar; 4 }; This struct can hold an integer foo value and a double-precision value bar simultaneously. Now.consider the following union: 1 union int foo; double bar; 2 3 This can hold either foo or bar, but not both simultaneously. The struct has enough space to hold both 12 bytes on most systems - while the union only has enough space for the largest of its items -- the double, which is 8 bytes To try this method out, you will need to modify the following files: simpleStack.h simpleStack.cc stacktesti.cc First, modify simpleStack.h replacing the typedef line with the following: 1 union UItem { int ival; double dval; 2 3 5 6 typedef union VItem StackType; This defines the union and creates an alias StackType: the compiler will replace all occurrences of StackType with union Ultem In stacktesti.cc, create a single StackType variable: add 1 StackType digit; as a local variable inside main(). Replace the push() call with 1 digit.ival = d; 2 myStack.push(digit); Finally, replace the pop() and cout lines with 1 digit = myStack.pop(); 2 cout This establishes the class as a template, using StackType as a placeholder Next. In stackTest3.cc.change the stack declaration to Stack int> myStack; That's all... compile stackTest3.cc and it should work just as the other two files. Method 4 - Shallow copy There is a fourth method to create generic containers, but we will not attempt it in this lab. The idea is for the container to only store pointers to data elements, not the elements themselves. This is called shallow copying. The pointers or references can be made generic enough to store any kind of data in the container, one container can even store multiple types of data simultaneously - a limitation on template containers. However, care must be taken to ensure that each item stored in this manner is actually a unique element and not just multiple pointers to the same object

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago