using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class WordCountProgram
{
static void Main(String[] args)
{
var operatorSet = new Tuple<String, Func<String, String>>[]
{ Tuple.Create<String, Func<String, String>>("w", i => String.Join(" ",i.Split(Estring.seperators, StringSplitOptions.RemoveEmptyEntries)) )
, Tuple.Create<String, Func<String, String>>("r", i => i.Reverse() )
, Tuple.Create<String, Func<String, String>>("u", i => i.ToUpper() )
, Tuple.Create<String, Func<String, String>>("h", i => helpMessage )
};
operatorSet.filter(i=>IsGivenInArgs(i.Item1,args)).foldl(GetStringFromArgs(args),(b,f) => f.Item2(b)).ShowScreen();
}
static bool IsGivenInArgs(string command,string[] options )
{
Regex regex = new Regex(@"^-(\w)*" + command + @"(\w)*");
return Array.Exists(options, (s) => regex.IsMatch(s));
}
static string GetStringFromArgs(string[] options)
{
Regex regex = new Regex(@"^-(\w)*");
return Array.Find(options, (s) => !regex.IsMatch(s));
}
static string helpMessage = "用法: DealSentenct [-w] [-r] [-u] <\"a sentence\">\n\n"
+ "其中 [] 表示可选参数项,句子串以双引号引出\n"
+ "-w 分解出单词并分行输出\n"
+ "-r 将原句子字串反向输出(若同时又-w选项,按单词分行反向输出)\n"
+ "-u 处理结果以大写字母输出\n"
+ "-h 输出此帮助信息\n";
}
static class Estring
{
static public String Reverse(this String s)
{
Char[] t = s.ToCharArray();
Array.Reverse(t);
return new String(t);
}
// print the object to console
static public Boolean ShowScreen<T>(this T m)
{
try { Console.WriteLine(m.ToString());}
catch { return false; }
return true;
}
static public Char[] seperators = { ' ', '\n', '\t', '.', '\"', ';', ',', '!', '?', '(', ')', '<', '>', '[', ']' };
}
/* ------------------------------------------------------------------------------------
* The Generic Extention to T[]
* Making the original function side-effect-less(Reference Tranparent)
* ------------------------------------------------------------------------------------
*/
static class XList
{
//take head of the array or throw an error
static public T head<T>(this T[] x)
{
return x[0];
}
//take all elements of the array except its head
static public T[] tails<T> (this T[] x)
{
int n = x.Length;
if (n == 0)
{
return x;
}
else
{
T[] y = new T[n-1];
for (int i = 0; i < (n-1); i++)
{
y[i] = x[i+1];
}
return y;
}
}
//let f acts on every element (Functor Map)
static public T2 [] map<T1, T2> (this T1[] x, Func<T1,T2> f)
{
if (x.Length == 0)
{
return new T2 [] {};
}
else
{
return f(x.head()).con(x.tails().map(f));
}
}
//the elements become the head of an array
static public T[] con<T>(this T a, T[] x)
{
var y = new T[x.Length + 1];
x.CopyTo(y, 1);
y[0] = a;
return y;
}
//applied to a predicate and a list, returns the list of those elements that satisfy the predicate;
static public T[] filter<T>(this T[] xs, Func<T,Boolean> p)
{
if (xs.Length == 0)
{
return new T[] {};
}
else
{
if (p(xs.head()))
{
return xs.head().con(xs.tails().filter(p));
}
else
{
return xs.tails().filter(p);
}
}
}
//applied to a binary operator, a starting value (typically the left-identity of the operator), and a list, reduces the list using the binary operator, from left to right:
static public T2 foldl<T1,T2>(this T1[] xs,T2 b, Func<T2,T1,T2> f)
{
if (xs.Length == 0)
{
return b;
}
else
{
return xs.tails().foldl(f(b,xs.head()), f);
}
}
}
}
魔改C#(字符统计处理)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到Github Pages浏览最新内容。...
- title: '[考研]东大C语言编程题——11统计字符'date: 2017-10-16 16:33:19tag...
- 题目如下: 1、求输入英文句子单词的平均长度。(以空格,逗号,感叹号,问号等符号隔开单词,最后以实心句号结束。)如...