簡體   English   中英

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

[英]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;
}

該代碼有兩個問題。

  1. isspace的特殊處理已中斷:

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

    這實質上意味着,如果此循環中的第一個字符為空格,則將其添加兩次。

  2. infile.get()收到的每個字符僅添加一次到seed_stringisspace字符除外)。

編碼的更好方法是認識到:

  1. 您必須忽略連續的isspace字符。
  2. 通過刪除前一個序列的第一個字符並從文件中追加下一個字符,可以獲得每個序列。

這是一個更好的實現; 它在第一個命令行參數中采用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM