Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Stacks C++

$
0
0
I need help with my stacks problem in microsoft visual studio 2010. The question is
In a container of candies there are 3 colors randomly placed: red, yellow and blue. Assume that the container holds a maximum of 12 candies. Use a stack program to simulate randomly placing some (between 5 and 12 candies) initially into the container (stack).
Now once that is done have the "user" choose a color and using ONLY stack functions remove all candies of that color. All candies in the container (stack) that are NOT of that color are returned to the candy in the same order they were in prior to the color removal. Actually loop the program twice to simulate "filling" and "removing" two sets of candies. Don't assume that there are necessarily all colors of candy all the time...so for example someone might want all red candies but there might not be any red candies at that time.
Fill the container (or partially fill) only ONCE for each run. Here is an example:

Suppose you place candies in this order: R R Y B R B Y B Y R B B
Then the stack would look like this:
R
R
Y
B
R
B
Y
B
Y
R
B
B (top of stack)
If you asked to remove all yellow the resulting stack would be:
R
R
B
R
B
B
R
B
B (top of stack)

The code i have is faulty and would like to get new code to show what is wrong and how to fix it
// Sample Stack Program

#include <iostream>
# include <fstream>
using namespace std;
const maxstack = 21;
class stack_type		// declaration of class//
{
public:
		void clear_stack();	//member functions//
		bool empty_stack(); //    of class stack_type//
		bool full_stack();
		void push(int numb);
		void pop(int& numb);

		int stack[maxstack];
		int top;
};

//---------------------------------------------------------------------

int main()
{
	stack_type s;
	int i,n;
	
	s.clear_stack();

	cout << "Stack top after clearstack is " << s.top << endl;

	i = 0;
	cout << "\nEnter up to 20 integers for the stack..enter -999 to quit\n";
	cin >> n;
	cout << n << "\n";
	while (!(s.full_stack()) && n != -999)
	{
			s.push(n);
			i++;
			if (!(s.full_stack()))
			{
				cin >> n;
				cout << n << endl;
			}

	}
	cout << "\nthere are "<< i << "  values in the stack \n";
	cout << "\nValues from the stack are:" << endl;
	if (s.full_stack())
		cout << "\nFull Stack! \n";
	while (!(s.empty_stack()))
	{
		s.pop(n);
		cout << n << "\n";
	}

	return 0;
}

//----------------------------------------------------------------------
void stack_type::clear_stack()
{
	top = 0;
}
//----------------------------------------------------------------------
bool stack_type::empty_stack()
{
	if (top==0)
		return true;
	else
		return false;
}
//----------------------------------------------------------------------
bool stack_type::full_stack()
{
	if (top==maxstack-1)
		return true;
	else
		return false;
}
//----------------------------------------------------------------------
void stack_type::push(int numb)
{
	top = top + 1;
	stack[top]=numb;
}
//----------------------------------------------------------------------
void stack_type::pop(int& numb)
{
	numb = stack[top];
	top = top - 1;
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>