1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
/// my very own subnet calculator 3
/// ~speci4lzero
#include <iostream>
#include <iomanip>
#include <string.h>
#include <string>
#include <cmath>
using namespace std;
char tv2[8]; ////
int we, yo; // some useful global variables
/* define a struct that allows subnet manipulation */
struct ipv4 {
int oa, ob, oc, od;
/* func ct determines the subnet class, assigns we # of masks in "working subnet octet" */
static int ct(ipv4 SUB) {
if (SUB.ob == 0 && SUB.oc == 0 && SUB.od == 0) {
we = cr(SUB.oa);
}
if (SUB.oa == 255 && SUB.ob >= 128) {
we = cr(SUB.ob);
}
if (SUB.oa == 255 && SUB.ob == 255 && SUB.oc >= 128) {
we = cr(SUB.oc);
}
if (SUB.od >= 128) {
we = cr(SUB.od);
}
yo = 8 - we; //// get # of zeroes in subnet mask
we = cs(we);
yo = cs(yo);
return 0;
}
/* func cr counts ones in octet */
static int cr(int subnet) {
int na = 0;
itoa (subnet, tv2, 2);
for (int j = 0; j < 8; j++) {
if (tv2[j] == '1')
++na;
}
return na;
}
/* func cs is the formula that gives number of usable subnets/usable hosts per subnet */
static int cs(int ne) {
return (pow(2, ne) - 2);
}
};
int main() {
char *nes, *nfs, *ngs, *nhs, S[30];
int sen, sfn;
cout << "Enter a subnet!" << endl;
cout << "Enter \"x\" to exit." << endl;
cin >> setw(30) >> S;
nes = strtok( S, "." ); ////
nfs = strtok ( 0, "." ); //
ngs = strtok ( 0, "." ); //
nhs = strtok ( 0, " " ); // split S into 4 strings of octets
ipv4 subn; //// declare ipv4 variable
subn.oa = stoi(nes); ////
subn.ob = stoi(nfs); //
subn.oc = stoi(ngs); //
subn.od = stoi(nhs); // cast char octets to type int
ipv4::ct(subn); //// run subnet calculation on subn
sen = we;
sfn = yo * we;
cout << sen << "\tUSABLE SUBNETS" << endl;
cout << sfn << "\tUSABLE HOSTS PER SUBNET" << endl;
cin >> setw(30) >> S; //// keep command prompt open
return 0;
}
|