微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何计算文本文件中的哪一行是 C++

我有一个练习,我需要在文本文件中查找以用户输入符号开头的单词。我还需要确定这个词在哪一行,并在不同的文本文件输出。 我设法编写代码输出以符号开头的单词并计算单词在文本中的位置,但我无法弄清楚如何计算该单词在哪一行。我还需要找到那些带有 ? ! 等符号的单词,而不仅仅是 ' ' 例如,如果我想找到以 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. 

输出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,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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?