Skip to content

Instantly share code, notes, and snippets.

@sdkfz181tiger
Last active May 13, 2024 13:31
Show Gist options
  • Save sdkfz181tiger/4f2b82cbe5d78f4e7a5981df96e2f8fd to your computer and use it in GitHub Desktop.
Save sdkfz181tiger/4f2b82cbe5d78f4e7a5981df96e2f8fd to your computer and use it in GitHub Desktop.
C/C++課題ネタ04_IPv4クラス
//==========
// IPv4データ型
// 1, Compile
// g++ -std=c++17 -Wall -Wextra main.cpp
// 2, Run
// ./a.out
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
class IPv4{
public:
vector<uint> octs;
public:
IPv4(const string ip){
//printf("IPv4\n");
stringstream ss(ip);
string str;
while(getline(ss, str, '.')){
uint num = stoi(str);
if(num < 0) num = 0;
if(255 < num) num = 255;
octs.push_back(num);
}
}
void show(){
printf("IPv4: %d.%d.%d.%d\n", octs[0], octs[1], octs[2], octs[3]);
}
// operator ==
friend bool operator==(const IPv4 &a, const IPv4 &b){
for(int i=0; i<4; i++){
if(a.octs[i] != b.octs[i]) return false;
}
return true;
}
// operator !=
friend bool operator!=(const IPv4 &a, const IPv4 &b){
return !(a == b);
}
// operator <
friend bool operator<(const IPv4 &a, const IPv4 &b){
for(int i=0; i<4; i++){
if(a.octs[i] == b.octs[i]) continue;
return a.octs[i] < b.octs[i];
}
return false;
}
friend bool operator<=(const IPv4 &a, const IPv4 &b){
if(a < b) return true;
if(a == b) return true;
return false;
}
// operator >
friend bool operator>(const IPv4 &a, const IPv4 &b){
for(int i=0; i<4; i++){
if(a.octs[i] == b.octs[i]) continue;
return b.octs[i] < a.octs[i];
}
return false;
}
friend bool operator>=(const IPv4 &a, const IPv4 &b){
if(a > b) return true;
if(a == b) return true;
return false;
}
};
int main(){
printf("Hello, Cpp!!\n");
// IPv4
IPv4 ipA("192.168.1.1");
ipA.show();
IPv4 ipB("192.168.1.2");
ipB.show();
// Compare
if(ipA == ipB) printf("ipA == ipB\n");
if(ipA < ipB) printf("ipA < ipB\n");
if(ipA <= ipB) printf("ipA <= ipB\n");
if(ipA > ipB) printf("ipA > ipB\n");
if(ipA >= ipB) printf("ipA >= ipB\n");
return 0;
}
//==========
// IPv4データ型(2進文字列として格納)
// 1, Compile
// g++ -std=c++17 -Wall -Wextra main.cpp
// 2, Run
// ./a.out
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
class IPv4{
public:
vector<uint> octs;
string bin;
uint addr;
public:
IPv4(const string ip){
//printf("IPv4\n");
// Parse to vector
stringstream ss(ip);
string str;
while(getline(ss, str, '.')){
uint num = stoi(str);
if(num < 0) num = 0;
if(255 < num) num = 255;
octs.push_back(num);
}
// Parse to string and uint
for(uint num:octs) bin += bitset<8>(num).to_string();
printf("bin: %s\n", bin.c_str());
// Parse to uint
addr = stoul(bin, 0, 2);
printf("addr: %u\n", addr);
// Test
//const string test = bitset<32>(addr).to_string();
//printf("test: %s\n", test.c_str());
}
void show(){
printf("IPv4: %s\n", bin.c_str());
}
// operator ==
friend bool operator==(const IPv4 &a, const IPv4 &b){
return a.addr == b.addr;
}
// operator !=
friend bool operator!=(const IPv4 &a, const IPv4 &b){
return !(a == b);
}
// operator <
friend bool operator<(const IPv4 &a, const IPv4 &b){
return a.addr < b.addr;
}
friend bool operator<=(const IPv4 &a, const IPv4 &b){
if(a < b) return true;
if(a == b) return true;
return false;
}
// operator >
friend bool operator>(const IPv4 &a, const IPv4 &b){
return a.addr > b.addr;
}
friend bool operator>=(const IPv4 &a, const IPv4 &b){
if(a > b) return true;
if(a == b) return true;
return false;
}
};
int main(){
printf("Hello, Cpp!!\n");
// IPv4
IPv4 ipA("192.168.1.1");
ipA.show();
IPv4 ipB("192.168.1.2");
ipB.show();
// Compare
if(ipA == ipB) printf("ipA == ipB\n");
if(ipA < ipB) printf("ipA < ipB\n");
if(ipA <= ipB) printf("ipA <= ipB\n");
if(ipA > ipB) printf("ipA > ipB\n");
if(ipA >= ipB) printf("ipA >= ipB\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment