我需要用Pig拉丁形式写一个句子,我几乎成功了,除了一个案例,我几乎放弃了例如:如果我的单词以a\e\o\u\i开头,这个单词看起来会像easy->easyway,apple->appleway
如果不是以我在上面写的一封信开头,它看起来会是这样的:box->oxbay,king->ingkay
粗体部分我成功了,但在第一部分开头有一个字母e\o\u\i,我不知道把w放在哪里,需要一些帮助
这是我的密码,提前谢谢
# include <iostream>
//Since those are used in ALL function of program, it wont hurt to set it to global
//Else it is considered EVIL to declare global variables
const int maxLine = 100;
char phraseLine[maxLine] = { '\0' };
void pigLatinString();
using namespace std;
void main()
{
// Displayed heading of program
cout << "* You will be prompted to enter a string of *" << endl;
cout << "* words. The string will be converted into *" << endl;
cout << "* Pig Latin and the results displayed. *" << endl;
cout << "* Enter as many strings as you would like. *" << endl;
//prompt the user for a group of words or press enter to quit
cout << "Please enter a word or group of words. (Press enter to quit)\n";
cin.getline(phraseLine, 100, '\n');
cout << endl;
// This is the main loop. Continue executing until the user hits 'enter' to quit.
while (phraseLine[0] != '\0')
{
// Display the word (s) entered by the user
cout << "You entered the following: " << phraseLine << endl;
// Display the word (s) in Pig Latin
cout << "The same phrase in Pig latin is: ";
pigLatinString();
cout << endl;
//prompt the user for a group of words or press enter to quit
cout << "Please enter a word or group of words. (Press enter to quit)\n";
cin.getline(phraseLine, 100, '\n');
}
return;
}
void pigLatinString() //phraseLine is a cstring for the word, maxline is max length of line
{ //variable declarations
char tempConsonant[10];
tempConsonant[0] = '\0';
int numberOfConsonants = 0;
char previousCharacter = ' ';
char currentCharacter = ' ';
bool isInWord = 0;
// for loop checking each index to the end of whatever is typed in
for (int i = 0; i < maxLine; i++)
{
//checking for the end of the phraseline
if (phraseLine[i] == '\0')
{//checking to see if it's in the word
if (isInWord)
{//checking to see that there wasn't a space ahead of the word and then sending the cstring + ay to the console
if (previousCharacter != ' ')
cout << tempConsonant << "ay" << endl;
}
return;
}
// this covers the end of the word condition
if (isInWord)
{// covers the condition of index [i] being the space at the end of the word
if (phraseLine[i] == ' ')
{
// spits out pig latin word, gets you out of the word, flushes the temp consonants array and resets the # of consonants to 0
cout << tempConsonant << "ay";
isInWord = 0;
tempConsonant[0] = '\0';
numberOfConsonants = 0;
}
cout << phraseLine[i] ;
}
else
{//this covers for the first vowel that makes the switch
if (phraseLine[i] != ' ')
{// sets the c string to what is in the phraseline at the time and makes it capitalized
char currentCharacter = phraseLine[i];
currentCharacter = toupper(currentCharacter);
// this takes care of the condition that currentCharacter is not a vowel
if ((currentCharacter != 'A') && (currentCharacter != 'E') &&
(currentCharacter != 'I') && (currentCharacter != 'O') && (currentCharacter != 'U'))
//this sets the array to temporarily hold the consonants for display before the 'ay'
{//this sets the null operator at the end of the c string and looks for the next consonant
tempConsonant[numberOfConsonants] = phraseLine[i];
tempConsonant[numberOfConsonants + 1] = '\0';
numberOfConsonants++;
}
else
{// this sets the boolean isInWord to true and displays the phraseline
isInWord = 1;
cout << phraseLine[i];
}
}
else
{
cout << phraseLine[i] ;
}
}
previousCharacter = phraseLine[i];
}
return;
}
1条答案
按热度按时间oiopk7p51#
你有两个条件要考虑。
if
你的单词以元音开头,在单词末尾加上“way”,else
移动第一个字母并在末尾加上“ay”。这是一项可以通过使用
std::string
而不是c字串。这是因为您现在不再关心超出长度或丢失空字符。它还允许更容易地访问标准库算法。上面的函数强调了如何组织转换。你只需要知道你的单词是否以元音开头,然后采取适当的行动。
输出:
我没有得到这样的印象,你必须关心的话,如'电话'。
通过查看代码,您应该在分离关注点方面做得更好。pig latin一次只写一个单词比较容易,但是在pig latin函数中有字符串拆分代码和许多“not pig latin”代码。你的main可以处理输入。您可能应该有一个单独的函数,使用
std::vector
保留这些词是最好的,因为它可以按需增长,而不必预先知道具体的容量。然后遍历单词数组并分别翻译它们。根据您的实际需求,您甚至不必存储翻译的单词,只需将它们直接打印到屏幕上即可。这是相同的程序,但现在它可以分离单词。注意pig-latin函数不必为了添加添加的功能而改变(很多,我添加了大写元音,只是因为我不想麻烦地转换单词)。
输出: