当前位置:网站首页>HJ18 识别有效的IP地址和掩码并进行分类统计
HJ18 识别有效的IP地址和掩码并进行分类统计
2022-07-20 13:02:00 【fareast_mzh】
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
// #define DEBUG
#ifdef DEBUG
#include <fstream>
#endif
using namespace std;
class HJ18 {
private:
int a, b, c, d, e, x, p;
static unsigned str2unsigned(std::string s) {
unsigned m = 0;
for (int i = 0; i < s.size(); i++) {
m = 10 * m + s.at(i) - '0';
}
return m;
}
static bool isValidMask(unsigned mask) {
// 二进制下全是1或者全是0均为非法子网掩码
if (0 == mask || mask == 0xffffffff) {
return false;
}
int i;
for (i = 31; i >=0; i--) {
if ((mask & (1<<i)) == 0) {
break; // 找到变成0的位置
}
}
while (--i >= 0) {
if ((mask & (1<<i))) {
return false; // 0->1
}
}
return true;
}
public:
HJ18() {
this->a = this->b = this->c = this->d = this->e = this->x = this->p = 0;
}
HJ18 add(string input) {
vector<string> vs = split(std::move(input), '~');
#ifdef DEBUG
cout.setf(ios::showbase);
std::cout << "ip=" <<vs[0] << ", mask=" << vs[1] << std::endl;
#endif
unsigned ip, mask;
try {
ip = ip2long(vs[0]);
mask = ip2long(vs[1]);
} catch (bad_exception &e) {
this->x += 1;
return *this;
}
#ifdef DEBUG
std::cout << "ip=" << hex <<ip <<std::endl;
std::cout << "mask=" <<hex <<mask <<std::endl;
#endif
unsigned fb = (ip >> 24) & 0x000000ff; // first byte
unsigned nb = (ip >> 16) & 0x000000ff; // second byte
// 类似于【0.*.*.*】和【127.*.*.*】的IP地址不属于上述输入的任意一类,也不属于不合法ip地址,计数时请忽略
if (fb == 0 || fb == 127) {
return *this;
}
if (!isValidMask(mask)) {
this->x += 1;
return *this;
}
if (fb < 127) {
this->a += 1;
if (fb == 10) {
this->p += 1;
}
// std::cout << "A class\n";
} else if (fb < 192) {
this->b += 1;
if (fb == 172 && (16<=nb && nb < 32)) {
this->p += 1;
}
// std::cout << "B class\n";
} else if (fb < 224) {
this->c += 1;
// std::cout << "C class " << "fb="<<fb <<",nb="<<nb<< std::endl;
if (fb == 192 && nb == 168) {
this->p += 1;
}
} else if (fb < 240) {
this->d += 1;
} else {
this->e += 1;
}
return *this;
}
string toString() const {
stringstream ss;
ss << a << " "<< b << " " << c << " " << d << " " << e << " "<< x << " " << p;
string s = ss.str();
ss.clear();
return s;
}
static unsigned int ip2long(string s) {
vector<string> vs = split(std::move(s), '.');
unsigned int ip = 0;
unsigned u;
for (auto & v : vs) {
if (v.empty()) {
throw bad_exception();
}
u = str2unsigned(v);
if (u < 0 || u > 255) {
throw bad_exception();
}
ip = (ip << 8) | u;
}
return ip;
}
static vector<string> split(string s, char delim) {
vector<string> vec;
int i;
int subLen = 0;
int len = s.size();
for (i = 0; i < len; i++) {
if (s.at(i) != delim) {
subLen += 1;
continue;
}
vec.push_back(s.substr(i-subLen, subLen));
subLen = 0;
}
vec.push_back(s.substr(i-subLen, subLen));
return vec;
}
};
int main() {
HJ18 solution;
string line;
#ifndef DEBUG
while (cin >> line) {
solution.add(line);
}
#else
std::ifstream ifs;
ifs.open("../input4.txt", std::ios_base::in);
if (!ifs.is_open()) {
std::cout << "打开../input4.txt失败";
return -2;
}
int i = 0;
while (ifs >> line) {
solution.add(line);
cout.unsetf(ios::hex);
std::cout <<"----------"<<++i<<"-------------\n";
}
ifs.close();
#endif
cout << solution.toString() << endl;
}
边栏推荐
- Selenium Grid 安装
- Google Earth Engine——1980-至今全球压力、温度、风等数据集
- Several silly built-in functions about relative path / absolute path operation in CAPL script
- Lazy to the bone, I am too lazy to write articles in CSDN. I write articles based on selenium simulation
- 3. Rsync backup case
- Redis在Centos7上的安装部署[通俗易懂]
- FreeRTOS个人笔记-FreeRTOSConfig.h
- 在CANoe中通过Panel面板控制Test Module 运行(初级)
- 【26. 字符串哈希】
- 亮点抢先看!2022开放原子全球开源峰会定于7月25-29日在北京举办
猜你喜欢
Yu Meimei, Ji Gongdu
Cause and effect of memory alignment
进程间通信-共享内存shmat
你怎么看CV-Transformer【秋招面经分享】
手撸第三人称角色控制器——Unity制作魂类游戏笔记 01
How many months did you write your first SCI?
4.存储NFS
Chapter3 : Fighting COVID-19 with Artificial Intelligence
实例说明代码段(.text)、数据段(.data)、bss段、只读数据段(.rodata)、堆栈的划分依据
解决函数名冲突问题(dlopen,dlsym,dlclose)
随机推荐
mybats中xml参数类型的设置
中继器、集线器、网桥、交换机、路由器
从0到1 拿下C语言—程序结构及使用示例
Locate the kernel code through vmlinuz
AD活动目录和域网络
Worthington酒精脱氢酶的特异性和五个应用
SAP Spartacus - Progressive Web Applications,渐进式 Web 应用程序
Detailed explanation of C language array
Installation and deployment of redis on centos7 [easy to understand]
Spatial noise reduction and time domain noise reduction
你还觉得难吗?那就给盘活数据支个招
Adresse de téléchargement Canoe et téléchargement et activation de Can Demo 16, avec toutes les versions logicielles Canoe jointes
FreeRTOS个人笔记-FreeRTOSConfig.h
Examples illustrate the division basis of code segment (.Text), data segment (.Data), BSS segment, read-only data segment (.Rodata) and stack
从零开始学习CANoe(十六)—— Graphics
Wireless communication technology commonly used in the Internet of things
Google Earth Engine——1980-至今全球压力、温度、风等数据集
Repeaters, hubs, bridges, switches, routers
Vh6501 Learning Series
除了定时器,真的没法在Simulation Node 类型的CAPL节点中实现延时了吗?