텍스트 파일을 읽는 C++ 프로그램을 작성했습니다. 그러나 프로그램에서 단어가 나타나는 횟수를 세고 싶습니다. 예를 들어 출력은 다음과 같이 표시되어야합니다.단어 문자열의 빈도 (즉, 파일 I/O)?
Word Frequency Analysis
Word Frequency
I 1
don't 1
know 1
the 2
key 1
to 3
success 1
but 1
key 1
failure 1
is 1
trying 1
please 1
everybody 1
각 단어가 한 번만 나타나는 방식에 유의하십시오. 이 효과를 얻으려면 무엇을해야합니까 ??
I don't know the key to success, but the key to failure is trying to please everybody.
여기에 지금까지 내 코드입니다 : 여기
텍스트 파일 (즉 BillCosby.txt 이름)입니다. 나는 극단적 인 정신적 장애가 있으며 프로그램에 단어가 나오는 횟수를 읽는 방법을 찾아 낼 수 없습니다.#include <iostream>
#include <fstream>
#include <iomanip>
const int BUFFER_LENGTH = 256;
const int NUMBER_OF_STRINGS = 100;
int numberOfElements = 0;
char buffer[NUMBER_OF_STRINGS][BUFFER_LENGTH];
char * words = buffer[0];
int frequency[NUMBER_OF_STRINGS];
int StringLength(char * buffer);
int StringCompare(char * firstString, char * secondString);
int main(){
int isFound = 1;
int count = 1;
std::ifstream input("BillCosby.txt");
if(input.is_open())
{
//Priming read
input >> buffer[numberOfElements];
frequency[numberOfElements] = 1;
while(!input.eof())
{
numberOfElements++;
input >> buffer[numberOfElements];
for(int i = 0; i < numberOfElements; i++){
isFound = StringCompare(buffer[numberOfElements], buffer[i]);
if(isFound == 0)
++count;
}
frequency[numberOfElements] = count;
//frequency[numberOfElements] = 1;
count = 1;
isFound = 1;
}
numberOfElements++;
}
else
std::cout << "File is not open. " << std::endl;
std::cout << "\n\nWord Frequency Analysis " << std::endl;
std::cout << "\n" << std::endl;
std::cout << "Word " << std::setw(25) << "Frequency\n" << std::endl;
for(int i = 0; i < numberOfElements; i++){
int length = StringLength(buffer[i]);
std::cout << buffer[i] << std::setw(25 - length) << frequency[i] <<
std::endl;
}
return 0;
}
int StringLength(char * buffer){
char * characterPointer = buffer;
while(*characterPointer != '\0'){
characterPointer++;
}
return characterPointer - buffer;
}
int StringCompare(char * firstString, char * secondString)
{
while ((*firstString == *secondString || (*firstString == *secondString - 32) ||
(*firstString - 32 == *secondString)) && (*firstString != '\0'))
{
firstString++;
secondString++;
}
if (*firstString > *secondString)
return 1;
else if (*firstString < *secondString)
return -1;
return 0;
}
이것에 대한 조사를 해본 적이 있습니까? 매우 일반적인 질문입니다. 그래서 사람들을 위해 숙제하는 것을 좋아하지 않습니다. –
이 질문을'C'로 표시했을 수도 있습니다. 실제 C++ 기능을 사용하고 있지 않습니다. http://codereview.stackexchange.com/questions (부러울 때 질문하지 마십시오. 처음에 수정하려면 여기로 다시 보내주십시오). –