/** 。。。。
*
file: CallBack.cpp
*
athuor:wxd
*
email: wxd273612158@
*/
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class StrEdit
{
public:
bool clear(char *p)
{
cout <<p
<< ":"<< "clear" << endl;
return false;
}
bool save(char *p)
{
cout <<p << ":"<< "save" << endl;
return false;
}
bool reset(char *p)
{
cout <<p << ":"<< "reset" << endl;
return false;
}
bool end(char *p)
{
cout <<p
<< ":"<< "end" << endl;
return false;
}
private:
//pass
vector<string> arr;
};
class RegHandle
{
public:
typedef bool (StrEdit::*XregFun)( char* p);
RegHandle() {}
void Rh()//注册函数
{
this->p[0] = &StrEdit::clear;
this->p[1] = &StrEdit::save;
this->p[2] = &StrEdit::reset;
this->p[3] = &StrEdit::end;
}
void Call(int ip, char *pp)
{
(s.*p[ip])(pp);//回调函数
}
int IsChar(unsigned char ch)
{
char *p = "$%*#";
for(int i =0; i < strlen(p); i ++)
if(ch == p[i])
return i;
return -1;
}
void run()
{
char ch[24] = {0};
char buf[24] = {0};
string s;
int map = 0;
int cur = 0;
cin >> ch;
for(int i = 0; i < strlen(ch); i ++)
{
map = IsChar(ch[i]);
if (map != -1)
{
s.assign(ch + cur, i - cur);
cur = i + 1;
Call(map, (char*)s.c_str());
s.erase();
}
}
}
private:
StrEdit s;
XregFun p[4];
};
// reg.RegFun(StrEdit::clear, 1);
// 利用的你的小程序原理与实现结果,俺写了个回调函数处理小程序!
// 输入:wang%dong%dong%waa$
// 结果:
// wang:save
// dong:save
// dong:save
// waa:clear
int main()
{
static RegHandle reg;
reg.Rh();
reg.run();
return 0;
}