Question: CSC 220 Assignment Base conversion program Write a program to display the base-two representation of a base-ten number. The conversion algorithm is in Page 318
CSC 220
Assignment Base conversion program
Write a program to display the base-two representation of a base-ten number. The conversion algorithm is in Page 318 of your textbook.
Example: 26 (Base-ten) = 11010 (Base-two)
You are required to use the attached header file (stack.h), resource file (stack.cpp) that has the stack member functions Stack constructor, push, pop, empty.
/*--------------------------------------Stack.h------------------------------------------
This header file defines a stack data type.
Basic operations:
constructor: Costructs an empty stack empty: Check if the stack is empty
push: Modifies a stack by adding a value at the top
pop: Remove an element from top of the stack
*/
#ifndef STACK_H #define STACK_H using namespace std;
const int STACK_CAPACITY =20; typedef int StackElement;
class Stack { private:
/******* Data Memebrs **************/
StackElement myArray[ STACK_CAPACITY]; int myTop;
public:
/******* Function Memebrs **************/
Stack(); //Constructor - An empty stack has been constructed. bool empty() const; //Check if the stack is empty. void push(const StackElement & value); //Adds an element to a stack.
StackElement pop(); //Remove an element from top of the stack
};
#endif
/* This file implements Stack member functions. stack.cpp
-------------------------------------------------------*/
#include "Stack.h" #include
//---------Definition of Stack constructor---------------
Stack::Stack()
{
//Default Constructor
myTop = -1;
}
//---------Definition of empty()------------------------- bool Stack::empty() const
{
return (myTop == -1);
}
//---------Definition of push()-------------------------- void Stack::push(const StackElement & value)
{
if (myTop < STACK_CAPACITY -1)
{
++myTop;
myArray[myTop] = value;
}
else
{
cerr << "*** Stack full -- can't add new value Must increase value of
STACK_CAPACITY in Stack.h *** ";
exit(1);
}
}
//---------Definition of pop()--------------------------
StackElement Stack::pop()
{
StackElement value;
if (!empty())
{
value = myArray[myTop];
myTop--;
}
else
{
cerr << "*** Stack is empty --can't remove a value *** "; exit(1);
}
return value;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
