C + + statistics How often each word appears in a text

Source: Internet
Author: User
Tags getbase

#include <iostream>

using namespace Std;

/* Run this program using the console Pauser or add your own getch, System ("pause") or input loop */

Class Sqstring
{
Private
char * BASE;
int length;
Public
Sqstring ()
{
}
Sqstring (char * s)
{
length=0;
Base=s;
int i=0;
while (s[i]!= ')
{
++i;
++length;
}
}
char * getbase ()
{
return base;
}
int GetLength ()
{
return length;
}
void Strconcat (sqstring ss)
{
char * newbase=new char[ss.getlength () +length+1];
for (int i=0;i<length;i++)
{
Newbase[i]=base[i];
}
for (int j=0;j<=ss.getlength (); j + +)
{
NEWBASE[J+LENGTH]=SS.BASE[J];
}
Base=newbase;
Length=ss.getlength () +length;
}
int getnumofsonsqstring (sqstring son)
{
int num=0;
for (int i=0;i<=length-son.getlength (); i++)
{
int j=0;
For (J=0;j<son.getlength (); j + +)
{
if (Son.base[j]!=base[i+j])
{
Break
}
}
if (J==son.getlength ())
{
num++;
}
}
return Num;
}
BOOL IsEqual (sqstring s)
{
int i=0;
while (s.base[i]!= ' &&s.base[i]==base[i]&&base[i]!= ')
{
++i;
}
if (i==length&&i==s.length)
{
return true;
}
Else
{
return false;
}
}
};


Class Sqstringnode
{
Public
Sqstringnode * PNEXT;
Sqstring data;
};


Class Sqstringlist
{
Public
Sqstringnode * PHEAD;
Sqstringnode * PTAIL;
int length;
Public
Sqstringlist ()
{
Phead=new Sqstringnode;
Ptail=phead;
ptail->pnext=null;
length=0;
}
void Insertintolist (sqstring s)
{
Sqstringnode * Snode=new Sqstringnode;
snode->data=s;
snode->pnext=null;
ptail->pnext=snode;
Ptail=snode;
length++;
}
void Show ()
{
Sqstringnode * temp=phead->pnext;
while (temp)
{
Cout<<temp->data.getbase () <<endl;
temp=temp->pnext;
}
}
int Getlistlength ()
{
return length;
}
BOOL Ishave (sqstring s)
{
Sqstringnode * temp=phead->pnext;
int i=0;
while (temp)
{
if (temp->data.isequal (s))
{
Break
}
temp=temp->pnext;
i++;
}
if (i>=length)
{
return false;
}
Else
{
return true;
}
}
};

Class Text
{
Private
Sqstring content;
char * inside;
int length;
Public
Text (sqstring & S)
{
Content=s;
Length=s.getlength ();
Inside=content.getbase ();
}
char * getinside ()
{
return inside;
}
Sqstring getcontent ()
{
return content;
}
int Gettextlength ()
{
return length;
}
Sqstringlist Getallwordsfromtext ()
{
char * temp=new char[20];
Sqstringlist list;
int j=0;
for (int i=0;i<length;i++)
{
if (inside[i]== ' | | | inside[i]== ', ' | | inside[i]== '. ')
{
temp[j]= ' + ';
if (temp[0]!= ')
{
sqstring sqstring (temp);
if (!list.ishave (temp))
{
List.insertintolist (sqstring);
}
}
Temp=new CHAR[20];
j=0;
while (inside[i]== ' | | inside[i]== ', ' | | inside[i]== '. ')
{
++i;
}
}
Temp[j]=inside[i];
j + +;
}
The judgment of the last word
temp[j]= ' + ';
sqstring sqstring (temp);
if (!list.ishave (temp))
{
List.insertintolist (sqstring);
}
return list;
}
};


int main (int argc, char *argv[]) {
Sqstring sqstring ("Flypie is Good,flypie are Better,flypie is Best,i love Flypie,flypie are Good,flypie is Better,flypie are Best,i Love Flypie,flypie are Good,flypie is Better,flypie are best,i love Flypie,flypie are Good,flypie is Better,flypie are Best,i Love Flypie,flypie are Good,flypie is Better,flypie are best,i love Flypie,flypie are Good,flypie is Better,flypie are Best,i Love Flypie,flypie are Good,flypie is Better,flypie are best,i love Flypie,flypie are Good,flypie is Better,flypie are Best,i love Flypie ");
Text text (sqstring);
cout<< "This text is:" <<endl;
Cout<<text.getinside () <<endl;
cout<< "----------------------------------" <<endl;
cout<< "statistic result:" <<endl;
Sqstringlist List=text.getallwordsfromtext ();
List.show ();
Sqstringnode * temp=list.phead->pnext;
while (temp)
{
Cout<<temp->data.getbase () << ":" <<sqstring.getnumofsonsqstring (Temp->data) <<endl;
temp=temp->pnext;
}
return 0;
}

C + + statistics How often each word appears in a text

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.