Sunday, October 12, 2008

Regular Expression (C#)

using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
/// this method only match the first position
///

class Program
{
static void Main(string[] args)
{
string input= "the quick brown fox jumped over the lazy dog";
Regex reg = new Regex("the");//define the match metacharacter
Match matchSet;
int matchPos;
matchSet = reg.Match(input);//veryfy the string with the metacharcters
if (matchSet.Success)//if there's matched string, return true
{
matchPos = matchSet.Index;
Console.WriteLine("found the match at position: "+matchPos);
}
}
}
}

***************************************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
/// this method match mutiple position
///

class Program
{
static void Main(string[] args)
{
string input= "the quick brown fox jumped over the lazy dog";
Regex reg = new Regex("the");//define the match metacharacter
MatchCollection matchSet;
matchSet = reg.Matches(input);
if (matchSet.Count > 0)
{
foreach (Match aMatch in matchSet)
{
Console.WriteLine("found a match at: "+aMatch.Index);
}
}
Console.WriteLine();
}
}
}
**********************************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
/// regular expression also has a Replace method
///

class Program
{
static void Main(string[] args)
{
string input= "the quick brown fox jumped over the lazy dog";
Regex reg = new Regex("the");//define the match metacharacter
input = Regex.Replace(input, "brown", "black");
Console.WriteLine(input);
}
}
}
*****************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///

class Program
{
static void Main(string[] args)
{
string[] words = new string[]{"bad","boy","baaad","bear","bend","ba"};
foreach (string word in words)
{
if (Regex.IsMatch(word, "ba+"))
{
Console.WriteLine(word);
}
}
}
}
}
*****************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///

class Program
{
static void Main(string[] args)
{
string[] words = new string[]{"bad","boy","baaad","bear","bend","baad"};
foreach (string word in words)
{
if (Regex.IsMatch(word, "ba{2}d"))
{
Console.WriteLine(word);
}
}
}
}
}
********************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///

class Program
{
static void Main(string[] args)
{
string[] words = new string[]{"part","of","this","string","is","bold"};
//string regExp = "<.*>";//this is a kind of expression
//string regExp = "<.+>";//
string regExp = "<.+?>";//try these three expressions
MatchCollection aMatch;
foreach (string word in words)
{
if (Regex.IsMatch(word, regExp))
{
aMatch = Regex.Matches(word,regExp);
for (int i = 0; i < aMatch.Count; i++)
{
Console.WriteLine(aMatch[i].Value);
}
}
}
}
}
}
***********************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///

class Program
{
static void Main(string[] args)
{
string input = "the quick brown fox jumped over the lazy dog";
MatchCollection matchSet;
//matchSet = Regex.Matches(input,".");//out put all characters index
matchSet = Regex.Matches(input, "t.e");//out put two "the" characters index
foreach (Match aMatch in matchSet)
{
Console.WriteLine("matches at: "+aMatch.Index);
}
}
}
}

**************************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///

class Program
{
static void Main(string[] args)
{
string input = "THE quick BROWN FOX JUMPED OVER THE LAZY dog";
MatchCollection matchSet;
//matchSet = Regex.Matches(input,".");//out put all characters index
matchSet = Regex.Matches(input, "[a-z]");//out put two "the" characters index
foreach (Match aMatch in matchSet)
{
Console.WriteLine("matches at: "+aMatch.Index);
}
}
}
}
***************************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///

class Program
{
static void Main(string[] args)
{
string[] words = new string[] { "heal","heel","noah","techno"};
//string regExp = "^h";//match at the beginning of word
string regExp = "h$";//match at the end of word
Match aMatch;
foreach (string word in words)
{
if (Regex.IsMatch(word, regExp))
{
aMatch = Regex.Match(word, regExp);
Console.WriteLine("Matched: "+word+" at position: "+aMatch.Index);
}
}
}
}
}
**************************************
using System.Text.RegularExpressions;//
namespace RegularExpression
{
///
///
///

class Program
{
static void Main(string[] args)
{
string words = "08/14/1957 46 02/12/1959 45 08/09/1985 18 03/25/1988 15

09/09/1990 13";
string regExp = "(\\s\\d{2}\\s)";//method to get the age
string regBirthDay = "(?(\\d{2}/\\d{2}/\\d{4}))\\s";//method to get the

birth day

MatchCollection matchSet = Regex.Matches(words,regExp);
foreach (Match aMatch in matchSet)
{
Console.WriteLine(aMatch.Groups[0].Captures[0]);
}
MatchCollection matchSetDate = Regex.Matches(words,regBirthDay);
foreach (Match amatch in matchSetDate)
{
Console.WriteLine("Date: "+amatch.Groups["dates"]);
}
}
}
}

