简体   繁体   English

无法弄清楚如何在C ++中打印字母序列

[英]Can't figure out how to print sequences of letters in C++

I am working on a project where I will be able to read in a file that contains any text, like the sample text below. 我正在做一个项目,在其中我将能够读取包含任何文本的文件,例如下面的示例文本。 Then, character by character, it will be able to output n-character long sequences (represented below as a read-in value given by the user along the lines of 1, 2, 3, 4...) along the whole length of the text. 然后,可以逐个字符地沿整个长度输出n个字符的长序列(以下表示为用户沿1、2、3、4 ...的行给出的读入值)。文本。 So, for example: 因此,例如:

As Gregor Samsa awoke one morning from uneasy dreams he found himself transformed in his bed into a gigantic insect. 格雷戈尔·萨姆萨(Gregor Samsa)一天早晨从不安的梦中醒来时,发现自己躺在床上变成了巨大的昆虫。

If the user provided 2 as the sequence length, the program should spit out: "As" "s " " G" "Gr" "re" "eg" "go" "or" "r " and so on... 如果用户提供2作为序列长度,则程序应吐出:“ As”,“ s”,“ G”,“ Gr”,“ re”,“ eg”,“ go”,“ or”,“ r”等。

I have written this code but don't know why it won't work. 我已经编写了这段代码,但是不知道为什么它不起作用。 Right now, it doesn't spit out every possible variation of the sequence. 现在,它不会吐出该序列的所有可能变化。 Any suggestions would be very helpful. 任何建议将非常有帮助。 Thanks. 谢谢。

#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;
}

The code has two problems. 该代码有两个问题。

  1. The special handling for isspace is broken: isspace的特殊处理已中断:

     if (isspace(ch) && i == 0) { seed_string += ch; } else { seed_string += ch; ch = infile.get(); } 

    This essentially means that if the first character in this loop is a space, it will be added twice. 这实质上意味着,如果此循环中的第一个字符为空格,则将其添加两次。

  2. Every character received from infile.get() is only added to seed_string once (with the exception of isspace characters). infile.get()收到的每个字符仅添加一次到seed_stringisspace字符除外)。

A better way to code this is to recognize that: 编码的更好方法是认识到:

  1. You have to ignore consecutive isspace characters. 您必须忽略连续的isspace字符。
  2. Every sequence can be obtained by removing the first character of the preceding sequnce and appending the next character from the file. 通过删除前一个序列的第一个字符并从文件中追加下一个字符,可以获得每个序列。

Here is a better implementation; 这是一个更好的实现; it takes the order of the Markov model in the first command line parameter and takes the text from standard input. 它在第一个命令行参数中采用Markov模型的顺序,并从标准输入中获取文本。 By encapsulating the skipping of duplicate spaces in a separate function, you don't have to deal with it in the main body of the algorithm. 通过将重复空间的跳过封装在单独的函数中,您不必在算法主体中进行处理。

#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;
}

Example input: 输入示例:

This  is a    test

Example output: 输出示例:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM