[ C# 演算法] 模擬分析指定列印字串
[ C# 演算法] 模擬分析指定列印字串
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
CS_Console_printpageAnalytics
{
class Program
{
static void Main(string[]
args)
{
int[]
array =
new int[256];
for
(int m = 0; m < array.Length; m++)
{
array[m] = 0;
}
String
StrBuf = “1,2,12,15-25,28-100,200-,210-255,13”;//輸e入J資Me料A
Console.WriteLine(“輸e入J判±P斷_的o資Me料A:” + StrBuf);
////////////////////////
//資Me料A正D?確T性E判±P斷_
byte[]
array1 =
Encoding.ASCII.GetBytes(StrBuf);
Boolean
blnRegular = true;
// Loop
through contents of the array.
foreach
(byte element in
array1)
{
//Console.WriteLine(“{0}
= {1}”, element, (char)element);
blnRegular = true;
if
((element >= 48) && (element <= 59))
{
continue;
}
if
((element == 45) || (element == 44))
{
continue;
}
blnRegular = false;
break;
}
////////////////////////
int
intfindCharIndex=-1;
int
intRangeS, intRangeE;
if
(blnRegular)
{
Console.WriteLine(“資Me料A格a式!正D?確T”);
string[]
strs = StrBuf.Split(
‘,’);//先Dy拆i出DX單a獨W列C印L
for
(int i = 0; i < strs.Length; i++)//單a獨W列C印L迴Xj圈Xe
{
intfindCharIndex = -1;
intfindCharIndex =
strs[i].IndexOf(
‘-‘);
if
(intfindCharIndex >= 0)//在b單a獨W列C印L中?判±P斷_是O否±_為¢X區XI間!列C印L
{
string[]
strs1 = strs[i].Split(
‘-‘);
intRangeS=-1;
intRangeE=-1;
for (int j = 0; j <
strs1.Length; j++)
//尋¦M找±a區XI間!列C印L的o兩La個O數A
{
if (j %
2 == 0)
{
intRangeS = Convert.ToInt32(strs1[j], 10);
}
else
{
try
{
intRangeE =
Convert.ToInt32(strs1[j], 10);
}
catch
{
intRangeE =
-1;
}
break;
}
}
if (intRangeS > 0 && intRangeE > 0)//列C印L出DX區XI間!範d圍o的o所O有3數A
{
if (intRangeS <= intRangeE)
{
for (int k =
intRangeS; k <= intRangeE; k++)
{
Console.Write(k
+
“,”);
if (k <= 256)
{
array[k
– 1] = 1;
}
}
Console.WriteLine();
}
else
{
for (int l =
intRangeE; l <= intRangeS; l++)
{
Console.Write(l + “,”);
if (l <= 256)
{
array[l
– 1] = 1;
}
}
Console.WriteLine();
}
}
else
{
Console.WriteLine(intRangeS+“,”);
if
(intRangeS <= 256)
{
array[intRangeS
– 1] = 1;
}
}
}
else//列C印L出DX單a一@列C印L的o數A
{
intRangeS = Convert.ToInt32(strs[i], 10);
Console.WriteLine(intRangeS + “,”);
if (intRangeS <= 255)
{
array[intRangeS –
1] = 1;
}
}
}
}
else
{
Console.WriteLine(“資Me料A格a式!錯u誤~”);
}
Console.WriteLine();
for
(int n = 0; n < array.Length; n++)
{
Console.Write(array[n]
+
“,”);
}
//////////////////////////
Pause();
}
static void Pause()
{
Console.Write(“Press any key to continue . . . “);
Console.ReadKey(true);
}
}
}