forked from Dashark/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperPrime2.cpp
124 lines (124 loc) · 2.29 KB
/
SuperPrime2.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>
class Prime {
public:
Prime():number(0) {
}
Prime(int n):number(n) {
}
~Prime() {
}
bool isPrime() {
//2到number-1的因子
return false;
}
private:
const int number;
};
class PrimeSet {
public:
PrimeSet(int size) {
//集合的构造什么?
N = new Prime*[size];
this->size = size;
index = 0;
}
~PrimeSet() {
for (int i = 0; i < index; ++i) //销毁对象
delete N[i];
delete[] N;
}
bool add(int n) {
if(index == size) return false;
Prime *p = new Prime(n);
N[index] = p;
index += 1;
return true;
}
bool isAllPrime() {
for(int i = 0; i < index; i++)
if (!N[i]->isPrime())
return false;
return true;
}
private:
Prime **N;
int size, index;
};
class SuperPrime {
public:
SuperPrime():number(0), pset(3) { //为什么必须有?
}
SuperPrime(int n):number(n), pset(3) {
split(); //它就是构造对象
}
~SuperPrime() {
}
bool isSuperPrime() {
//怎么使用pset?
Prime p(number);
if (p.isPrime())
return true;
return false;
}
private:
const int number;
PrimeSet pset;
void split() { //工厂方法设计模式
// number split into N
int temp = number;
while(temp > 0) {
int n = temp % 10;
temp /= 10;
pset.add(n); //作业:单个数字为对象?还是和/积/平方和为对象?
}
}
int sum() {
return 0;
}
int multi() {
return 0;
}
int squareSum() {
return 0;
}
};
class SuperPrimeSet {
public:
SuperPrimeSet(int from, int to) {
size = to - from;
for (int i = from; i < to; i++)
set[i-from] = new SuperPrime(i);
}
~SuperPrimeSet() {
for(int i = 0; i < size; i++)
delete set[i];
}
int count() {
int count = 0;
for (int i = 0; i < size; i++)
if(set[i]->isSuperPrime())
count += 1;
return count;
}
int sum() {
int sum = 0;
/*
for (int i = 0; i < size; i++)
if(set[i].isSuperPrime())
sum += set[i];
*/
return sum;
}
private:
SuperPrime **set;
int size, index;
};
int main() {
SuperPrime sp(113);
if (sp.isSuperPrime())
std::cout << "113 is SuperPrime" << std::endl;
else
std::cout << "113 is NOT SuperPrime" << std::endl;
return 0;
}