使用特定功能在C ++中将英语翻译成Pig Latin

时间:2018-10-13 06:16:21

标签: c++ string function data-structures

好的,所以我正在做一个项目,要求我们将英语翻译成Pig Latin。我试图复制和调整一些我在网上找到的代码,但这样做却相反,因为这些函数看上去很相似。在我的课堂上,我们获得了某些必须在代码中实现的功能。

这必须出现在我的源代码中:

struct Word
{
    string english;
    string pigLatin;
};

功能1:Word * splitSentence(const string words, int &size);〜将英语句子作为1个字符串

功能2:void convertToPigLatin(Word [] wordArr, int size);〜将英语转换为Pig Latin

功能3:void convertToPigLatin(Word [] wordArr, int size);〜显示

这是我的代码。 编辑:我遇到的问题是我的代码中没有声明wordArr,因此无法编译。我还注意到了一些小错误,并更改了我的代码。您必须原谅我,我现在很累。

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

struct Word
{
    string english;
    string pigLatin;
};

Word * splitSentence(wordArr&; int &);
void convertToPigLatin (Word[], int);
void displayPigLatin (Word[], int);

int main()
{
    string userInput;
    int size;

    cout <<"This is an English to Pig Latin translator.\n";
    cout <<"Enter a sentance that you want to translate.\n";
    getline(cin, userInput);

    Word *wordArr = convertToPigLatin(userInput, size); 
    displayPigLatin(wordArr, size);

return 0;
}

Word * splitSentence(const Word [] wordArr, int &size)
{

    int num = 0;
    int phraseLength = userInput.length();

    for (int i = 0; i < size; i++)
    {
        if (isspace(userInput[i]))
        {
            if (isspace(userInput[ i - 1]))
            {
                num--;
            }
            num++;
        }
    }

    if (ispunct(userInput[i]))
    {
        if (i != (phraseLength - 1))
        {
            userInput.erase(1--, 1)
        }
    }
}

void convertToPigLatin(Word wordArr[], int size)
{
    for (int i = 0; i < size; i++)
    {
        int stringLength;

        if (i == (size - 1))
            {
                stringLength = wordArr[i].userInput.length();
            }

        else
            {
                stringLength = wordArr[i].userInput.length() - 1;
            }

        string vowel, way;
        vowel = wordArr[i].userInput.at(stringLength - stringLength);
        way = wordArr[i].userInput.at(stringLength - 3);
        bool markV = ((vowel == 'A') || (vowel == 'a') (vowel == 'E') || (vowel == 'e')
        (vowel == 'I') || (vowel == 'i') (vowel == 'O') || (vowel == 'o') 
        (vowel == 'U') || (vowel == 'u'));

        wordArr[i].userInput.erase(stringLength - 3, 3);

        if (!(markV && (way == 'w')))
        {
            wordArr[i].english = way + wordArr[i].userInput;
        }
    }

    displayPigLatin(wordArr, stringLength);
}

void displayPigLatin(const Word wordArr[], int size);
{
    cout << "\nHere is your phrase in PigLatin: ";
    for (int i = 0 i < size; i++)
    {
        cout << wordArr[i].userInput << " ";
    }
}

1 个答案:

答案 0 :(得分:0)

  

重要说明::如果编译器不支持 C ++ 11,则只能在 C ++ 11或更高版本中使用range based for-loops 或更高版本,请使用regular for-loops ...

给出您的问题,您已经使用此结构(等同于问题中的结构):

typedef struct
{
    std::string english;
    std::string pig_latin;
} Word;

这是一个宏,将用于检查第一个字母是元音还是辅音(因此,!IS_VOWEL(some_char))。

#define IS_VOWEL(x) ((x) == 'A' || (x) == 'E' || (x) == 'I' || (x) == 'O' || (x) == 'U' || \
    (x) == 'a' || (x) == 'e' || (x) == 'i' || (x) == 'o' || (x) == 'u')

另一个功能,我们只需要获取单词中包含字母(不包括符号和数字)的部分:

std::pair<unsigned, unsigned> GetWord(std::string word)
{
    auto start = word.end();
    for (auto it = word.begin(); it != word.end(); ++it)
        if (tolower(*it) >= 'a' && tolower(*it) <= 'z' && start == word.end())
            start = it;
        else if (start != word.end() && !(tolower(*it) >= 'a' && tolower(*it) <= 'z'))
            return std::make_pair(std::distance(word.begin(), start), std::distance(word.begin(), it));
    return std::make_pair(start == word.end() ? 0 : std::distance(word.begin(), start), std::distance(word.begin(), word.end()));
}

最后但并非最不重要的是,将英语转换为Pig-Latin的功能(我知道它非常大):

std::vector<Word> CreatePigLatinWordsFromEnglish(std::string english, bool sentence_case = true)
{
    // You can break it from here to use inside another function (viz., splitSentence)
    std::transform(english.begin(), english.end(), english.begin(), ::tolower);
    std::stringstream english_stream(english);
    std::vector<Word> words;
    std::string temporary;
    while (std::getline(english_stream, temporary, ' '))
        words.emplace_back(Word({ temporary, "" }));
    // Till here...
    // From here the conversion starts...
    for (auto &word : words)
    {
        auto const word_it = GetWord(word.english);
        if (!IS_VOWEL(word.english[word_it.first]) && !std::string(std::next(word.english.begin(), word_it.first),
            std::next(word.english.begin(), word_it.second)).empty())
        {
            word.pig_latin.append(std::string(word.english.begin(), std::next(word.english.begin(), word_it.first)));
            word.pig_latin.append(std::string(std::next(word.english.begin(), word_it.first + 1), std::next(word.english.begin(), word_it.second)));
            word.pig_latin.append(1, word.english[word_it.first]);
            word.pig_latin.append(std::string("ay"));
            word.pig_latin.append(std::next(word.english.begin(), word_it.second), word.english.end());
        }
        else
            word.pig_latin = std::string(word.english.begin(), std::next(word.english.begin(), word_it.second)) + "way"
                + std::string(std::next(word.english.begin(), word_it.second), word.english.end());
    }
    // Conversion ends here...
    // Changing the case from lower case to sentence case if needed...
    if (sentence_case)
    {
        words[0].english[0] = toupper(words[0].english[0]);
        words[0].pig_latin[0] = toupper(words[0].pig_latin[0]);
    }
    return words; // Returning the list of words we got...
}
  

嗯,一个演示此方法的示例:

int main()
{
    auto const test = "An apple a day keeps the doctor away!";
    for (auto word : CreatePigLatinWordsFromEnglish(test))
        std::cout << word.pig_latin << " ";
    return 0;
}
     
    

输出:

         

离开Appleway离开Ayday eepskay hetay八月天离开!

  
     

尝试一下,看看它是否能为您提供所需的结果...

亲切的问候,

Ruks。