第2个回答 2010-05-21
#include <string>
#include <functional>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class QUESSION2:public std::unary_function<char,void>
{
public:
QUESSION2(int& A, int& B, int& C)
:A(A),B(B),C(C)
{
this->A = A;
this->B = B;
this->C = C;
}
void operator()(char c)
{
if (c >= 'A' && c <= 'Z')
{
A++;
}
else if (c >= 'a' && c <= 'z')
{
B++;
}
else if (c >= '0' && c <= '9')
{
C++;
}
}
private:
int& A;
int& B;
int& C;
};
char* pStr[]=
{
"ZERO-"
"ONE-",
"TWO-",
"THREE-",
"FOUR-",
"FIVE-",
"SIX-",
"SEVEN-",
"EIGHT-",
"NIGHT-",
"TEN-",
};
class QUESSION3:public std::unary_function<char,void>
{
public:
void operator()(char c)
{
switch (c)
{
case '0':
cout<<pStr[0];
break;
case '1':
cout<<pStr[1];
break;
case '2':
cout<<pStr[2];
break;
case '3':
cout<<pStr[3];
break;
case '4':
cout<<pStr[4];
break;
case '5':
cout<<pStr[5];
break;
case '6':
cout<<pStr[6];
break;
case '7':
cout<<pStr[7];
break;
case '8':
cout<<pStr[8];
break;
case '9':
cout<<pStr[9];
break;
default:
cout<<c;
break;
}
}
};
int count_str(string total_str, string mask_str)
{
int nNum = 0;
int nPos = 0;
do
{
nPos = total_str.find(mask_str, nPos);
if(nPos != -1)
{
nNum++;
nPos += mask_str.size();
}
} while (nPos != -1);
return nNum;
}
int _tmain(int argc, _TCHAR* argv[])
{
string total_str,mask_str;
total_str="123asdfSDFASDFasdf4561237aDFSDFASDFsdfasdf12389123";
mask_str="123";
//(1)请编写在字符串S1查找字符串S2出现次数的函数;
cout<<"QUESSION1:"<<count_str(total_str, mask_str)<<endl;
//(2)输入一串字符,统计各字符出现的次数(分为大写字母、小写字母、数字);
int A = 0, B = 0, C = 0;
for_each(total_str.begin(), total_str.end(), QUESSION2(A,B,C));
cout<<"QUESSION2"<<"大写字母"<<A<<" 小写字母"<<B<<" 数字"<<C<<endl;
//将数字编号“翻译”成英文编号。例如:将编号35706“翻译”成英文编号为:three-five-seven-zero-six
string test_str="0983343434";
cout<<"QUESSION3:"<<endl;
for_each(test_str.begin(), test_str.end(), QUESSION3());
cout<<endl;
return 0;
}
今天就管点闲事吧....
我是STL流....不知楼主是何派本回答被提问者采纳