簡體   English   中英

C ++字計數器

[英]C++ word counter

如何在此c ++代碼中將“ char word [50]”更改為“ string word [50]”,因為我不僅要計算和計算文本中的每個單詞,而不僅僅是我的輸入單詞。 有關TF / IDF計算的所有計算。

#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iomanip>  
#include <cmath>

using namespace std;

/**************************************************/

這里是char字[50];

 int ch,match[3],cnt,i,cntt[3],matchh[3],occurence,flg[3];
 float tf,idf;
 char word[50],line[50];
 int nd=3,iterm;
 int doc[20];

 /**************************************************/

 void case1() 
 {

 FILE *fill[3];
 fill[1]=fopen("doc1.txt","r");
 fill[2]=fopen("doc2.txt","r"); 
 fill[3]=fopen("doc3.txt","r");
 match[i]=0;
 cnt=0;

我需要在這里更改word,當我將char word [50]更改為字符串word [50]時,出現了一些錯誤

for(i=1;i<4;i++)
{
while(!feof(fill[i]))
{
fscanf(fill[i],"%s",line);
//  if(strcmp(line,word)==0)

   if(strstr(line,word)!=0)

   match[i]++;
   cntt[i]++;
}

fclose(fill[i]);

}

cout<<"\n----------------Total # of Word-------------------\n";

for(i=1;i<4;i++)

{

cout<<"\n Documant "<<i<<" = "<<cntt[i];

}

cout<<"\n\n------------------Term Counts--------------------\n";


cout<<word<<"  ";
cout<<"(";
for(i=1;i<4;i++)
{

cout<<"Doc"<<i<<","<<match[i]<<" ; ";

}
cout<<")";
cout << "\n\n\n |  Words  |    D1     |    D2    |    D3    |  \n";
cout<<"----------------------------------------------------------\n";


 cout<<"     "<<word;

for(i=1;i<4;i++)

{

 tf=(float)match[i]/cntt[i];   //Term Frequency

 cout<<"      "<<tf;

}


cout<<"\n\n\n\n\n\n\n";
}

/**************************************************/

void case2() 
{
FILE *fill[3];
fill[1]=fopen("doc1.txt","r");
fill[2]=fopen("doc2.txt","r");
fill[3]=fopen("doc3.txt","r");
match[i]=0;
cnt=0;

cout<<"\n Total Number Of Documants => 3";
for(i=1;i<4;i++)
{
while(!feof(fill[i]))
{
    fscanf(fill[i],"%s",line);
    //  if(strcmp(line,word)==0)
    if(strstr(line,word)!=0)
    {
        match[i]++;
        matchh[i]++;
        flg[i]=1;
    }
    cnt++;
    cntt[i]++;
}

fclose(fill[i]);
}

cout<<"\n----------------Total # of Word-------------------\n";

for(i=1;i<4;i++)
{

 cout<<"\n Documant "<<i<<" are = "<<cntt[i];

}

cout<<"\n\n------------------Term Counts--------------------\n";

for(i=1;i<4;i++) 
{

cout<<"\n Documant "<<i<<" = "<<matchh[i];

}

for(i=1;i<4;i++)
{
if(matchh[i]>0)
{

matchh[i]=1;
flg[i]=flg[i]+matchh[i];
occurence=occurence+flg[i];

}

else
matchh[i]=0;

}


tf=(float)3/occurence;
idf=log10(tf);

cout<<"\n\n-----------------Inverse Document Frequency-------------------\n";


cout<<"\n IDF = "<<idf;

for(i=1;i<4;i++)

matchh[i]=cntt[i]=flg[i]=0;

cout<<"\n";


}
/**************************************************/



int main()

{


do

{
cout<<"\n************** Menu ****************\n";
cout<<"\n (1) Term Frequency";
cout<<"\n (2) Inverse Document Frequency";
cout<<"\n (3) Exit";
cout<<"\n************************************\n";
cout<<"\n Select from Menu => ";cin>>ch;
switch(ch)

{

在這里主要部分我不想輸入單詞,我想列出每個單詞的數量。

    case 1:


            cout<<"\n Enter The Word  => ";
            cin>>word;

            case1();
            break;

    case 2: cout<<"\n Enter The Word  => ";
            cin>>word;
            case2();
            break;

    case 3: exit(0);

}


}

while(ch!=3);

getch();

}

使用std::string 普通的老式C風格字符串在可能的情況下值得避免,因為它們易於濫用。 C ++字符串更安全,使用起來通常更簡單。

std::string text = "some long line of text";
std::string word = "line";

if (text.find(word) != std::string::npos)
  std::cout << "Found the word!\n";

如果您有多個單詞:

std::vector<std::string> words = { "one", "word", "is", "badgers" };

for (const auto& word : words)
  if (text.find(word) !=  std::string::npos)
    std::cout << "Found \"" << word << "\"!\n";

如果希望從文件中加載單詞,則可以完全使用(相對)安全的C ++ io類和函數來完成。

std::ifstream wordfile("where/your/file/is");
std::string word;

while (std::getline(wordfile, word).good())
{
    words.push_back(word);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM