判斷多個IP是否合法

2021-06-20 18:54:53 字數 1187 閱讀 9770

bool getipaddress(hwnd hwnd)

else

}char *ptemp = null;

int icount = 0; //存放ip個數

//獲得ip個數

ptemp = strchr(str, ',');

while (ptemp != null)

char *pp; //存放每次找到','符號後面的位址

char *pstr[max_path] = ; //存放ip指標

int i = 0;

//分別取出ip,儲存在pstr

char *p = strchr(str, ',');

*p = '\0';

pstr[i++] = str;

p++;

while (p != null)

else

}int mask = 0; //用來判斷是否有合法的ip

zeromemory(g_szipaddress, sizeof(g_szipaddress));

//呼叫isipaddress,如果ip合法 就存放在g_szipaddress

for (int y = 0; y < icount; y++)

mask++;

} }//至少要有乙個合法的ip,否則放回false

if (mask > 0)

else if (mask == 0)

return true;

}

bool isipaddress(hwnd hwnd, char *str)

if (icount != 3)

const char *pchar;

bool rv = true;

int tmp1, tmp2, t***, tmp4, i;

i = sscanf(str, "%d.%d.%d.%d", &tmp1, &tmp2, &t***, &tmp4);

if( i != 4 )

if ((tmp1 > 255) || (tmp2 > 255) || (t*** > 255) || (tmp4 > 255))

for (pchar = str; *pchar != 0; pchar++) }

return true;

}

判斷IP位址是否合法

1 字串切割判斷 def legalip ip ip ip.split m len ip if m 4or not ip 0 isdigit orint ip 0 0or int ip 0 255 print illegal else for i in range 1 m if ip i isdig...

c 判斷IP位址是否合法

以字串形式輸入n行ip位址,格式為a.b.c.d,每個數字均在 0,255 之間,判斷ip位址是否合法,是輸出yes,否輸出no。輸入樣例 21.2.3.4 172.168.0.300 輸出樣例 yesno 注意 string轉int需要標頭檔案 include include using name...

華為oj 判斷IP位址是否合法

include includeusing namespace std int valid digit char ip str return 1 int is valid ip char ip str else return 0 valid ip string must contain 3 dots ...