1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class for integer stacks.
2. (20’) In stackTest.cpp, complete the implementation of function
postfixTest(), which use an integer stack to evaluate post-fix
expressions. For simplicity, you can assume the post-fix expression
is input character by character (i.e., not an entire string), and
each operand is a non-negative, single-digit integer (i.e.,
0,1,…,9). However, you are supposed to detect invalid/ illegal
post-fix expression input, e.g., “4 5 + -“.
3. (40’) In myQueue.h, implement the queue class template, myQueue.
Keep in mind, the arrayLength needs to be one more than the
capacity of the queue. Also, under this implementation, make sure
your calculation of currentSize is correct, and the conditions for
“Full” and “Empty” are correct. One shortcut could be: once you
make sure
currentSize() is implemented correctly, you might use it in
isFull() and isEmpty(), and the number of elements in the queue
must range from 0 to arrayLength – 1.
Can u please complete the todo part of the code
Stacktest.cpp file for stack
#include <iostream>
#include "myStack.h"
using namespace std;
void postfixTest() {
myStack operandStack(100);
cout << "Please enter the operands (integers
1~9) and operators (+, -, *, /) one by one..." << endl;
cout << "and enter '=' to indicate the end of
the expression and to output the result." << endl;
while(1){
char inputHolder;
cin >> inputHolder;
// TODO
}
}
int main()
{
cout << "Testing the basic functions of the stack..."
<< endl;
cout << "Please enter the max capacity of the
testStack: ";
int testSize;
cin >> testSize;
myStack testStack(testSize);
cout << "Testing..." << endl;
while(1) {
cout << "Please enter 'p' for
push, 'o' for pop, 'e' for exit: ";
char userChoice;
cin >> userChoice;
if(userChoice == 'e')
break;
switch (userChoice) {
case
'p':
if(!testStack.isFull()) {
cout << "Please enter
the integer you would like to push: ";
int userInt;
cin >> userInt;
testStack.push(userInt);
}
else
cout << "Nothing has
been pushed in. The stack is full!" << endl;
break;
case 'o':
if(!testStack.isEmpty())
cout << testStack.pop()
<< " has been popped out" << endl;
else
cout << "Nothing has
been popped out. The stack is empty!" << endl;
break;
default:
cout << "Illegal user-input character.
Please try again." << endl;
}
}
cout << "Now, start to use a stack to
evaluate postfix expressions..." << endl;
postfixTest();
return 0;
}
Header file
#ifndef _MYSTACK_H_
#define _MYSTACK_H_
class myStack {
public:
myStack(int maxSz);
~myStack();
void push(int element);
int pop();
bool isEmpty() const;
bool isFull() const;
private:
int *contents; /*Dynamic initiate (C++ keyword new)
the holder array*/
int top; /*Index in the array of the top
element*/
int maxSize; /*Max number of elements could be in this
stack*/
};
#endif
myStack.cpp file
#include <iostream>
#include "myStack.h"
using namespace std;
/*
* Constructor
* Usage: myStack(maxSz);
* -------------------------
* A new stack variable is initialized. The initialized
* stack is made empty. maxSz is used to determine the
* maximum number of character that can be held in the
* stack.
*/
myStack::myStack(int maxSz) {
// TODO
}
/* Destructor
* Usage: delete ptr
* -----------------------
* This frees all memory associated with the stack.
*/
myStack::~myStack() {
// TODO
}
/*
* Functions: push, pop
* Usage: s1.push(element); element = s1.pop();
* --------------------------------------------
* These are the fundamental stack operations that add an element
to
* the top of the stack and remove an element from the top of the
stack.
* A call to pop on an empty stack or to push on a full stack
* is an error. Make use of isEmpty()/isFull() (see below)
* to avoid these errors.
*/
void myStack::push(int element) {
// TODO
}
int myStack::pop() {
// TODO
}
/*
* Functions: isEmpty, isFull
* Usage: if (s1.isEmpty()) ...
* -----------------------------------
* These return a true value if the stack is empty
* or full (respectively).
*/
bool myStack::isEmpty() const {
// TODO
}
bool myStack::isFull() const {
// TODO
}
3rd question
queueTest.cpp file for queue
#include <iostream>
#include "myQueue.h"
using namespace std;
int main() {
cout << "Testing the template myQueue, try an
integer queue as an example..." << endl;
cout << "Please enter the max size of the int
queue: ";
int capacity;
cin >> capacity;
myQueue<int> testIntQ(capacity);
while(1) {
cout << "Please enter 'e' for
enqueue, 'd' for dequeue, and 's' for stop." << endl;
char userOption;
cin >> userOption;
if(userOption == 's')
break;
switch(userOption) {
case 'e':
if(!testIntQ.isFull()) {
cout << "Please enter
the integer you want to enqueue: ";
int val;
cin >> val;
testIntQ.enqueue(val);
}
else
cout << "Cannot
enqueue. The queue is full." << endl;
break;
case 'd':
if(!testIntQ.isEmpty())
cout <<
testIntQ.dequeue() << " has been popped out." <<
endl;
else
cout << "Cannot pop.
The queue is empty." << endl;
break;
default:
cout << "Illegal input character for
options." << endl;
}
}
return 0;
}
Header file
#ifndef _MYQUEUE_H_
#define _MYQUEUE_H_
using namespace std;
template <class T>
class myQueue {
public:
myQueue(int maxSz);
~myQueue();
void enqueue(T item);
T dequeue();
int currentSize();
bool isEmpty();
bool isFull();
private:
T *contents; /*Dynamic initiate (C++ keyword new) the
holder array*/
int front,rear; /*Index in the array of the front and
rear element*/
int arrayLength; /*The length of the contents holder
array*/
/* Keep in mind that the Queue will
only hold up to (arrayLength - 1) elements*/
};
template <class T>
myQueue<T>::myQueue(int maxSz) {
// TODO
}
template <class T>
myQueue<T>::~myQueue() {
// TODO
}
template <class T>
void myQueue<T>::enqueue(T item) {
// TODO
}
template <class T>
T myQueue<T>::dequeue() {
// TODO
}
template <class T>
int myQueue<T>::currentSize() {
// TODO
}
template <class T>
bool myQueue<T>::isEmpty() {
// TODO
}
template <class T>
bool myQueue<T>::isFull() {
// TODO
}
#endif
myStack.h
============================
#ifndef _MYSTACK_H_
#define _MYSTACK_H_
class myStack {
public:
myStack(int maxSz);
~myStack();
void push(int element);
int pop();
bool isEmpty() const;
bool isFull() const;
private:
int* contents; /*Dynamic initiate (C++ keyword new)
the holder array*/
int top; /*Index in the array of the top
element*/
int maxSize; /*Max number of elements could be in this
stack*/
};
#endif
===============================
myStack.cpp
===============================
#include <iostream>
#include "myStack.h"
using namespace std;
/*
* Constructor
* Usage: myStack(maxSz);
* -------------------------
* A new stack variable is initialized. The initialized
* stack is made empty. maxSz is used to determine the
* maximum number of character that can be held in the
* stack.
*/
myStack::myStack(int maxSz) {
// initialize variables
top = -1; // no element in stack
maxSize = maxSz; // se maximum size of stack
// initialize array
contents = new int[maxSize];
}
/* Destructor
* Usage: delete ptr
* -----------------------
* This frees all memory associated with the stack.
*/
myStack::~myStack() {
// delete the array of integers
delete[] contents;
}
/*
* Functions: push, pop
* Usage: s1.push(element); element = s1.pop();
* --------------------------------------------
* These are the fundamental stack operations that add an element
to
* the top of the stack and remove an element from the top of the
stack.
* A call to pop on an empty stack or to push on a full stack
* is an error. Make use of isEmpty()/isFull() (see below)
* to avoid these errors.
*/
void myStack::push(int element) {
// increase index of top element
top++;
// add element at index of top
contents[top] = element;
}
int myStack::pop() {
// copy element from top index
int element = contents[top];
// reduce index of top element
top--;
// return the element
return element;
}
/*
* Functions: isEmpty, isFull
* Usage: if (s1.isEmpty()) ...
* -----------------------------------
* These return a true value if the stack is empty
* or full (respectively).
*/
bool myStack::isEmpty() const {
// check top of stack to see if its empty
if (top < 0) {
return true;
}
else {
return false;
}
}
bool myStack::isFull() const {
// check top element is at last index of array
if (top == maxSize - 1) {
return true;
}
else {
return false;
}
}
==================================
Stacktest.cpp
==================================
#include <iostream>
#include "myStack.h"
using namespace std;
void postfixTest() {
myStack operandStack(100);
cout << "Please enter the operands (integers
1~9) and operators (+, -, *, /) one by one..." << endl;
cout << "and enter '=' to indicate the end of
the expression and to output the result." << endl;
while (1) {
char inputHolder;
cin >> inputHolder;
// check uset input
if (inputHolder >= '0'
&& inputHolder <= '9') {
// input is a
digit
// add it to
stack
operandStack.push(inputHolder);
}
else if (inputHolder == '+' ||
inputHolder == '-' || inputHolder == '*' || inputHolder == '/')
{
// input is a
operator
// perform
postfix operation
// get second
operands from stack
int second =
0;
if
(!operandStack.isEmpty()) {
second = operandStack.pop();
}
else {
// print error
cout << "illegal post-fix expression
input!" << endl;
cout << "try again" << endl;
}
// get first
operand
int first =
0;
if
(!operandStack.isEmpty()) {
first = operandStack.pop();
}
else {
// print error
cout << "illegal post-fix expression
input!" << endl;
cout << "try again" << endl;
}
// create result
of operation
int result =
0;
if (inputHolder
== '+') {
result = first + second;
}
else if
(inputHolder == '-') {
result = first - second;
}
else if
(inputHolder == '*') {
result = first * second;
}
else {
// inputHolder == '/'
result = first / second; // this will perform
integer division. it trucates decimal values
}
// add result
back to the stack
operandStack.push(result);
}
else if (inputHolder == '=')
{
int result =
0;
// at end stack
should contain only one element that is result of expression
if
(!operandStack.isEmpty()) {
result = operandStack.pop();
}
if
(!operandStack.isEmpty()) {
// print error
cout << "illegal post-fix expression
input!" << endl;
cout << "try again" << endl;
}
else {
// print resut
cout << "Result: " << result
<< endl;
// end of expresion
break;
}
}
else {
// user entered
invalid value
// print error
and ignore value
cout <<
"invalid input" << endl;
}
}
}
int main()
{
cout << "Testing the basic functions of the
stack..." << endl;
cout << "Please enter the max capacity of the
testStack: ";
int testSize;
cin >> testSize;
myStack testStack(testSize);
cout << "Testing..." << endl;
while (1) {
cout << "Please enter 'p' for
push, 'o' for pop, 'e' for exit: ";
char userChoice;
cin >> userChoice;
if (userChoice == 'e')
break;
switch (userChoice) {
case 'p':
if
(!testStack.isFull()) {
cout << "Please enter the integer you
would like to push: ";
int userInt;
cin >> userInt;
testStack.push(userInt);
}
else
cout << "Nothing has been pushed in. The
stack is full!" << endl;
break;
case 'o':
if
(!testStack.isEmpty())
cout << testStack.pop() << " has
been popped out" << endl;
else
cout << "Nothing has been popped out. The
stack is empty!" << endl;
break;
default:
cout <<
"Illegal user-input character. Please try again." <<
endl;
}
}
cout << "Now, start to use a stack to
evaluate postfix expressions..." << endl;
postfixTest();
return 0;
}
================================
ANSWER 3
================================
myQueue.h
================================
#ifndef _MYQUEUE_H_
#define _MYQUEUE_H_
using namespace std;
template <class T>
class myQueue {
public:
myQueue(int maxSz);
~myQueue();
void enqueue(T item);
T dequeue();
int currentSize();
bool isEmpty();
bool isFull();
private:
T* contents; /*Dynamic initiate (C++ keyword new) the
holder array*/
int front, rear; /*Index in the array of the front and
rear element*/
int arrayLength; /*The length of the contents holder
array*/
/* Keep in mind that the Queue will
only hold up to (arrayLength - 1) elements*/
};
template <class T>
myQueue<T>::myQueue(int maxSz) {
// initialize variables
// arrayLength needs to be one more than the capacity
of the queue
arrayLength = maxSz + 1; // capacity of the queue is
maxSz
// initialize array with size of arraylength
contents = new T[arrayLength];
// set front element to 0 and rear to 0
// no elements in queue so front and rear are
same
front = 0; // in queue index of first element is
always 0!
rear = 0;
}
template <class T>
myQueue<T>::~myQueue() {
// delete the array
delete[] contents;
}
template <class T>
void myQueue<T>::enqueue(T item) {
// increase index of rear element
rear++;
// add element at rear index
contents[rear] = item;
}
template <class T>
T myQueue<T>::dequeue() {
// move all element 1 place left in array
for (int i = 1; i <= rear; i++) {
contents[i - 1] =
contents[i];
}
// decrease index of rear
rear--;
// return the element at front
return contents[front];
}
template <class T>
int myQueue<T>::currentSize() {
// size fo array is rear element index - front
index
return rear - front;
}
template <class T>
bool myQueue<T>::isEmpty() {
// if current size is 0 then queue is empty
return currentSize() == 0;
}
template <class T>
bool myQueue<T>::isFull() {
// number of elements in the queue must range from 0
to arrayLength – 1
// if current size is arrayLength -1 then queue is
full
return currentSize() == arrayLength - 1;
}
#endif
===========================
queueTest.cpp
===========================
#include <iostream>
#include "myQueue.h"
using namespace std;
int main() {
cout << "Testing the template myQueue, try an
integer queue as an example..." << endl;
cout << "Please enter the max size of the int
queue: ";
int capacity;
cin >> capacity;
myQueue<int> testIntQ(capacity);
while (1) {
cout << "Please enter 'e' for
enqueue, 'd' for dequeue, and 's' for stop." << endl;
char userOption;
cin >> userOption;
if (userOption == 's')
break;
switch (userOption) {
case 'e':
if
(!testIntQ.isFull()) {
cout << "Please enter the integer you want
to enqueue: ";
int val;
cin >> val;
testIntQ.enqueue(val);
}
else
cout << "Cannot enqueue. The queue is
full." << endl;
break;
case 'd':
if
(!testIntQ.isEmpty())
cout << testIntQ.dequeue() << " has
been popped out." << endl;
else
cout << "Cannot pop. The queue is empty."
<< endl;
break;
default:
cout <<
"Illegal input character for options." << endl;
}
}
return 0;
}
let me know if you have any problem or doubt. tahnk you.
1. (40’) In myStack.cpp, implement the member functions of the class myStack, which is the class...
The class pictured below is designed to implement an integer queue using two stacks. Assume the stack methods all work as desired (though their implementations are not shown). .(a) Trace what happens in the following situation, showing intermediate steps (values of variables, what is in the stacks, and what is in the queue at various points in the methods, not just the results of the methods). • Start with an empty queue. Enqueue 5, then 16, then 7. Dequeue twice....
- implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
I need help fixing my code. My output should be the following. Hello, world! : false A dog, a panic in a pagoda : true A dog, a plan, a canal, pagoda : true Aman, a plan, a canal--Panama! : true civic : true If I had a hi-fi : true Do geese see God? : true Madam, I’m Adam. : true Madam, in Eden, I’m Adam. : true Neil, a trap! Sid is part alien! : true Never odd...
stack.h template class Stack{ public: Stack(int max = 10); ~Stack() {delete [] stack;} bool isEmpty() const { return top == -1; } bool isFull() const { return top == MaxStackSize; } T peek() const; void push(const T& x); void pop(); private: int top; int MaxTop; T * stack; } source.cpp What is printed by the following program segment? Stack s; int n; s.push(4); s.push(6); s.push(8); while(!s.isEmpty()) { n = s.peek(); cout << n << ‘ ‘; s.pop(); } cout<< endl;...
In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...
HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...
in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...
C++ Please ensure code is fully working, will rate Question to be answered: Repeat Exercise 1 for the class linkedStackType question one: Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...
Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...