[英]Can't figure out how to print sequences of letters in C++
我正在做一个项目,在其中我将能够读取包含任何文本的文件,例如下面的示例文本。 然后,可以逐个字符地沿整个长度输出n个字符的长序列(以下表示为用户沿1、2、3、4 ...的行给出的读入值)。文本。 因此,例如:
格雷戈尔·萨姆萨(Gregor Samsa)一天早晨从不安的梦中醒来时,发现自己躺在床上变成了巨大的昆虫。
如果用户提供2作为序列长度,则程序应吐出:“ As”,“ s”,“ G”,“ Gr”,“ re”,“ eg”,“ go”,“ or”,“ r”等。
我已经编写了这段代码,但是不知道为什么它不起作用。 现在,它不会吐出该序列的所有可能变化。 任何建议将非常有帮助。 谢谢。
#include "genlib.h"
#include <iostream>
#include <fstream>
#include "simpio.h"
#include "random.h"
#include "vector.h"
#include "map.h"
/* Private Instance Variables */
int seed_length;
string line;
string seed_string;
string next_string;
char ch;
/* Function Prototypes */
string promptUserForFile(ifstream & infile);
int main() {
ifstream infile;
promptUserForFile(infile);
// Ask what order of Markov model to use.
cout << "What order of Markov model should we use? ";
cin >> seed_length;
while (infile.eof() == false) {
ch = infile.get();
for (int i = 0; i < seed_length - 1; i++) {
cout << "ch up here is " << ch << endl;
if (isspace(ch) && i == 0) {
seed_string += ch;
} else {
seed_string += ch;
ch = infile.get();
}
}
next_string = ch;
if (isspace(ch)) {
next_string = " ";
} else {
char trythis = infile.get();
next_string += trythis;
}
cout << seed_string << endl;
cout << next_string << endl;
seed_string = "";
next_string = "";
}
cout << "TEST" << endl;
// Close the file when you're done storing all of the scores.
infile.close();
return 0;
}
string promptUserForFile(ifstream & infile) {
string prompt = "Please input your filename: ";
while(true) {
cout << prompt;
string filename;
getline (cin, filename);
infile.open(filename.c_str());
if(!infile.fail()) return filename;
infile.clear();
cout << "Unable to open that file. Try again." << endl;
if (prompt == "") prompt == "Input file: ";
}
return 0;
}
该代码有两个问题。
isspace
的特殊处理已中断:
if (isspace(ch) && i == 0) { seed_string += ch; } else { seed_string += ch; ch = infile.get(); }
这实质上意味着,如果此循环中的第一个字符为空格,则将其添加两次。
从infile.get()
收到的每个字符仅添加一次到seed_string
( isspace
字符除外)。
编码的更好方法是认识到:
isspace
字符。 这是一个更好的实现; 它在第一个命令行参数中采用Markov模型的顺序,并从标准输入中获取文本。 通过将重复空间的跳过封装在单独的函数中,您不必在算法主体中进行处理。
#include <iostream>
#include <cstdlib>
char next_character() {
static bool was_space = false;
char ret = 0;
do {
ret = std::cin.get();
} while (was_space && std::isspace(ret));
if (std::isspace(ret)) {
was_space = true;
ret = ' ';
} else {
was_space = false;
}
return ret;
}
int main(int argc, char **argv) {
if (argc != 2) return 0;
int mlen = std::atoi(argv[1]);
std::string seq;
for (unsigned i = 0; i < mlen; ++i) {
seq += next_character();
}
std::cout << seq << '\n';
while (true) {
seq.erase(0, 1);
char c = next_character();
if (std::cin.eof()) break;
seq += c;
std::cout << seq << '\n';
}
return 0;
}
输入示例:
This is a test
输出示例:
This
his i
is is
s is
is a
is a
s a t
a te
a tes
test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.