/*统计除了>之外的行里面CHED四个字母总数*/
#include<fstream>
#include<iostream>
#include<cstring>
using namespace std; class FindLetter
{
private:
ifstream m_r;
ofstream m_w;
string m_falphabet,m_temp;
char m_jump;//要跳跃的字符
unsigned int *m_number,m_length;//计数 和 文件中的一行的个数
public:
FindLetter(const char * falphabet,char jump)
//参数意思为:第一个参数,要找的字符串;第二个参数,在此行的第一个字符是jump不读取。
{
m_jump=jump;
m_falphabet=falphabet;
m_number=new unsigned int [m_falphabet.length()];
memset(m_number,,sizeof(int)*m_falphabet.length()); }
~FindLetter()
{
delete []m_number;
m_r.close();
m_w.close();
}
void setFindLetter(const char *falphabet,const char jump)//与构造函数相同
{
//写完后,请尝试this指针 调用构造函数
m_jump=jump;
m_falphabet=falphabet;
m_number=new unsigned int[m_falphabet.length()];
memset(m_number,,sizeof(int)*m_falphabet.length()); }
void countAlphabet(const char * filename);
bool wTxt(const char * filename);
};
void FindLetter::countAlphabet(const char * filename)
{
m_r.open(filename);
while(getline(m_r,m_temp))
{
m_length=m_temp.length();//文本文件中的一行字符的长度。 while(--m_length!=-&&m_temp[]!=m_jump)
{
int temp=m_falphabet.length();
//判断是否为要找的字符
while(--temp!=-)//从n-0数
if(m_falphabet[temp]==m_temp[m_length])
{
++m_number[temp]; }
} }
for(int i=m_falphabet.length()-;i>=;--i)
cout<<m_falphabet[i]<<" : "<<m_number[i]<<endl;
}
bool FindLetter::wTxt(const char * filename)
{
m_w.open(filename); for(int i=m_falphabet.length()-;i>=;--i)
m_w<<m_falphabet[i]<<" : "<<m_number[i]<<endl;
}
此用以备忘!!!