0

我已经为此工作了一段时间,但我一直无法解决这个问题。本质上,如果要给用户一个“.txt”文件,其中写入了以下内容"tHe@doG#wENt&uP$tHE!hiLL!",则最终结果需要如下所示:

the
hed
edo
dog
ogw
gwe
wen
ent
ntu
...

我可以继续,但我确信这是不言自明的。我一直用来尝试完成此操作的代码如下所示。

#include "functions.h"
#include "bigint/bigint.h"
#include <ctype.h>
#include <string>
#include <cstdio>
#include <stdio.h>


int main(int argc, char *argv[]) {

std::vector<std::string> v(17575);
std::string tri1;
std::string tri2;
std::string tri3;

for (int i = 1; i < argc; i++) {

    char* filename = argv[i];
    std::ifstream infile;

    infile.open(filename);

    // executes for discoverable files  
    if(! infile.fail()) {

        char ch;

        while (infile.get(ch)) {

            if (isspace(ch) || ispunct(ch) || isdigit(ch)) {
                continue;
            }

            else if (isupper(ch)) {
                ch = tolower(ch);
            }

            tri1 += ch;

            char ch2 = infile.peek();

            if (isspace(ch2) || ispunct(ch2) || isdigit(ch2)) {
                ch2 = infile.peek() + 1;
            }

            else if (isupper(ch2)) {
                ch2 = islower(ch2);
            }

            tri2 += ch2;

            char ch3 = infile.peek() + 1;

            if (isspace(ch3) || ispunct(ch3) || isdigit(ch3)) {
                ch3 = infile.peek() + 2;
            }

            else if (isupper(ch3)) {
                ch3 = islower(ch3);
            }

            tri3 += ch3;

            if (tri1.length() == 3) {
                std::cout << tri1 << std::endl;                 
                v.push_back(tri1);
                tri1 = "";
            }

            if (tri2.length() == 3) {
                std::cout << tri2 << std::endl;                 
                v.push_back(tri2);
                tri2 = "";
            }

            if (tri3.length() == 3) {
                std::cout << tri3 << std::endl;                 
                v.push_back(tri3);
                tri3 = "";
            }

        } //while


        infile.close();
    }

    // executes for non-discoverable files
    else {
        std::cerr << "Unable to open file: " << filename << '\n';
    }
}

return 0;

}

这与三元组的频率分析(我稍后将使用的向量)有关,我觉得有一种方法可以减少我拥有的条件句的数量,我只是不知道如何。任何帮助将不胜感激!

4

0 回答 0