forked from Dashark/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperPrime_HW2.cpp
94 lines (90 loc) · 1.86 KB
/
SuperPrime_HW2.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
#include <iostream>
#include <vector>
class Nature {
private:
int num;
public:
Nature():num(0){
std::cout << "Default Create Nature as " << num << std::endl;
}
Nature(int n):num(n) {
std::cout << "Create Nature as " << num << std::endl;
}
Nature(const Nature &nat):num(nat.num){
std::cout << "Copy Create Nature as " << num << std::endl;
}
~Nature() {
std::cout << "Destroy Nature as " << num << std::endl;
}
bool isPrime() {
if(num == 1 || num == 0)
return false;
for(int i = 2; i <= (int)sqrt(num); i++)
{
if(num % i == 0)
return false;
}
return true;
}
int compare(const Nature &nat) {
if (num > nat.num)
return 1;
else if(num == nat.num)
return 0;
return -1;
}
private:
};
class SuperPrime : public Nature {
private:
int num;
public:
SuperPrime(int n):num(n) {
}
bool isPrime() {
Nature nat(num);
return nat.isPrime();
}
int compare(const SuperPrime &nat) {
if (num > nat.num)
return 1;
else if(num == nat.num)
return 0;
return -1;
}
};
class Container {
private:
std::vector<SuperPrime> natures;
public:
Container(int a, int b) {
std::cout << "Create SuperPrime from " << a << " to " << b << std::endl;
for(int i = a; i < b; i++) {
SuperPrime nat(i);
std::cout << "HAHA" << std::endl;
if(nat.isSuperPrime())
natures.push_back(nat);
std::cout << "DDDDD" << std::endl;
}
}
~Container() {
std::cout << "Destroy SuperPrime " << std::endl;
}
SuperPrime max() {
std::vector<SuperPrime>::iterate it = natures.begin();
Nature max(0);
for(; it != natures.end(); it ++) {
if (max.compare(*it)) {
max = *it;
}
}
return max;
}
};
int main() {
SuperPrime sp(10, 13);
Nature n = sp.max();
std::cout << "���������" ;
n.show();
return 0;
}