簡體   English   中英

如何計算文本文件中的哪一行單詞是 C++

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

我有一個練習,我需要在以用戶輸入符號開頭的文本文件中查找單詞。 我還需要確定該單詞在哪一行以及 output 在文本不同的文本文件中。 我設法將代碼寫入以符號開頭的 output 單詞並在文本中計數單詞的 position,但我無法弄清楚如何計算該單詞在哪一行。 我還需要找到那些有符號的單詞? ! 等不僅' '例如,如果我想找到以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;

}

示例輸入:用戶想要查找以a單詞。

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

a 1
a 3
and 4
advanced 4

您正在逐字閱讀文件,計算每個單詞。 您需要逐行讀取文件,計算每一行,然后您可以從每一行讀取單詞。

嘗試這個:

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

我做的! 這行得通!

#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