C ++将英语句子翻译成Pig Latin [英] C++ Translate English Sentence to Pig Latin

查看:103
本文介绍了C ++将英语句子翻译成Pig Latin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我因为翻译成拉丁语而输入一个句子时遇到了问题。它现在被设置为将整个句子作为一个单词阅读。我想知道如何查看句子中的每个单词,以便将每个单词单独翻译为Pig latin。随附的是我的代码:谢谢你的时间。


你好世界的输出出来了:ello worldhay

我想要它说:ellohay orldway


// testpig.cpp:定义控制台应用程序的入口点。

//


#包括stdafx.h

#include< iostream>

#include< string>

#include< cstdlib>

使用命名空间std;



bool isVowel(char ch);

string rotate(string pStr);

string pigLatinString(string pStr);


int main()

{

const int LENGTH = 100;

char str [LENGTH];



字符串字,结果;


cout<< 输入字符串,或输入空白以退出。 << endl;

cout<< 程序将输出Pig Latin中的单词。<< endl;

cin.getline(str,LENGTH);

cout<< endl ;


cout<< 猪的拉丁形式 << str<<<<是:\\\
"<< pigLatinString(str)<< endl;


返回0;

/ *逐字处理输入行* /


/>
};







bool isVowel(char ch)

{

switch(ch)

{

case''A '':案例''E'':

案例''我':案例''O'':

案例''U'':

case''a'':case''e'':

case''i'':case''o'':

case ''你':

返回true;

默认值:返回false;

};

} / / end isVowel


字符串旋转(字符串pStr)


{

string :: size_type len = pStr。 length();


string rStr;


rStr = pStr.substr(1,len-1)+ pStr [0]; / ///


返回rStr;

}


string pigLatinString(string pStr)


{

string :: size _type len;


bool foundVowel;


string :: size_type counter;


if( isVowel(pStr [0]))


pStr = pStr +" way";


else

{//没有用元音开头


pStr = pStr +"" ;;

pStr = rotate(pStr);

len = pStr.length();

foundVowel = false;


for(counter = 1;计数器< LEN-1;计数器++)


if(isVowel(pStr [0]))


{

foundVowel = true;

休息;

}


其他


pStr = rotate(pStr);


if(!foundVowel)


pStr = pStr.substr(1,len)+" way";


其他


pStr = pStr +" ay" ;;

}

返回pStr;


}

解决方案


我在翻译句子时遇到问题进入猪拉丁语。它现在被设置为将整个句子作为一个单词阅读。我想知道如何查看句子中的每个单词,以便将每个单词单独翻译为Pig latin。附上我的代码:谢谢你的时间。



查看C ++函数strtok()。我想你可以用它来分隔单词,只要你知道分隔它们的分隔符(比如空格)。


你可以把它作为字符读出来,然后发出信号每次看到一个空格时,一个单词的结尾...这是一种方法来完成它...完全避免字符串。


你们两个都知道我在哪里关于这个的教程?我的书不是很清楚,我的教授示例程序都没有显示出来。感谢

I am having problems entering a sentence for translating into pig latin. It is set up now to read the entire sentence as one word. I would like to know how to look at each word in the sentence so that each word is translated to Pig latin seperately. Attached is my code: Thank you for your time.

the out put for hello world comes out :ello worldhay
I want it to say: ellohay orldway

// testpig.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;



bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);

int main ()
{

const int LENGTH = 100;
char str [LENGTH];



string word, result;

cout << "Enter a string or, blank to exit." <<endl;
cout << "The program will output the words in Pig Latin."<<endl;
cin.getline(str,LENGTH);
cout<<endl;

cout << "The pig Latin form of " << str <<" is:\n"<< pigLatinString(str) << endl;

return 0;
/* Process the input line word by word */



};









bool isVowel(char ch)
{
switch (ch)
{
case ''A'': case ''E'':
case ''I'': case ''O'':
case ''U'':
case ''a'': case ''e'':
case ''i'': case ''o'':
case ''u'':
return true;
default: return false;
};
}//end isVowel

string rotate(string pStr)

{
string::size_type len=pStr.length();

string rStr;

rStr=pStr.substr(1,len-1)+pStr[0];////

return rStr;
}

string pigLatinString(string pStr)

{
string::size_type len;

bool foundVowel;

string::size_type counter;

if (isVowel(pStr[0]))

pStr=pStr+"way";

else
{//didn''t start with a vowel

pStr = pStr + "";
pStr=rotate(pStr);
len=pStr.length();
foundVowel=false;

for (counter = 1; counter<len-1; counter++)

if (isVowel(pStr[0]))

{
foundVowel=true;
break;
}

else

pStr=rotate(pStr);

if (!foundVowel)

pStr=pStr.substr(1,len)+"way";

else

pStr = pStr + "ay";
}
return pStr;

}

解决方案

I am having problems entering a sentence for translating into pig latin. It is set up now to read the entire sentence as one word. I would like to know how to look at each word in the sentence so that each word is translated to Pig latin seperately. Attached is my code: Thank you for your time.

Check out the C++ function strtok(). I think you can use that to separate out the words, as long as you know the delimiters that will be separating them (like spaces).


you can read it in as characters, and signal the end of a word every time it sees a space...that''s one way to do it...by avoid strings entirely.


do either of you know where I get a tutorial on this? My book is not very clear and none of my professors example programs show this. Thanks


这篇关于C ++将英语句子翻译成Pig Latin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