子弹 の VISIONS

NEVER back down ~~

C++博客 首页 新随笔 联系 聚合 管理
  112 Posts :: 34 Stories :: 99 Comments :: 0 Trackbacks
 1 //  created by ztwaker on 2006-8-10
 2 //  Used for: email
 3
 4 #include  < iostream >
 5 #include  < string >
 6 #include  < algorithm >
 7 using   namespace  std;
 8
 9 inline  bool  isbegch( const   int  c)
10 {
11      return  ( c  ==   ' _ '   ||  (c  >=   ' A '   &&  c  <=   ' Z ' ||  
12             (c  >=   ' a '    &&  c  <=   ' z ' ) );
13 }

14
15 inline  bool  isfolch( const   int  c)
16 {
17      return  ( c  ==   ' _ '   ||  c  ==   ' . '   ||  (c  >=   ' A '   &&  c  <=   ' Z ' ||  
18             (c  >=   ' a '    &&  c  <=   ' z ' ||  (c  >=   ' 0 '   &&  c  <=   ' 9 ' ) );
19 }

20
21 bool  isValidName( const   string &  name)
22 {
23     typedef  string ::const_iterator  SIT;
24         
25     SIT it  =  name.begin();    
26      if  ( ! isbegch(static_cast < int > ( * it)))  return   false ;
27      for  (;it  !=  name.end();  ++ it)
28      {
29          if  ( ! isfolch(static_cast < int > ( * it)))  return   false ;
30     }

31     
32      return   true ;
33 }

34
35 bool  isValidDomain( const   string &  domain)
36 {
37     typedef  string ::const_iterator  SIT;
38      for  (SIT it  =  domain.begin(); it  !=  domain.end();  ++ it)
39      {
40          if  ( ! isfolch(static_cast < int > ( * it)))  return   false ;
41     }

42     
43      return   true ;
44 }

45
46
47 void  getMail( const   string &  eml,  string &  name,  string &  domain)
48 {
49      if  ( string ::npos  !=  eml.find( " @ " ))
50      {
51         name    =  isValidName(eml.substr( 0 , eml.find( " @ " )))  ?  
52             eml.substr( 0 , eml.find( " @ " )) :  "" ;
53         
54         domain  =  isValidDomain(eml.substr(eml.find( " @ " ) + 1 , eml.size() - eml.find( " @ " )))  ?
55             eml.substr(eml.find( " @ " ) + 1 , eml.size() - eml.find( " @ " )) :  "" ;
56     }

57 }

58
59
60 void  test()
61 {
62      const   string  eml  =   " waker615@163.com " ;
63      string  name, domain;
64     getMail(eml, name, domain);
65     cout  <<  name.c_str()  <<   " @ "   <<  domain.c_str()  <<  endl;    
66 }

67
68 int  main()
69 {
70     test();
71      return   0 ;
72 }
posted on 2006-08-10 18:09 子弹のVISIONS 阅读(270) 评论(1)  编辑 收藏 引用

Feedback

# re: [OPPD] Email 判断[未登录] 2007-05-10 18:48 galaxy
学习, 看出你对stl 理解不错
  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理