简体   繁体   English

如何计算文本文件中的哪一行单词是 C++

[英]How to count in which line in text file the word is C++

I have an exercise in which I need to find words in a text file starting with user input symbol.我有一个练习,我需要在以用户输入符号开头的文本文件中查找单词。 I also need to determine in which line that word is and output that in text different text file.我还需要确定该单词在哪一行以及 output 在文本不同的文本文件中。 I managed to write code to output words starting with symbol and count word's position in text, but i cannot figure out how to count in which line that word is.我设法将代码写入以符号开头的 output 单词并在文本中计数单词的 position,但我无法弄清楚如何计算该单词在哪一行。 Also i need to find those words which have symbols like ?我还需要找到那些有符号的单词? ! etc. not only ' ' For example if i wanna find words starting with c then my program finds only "cerebral, cortex, could, create" but not "construct, capable, computers" from my example input which is below my code.等不仅' '例如,如果我想找到以c开头的单词,那么我的程序从我的示例输入中仅找到“大脑、皮质、可以、创建”而不是“构造、有能力、计算机”,该输入位于我的代码下方。

#include <iostream>
#include <fstream>
using namespace std;

int main() {

    fstream input;
    fstream output;
    string word, line;
    char startOfWord;

    cout << "I wanna find words starting with symbol: \n";

    cin >> startOfWord;

    int lineNumber = 0;

    input.open("f.txt");
    output.open("f1.txt");

    while (!input.eof()) {

        input >> word;
        lineNumber++;
        if (word.length() > 40) {
            continue;
        }
        if (word[0] == startOfWord) {
            output << word << ' ' << lineNumber << '\n';
        }
    }

    input.close();

    output.close();

    return 0;

}

Example input: user wanna find words starting with a .示例输入:用户想要查找以a单词。

f.txt : f.txt

A Stanford University project to?construct a model 
of the cerebral cortex in silicon could help scientists 
gain a better understanding of the brain, in order to 
create more,capable.computers and advanced 
neural prosthetics. 

Output: f1.txt Output: f1.txt

a 1
a 3
and 4
advanced 4

You are reading the file word by word, counting each word.您正在逐字阅读文件,计算每个单词。 You need to read the file line by line, counting each line, and then you can read words from each line.您需要逐行读取文件,计算每一行,然后您可以从每一行读取单词。

Try this:尝试这个:

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main() {
    ifstream input;
    ofstream output;
    string word, line;
    char startOfWord;

    cout << "I wanna find words starting with symbol: \n";

    cin >> startOfWord;

    int lineNumber = 0;

    input.open("f.txt");
    output.open("f1.txt");

    while (getline(input, line)) {
        ++lineNumber;
        istringstream iss(line);
        while (iss >> word) {
            if (word.length() > 40) {
                continue;
            }
            if (word[0] == startOfWord) {
                output << word << ' ' << lineNumber << '\n';
            }
        }
   }

    input.close();
    output.close();

    return 0;
}

I did it!我做的! This works!这行得通!

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

void getWordsFromLine(string word, int index, vector<string> &memory) { //funkcija kas izvelk vardus no rindam
    string newWord;
    for (int i = index; i < word.length(); i++) {
        if (word[i] == ')' || word[i] == '(' || word[i] == '.' || word[i] == ',' || word[i] == '=' || word[i] == '?' || word[i] == '!') { //parbauda visas iespejamas pieturzimes
            i++;
            getWordsFromLine(word, i, memory);
            break;
        }
        else {
            newWord += word[i];
        }
    }
    memory.push_back(newWord);
}

int main() {
    int ok;
    do
    {
    ifstream input;
    ofstream output;
    string word, line;
    char startOfWord;
    char symbol;

    cout << "I wanna find words starting with symbol: \n";

    cin >> startOfWord;

    int lineNumber = 0;

    input.open("f.txt");
    output.open("f1.txt");

    while (getline(input, line)) {
        ++lineNumber;   //skaita rindas
        istringstream iss(line);
        while (iss >> word) {
            if (word.length() > 40) {
                continue;
            }
            vector<string> words;
            getWordsFromLine(word, 0, words);
            for (int i = 0; i < words.size(); i++) {
                if (words[i][0] == startOfWord) { //atrod vardus ar sakuma simbolu
                    output << words[i] << ' ' << lineNumber << '\n';
                }
            }
        }
    }

    input.close();
    output.close();
    cout<<"Vai turpinat (1) vai beigt (0)?"<<endl;
    cin>>ok;
    } while (ok==1);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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