统计短文中各字符的频度

//主要算法,二叉排序树的查找
//以出现的各字符构成一棵二叉排序树,针对每一个字符在二叉树中查找,如果找到了就增加计数,否则就插入
#include<iostream>
#include
<string>
#include
<fstream>
using namespace std;
typedef
char KeyType;
struct BstNode
{
KeyType data;
int num;
BstNode
*lch,*rch;
};
class BstTree
{
private:
BstNode
*root;
BstNode
* insert(BstNode *t,BstNode *s);
void inorder(ofstream& outfile,BstNode *p);
void deletenode(BstNode *p);

public:
BstTree(){root
=NULL;}
~BstTree(){delete(root);root=NULL;}
void read_creat(ifstream& infile);
void print_inorder(ofstream& outfile)
{inorder(outfile,root);}
};
BstNode
* BstTree::insert(BstNode *t,BstNode *s)
{
if(t==NULL){t=s;t->num=1;}
else if(t->data==s->data){delete(s);s=NULL;t->num++;}
else
{
if(t->data>s->data)t->lch=insert(t->lch,s);
else t->rch=insert(t->rch,s);
}
return t;
}
void BstTree::read_creat(ifstream& infile)
{
char ch;
infile.
get(ch);
while(infile)
{
BstNode
*s;s=new BstNode;s->data=ch;s->num=1;s->lch=s->rch=NULL;
root
=insert(root,s);
infile.
get(ch);
}
}
void BstTree::deletenode(BstNode *p)
{
if(p!=NULL)
{
deletenode(p
->lch);deletenode(p->rch);
delete p;
}
}
void BstTree::inorder(ofstream& outfile,BstNode *p)
{
if(p!=NULL)
{
inorder(outfile,p
->lch);
outfile
<<p->data<<" "<<p->num<<endl;
inorder(outfile,p
->rch);
}
}
void main()
{
ifstream infile;ofstream outfile;infile.open(
"E:\\c++.txt");
if(!infile){cout<<"open file failure!\n";return;}
outfile.open(
"E:\\b++.txt");

BstTree tree;
tree.read_creat(infile);
tree.print_inorder(outfile);
infile.close();outfile.close();
}

  

读取文本中的字符并计算出各字母出现的次数
                           //读取文本中的字符并计算出各字母出现的次数

#include
<iostream>
#include
<fstream>
#include
<cctype>

using namespace std;

void initialize(int& lc,int list[]);
void copyText(ifstream& intext,ofstream& outtext,char& ch,int list[]);
void characterCount(char ch,int list[]);
void writeTotal(ofstream& outtext,int lc,int list[]);
int main()
{
int lineCount;
int listLetter[26];
char ch;
ifstream infile;
ofstream outfile;
infile.open(
"C:\\c++.txt");
if(!infile)
{
cout
<<"Cannot open input file."<<endl;
return 1;
}
outfile.open(
"C:\\b++.txt");
initialize(lineCount,listLetter);
infile.
get(ch);
while(infile)
{
copyText(infile,outfile,ch,listLetter);
lineCount
++;
infile.
get(ch);
}
writeTotal(outfile,lineCount,listLetter);
infile.close();
outfile.close();

return 0;
}
void initialize(int& lc,int list[])
{
int j;
lc
=0;
for(j=0;j<26;j++)
list[j]
=0;
}

void copyText(ifstream& intext,ofstream& outtext,char& ch,int list[])
{
while(ch!='\n')
{
outtext
<<ch; //输出原文本的信息
characterCount(ch,list);
intext.
get(ch);
}
outtext
<<ch; //原文本断行时新文本也断行
}
void characterCount(char ch,int list[])
{
int index;
ch
=toupper(ch);
index
=static_cast<int>(ch)-65;
if(0<=index && index<26)
list[index]
++;
}
void writeTotal(ofstream& outtext,int lc,int list[])
{
int index;
outtext
<<endl<<endl;
outtext
<<"The number of lines="<<lc<<endl;
for(index=0;index<26;index++)
outtext
<<static_cast<char>(index+65)<<" count = "<<list[index]<<endl;
}

/*
sample
C:\\c++.txt:

fwwwewefqtgqtggrqg
rgqeerghhwhwewhtfwqfqgqrewgqe
gqrgergqegrq
cwoifuqiwofjeiopjwe
svjxvzvmxnbdmeioejgeorigjqioadf;klsajfpoiqfew;fjwf
54651844fwgqrwegergqwerea


C:\\b++.txt:

fwwwewefqtgqtggrqg
rgqeerghhwhwewhtfwqfqgqrewgqe
gqrgergqegrq
cwoifuqiwofjeiopjwe
svjxvzvmxnbdmeioejgeorigjqioadf;klsajfpoiqfew;fjwf
54651844fwgqrwegergqwerea


The number of lines=6
A count = 3
B count = 1
C count = 1
D count = 2
E count = 19
F count = 12
G count = 17
H count = 4
I count = 7
J count = 7
K count = 1
L count = 1
M count = 2
N count = 1
O count = 7
P count = 2
Q count = 16
R count = 11
S count = 2
T count = 3
U count = 1
V count = 3
W count = 17
X count = 2
Y count = 0
Z count = 1

*/

  

posted on 2011-08-23 17:08  sysu_mjc  阅读(263)  评论(0编辑  收藏  举报

导航