翻译计划 [英] Translating Program

查看:146
本文介绍了翻译计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始写一个程序翻译这将转化使用并行阵列一个文件中找到的文本字符串。翻译的语言是猪拉丁语。我创建了一个文本文件作为拉丁英语词典猪使用。我不想用任何二维数组;我想保持阵列在一个维度。
基本上,我想读写在PigLatin和使用字典的文本文件我创造我想要的输出翻译为英语在命令行上。

I am beginning to write a translator program which will translate a string of text found on a file using parallel arrays. The language to translate is pig Latin. I created a text file to use as a pig latin to English dictionary. I didn't want to use any two dimension arrays; I want to keep the arrays in one dimension. Basically I want to read a text file written in PigLatin and using the dictionary I created I want to output the translation to English on the command line.

我的伪code思路是:

My pseudo-code idea is:


  • 打开字典的文本文件。

  • Open the dictionary text file.

询问用户写在PigLatin的文本文件,他/她要翻译成英文名称

Ask the user for the name of the text file written in PigLatin that he/she wants to translate to English

在搜索用户的文本文件的每个字,并比较所述词典以然后相应翻译的单词。继续下去,直到有没有更多的话来翻译。

Searching each word on the user's text file and comparing to the Dictionary to then translate the word accordingly. Keep on going until there are no more words to translate.

出现命令行屏幕上翻译的单词。

Show the translated words on the command line screen.

我想上使用并行阵列,包含英语翻译的单词和含猪拉丁词另一个之一。

I was thinking on using a parallel arrays, one containing the english translated words and another one containing the pig latin words.

我想知道我怎么可以操作使用数组在C ++中的字符串?

I would like to know how can I manipulate the strings using arrays in C++?

感谢您。

推荐答案

声明字符串数组是容易的,与声明的任何其他数组。

Declaring an array of strings is easy, the same as declaring an array of anything else.

const int MaxWords = 100;
std::string piglatin[MaxWords];

这是100字符串对象的数组,数组被命名为 piglatin 。该字符串开始是空的。您可以填写该数组是这样的:

That's an array of 100 string objects, and the array is named piglatin. The strings start out empty. You can fill the array like this:

int numWords = 0;
std::ifstream input("piglatin.txt");
std::string line;
while (std::getline(input, line) && numWords < MaxWords) {
  piglatin[numWords] = line;
  ++numWords;
}
if (numWords == MaxWords) {
  std::cerr << "Too many words" << std::endl;
}



我强烈建议你的的使用数组。使用容器对象,如 的std ::载体 的std :: deque的 代替。这样,你可以加载文件的内容,而无需事先知道文件有多大。上面的例子声明,你需要确保你没有在你的文件超过100个条目,如果有少于100,那么你需要跟踪的多少项阵列中是有效的。

I strongly recommend you not use an array. Use a container object, such as std::vector or std::deque, instead. That way, you can load the contents of the files without knowing in advance how big the files are. With the example declaration above, you need to make sure you don't have more than 100 entries in your file, and if there are fewer than 100, then you need to keep track of how many entries in your array are valid.

std::vector<std::string> piglatin;

std::ifstream input("piglatin.txt");
std::string line;
while (std::getline(input, line)) {
  piglatin.push_back(line);
}

这篇关于翻译计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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