#include <iostream>
using namespace std;
const int INT_ZERO = 0;
const int INDEX_ZERO = 0;
const int INT_ONE = 1;
const int INDEX_ONE = 1;
const int HAS_NO_ELEMENT = 0;
const int MAX_LENGTH = 30;
const char NULL_TERMINATED = '\0';
const char CHAR_AT = '@';
const char CHAR_POINT = '.';
const char CHAR_LINE = '-';
const char CHAR_UNDER_LINE = '_';
//自定义一些Email的合法字符
bool isAllowChar( char ch )
{
if (ch == CHAR_AT
|| ch == CHAR_POINT
|| ch == CHAR_LINE
|| ch == CHAR_UNDER_LINE)
{
return true;
}
else
{
return false;
}
}
//dizly@qq.com
bool isEmail( char *str )
{
if (NULL == str)
{
cout << "参数不能指向空的地址!" << endl;
throw;
}
if (MAX_LENGTH < strlen( str ))
{
cout << "Email不能超过" << MAX_LENGTH << "个字符!" << endl;
system( "PAUSE" );
throw;
}
//'@'或'.'不能出现在第一个字符的位置上
if (CHAR_AT == *str || CHAR_POINT == *str)
{
return false;
}
int countCharAt = INT_ZERO;
int countCharPoint = INT_ZERO;
while (NULL_TERMINATED != *str)
{
if (!isalnum( *str ) && !isAllowChar( *str )) //合法的字符应该是:数字、字母以及本程序另外允许的字符
{
return false;
}
if (CHAR_AT == *str)
{
++countCharAt;
}
if (CHAR_POINT == *str)
{
++countCharPoint;
}
//'@'和'.'不能相邻
if ((CHAR_AT == *str && CHAR_POINT == *(str+INDEX_ONE))
|| (CHAR_POINT == *str && CHAR_AT == *(str+INDEX_ONE)))
{
return false;
}
++str;
}
//'@'或'.'不能出现在最后一个字符的位置上
if (CHAR_AT == *(str - INDEX_ONE) || CHAR_POINT == *(str - INDEX_ONE))
{
return false;
}
if (INT_ONE != countCharAt || INT_ONE != countCharPoint) //一个Email有且仅有一个'@'和'.'
{
return false;
}
return true;//程序能够执行到这里,表示所设的关卡都通过了
}
void testFunc( char *str )
{
if (NULL == str)
{
throw;
}
if (isEmail( str ))
{
cout << str << " [合法]" << endl;
}
else
{
cout << str << " [不合法]" << endl;
}
}
int main( void )
{
testFunc( "c_p-p@ok2002.com" );
testFunc( "123456789@ok2002.com" );
testFunc( "abcdefghi@ok2002.com" );
testFunc( "jklmnopqr@ok2002.com" );
testFunc( "stuvwxyz@ok2002.com" );
testFunc( "ABCDEFGHI@ok2002.com" );
testFunc( "JKLMNOPQR@ok2002.com" );
testFunc( "STUVWXYZ@ok2002.com" );
testFunc( "@cppok2002.com" ); //'@'作首字符
testFunc( ".cpp@ok2002com" ); //'.'作首字符
testFunc( "cpp@ok@2002.com" ); //两个'@'
testFunc( "cpp@ok.2002.com" ); //两个'.'
testFunc( "cppok2002.com@" ); //'@'作尾字符
testFunc( "cpp@ok2002com." ); //'.'作首字符
testFunc( "cpp@.ok2002com" ); //'@'和'.'相邻
testFunc( "cpp.@ok2002com" ); //'@'和'.'相邻
testFunc( "cpp@ok*2002.com" ); //含有非法字符
testFunc( "cpp@ok'2002.com" ); //含有非法字符
testFunc( "cpp@ok+2002.com" ); //含有非法字符
testFunc( "cpp@ok#2002.com" ); //含有非法字符
testFunc( "cpp@ok/2002.com" ); //含有非法字符
testFunc( "cpp@ok|2002.com" ); //含有非法字符
testFunc( "cpp@ok\2002.com" ); //含有非法字符
testFunc( "cpp@ok?2002.com" ); //含有非法字符
testFunc( "cpp@ok$2002.com" ); //含有非法字符
testFunc( "cpp@ok^2002.com" ); //含有非法字符
testFunc( "cpp@ok%2002.com" ); //含有非法字符
testFunc( "cpp@ok&2002.com" ); //含有非法字符
testFunc( "cpp@ok=2002.com" ); //含有非法字符
testFunc( "cpp@ok(2002.com" ); //含有非法字符
testFunc( "cpp@ok)2002.com" ); //含有非法字符
testFunc( "cpp@ok[2002.com" ); //含有非法字符
testFunc( "cpp@ok]2002.com" ); //含有非法字符
testFunc( "cpp@ok,2002.com" ); //含有非法字符
testFunc( "cpp@ok;2002.com" ); //含有非法字符
testFunc( "cpp@ok:2002.com" ); //含有非法字符
testFunc( "cpp@ok<2002.com" ); //含有非法字符
testFunc( "cpp@ok>2002.com" ); //含有非法字符
testFunc( "cpp@ok!2002.com" ); //含有非法字符
testFunc( "cppok2002.com" ); //缺少'@'
testFunc( "cpp@ok2002com" ); //缺少'.'
testFunc( "cpp@ok2002.comcpp@ok2002.comcpp@ok2002.comcpp@ok2002.com" ); //太长了
system( "PAUSE" );
return EXIT_SUCCESS;
}
/*-----------
c_p-p@ok2002.com [合法]
123456789@ok2002.com [合法]
abcdefghi@ok2002.com [合法]
jklmnopqr@ok2002.com [合法]
stuvwxyz@ok2002.com [合法]
ABCDEFGHI@ok2002.com [合法]
JKLMNOPQR@ok2002.com [合法]
STUVWXYZ@ok2002.com [合法]
@cppok2002.com [不合法]
.cpp@ok2002com [不合法]
cpp@ok@2002.com [不合法]
cpp@ok.2002.com [不合法]
cppok2002.com@ [不合法]
cpp@ok2002com. [不合法]
cpp@.ok2002com [不合法]
cpp.@ok2002com [不合法]
cpp@ok*2002.com [不合法]
cpp@ok'2002.com [不合法]
cpp@ok+2002.com [不合法]
cpp@ok#2002.com [不合法]
cpp@ok/2002.com [不合法]
cpp@ok|2002.com [不合法]
cpp@ok2.com [不合法]
cpp@ok?2002.com [不合法]
cpp@ok$2002.com [不合法]
cpp@ok^2002.com [不合法]
cpp@ok%2002.com [不合法]
cpp@ok&2002.com [不合法]
cpp@ok=2002.com [不合法]
cpp@ok(2002.com [不合法]
cpp@ok)2002.com [不合法]
cpp@ok[2002.com [不合法]
cpp@ok]2002.com [不合法]
cpp@ok,2002.com [不合法]
cpp@ok;2002.com [不合法]
cpp@ok:2002.com [不合法]
cpp@ok<2002.com [不合法]
cpp@ok>2002.com [不合法]
cpp@ok!2002.com [不合法]
cppok2002.com [不合法]
cpp@ok2002com [不合法]
Email不能超过30个字符!
请按任意键继续. . .
--------------------------*/