Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

c++ A stack is a LIFO (last in, first out) data structure. Stacks are extremely useful in programming. A stack can be implemented using an

c++

A stack is a LIFO (last in, first out) data structure. Stacks are extremely useful in programming. A stack can be implemented using an array to hold values, and a counter to indicate where the "top" item on the stack is. See the first example. (You will need to download the .zip file and decompress it first.) This is a class declaration for a stack of doubles. Store your class declaration in a file called dblstk.h and implement the specified functions exactly as given. You may define whatever private members you think you need. You may also specify additional member functions if you find them useful.

Develop a test program in a file called Stack_driver.cpp for your stack implementation. This driver should define a stack to hold ten values and allow the user to specify, via a menu, the following operations. Allow the user to enter in the values to be pushed to the top of the stack 1 via menu option 2. Do not initialize stack 1 with any values, all values are to be entered by the user. The user menu should appear exactly as shown:

  1. Display stack 1
  2. Place a double value onto the top of stack 1
  3. Remove a value from the top of stack 1
  4. Check the total capacity stack 1
  5. Check current number of items on stack 1
  6. Copy stack 1 to stack 2
  7. Check to see if the two stacks are equal
  8. Quit

Enter menu selection:

Finally, obtain a copy of the instructor's program postfix.cpp; it should be in the downloadable sample source code, as described in Module 1. This program evaluates postfix arithmetic expressions using your stack implementation. Replace your driver module with the postfix.cpp module. The program should run without modification, assuming you have implemented the stack correctly.

Now

Building on the stacks you implemented earlier, add exceptions to the stack class. You should have at least two exceptions: Underflow (popping an empty stack) and Overflow (pushing onto a full stack). Your exceptions should pass along the identity of the stack that caused the exception.

Submit your source code as attachments to the drop box for this module's programming assignment. You can find the drop box at the end of this module's tab page. Do not compress your attachments. Please review the requirements for submitting on the course syllabus.

Finally

Building on the stacks you implemented in Programming Assignment 4, write a generic stack package using templates, so that a user of your package can create stacks of the integer data type.

The program should still provide all menu options identified in Programming Assignment 4 as well as allowing the user to select the menu options and push user entered values to stack 1.

Now two files

dbstk.h

// MODULE: dblstk1.h

// PROGRAMMER: Gary J. Blair

// LANGUAGE: C++

// DATE: 96/07/03

//

// PURPOSE:

// Declarations for class DoubleStack.

#include

class DoubleStack

{

public:

// Constructor.

DoubleStack( size_t capacity );

// Copy Constructor.

DoubleStack( const DoubleStack& rhs );

// Destructor.

~DoubleStack(void);

// Assignment operator=.

DoubleStack& operator=( DoubleStack& rhs);

// Add item to stack, making it the topmost item.

// Return value 1=success/0=failure.

int push( double& item );

// Remove top item from stack and return it in item.

// Return value 1=success/0=failure.

int pop( double& item );

// Determine if the stack is empty.

// Return value 1=empty/0=non-empty.

int empty(void);

// Inquire the capacity of the stack.

size_t capacity(void);

// Inquire the number of items on the stack.

size_t size(void);

// Compare 2 stacks to see if the contain the same data.

int operator==( DoubleStack& rhs );

private:

double *data; // Pointer to dynamic stack area.

size_t stack_size; // Capacity of stack.

size_t tos; // Top of stack. tos==0 ==> empty stack.

};

postfix.cpp

// MODULE: postfix.cpp

// PROGRAMMER: Gary J. Blair

// LANGUAGE: C++

// DATE: 96/7/03

//

// PURPOSE:

// Evaluate postfix arithmetic expressions.

// Test program for Homework 3.

#include

#include

#include

#include

#include "string.h"

#include "dblstk1.h"

class Token

{

public:

typedef enum {operand_token, operator_token, eod_token } TokenType;

typedef enum {add_op, sub_op, mul_op, div_op} ArithmeticOperator;

Token();

TokenType NextToken();

Token& operator=( char *text );

TokenType Type();

ArithmeticOperator Operator(void);

double Value();

private:

TokenType token_type; // The type of token.

ArithmeticOperator oper; // The operator, if an operator_token.

double value; // The value, if an operand_token.

char *source; // The string from which tokens are generated.

char *next; // The next char to be looked at.

};

