简体   繁体   中英

C++ word counter

How can I change in this c++ code "char word[50]" to "string word[50]" because I want to count and calculate each words in text not only my input word. All calculation about TF/IDF calculation.

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

using namespace std;

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

Here char word[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;

I need to change here word , When I do char word[50] to string word[50] here giving some error

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)

{

Here main part I dont want to enter the word , I want to list each word count.

    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();

}

Use std::string . Plain old C-style strings are worth avoiding, where possible, because they are easy to misuse. C++ strings are much safer and generally simpler to work with.

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";

If you have multiple words:

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";

If you wish to load your words from a file, you can do so entirely using the (relatively) safe C++ io classes and functions.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM