Sunday, September 28, 2008

Sort data with Queue(c#)

The Queue is FIFO(first in, first out), this is different from ArrayList. now let's check out how to sort data with queue.
ok, let's go!


using System.Collections;//
namespace SortDataWithQueues
{
class Program
{
enum Digittype {ones = 1,tens = 10 };
static void DisplayArray(int[] n)
{
for (int i = 0; i <= n.GetUpperBound(0); i++)
{
Console.Write(n[i]+" ");
}
}
static void RSort(Queue[] que, int[] n, Digittype digit)
{
int snum;
for (int i = 0; i <= n.GetUpperBound(0); i++)
{
if (digit == Digittype.ones)
{
snum = n[i] % 10;
}
else
{
snum = n[i] / 10;
}
que[snum].Enqueue(n[i]);
}
}
static void BuildArray(Queue[] que,int[] n)
{
int y = 0;
for (int i = 0; i <= 9; i++)
{
while (que[i].Count > 0)
{
n[y] = Int32.Parse(que[i].Dequeue().ToString());
y++;
}
}
}
static void Main(string[] args)
{
Queue[] numQueue = new Queue[10];
int[] nums = new int[] {91,46,85,15,92,35,31,22 };
int[] random = new Int32[99];
//display original list
for (int i = 0; i < 10; i++)
{
numQueue[i] = new Queue();
}
RSort(numQueue,nums,Digittype.ones);
BuildArray(numQueue,nums);
Console.WriteLine();
Console.WriteLine("first pass results: ");
DisplayArray(nums);
//second pass sort
RSort(numQueue,nums,Digittype.tens);
BuildArray(numQueue,nums);
Console.WriteLine();
Console.WriteLine("second pass results: ");
//display final results
DisplayArray(nums);
Console.WriteLine();
}
}
}

Generate the prime numbers using bitArray(c#)

here's a method to generate prime numbers.
ok, let's go!!


namespace bitArray
{
class CArray
{
private int[] arr;
private int upper;
private int numElements;
public CArray(int size)
{
arr = new int[size];
upper = size - 1;
numElements = 0;
}
public void Insert(int item)
{
arr[numElements] = item;
numElements++;
}
public void DisplayElements()
{
for (int i = 0; i <= upper; i++)
{
Console.Write(arr[i]+" ");
}
}
public void Clear()
{
for (int i = 0; i <= upper; i++)
{
arr[i] = 0;
numElements = 0;
}
}
//generate the prime number
public void GenPrime()
{
int temp;
for (int i = 2; i <= arr.GetUpperBound(0); i++)
{
for (int j = i + 1; j <= arr.GetUpperBound(0); j++)
{
if (arr[j] == 1)
{
if ((j % i) == 0)
{
arr[j] = 0;
}
}
}
}
}
//dsiplay the prime number
public void ShowPrime()
{
for (int i = 2; i <= arr.GetUpperBound(0); i++)
{
if (arr[i] == 1)
{
Console.Write(i+" ");
}
}
}
}
class Program
{
static void Main(string[] args)
{
int size = 100;
CArray primes = new CArray(size);
for (int i = 0; i <= size - 1; i++)
{
primes.Insert(1);
}
primes.GenPrime();
primes.ShowPrime();
}
}
}

Friday, September 26, 2008

Money format---insert comma every three number(C#)

when you input a money number , such as:12345678.12, maybe you want to change it as"12,345,678.12",coz this format is earier to read.
Now, here is a method we can do it. I know you can use some current method in .NET framwork, but I think it's helpful for you to write some code by yourself.
OK, let's go!


using System.Collections;//add it for ArrayList
namespace FormatMoney
{
class CStack
{
private int p_index;
private ArrayList list;
public CStack()
{
list = new ArrayList();
p_index = -1;
}
public int count
{
get
{
return list.Count;
}
}
public void push(Object item)
{
list.Add(item);
p_index++;
}
public object pop()
{
object obj = list[p_index];
list.RemoveAt(p_index);
p_index--;
return obj;
}
}
class Program
{
static void Main(string[] args)
{
CStack alist = new CStack();
string ch;
Console.WriteLine("please input the money number...");
string myMoney = Console.ReadLine().ToString();
for (int i = 0; i < myMoney.Length; i++)
{
alist.push(myMoney.Substring(i,1));
}
int num = 0;
StringBuilder sb = new StringBuilder();
while (alist.count > 0)
{
ch = alist.pop().ToString();
if (ch != ".")
{
if (num == 3)
{
sb.Append("," + ch);
num = 1;
}
else
{
sb.Append(ch);
num++;
}
}
else
{
sb.Append(ch);
num = 0;
}
}
StringBuilder sbOut = new StringBuilder();
for (int i = sb.Length-1; i > -1; i--)
{
sbOut.Append(sb.ToString().Substring(i,1));
}
Console.WriteLine(sbOut.ToString());
}
}
}

Thursday, September 25, 2008

Verify the string whether it is palindrome(c#)

Some times we need to verify a string whether it is palindrome, here is a method with stack theory to cope with it.
Let's go!

using System.Collections;//

namespace Palindrome

{

class CStack

{

private int p_index;

private ArrayList list;
//initiate the arraylist

public CStack()

{

list = new ArrayList();

p_index = -1;

}
//return the arraylist length as count number property

public int count { get { return list.Count; } }


//add items into arraylist

public void push(Object item) {

list.Add(item);

p_index++;

}
//retrieve the top data and remove it from arraylist

public object pop() {

object obj = list[p_index];

list.RemoveAt(p_index);

p_index--;

return obj;

}
//remove all data from arraylist

public void clear() {

list.Clear();

p_index = -1;

}
//display all elements of arraylist

public object peek() {

return list[p_index];

}

}
class Program

{

static void Main(string[] args)

{

CStack alist = new CStack();

string ch; Console.WriteLine("input you word....");

string word = Console.ReadLine();

bool isPalindrome = true;
for (int x = 0; x < pos =" 0;"> 0)

{

ch = alist.pop().ToString();

if (ch != word.Substring(pos, 1))

{

isPalindrome = false;

break;

}

pos++;

}

//show the result

if (isPalindrome)

{

Console.WriteLine(word + " is a palindrome");

}

else

{

Console.WriteLine(word+" is not a palindrome");

}

Console.WriteLine();

}

}

}

Basic searching algorithm

Searching a data set for a value is a ubiquitous computational operation. The simplest method of searching a data set is to start at the beginning and search for the item until either the item is found ot the end of data set is reached. This searching method works best when the data set is relatively small and unordered.
If the data set is ordered, the binary searching algorithm is the better choice. Binary search works by continually subdividing the data set until the item being searched for is found. You can write the binary searching algorithm using both iterative and recursive codes. The Array class in C# includes a built-in binary search method, which should be used whenever a binary search is called for.
ok, let's save the words and goto code!


namespace BasicSearchAlgorithm
{
class CArray
{
//binary seach algorithm
public int binSearch(int[] arr,int value)
{
int upperBound, lowerBound, mid;
upperBound = arr.Length - 1;
lowerBound = 0;
while (lowerBound <= upperBound)
{
mid = (lowerBound + upperBound)/2;
if (arr[mid] == value)
{
return mid;
}
else if (value < arr[mid])
{
upperBound = mid - 1;
}
else
{
lowerBound = mid + 1;
}
}
return -1;
}
}
class Program
{
static void Main(string[] args)
{
int[] arr = new int[10] { 12, 5, 21, 89, 63, 45, 72, 50, 43, 16 };
//Insertion Sort
int inner, temp;
int upper = arr.Length - 1;
for (int i = 1; i <= upper; i++)
{
temp = arr[i];
inner = i;
while (inner > 0 && arr[inner - 1] >= temp)
{
arr[inner] = arr[inner - 1];
inner -= 1;
}
arr[inner] = temp;
}
foreach (int i in arr)
{
Console.Write(i+", ");
}
Console.WriteLine();
//end insertion sort
int seek = 43;
CArray myNum = new CArray();
int position = myNum.binSearch(arr, seek);
if (position > -1)
{
Console.WriteLine(seek+" is found item");
}
else
{
Console.WriteLine("the item "+seek+" is not found");
}
}
}
}

that's a code statement for binary search algorithm. since the sequential search is easier, so we won't write this time.

Sorting Algorithm with three popular method(c#)

some times we need to sort the array, it's general in our coding life. so there're three popular method i got. if you want, you can create a random array with Random class, i have mentioned below.
ok, let's go!

namespace SortingAlgorithm
{
public class CArray
{
private int[] arr;
private int upper;
private int numElements;
public CArray(int size)
{
arr = new int[size];
upper = size - 1;
numElements = 0;
}
public void Insert(int item)
{
arr[numElements] = item;
numElements++;
}
public void DisplayElements()
{
for (int i = 0; i <= upper; i++)
{
Console.Write(arr[i]+", ");
}
Console.WriteLine();
}
public void Clear()
{
for (int i = 0; i <= upper; i++)
{
arr[i] = 0;
numElements = 0;
}
}
//Bubble Sort
public void BubbleSort()
{
int temp;
//record how many times in this sort
int SortTimes = 0;
for (int i = upper; i >= 1; i--)
{
for (int j = 0; j <= i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
SortTimes++;
}
}
Console.WriteLine(SortTimes);
}
//Selection Sort
public void SelectionSort()
{
int min;
int temp;
//record how many times in this sort
int SortTimes = 0;
for (int i = 0; i <= upper; i++)
{
min = i;
for (int j = i + 1; j <= upper; j++)
{
if (arr[min] > arr[j])
{
min = j;
}
SortTimes++;
}
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
Console.WriteLine(SortTimes);
}
//Insertion Sort
public void InsertionSort()
{
int inner, temp;
//record how many times in this sort
int SortTimes = 0;
for (int i = 1; i <= upper; i++)
{
temp = arr[i];
inner = i;
while (inner > 0 && arr[inner - 1] >= temp)
{
arr[inner] = arr[inner - 1];
inner -= 1;
SortTimes++;
}
arr[inner] = temp;
}
Console.WriteLine(SortTimes);
}
}
class Program
{
static void Main(string[] args)
{
CArray nums = new CArray(10);
Random rnd = new Random(100);
for (int i = 0; i < 10; i++)
{
//create random number
nums.Insert((int)(rnd.NextDouble()*100));
}
Console.WriteLine("before sorting...");
nums.DisplayElements();
//Console.WriteLine("during bubble sorting...");
//nums.BubbleSort();
//Console.WriteLine("during selection sorting...");
//nums.SelectionSort();
Console.WriteLine("during Insertion sorting...");
nums.InsertionSort();
Console.WriteLine("after sorting...");
nums.DisplayElements();
}
}
}

Tuesday, September 16, 2008

Later at night, do you turn off your cell phone?

I usually do not turn off my cell phone. Why? I have no idea. After reading an article, I seemed to understand a little bit: for that little bit of caring. I am now sharing this story with you.
The girl would turn her cell phone off and put it by her photo on the desk every night before going to bed. This habit has been with her ever since she bought the phone. The girl had a very close boyfriend. When they couldn't meet, they would either call or send messages to each other. They both liked this type of communication. One night, the boy really missed the girl. When he called her however, the girl's cell phone was off because she was already asleep. The next day, the boy asked the girl to leave her cell phone on at night because when he needed to find her and if could not, he would be worried.
From that day forth, the girl began a new habit. Her cell phone never shuts down at night. Because she was afraid that she might not be able to hear the phone ring in her sleep, she tried to stay very alert. As days passed, she became thinner and thinner. Slowly, a gap began to form between them. The girl wanted to revive their relationship. One night, she called the boy. However what she got was a sweet female voice: "Sorry, the subscriber you dailed is power off." The girl knew that her love has just been turned off. After a long time, the girl has a new love. No matter how well they got along, the girl however refused to get married. In the girl's heart, she always remembered that boy's words and the night when that phone was power off.
The girl still keeps the habit of leaving her cell phone on all throughout the night, but not expecting that it'll ring. One night, the girl fell ill. In moment of fluster, instead of calling her parents, she dialed the new boy's cell phone. The boy was already asleep but his cell phone was still on. Later, the girl asked the boy: "Why don't you turn your cell phone off at night?"
The boy answered: "I'm afraid that if you need anything at night and aren't able to find me, you'll worry." The girl finally married the boy.
Later at night, do you turn off your cell phone?

Monday, September 8, 2008

The Real Meaning of Peace

There's once a king who offered a prize to the artist who would paint the best picture of peace. Many artists tried. The king looked at all the pictures. But there were only two he really liked, and he had to choose one between them. One picture was of a clam lake. The lake was a perfect mirror for peaceful towering mountains all around it. Overhead was a blue sky with fluffy white clouds. All who saw this picture thought that it was a perfect picture of peace.
The other picture had mountains,too. But these were rugged and bare. Above was an angry sky, from which rain fell and in which lightning played. Down the side of the mountain tumbled a foaming waterfall. This did not look peacefull at all.
But when the king looked closed, he saw behind the waterfall a tiny bush growing in a crack in the rock. In the bush a mother bird had built her nest. There, in the midst of the rush of angry water, sat the mother bird on her nest-----in perfect peace.
Which picture do you think won the prize? The king chose the second picture. Do you know why?
"Because" explained the king,"peace doesn't mean to be in a place where there's no noise, trouble or hard work. Peace means to be in the midst of all those things and still be calm in your heart. That is the real meaning of the peace."


comments:
The great people don't mean they do some greatness tasks, but coz they have a great heart to the world.

Monday, September 1, 2008

Run Through The Rain

She had been shopping with her Mom in Wal-Mart. She must have been six years old, this beautiful brown haired, freckle-faced image of innocence. It's pouring outside. The kind of rain that
gushes over the top of rain gutters, so much in a hurry to hit the earth, it has no time to flow down the spout.
We all stood there under the awning and just inside the door of Wal-Mart. We waited, some patiently, others irritated because the nature messed up their hurried days. I'm always mesmerized by rainfall. I got lost in the sound and sight of the heavens washing away the dirt and the dust of the world. Memories of running, splashing so carefree as a child coming pouring in as welcome reprieve from the worries of my day.
Her voice was so sweet as it broke the hypmotic trance we were all caught in. "Mom, let's run through the rain." she said.
"What?" Mom asked.
"Let's rn through the rain." She repeated.
"No, honey. We'll wait untill it slows down a bit" Mom replied.
This young child waited about another minute and repeated: "Mom. let's run through the rain."
"We'll get soaked if we do." Mom said.
"No. we won't, Mom. That's not what you said in this morning", the young girl said as she tugged at her Mom's arms.
"This morning? When did I say we could run through the rain and not get wet?"
"Don't you remember? When you were talking to Daddy about his cancer, you said. If God can get us through this, he can get us through anything."
The entire crowd stopped dead silent. I swear you couldn't hear anything but the rain. We all stood silently. No one came and left in the next few minutes. Mom paused and thought for a moment about what she would say.
Now some would laught it off and scold her for being silly. Someone might ignore what was said. But this was a moment of affirmation in a young child's life. Time when innocent trust can be nurtured so that it will bloom into the faith. "Honey, you're absolutely right. Let's run through the rain. If get wet, well maybe we just needed washing." Mom said, then off they ran.
We all stood watching, smiling and laughing as they darted past the cars, and yes, through the puddles. They held their shopping bags over their heads just in case. They got soaked. But they were followed by a few who screamed and laughed like children all the way to their cars. And yes, I did, I ran, I got wet. I needed washing. Curcumstances or people can take away your material possessions, they can take away your money, and they can take away your health. But no one can ever take away you precious memories. So, don't forget to make time and take the opportunities to make yourself happy as a good memories every day.
To everything there is a reason and a time to every purpose under heaven. I hope you still take a time to run through the rain.