Visual C# 2005 is used to build a simple consol application "Stack Component". The application takes advantage of component-oriented features such as properties, indexers, and XML comments.
using System;
using System.Collections.Generic;
using System.Text;
namespace StackComponent
{
class Stack
{
const int SizeIncrement = 10;
private int count = 0;
private object[] values = new object[SizeIncrement];
///
/// the number of elements in the stack
///
///
public virtual int Count
{
get
{
return count;
}
}
///
/// Remove the top element from the stack and return it
///
///The top element
///
public virtual object Pop()
{
// Stack is empty
if (count == 0)
throw new InvalidOperationException();
else
{
// return top of stack
count--;
object ans = values[count];
values[count] = null;
return ans;
}
}
///
/// Push element into the stack
///
/// The object to add
public virtual void Push(object obj)
{
// expand the stack if required
if (count == values.Length)
{
int newSize = values.Length + SizeIncrement;
object[] newValues = new object[newSize];
for (int i = 0; i < values =" newValues;">
/// indexer to peek into the current contents of the stack
///
///
public object this[int index]
{
get
{
return (values[count - index - 1]);
}
}
///
/// Produce a string representation of the stack
///
///The string representation
///
public override string ToString()
{
//throw new Exception("The method or operation is not implemented.");
// Convert all stack items to strings
string[] args = new string[count];
int index = 0;
for (int i = count - 1; i >= 0; i--)
{
args[index] = values[i].ToString();
index++;
}
// and join them together
return String.Join(", ", args);
}
}
class Test
{
public static void Main(string[] args)
{
Stack s = new Stack();
for (int i = 1; i <= 15; i++) { s.Push(i); } Console.WriteLine("Stack s = {0}", s); Console.WriteLine("Stack s[0] = {0}", s[0]); Console.WriteLine("Stack s[5] = {0}", s[5]); while (s.Count > 0)
Console.WriteLine("Popped {0}", s.Pop());
Console.WriteLine("Stack s = {0}", s);
}
}
}
/*** Output
Stack s = 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Stack s[0] = 15
Stack s[5] = 10
Popped 15
Popped 14
Popped 13
Popped 12
Popped 11
Popped 10
Popped 9
Popped 8
Popped 7
Popped 6
Popped 5
Popped 4
Popped 3
Popped 2
Popped 1
Stack s =
Press any key to continue . . .
***/
using System;
using System.Collections.Generic;
using System.Text;
namespace StackComponent
{
class Stack
{
const int SizeIncrement = 10;
private int count = 0;
private object[] values = new object[SizeIncrement];
///
/// the number of elements in the stack
///
///
public virtual int Count
{
get
{
return count;
}
}
///
/// Remove the top element from the stack and return it
///
///
///
public virtual object Pop()
{
// Stack is empty
if (count == 0)
throw new InvalidOperationException();
else
{
// return top of stack
count--;
object ans = values[count];
values[count] = null;
return ans;
}
}
///
/// Push element into the stack
///
/// The object to add
public virtual void Push(object obj)
{
// expand the stack if required
if (count == values.Length)
{
int newSize = values.Length + SizeIncrement;
object[] newValues = new object[newSize];
for (int i = 0; i < values =" newValues;">
/// indexer to peek into the current contents of the stack
///
///
public object this[int index]
{
get
{
return (values[count - index - 1]);
}
}
///
/// Produce a string representation of the stack
///
///
///
public override string ToString()
{
//throw new Exception("The method or operation is not implemented.");
// Convert all stack items to strings
string[] args = new string[count];
int index = 0;
for (int i = count - 1; i >= 0; i--)
{
args[index] = values[i].ToString();
index++;
}
// and join them together
return String.Join(", ", args);
}
}
class Test
{
public static void Main(string[] args)
{
Stack s = new Stack();
for (int i = 1; i <= 15; i++) { s.Push(i); } Console.WriteLine("Stack s = {0}", s); Console.WriteLine("Stack s[0] = {0}", s[0]); Console.WriteLine("Stack s[5] = {0}", s[5]); while (s.Count > 0)
Console.WriteLine("Popped {0}", s.Pop());
Console.WriteLine("Stack s = {0}", s);
}
}
}
/*** Output
Stack s = 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Stack s[0] = 15
Stack s[5] = 10
Popped 15
Popped 14
Popped 13
Popped 12
Popped 11
Popped 10
Popped 9
Popped 8
Popped 7
Popped 6
Popped 5
Popped 4
Popped 3
Popped 2
Popped 1
Stack s =
Press any key to continue . . .
***/
