2010-11-04 93 views
0

很显然,我对此很陌生,因此也是该项目的内容。我写了一些代码将英语翻译成Pig Latin。很简单。问题是我想找到一种方法来将猪拉丁文翻译成英文使用逻辑块。克隆字符串似乎是一种便宜的方法。有什么建议么?这是我的代码:从拉丁文翻译成英文的C#翻译器

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace FunctionTest 
{ 
    public class PigLatinClass 
    { 

     public static void pigTalk(string sentence) 
     { 
      try 
      { 
       while (sentence != "exit") 
       { 
        string firstLetter; 
        string afterFirst; 
        string pigLatinOut = ""; 
        int x; 
        string vowel = "AEIOUaeiou"; 

        Console.WriteLine("Enter a sentence to convert into PigLatin"); 

        sentence = Console.ReadLine(); 

        string[] pieces = sentence.Split(); 

        foreach (string piece in pieces) 
        { 
         afterFirst = piece.Substring(1); 
         firstLetter = piece.Substring(0, 1); 
         x = vowel.IndexOf(firstLetter); 

         if (x == -1) 
         { 
          pigLatinOut = (afterFirst + firstLetter + "ay "); 
         } 
         else 
         { 
          pigLatinOut = (firstLetter + afterFirst + "way "); 
         } 

         Console.Write(pigLatinOut); 
        } 

        Console.WriteLine("Press Enter to flip the sentence back."); 
        Console.ReadKey(true); 
        string clonedString = null; 
        clonedString = (String)sentence.Clone(); 
        Console.WriteLine(clonedString); 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e.ToString()); 
      } 

     } 
    } 
} 

问题是没有真正的规则可行。例如:如果上一个第三个字母 是“w”,那么您可能想要说这是一个元音字,但是,以“w”开头的辅音字也可以符合此规则。如果第一个字母再次是元音字母,你可能会想说这是一个元音字,但是,从第一个字母移到后面(pat = atpay),辅音字也可以适合这个规则。我认为这是可能的唯一方法是有一个if语句来检查w是否在第三个位置,并且该单词以元音开头,该元音将要求运算符,如果您将它与字符串一起使用,则Visual Studio会对您大喊。

回答

4

的问题是,猪拉丁/英文翻译是不是双射函数。

例如,假设有2个英文单词,如"all""wall",相应的Pig拉丁词将始终为"allway"

这表明,如果你得到一个像"allway"这样的词,你不能给出一个英文的唯一翻译,但(至少)两个。

+0

究竟是什么即时通讯思维。所以,在这种特殊情况下,我不是真的错过了一些东西我遇到了不可能的事情? – 2010-11-04 15:44:56

+0

是的,我认为有一个独特的翻译(大部分时间)是不可能的。但是,如果存在多种可能性,也许你可以提供不同的翻译... – digEmAll 2010-11-04 15:56:50

+0

哇,需要大量头脑风暴才能找到所有多个案例......或者我可以创建一个函数来处理这个问题。好。我想我现在走在正确的道路上。感谢digEmAll和所有帮助我的人! – 2010-11-04 16:03:42

1

我假设这是作业。

你的教授可能想要的是给你一个句子转换成拉丁语和拉丁语。保留原始字符串的副本只允许您从已知非拉丁版本的句子中“翻转”。它不允许你从任何字符串翻转回来。

我想你想构建程序是这样的:

public class PigLatinClass 
{ 
    public static string ToPigLatin(string sentence) 
    { 
     // Convert a string to pig latin 
    } 

    public static string FromPigLatin(string sentence) 
    { 
     // Convert a string from pig latin (opposite logic of above) 
    } 

    public static string PigTalk() 
    { 
     string sentence; 

     Console.WriteLine("Enter a sentence to convert into PigLatin"); 
     sentence = Console.ReadLine(); 
     sentence = ToPigLatin(sentence); 
     Console.WriteLine(sentence); 

     Console.WriteLine("Press Enter to flip the sentence back."); 
     Console.ReadKey(true); 
     sentence = FromPigLatin(sentence); 
     Console.WriteLine(sentence); 
    } 
} 
+1

没有没有作业只是试图回到编码,这似乎是一个有据可查的项目。感谢帮助!是的,建立一个新班级来预制逻辑将会很好。问题是逻辑。我慢慢地想到它不可能。你怎么看? – 2010-11-04 15:34:28

+0

对于翻译器应用程序,返回原始字符串的副本好像在作弊... – 2010-11-07 12:23:55