void main(void)

{

DoubleStack ds(57); // Stack to evaluate expressions.

Token t; // One token at a time.

double lhs; // Left-hand side operand.

double rhs; // Right-hand side operand.

double result; // Result of lhs op rhs.

size_t i; // Loop variable.

const size_t n_data=3; // # of data items.

char *data[3]; // Pointers to data strings.

double answer[3]; // Array of answers.

data[0] = "3 2 + "; answer[0] = 5;

data[1] = "13 7 * 6 3 - * "; answer[1] = 273;

data[2] = "64 4 / 8 2 + 3 4 + - * "; answer[2] = 48;

ofstream fout("postfix.out");

for (i=0; i

{

fout << "Processing " << data[i] << endl

<< "the correct answer is " << answer[i]

<< " and the calculated answer is ";

t = data[i];

while (t.NextToken() != Token::eod_token)

{

if (t.Type() == Token::operand_token)

{

result = t.Value();

ds.push(result);

}

else

{

ds.pop(rhs);

ds.pop(lhs);

switch (t.Operator())

{

case Token::add_op:

result = lhs + rhs;

break;

case Token::sub_op:

result = lhs - rhs;

break;

case Token::mul_op:

result = lhs * rhs;

break;

case Token::div_op:

result = lhs / rhs;

break;

}

ds.push(result);

}

}

ds.pop(result);

fout << result << endl << endl;

if (! ds.empty())

{

fout << "*** Stack should be empty but is not. ";

exit(1);

}

}

fout.close();

}

// Constructor.

Token::Token()

{

source = NULL;

next = NULL;

}

// Process next token from source string and return

// the type of token it is.

Token::TokenType Token::NextToken()

{

int token_found=0; // Have we found a token yet?

char temp[1024]; // Temporary to sscanf an operand.

char *pt; // Pointer to temp.

while (*next)

{

if (*next == ' ' || *next =='\t')

{

++next;

continue;

}

else if (*next == '*')

{

token_type = operator_token;

oper = mul_op;

++next;

token_found = 1;

break;

}

else if (*next == '/')

{

token_type = operator_token;

oper = div_op;

++next;

token_found = 1;

break;

}

else if (*next == '+' && (! strchr("0123456789",*(next+1))))

{

token_type = operator_token;

oper = add_op;

++next;

token_found = 1;

break;

}

else if (*next == '-' && (! strchr("0123456789",*(next+1))))

{

token_type = operator_token;

oper = sub_op;

++next;

token_found = 1;

break;

}

else

{

pt = temp;

do

{

*(pt++) = *(next++);

}

while (*next && *next != ' ' && *next != '\t');

*pt = '\0';

if (! sscanf(temp,"%lf",&value))

{

cout << "*** Error performing sscanf of '" << temp << "' ";

exit(1);

}

token_type = operand_token;

token_found = 1;

break;

}

}

return (token_found) ? token_type : eod_token;

}

// Assign a source string to a token.

Token& Token::operator=( char *text )

{

if (source)

delete source;

source = new char[strlen(text) + 1];

strcpy(source,text);

next = source;

return *this;

}

// Inquire the type of a token.

Token::TokenType Token::Type()

{

return token_type;

}

// Inquire which operator a token represents.

Token::ArithmeticOperator Token::Operator(void)

{

if (token_type != operator_token)

{

cerr << "*** Incorrect inquiry. ";

exit(1);

}

return oper;

}

// Inquire the numberic value of a token.

double Token::Value()

{

if (token_type != operand_token)

{

cerr << "*** Incorrect inquiry. ";

exit(1);

}

return value;

}

its clear?
its written in the files the header file added below and postfix. its in the question

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

Spatio Temporal Database Management International Workshop Stdbm 99 Edinburgh Scotland September 10 11 1999 Proceedings Lncs 1678

Authors: Michael H. Bohlen ,Christian S. Jensen ,Michel O. Scholl

1999th Edition

3540664017, 978-3540664017

More Books

Students also viewed these Databases questions

Question

What are some of the more common applications of sprayed coatings?

Answered: 1 week ago

Question

define the term outplacement

Answered: 1 week ago

Question

describe the services that an outplacement consultancy may provide.

Answered: 1 week ago