还是单链表排序的问题(C#)

这个问题是一个叫wartim的仁兄提供的代码,很方便。谢谢

namespace WindowsFormsApplication34
{
public partial class Form1 : Form
{
public class LinkedList
{
public LinkedList node = null;
public int data;
}

class Tree
{
public Tree Left = null;
public Tree Right = null;
public int Value;
}

LinkedList LL, Head;
Tree T = null;

public Form1()
{
InitializeComponent();
LinkedList Temp = null;

// 给 LL 赋点值。。。
LL = new LinkedList();
LL.data = 10;
Temp = LL;
LL = new LinkedList();
LL.data = 34;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 6;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 52;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 6;
LL.node = Temp;
Temp = LL;
LL = new LinkedList();
LL.data = 7;
LL.node = Temp;
Head = LL;

// 构建排序二叉树
for (LL = Head; LL != null; LL = LL.node)
AddToTree(ref T, LL.data);

// 输出排序结果
String S = String.Empty;
GetResult(ref T, ref S);
MessageBox.Show(S);
}

private void Form1_Load(object sender, EventArgs e)
{

}

void AddToTree(ref Tree T, int value)
{
if (T == null)
{
T = new Tree();
T.Value = value;
}
else if (value < T.Value) // 比当前值小于的往左
AddToTree(ref T.Left, value);
else if (value >= T.Value) // 比当前值大于等于的往右
AddToTree(ref T.Right, value);
}

void GetResult(ref Tree T, ref String S)
{
if (T == null)
return; // 已经没有节点了
if (T.Left != null)
GetResult(ref T.Left, ref S); // 向左走
S += T.Value.ToString() + " ";
if (T.Right != null)
GetResult(ref T.Right, ref S); // 向右走
}
}
}

单链表排序问题(C#)

只是个一直困然我的问题,单链表排序。如果谁有更简单的方法,希望各位不吝赐教。
题目就是:一个单链表(LinkedList),里面存储着整形数据,但是是无序排列的,请写一个方法,将它按升序排列。


namespace testSortLinklist
{
public class Employee : IComparable<Employee>
{
private string name;
public Employee(string name)
{
this.name = name;
}
public override string ToString()
{
return this.name;
}

public int CompareTo(Employee rhs)
{
return this.name.CompareTo(rhs.name);
}
public bool Equals(Employee rhs)
{
return this.name == rhs.name;
}
}

public class Node<T> : IComparable<Node<T>> where T : IComparable<T>
{
private T data;
private Node<T> next = null;
private Node<T> prev = null;

public Node(T data)
{
this.data = data;
}
public T Data
{
get
{
return this.data;
}
}
public Node<T> Next
{
get
{
return this.next;
}
}

public int CompareTo(Node<T> rhs)
{
return data.CompareTo(rhs.data);
}
public bool Equals(Node<T> rhs)
{
return this.data.Equals(rhs.data);
}

public Node<T> Add(Node<T> newNode)
{
if (this.CompareTo(newNode) > 0)
{
newNode.next = this;
if (this.prev != null)
{
this.prev.next = newNode;
newNode.prev = this.prev;
}
this.prev = newNode;
return newNode;
}
else
{
if (this.next != null)
{
this.next.Add(newNode);
}
else
{
this.next = newNode;
newNode.prev = this;
}
return this;
}
}

public override string ToString()
{
string output = data.ToString();
if (next != null)
output += ", " + next.ToString();
return output;
}
}

public class LinkedList<T> where T : IComparable<T>
{
private Node<T> headNode = null;

public T this[int index]
{
get
{
int ctr = 0;
Node<T> node = headNode;
while (node != null && ctr <= index)
{
if (ctr == index)
{
return node.Data;
}
else
{
node = node.Next;
}
++ctr;
}
throw new ArgumentOutOfRangeException();
}
}

public LinkedList()
{
}

public void Add(T data)
{
if (headNode == null)
{
headNode = new Node<T>(data);
}
else
{
headNode =headNode.Add (new Node<T>(data));
}
}

public override string ToString()
{
if (this.headNode != null)
return this.headNode.ToString();
else
return string.Empty;
}
}

class Program
{
static void Main(string[] args)
{
Run();
}
public static void Run()
{
LinkedList<int> myLinkedList = new LinkedList<int>();
Random rand = new Random();
Console.Write("Adding: ");
for (int i = 0; i < 10; i++)
{
int nextInt = rand.Next(30);
Console.Write("{0} ", nextInt);
myLinkedList.Add(nextInt);
}

LinkedList<Employee> employees = new LinkedList<Employee>();
employees.Add(new Employee ("John"));
employees.Add(new Employee("Paul"));
employees.Add(new Employee("George"));
employees.Add(new Employee("Ringo"));

Console.WriteLine(" Retrieving collections...");

Console.WriteLine("Integers: " + myLinkedList);
Console.WriteLine("Employees: " + employees);

Console.ReadKey();
}
}
}