-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpa_lab_6_3_7_(1)-B.cpp
124 lines (102 loc) · 2.63 KB
/
cpa_lab_6_3_7_(1)-B.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <vector>
using namespace std;
class IPAddress
{
protected:
string ip;
public:
IPAddress(const IPAddress &ip)
{
this->ip = ip.ip;
};
IPAddress(string ip)
{
this->ip = ip;
};
void virtual Print()
{
cout << this->ip << endl;
}
};
class CheckedIPAddress : IPAddress
{
public:
CheckedIPAddress(string ip) : IPAddress(ip)
{
correct = check(ip);
}
CheckedIPAddress(const CheckedIPAddress &ip) : IPAddress(ip)
{
this->correct = ip.correct;
}
void Print()
{
cout << this->ip << (correct ? " - Correct" : " - Not Correct") << endl;
}
private:
bool correct;
bool String2Int(const string& str, int& result) {
string::const_iterator i = str.begin();
if (i == str.end())
return false;
bool negative = false;
if (*i == '-')
{
negative = true;
++i;
if (i == str.end())
return false;
}
result = 0;
for (; i != str.end(); ++i)
{
if (*i < '0' || *i > '9')
return false;
result *= 10;
result += *i - '0';
}
if (negative)
{
result = -result;
}
return true;
}
bool check(string s) {
vector<string> parts = vector<string>();
string temp;
int a;
while (s.find_first_of('.') != string::npos)
{
temp = s.substr(0, s.find_first_of("."));
if (temp.length() > 3) return false;
if (!String2Int(temp, a)) return false;
parts.push_back(temp);
s = s.substr(s.find_first_of(".") + 1);
}
parts.push_back(s);
if (parts.size() > 4) return false;
if (parts.size() < 4) return false;
for (int i = 0; i < parts.size(); i++)
{
String2Int(parts[i].c_str(), a);
if (a > 255 || a < 0) return false;
}
return true;
}
};
int main()
{
string sIP, sCIP, sCIP2;
cin >> sIP;
cin >> sCIP;
cin >> sCIP2;
IPAddress ip = IPAddress(sIP);
CheckedIPAddress cip = CheckedIPAddress(sCIP);
CheckedIPAddress cip2 = CheckedIPAddress(sCIP2);
cout << endl;
ip.Print();
cip.Print();
cip2.Print();
return 0;
}