C++ 用C+从英语到拉丁语+;使用特定功能
好的,我正在做一个项目,需要我们把英语翻译成拉丁语。我试图复制和调整一些我在网上找到的代码,但结果却恰恰相反,因为这些函数看起来有点相似。在我的类中,我们得到了一些必须在代码中实现的函数 这必须出现在我的源代码中:C++ 用C+从英语到拉丁语+;使用特定功能,c++,string,function,data-structures,C++,String,Function,Data Structures,好的,我正在做一个项目,需要我们把英语翻译成拉丁语。我试图复制和调整一些我在网上找到的代码,但结果却恰恰相反,因为这些函数看起来有点相似。在我的类中,我们得到了一些必须在代码中实现的函数 这必须出现在我的源代码中: struct Word { string english; string pigLatin; }; 功能1:Word*拆分句子(常量字符串词、int和size)~将英语句子作为一个字符串 函数2:void converttopigratin(Word[]wordAr
struct Word
{
string english;
string pigLatin;
};
功能1:Word*拆分句子(常量字符串词、int和size)代码>~将英语句子作为一个字符串
函数2:void converttopigratin(Word[]wordArr,int size)代码>~将英语转换为拉丁语
函数3:void converttopigratin(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 << " ";
}
}
#包括
#包括
#包括
使用名称空间std;
结构字
{
弦乐英语;
拉丁语;
};
单词*拆分句子(wordArr&;int&);
void转换为topiglatin(单词[],int);
拉丁语(单词[],int);
int main()
{
字符串用户输入;
整数大小;
库特
重要提示:如果编译器不支持C++11或更高版本,则只能在C++11或更高版本中使用,请使用
根据您的问题,您必须使用此结构(与问题中的结构相同):
这是一个宏,用于检查第一个字母是元音还是辅音(因此,!is_元音(某些字符)
)
另一个我们只需要获取单词中包含字母(而不是符号和数字)的部分的函数:
std::pair GetWord(std::string word)
{
自动开始=word.end();
for(auto it=word.begin();it!=word.end();+it)
如果(tolower(*it)>='a'和&tolower(*it)='a'和&tolower(*it)那么代码是否正常工作?如果没有,那又如何?问题是什么?它充满了错误。老实说,就像我的一些声明被搞砸了一样。无论我做什么,我都不能声明wordArr。编程是一项相当挑剔的工作。慢慢来。小心地做。写几行。编译它们。测试它们,直到它们正常工作后再写很好。如果你只是坐下来把信吐出来,就像你要读的是一篇社会学论文一样,那你就完了。写几行。确保它们是正确的。然后再写几行。冲洗,重复。如果你想得到错误方面的帮助,如果你告诉我们错误是什么,那会很有帮助。你能复制并粘贴编译程序吗如果你的问题中有行号,请务必告诉我们代码中哪些行号对应这些行号。
typedef struct
{
std::string english;
std::string pig_latin;
} Word;
#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()));
}
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;
}