Snowy Mountains

Thursday, November 10, 2005

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 . . .
***/

Monday, November 07, 2005

Visual C# 2005 is used to create a sample consol application "Hello World C#"


using System;
using System.Collections.Generic;
using System.Text;
namespace Hello_World
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hell World C#!\r\nPlease enter your name:");
string name = Console.ReadLine();
Console.Write("Hello {0}, please enter the year you were born: ", name);
int year = int.MaxValue;
while(year == int.MaxValue)
{
try
{
year = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.Write("You did not enter a valid number. ");
Console.WriteLine("Please enter an integer, such as 1940.");
}
}
Console.WriteLine("You must be approximately {0} years old!",
DateTime.Now.Year - year);
Console.WriteLine("Press the key to exit the application");
Console.Read();
}
}
}
/*
Hell World C#!
Please enter your name:Visual C# 2005
Hello Visual C# 2005, please enter the year you were born: That's private!
You did not enter a valid number. Please enter an integer, such as 2003.
2003
You must be approximately 2 years old!
Press the key to exit the application
Press any key to continue . . .
*/

Tuesday, October 11, 2005

September to November is season of spring here and therefore the best time to visit Australia. Under blue sky, flowers blossom and Kangaroos hop around the lavish green grass paddy field. Unfortunately most of people visit here in the wrong season and suffer from the local harsh climate.