Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with editing the .h file to make it compile and pass the tests in th

ID: 3567927 • Letter: I

Question

I need help with editing the .h file to make it compile and pass the tests in the sll_stack_tester.cpp

========================

sll_stack.h

========================

/
// sll_stack.h
//
// A container for storing unsigned integers. The implementation of the sll_stack will
// use a singly-linked list data structure.
//
// This version introduces an exception for empty sll_stack accesses.
//
#ifndef CSC116_STACK_H
#define CSC116_STACK_H

#include <iostream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

//
// This exception will be used to indicate when invalid access to
// an empty sll_stack occurs.
//
class stack_empty_exception {
      
   public:
       stack_empty_exception(const string & msg, unsigned int line) {
           _msg = msg; _line = line;
       }
       string what() {  
           ostringstream s;
           s << "stack empty in function: " << _msg << " at line: " << _line;
return s.str();
       }
   private:
       stack_empty_exception();
       string           _msg;
       unsigned int   _line;
};

class node
{
   public:
       node() : m_value(0), m_next(0) {}
       node( int val, node *nxt ) :
           m_value(val), m_next(nxt) {}

       int       m_value;
       node *   m_next;
};

class sll_stack
{
public:

   //
   // Construct a new, empty sll_stack.
   //
   // Pre-conditions:
   //   none
   sll_stack()
   {

   }
  
   //
   // Construct a new, sll_stack with the values from vector
   // pushed on to the stack.
   //
   // For example, if vec={1,2,3}, values are pushed
   // on the stack and s={TOP_3,2,1}
   //
   // Pre-conditions:
   //   none
   sll_stack(vector<int> &vec)
   {

   }
  
   //
   // De-constructor
   //
   ~sll_stack()
   {
       clear();
   }


   //
   // Make the sll_stack empty. For example, if s={TOP_1,2,3},
   // after s.clear(), s={}
   //
   // Pre-conditions:
   //   none
   void clear()
   {

   }

   //
   // Return the number of elements in the sll_stack
   //
   // Pre-conditions:
   //   none
   //
   // Returns:
   //   number of elements in the sll_stack
   //
   unsigned int   size() const
   {
       return NULL;
   }

   //
   // Is the sll_stack empty? Ie. the sll_stack contains no elements
   //
   // Pre-conditions:
   //   none
   //
   // Returns:
   //   true   the number of elements in the sll_stack is 0
   //   false   the number of elements in the sll_stack is > 0
   bool       empty() const
   {
       return true;
   }

   //
   // Insert a new element at the front of the sll_stack.
   //
   // For example, if s={TOP_1,2,3} and s.push(4) is called,
   // the result is: s={TOP_4,1,2,3}
   //
   // Pre-conditions:
   //   none
   //
   void   push(const int val)
   {

   }

   //
   // Return and remove the first element in the sll_stack.
   //
   // For example, if s={TOP_1,2,3} and s.pop() is called,
   // 1 is returned and s={TOP_2,3}
   //
   // Pre-conditions:
   //   none
   //
   // Returns
   //   the top element in the sll_stack
   //
   // Throws
   //   stack_empty_exception if size() == 0
   int    pop()
   {
       return NULL;
   }

   //
   // Returns the element at the top of the sll_stack.
   //
   // For example, if s={TOP_1,2,3} and s.peek() is called,
   // 1 is returned and s={TOP_1,2,3}
   //
   // Pre-conditions:
   //   none
   //
   // Returns
   //   the first element in the sll_stack
   //
   // Throws
   //   stack_empty_exception if size() == 0
   int peek()
   {
       return NULL;
   }

   //
   // Print the sll_stack to the stream. The format is: {TOP_1,2,3}
   // Do not print a newline in this method.
   // The empty sll_stack is: {TOP_}
   //
   // This is declared as a friend so the implementation can be
   // more efficient.
   //
   // Pre-conditions:
   //   ostream s is valid
   //   sll_stack s is valid
   friend std::ostream & operator<< (std::ostream &s, const sll_stack &stk)
   {
       return s;
   }


private:
  
   node*       m_top;
   unsigned int   m_size;
  

