/*16.5
(儿童黑话)编写一个将英语短语编码成pig Latin( 儿童黑话) 的程序。pig Latin 是一种常用于娱乐的编码语言形式。有许多种用于构成Pig Latin 短语的方法,出于简单性的考虑,这里使用如下的一种算法。
为了将每一个英语单词翻译成一个pig Latin 单词,将英语单词的第一个字母放在pigLatin单词的未尾,并在其后添加字母“ay”。这样,单词“jump”就变成了“umpjay" ;单词"the”变成"hetay" 。单词间的空格保持不变。假设有下列条件: 英语短语由用空格分隔的单词组成,没有标点符号且所有的单词都有两个或多个字母。让用户输人一个句子。利用本章中讨论过的的技术将这个句子拆分成单词。用GetPigLain 方法将每个单词翻译成pie Latin 单词。一个文本框中持续显示所有转换后的句子。
*/
using System;
using System.Windows.Forms;
namespace SentenceGenerator
{
public partial class SentenceGeneratorForm : Form
{
public SentenceGeneratorForm()
{
InitializeComponent();
}
string[] articles = { "the", "a", "one", "some", "any" };
string[] nouns = { "boy", "girl", "dog", "town", "car" };
string[] verbs = { "drove", "jumped", "ran", "walked", "skipped" };
string[] prepositions = { "to", "from", "over", "under", "on" };
private void generateButton_Click( object sender, EventArgs e )
{
Random random = new Random();
string sentence = string.Empty;
outputTextBox.Clear();
for ( int i = 0; i < 10; i++ )
{
sentence = string.Empty;
sentence += articles[ random.Next( articles.Length ) ] + " ";
sentence += nouns[ random.Next( nouns.Length ) ] + " ";
sentence += verbs[ random.Next( verbs.Length ) ] + " ";
sentence +=
prepositions[ random.Next( prepositions.Length ) ] + " ";
sentence += articles[ random.Next( articles.Length ) ] + " ";
sentence += nouns[ random.Next( nouns.Length ) ] + ".";
char firstLetter = Char.ToUpper(sentence[ 0 ]);
sentence = firstLetter + sentence.Substring( 1 );
outputTextBox.Text += sentence + " ";
}
}
}
}