| 代碼 | 含義 |
|---|---|
| 0 | 合法 |
| 1 | 長度不合法 |
| 2 | 第 1-17 位含非數字 |
| 3 | 第 18 位非數字且非 'x' |
| 4 | 年份非法 |
| 5 | 月份非法 |
| 6 | 日期非法 |
優先級遞減:長度 → 字元 → 日期。
| 代碼 | 含義 |
|---|---|
| 0 | 合法 |
| 1 | 長度不合法 |
| 2 | 含非數字 |
| 3 | 非 86 開頭 |
回傳:'1' 合法,'0' 不合法。
int isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int verifyID(char* inID) {
int len = strlen(inID);
if (len != 18) return 1;
for (int i = 0; i < 17; i++)
if (inID[i] < '0' || inID[i] > '9') return 2;
if (!((inID[17] >= '0' && inID[17] <= '9') || inID[17] == 'x')) return 3;
string s(inID);
int year = stoi(s.substr(6, 4));
if (year < 1900 || year > 2100) return 4;
int month = stoi(s.substr(10, 2));
if (month < 1 || month > 12) return 5;
int day = stoi(s.substr(12, 2));
int maxDay;
if (month == 2) maxDay = isLeapYear(year) ? 29 : 28;
else if (month == 4 || month == 6 || month == 9 || month == 11) maxDay = 30;
else maxDay = 31;
if (day < 1 || day > maxDay) return 6;
return 0;
}
int verifyMsisdn(char* inMsisdn) {
int len = strlen(inMsisdn);
if (len != 13) return 1;
for (int i = 0; i < len; i++)
if (inMsisdn[i] < '0' || inMsisdn[i] > '9') return 2;
if (inMsisdn[0] != '8' || inMsisdn[1] != '6') return 3;
return 0;
}
void vConvertMsg(char *pInputStr, long lInputLen, char *pOutputStr) {
int atCount = 0, spaceCount = 0, errorCount = 0;
for (int i = 0; i < lInputLen; i++) {
char c = pInputStr[i];
if (c == '@') atCount++;
else if (c == ' ') spaceCount++;
else if (!((c >= '1' && c <= '9') || (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') || c == '_' || c == '.' || c == '@'))
errorCount++;
}
if (atCount != 1 || spaceCount != 0 || errorCount != 0) {
*pOutputStr = '0'; return;
}
// 檢查 .com 結尾
if (lInputLen >= 4 && pInputStr[lInputLen-4] == '.' &&
pInputStr[lInputLen-3] == 'c' && pInputStr[lInputLen-2] == 'o' &&
pInputStr[lInputLen-1] == 'm')
*pOutputStr = '1';
else
*pOutputStr = '0';
}
869123456789 (12位) → 返回 188139123456789 → 返回 38613912345678 (13位) → 返回 0