 |
|
C# รบกวนพี่ๆที่พอจะอธิบายการทํางานของ Code ได้ ชวยหน่อยหน่ะครับ |
|
 |
|
|
 |
 |
|
อธิบายการทําของโค้ดให้หน่อยนะครับ
ทั้งหมดมี 4 โปรแกรมครับ ช่วยหน่อยครับ
Code (C#)
โปรแกรมที่ 1
static void Main()
{
string astring = "Now is the time";
int pos;
string word;
ArrayList words = new ArrayList();
pos = astring.IndexOf(" ");
while (pos > 0)
{
word = astring.Substring(0, pos);
words.Add(word);
astring = astring.Substring(pos + 1, astring.Length - (pos + 1));
pos = astring.IndexOf(" ");
if (pos == -1)
{
word = astring.Substring(0, astring.Length);
words.Add(word);
}
Code (C#)
โปรแกรมที่ 2
static void Main()
{
string astring = "now is the time for all good people ";
ArrayList words = new ArrayList();
words = SplitWords(astring);
foreach (string word in words)
Console.Write(word + " ");
Console.Read();
}
static ArrayList SplitWords(string astring)
{
string[] ws = new string[astring.Length - 1];
ArrayList words = new ArrayList();
int pos;
string word;
pos = astring.IndexOf(" ");
while (pos > 0)
{
word = astring.Substring(0, pos);
words.Add(word);
astring = astring.Substring(pos + 1, astring.Length - (pos + 1));
if (pos == -1)
{
word = astring.Substring(0, astring.Length);
words.Add(word);
}
Code (C#)
โปรแกรมที่ 3
static void Main() {
string data = "Mike,McMillan,3000 W. Scenic,North Little Rock,AR,72118";
string[] sdata;
char[] delimter = new char[] {','};
sdata = data.Split(delimter, data.Length);
foreach (string word in sdata)
Console.Write(word + " ");
string joined;
joined = string.Join(",", sdata);
Console.Write(joined);
}
Code (C#)
โปรแกรมที่ 4
static void Main() {
int size = 100;
Timing timeSB = new Timing();
Timing timeST = new Timing();
Console.WriteLine();
for(int i = 0; i <= 3; i++) {
timeSB.startTime();
BuildSB.(size);
timeSB.stopTime();
timeST.startTime();
BuilString(size);
timeST.stopTime();
Console.WriteLine
("Time (in milliseconds) to build StringBuilder"
+ "object for " & size & " elements : +
timeSB.Result.TotalMilliseconds);
Console.WriteLine
(("Time (in milliseconds) to build String object"
+ " for " & size & " elements : "+
timeSB.Result.TotalMilliseconds);
Console.WriteLine();
size *= 10;
}
}
static void BuildSB(int size) {
StringBuilder sbObject = new StringBuilder();
for(int i = 0; i <= size; i++)
sbObject.Append("a");
}
static void BuildString(int size) {
string stringObject = "";
for(int i = 0; i <= size; i++)
stringObject & = "a";
}
Tag : .NET, C#
|
|
 |
 |
 |
 |
Date :
2013-08-23 00:58:45 |
By :
กวาง |
View :
1866 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
โปรแกมที่ 5..
static void Main() {
Regex reg = new Regex("the");
String st1 = "the guick brow fox jumped over the lazy dog";
Match matchSet;
int matchPos;
matchSet = reg.Match(st1);
if (matchSet.Success) {
matchPos = matchSet.Index;
Console.WriteLine("found match at position:" + matchPos);
Console.Read();
}
ขอบคุณล่วงหน้าครับ
|
 |
 |
 |
 |
Date :
2013-08-23 01:02:24 |
By :
กวาง |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
โปรแกรมที่ 1 : นำข้อความในตัวแปร astring มาเก็บในตัวแปร words(ArrayList) โดยใช้ช่องว่างในการแบ่งแต่ละอาเรย์
สรุป : นำข้อความ มาแปลงเป็น คำ แล้วเก็บคำ แต่ละคำ เป็นอาเรย์
โปรแกรมที่ 2 : โปรแกรมนี้คล้าย ๆ กับโปรแกรมที่ 1 แต่การทำงานแตกต่างนิดหน่อย โดยการตัดข้อความให้เป็นคำ ๆ จะ
แยกการทำงานในส่วนนี้ออกมาเป็นเมธอดชื่อ SplitWords เมื่อตัดคำเสร็จแล้วจะคืนค่ากลับมาเป็นค่า ArrayList
ให้กับตัวแปร words หลังจากนั้นจะนำตัวแปร words ไปวนลูปให้ครบทุกสมาชิกในอาเรย์ เพื่อแสดงคำแต่ละคำ
มาต่อ ๆ กัน โดยมีช่องว่าง แบ่งในแต่ละคำในการแสดงผล
สรุป : นำข้อความ มาแปลงเป็น คำ โดยการทำงานจะเก็บไว้ในเมธอดหลังจากนั้นจะนำไปแสดงผล
โปรแกรมที่ 3 : โปรแกรมนี้จะตัดข้อความออกมาเป็นคำ ๆ โดยใช้อักษร , (ลูกน้ำ)
เป็นตัวแยกข้อความ และแสดงผลแต่ละคำออกมา หลังจากนั้นจะนำมารวมกันใหม่ โดยใช้ , (ลูกน้ำ)
คั้นแต่ละคำรวมเป็นข้อความเหมือนเดิม และนำมาแสดงผลทั้งข้อความอีกที
สรุป : นำข้อความตัดเป็นคำ ๆ โดยใช้ลูกน้ำในการแบ่งออกเป็นคำ ๆ และนำไปแสดงผล หลังจากนั้น
จะนำมารวมกันใหม่ และนำไปแสดงผลอีกรอบ
โปรแกรมที่ 4 : โปรแกรมนี้ตลกหน่อย ไม่รู้เหมือนกันว่าใครเขียนนะครับ แต่เหมือนจะเป็นลูกผสมระหว่าง VB กับ C# 
โปรแกรมนี้จะสร้างคลาสที่ใช้จับเวลามา (จริง ๆ เขาน่าจะใช้คลาส Stopwatch นะผมก็ไม่เคยเห็น Timing งง )
และจะจับเวลาระหว่างที่ทำการเชื่อมข้อความโดยใช้คลาส StringBuilder กับการเชื่อมข้อความโดยใช้คลาส String ทั่วไป
แล้วมันแสดงผลให้ดูว่าอันไหนมันใช้เวลาในการประมวลผลน้อยกว่ากัน
สรุป : โปรแกรมนี้เป็นการวัดความเร็วในการเชื่อมข้อความ โดยใช้คลาส String ทั่วไปกับ StringBuilder ซึ่งความเร็วในการเชื่อมข้อความของ String
โปรแกรมที่ 5 : เริ่มต้นจะสร้างตัวแปร reg เพื่อใช้เก็บคำที่ต้องการจะค้นหา และจะทำการค้นหาคำที่ตรงกับในตัวแปร reg
จากข้อความในตัวแปร st1 ซึ่งถ้าเจอคำที่แมชหรือตรงกันแล้วจะแสดงตำแหน่งตัวอักษรในข้อความที่ค้นพบนั้น
สรุป : ใช้ในการค้นหาคำที่แมชหรือตรงกันเพื่อแสดงตำแหน่งตัวอักษรในข้อความที่ค้นหาเจอ
หมายเหตุ : ทั้งหมดที่ผมทำให้ จุดประสงค์สุดท้ายเพื่อให้เข้าใจการทำงานภาพรวมของโปรแกรมครับ 
|
 |
 |
 |
 |
Date :
2013-08-23 09:37:58 |
By :
01000010 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|