   // These are private to prevent users of the sll_stack class
   // from copying and assigning sll_stacks
   //
   // The compiler provides default implementations of these
   // operations and they will not work for our sll_stack.
   //
   // Since we aren't going to implement these in the current
   // assignment, we will leave them private.
   sll_stack (const sll_stack &);
   sll_stack& operator= (sll_stack &);
};
#endif

===================================

sll_stack_tester.cpp

===================================

//
// sll_stack_tester.cc
//
// A test program for sll_sll_stack.h
//
// This program uses exceptions to handle error conditions.
//

#include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>
#include <exception>
#include <vector>
#include "sll_stack.h"

using namespace std;

unsigned int input[] = {1,2,3,4,5,6,7,8};
std::vector<unsigned int> values (input, input + sizeof(input) / sizeof(unsigned int) );

class stack_tester_exception
{
   public:
       stack_tester_exception (const string & msg, unsigned int line) : _msg(msg), _line(line) {}
       string what() const
       {
           ostringstream s;
           s << _line;
           return _msg + " line number: " + s.str();
       }
   private:
       stack_tester_exception();
       string _msg;
       unsigned int _line;
};

void test_push_empty_size()
{
   sll_stack<unsigned int> s;
   stringstream s1;
  
   if (!s.empty())
       throw stack_tester_exception(__func__,__LINE__);
      
   for (unsigned int i = 1; i <= 10; i++)
       s.push(i*10);

   if (s.empty())
       throw stack_tester_exception(__func__,__LINE__);

   if (s.size() != 10)
       throw stack_tester_exception(__func__,__LINE__);
  
   s1<<s;
   if (s1.str() != "{TOP_100,90,80,70,60,50,40,30,20,10}")
       throw stack_tester_exception(__func__,__LINE__);

   cout << __func__ << " passed." << endl;  
}

void test_pop_all_elements()
{
   stringstream s1;
   sll_stack<unsigned int> s(values);
   unsigned int expected = values.size();
   unsigned int popped = 0;  
  
   s1 << s;
   if (s1.str() != "{TOP_8,7,6,5,4,3,2,1}")
       throw stack_tester_exception(__func__,__LINE__);
          
   for (unsigned int i = values.size(); i>0; i--)
   {
       if (s.size() != i)
           throw stack_tester_exception(__func__,__LINE__);

       popped = s.pop();
      
       if (popped != expected)
           throw stack_tester_exception(__func__,__LINE__);

       expected--;
   }
   cout << __func__ << " passed." << endl;
}

void test_peek()
{
   sll_stack<unsigned int> s(values);
   unsigned int expected = values.size();
   unsigned int top = 0;  
          
   for (unsigned int i = 0; i < values.size(); i++)
   {
       top = s.peek();
       if (top != expected)
           throw stack_tester_exception(__func__,__LINE__);
      
       if (s.size() != 8)
           throw stack_tester_exception(__func__,__LINE__);
  
   }
   cout << __func__ << " passed." << endl;
}

void test_exceptions()
{

   sll_stack<unsigned int> s;
  
   try
   {
       unsigned int x = s.pop();
       // We shouldn't get here
       std::cout << x << std::endl;
       throw stack_tester_exception(__func__,__LINE__);
   }
   catch (stack_empty_exception & e)
   {
       std::cout << "exceptions working for pop"<< std::endl;
       // do nothing, this is the correct case
   }

   try
   {
       unsigned int x = s.peek();
       // We shouldn't get here
       std::cout << x << std::endl;
       throw stack_tester_exception(__func__,__LINE__);
   }
   catch (stack_empty_exception & e)
   {
       std::cout << "exceptions working for peek"<< std::endl;
       // do nothing, this is the correct case
   }


}

int main ()
{
   unsigned int tests_passed = 0;

   try
   {
       test_push_empty_size();
       tests_passed++;

       test_pop_all_elements();
       tests_passed++;

       test_peek();
       tests_passed++;

       test_exceptions();
       tests_passed++;
   }
   catch (stack_tester_exception const &e)
   {
       cout << "Failed test case: " << e.what() << std::endl;
   }
   catch (...)
   {
       cout << "Caught unhandled exception." << std::endl;
   }
   cout << "Passed: " << tests_passed << endl;
   return 0;
}

Explanation / Answer

http://www.writingcentre.us/ saves your time

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote