贵州大学2017机试——IP地址(C++)

IP地址

Time Limit: 1000 ms
Memory Limit: 256 mb

输入一串字符,判断该字符串是否为点分十进制的IP地址,若是则转换为16进制输出,否则输出“Error”

例如

输入:192.41.6.20

输出:0xC0290614

输入:257.32.23.1

输出:Error

 输入输出格式

输入描述:

按题意输入。
注意:输入可能是任意的一个字符串,比如“abc.bas.fefe.4r4”或者“23.23.11.23.123”
这都是不合法的IP地址

输出描述:

按题意输出。

 输入样例#:

192.41.6.20

 输出样例#:

0xC0290614
#include
#include
using namespace std;
//转化16进制
void hex(int x, char *ans){
	int i = 0;
	do{
		int temp = x % 16;
		if(temp < 10) ans[i++] = temp + '0';
		else{
			char temp_char = 'A' + (temp-10);
			ans[i++] = temp_char;
		}
		x = x / 16;
	}while(x > 0);
}
int main(){
	char str[105];
	while(cin>>str){
		int len = strlen(str);
		int count = 0,num[5];
		memset(num,0,sizeof(num));
		bool ch_flag = true,flag = true;
		for(int i = 0; i < len; i++){
			if(str[i] == '.'){
				count++;//count记录'.'的个数
				ch_flag = false;
			}
            //长度不符合标准或包含非法字符
			if((count>3)||((str[i]!='.')&&((str[i]<'0')||(str[i]>'9')))){
				cout<<"Error"< 255){
					cout<<"Error"<= 0; i--){
					cout<

 

你可能感兴趣的:(算法/数据结构)