gpt4 book ai didi

计算 .txt 文件中重复单词的 C++ 程序

转载 作者:行者123 更新时间:2023-11-30 03:33:33 25 4
gpt4 key购买 nike

<分区>

我正在尝试构建一个程序来计算 .txt 文件中的重复单词并输出重复单词及其重复次数。我有一种方法可以计算有多少个单词,但不计算重复的单词。这是代码:

#include <iostream>
#include <string>
#include <math.h>
#include <iomanip>
#include <fstream>
#include <vector>
#include "ProcessStatistics.h"

using namespace std;

ProcessStatistics::ProcessStatistics()
{
//constructor
}

//Finds out how many words are composed by an specific number of characters.

void ProcessStatistics::Length(std::vector<std::string> ArrayOfWords, int numberOfWords)
{
cout << "=== COMPUTING WORD S LENGTH ==== " << endl;
int vectorLength[30] = {0};

for(int i = 0; i < numberOfWords; i++)
{
for(int j = 0; j<20; j++)
{
if (ArrayOfWords [i].length()-1 == j)
vectorLength[j] = vectorLength[j]+1;
}
}

ofstream varlocal;
remove("WORDS_LENGTH.txt");
varlocal.open("WORDS_LENGTH.txt");
if(varlocal.is_open())
{
varlocal << "Total: " << numberOfWords << endl;
for(int i=0; i < 30; i++)
{
if(vectorLength[i] != 0)
{
varlocal << vectorLength[i] << " W " << i+1 << " CHAR " << " % " << setprecision(3) << vectorLength[i]*100/numberOfWords << endl;
}
}
}
varlocal.close();

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