gpt4 book ai didi

c++ - 将 ' , ' 和 ' . ' 读取为 .txt 文件中的空格

转载 作者:行者123 更新时间:2023-11-28 00:12:21 25 4
gpt4 key购买 nike

<分区>

我需要帮助读取 .txt 文件中的逗号和句点作为空格。该程序运行流畅并输出正确数量的单词和数字,但我需要带有句点和逗号的数字作为单独的数字读取。

#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int main(int argc, char* argv[]) {
int countWords = 0, countNumbers = 0;

ifstream file;
file.open ("input1.txt");
string word;

while (file >> word) // read the text file word-by-word
{
if (isdigit(word.at(0))) // if the first letter a digit it counts as number
{
countNumbers++;
}
else
{
countWords++;
}
cout << word << " ";
}

cout << endl;
cout << "Words = " << countWords << " Numbers = " << countNumbers << endl;

system("pause");
return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com