检查Email地址的合法输入[几乎可以用于实现开发当中]

本文提供了一个简单的C++程序,用于验证输入的电子邮件是否符合标准格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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个字符!
请按任意键继续. . .
--------------------------*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值