#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
int indexof(string ipaddress, char spliter)
{
for(int i = 0 ; i< ipaddress.length() ;i ++)
if(ipaddress[i] == spliter)
return i;
return -1;
}
string * split(string ipaddress, char spliter,int &count)
{
string *b = new string[4];
while(ipaddress.length() >= 0 )
{
//cout<<indexof(ipaddress,spliter)<<endl;
int indexofspliter = indexof(ipaddress,spliter);
if(indexofspliter != -1)
{
b[count++]= ipaddress.substr(0,indexofspliter);
ipaddress = ipaddress.substr(indexofspliter+1,ipaddress.length());
// cout<<ipaddress<<" is ip"<<endl;
}
else
{
b[count++]= ipaddress;
break;
}
}
return b;
}
int stringtochararray(string temp)
{
int size = temp.length();
int time = 0;
int result = 0;
while(time < size )
{
result *= 10;
result += temp[time++] - '0';
}
return result;
}
int main()
{
int splitercount = 0 ;
string ip;
cout<<"输入你要转换的Ip"<<endl;
string * ipstring = NULL;
int currentipaddress = 0;
char ipbinary[32];
while(1)
{
cin>>ip;
splitercount = 0 ;
ipstring = split(ip,'.',splitercount);
currentipaddress = 0;
for(int i= 0 ; i< 32;i ++)
ipbinary[i] = '0';
int count = 31;
while(currentipaddress < splitercount)
{
int intipaddress = stringtochararray(ipstring[currentipaddress]);
int tempcount = count - 7;
count -= 8;
while(intipaddress > 0)
{
ipbinary[tempcount++] = intipaddress%2 + '0';
intipaddress/=2;
}
currentipaddress++;
}
for(int i = 31; i >= 0 ; i --)
{
if(i != 31 &&(i+9)%8 == 0 )
cout<<".";
cout<<ipbinary[i];
}
cout<<endl;
}
}
打算写一个小的应用,主要是针对CCNA中的一些繁琐的问题,如子网划分等,不想手算啊