Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW_2 #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

HW_2 #134

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 111 additions & 83 deletions SuperPrime_HW2.cpp
Original file line number Diff line number Diff line change
@@ -1,94 +1,122 @@
#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;
}
int num;

Nature() :num(0) {
// std::cout << "Default Create Nature as " << num << std::endl;
}// num = 0

Nature(int n) :num(n) {
// std::cout << "Create Nature as " << num << std::endl;
}//num = n

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 is_superPrime(int a) {
int hun, ten, one, sum, mul, square_sum;
hun = a / 100;
ten = (a - 100 * hun) / 10;
one = a - 100 * hun - 10 * ten;
mul = hun * ten * one;
sum = hun + ten + one;
square_sum = hun * hun + ten * ten + one * one;
if (prime(a) && prime(sum) && prime(mul) && prime(square_sum))
{
return 1;
}
else return 0;

}
int prime(int a) {
int i, b = 0;
int c = 0;
if (a == 2)
b = 1;

for (i = 2; i < a; i++) {
if (a % i == 0)
{
c++;
}
}
if (c == 0) {
b = 1;
}

return b;

}//�ж�һ���������Dz������� ����Ϊ1������Ϊ0

};
class Container {



class SuperPrime {
private:
std::vector<SuperPrime> natures;
std::vector<Nature> 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;
SuperPrime(int a, int b) {
std::cout << "Create SuperPrime from " << a << " to " << b << std::endl;
for (int i = a; i < b; i++) {
Nature nat(i);
// std::cout << "HAHA" << std::endl;
if (nat.is_superPrime(i)){
natures.push_back(nat);
}

// std::cout << "DDDDD" << std::endl;
}
}

~SuperPrime() {
// std::cout << "Destroy SuperPrime " << std::endl;
}



Nature max() {
std::vector<Nature>::iterator it = natures.begin();
Nature max(0);
for (; it != natures.end(); it++) {
if ((*it).is_superPrime(it->num)) {

max = *it;

}
}
return max;
}
}
~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;
}
void show() {
std::vector<Nature>::iterator it = natures.begin();
int sum1 = 0;
int sum2 = 0;
for (; it != natures.end(); it++) {
if ((*it).is_superPrime(it->num)) {
sum1 += (*it).num;
sum2++;
//std::cout << std::endl;
//std::cout << (*it).num;
}
}
std::cout << "��������������" << sum2 << std::endl;
std::cout << "���������ĺͣ�" << sum1 << std::endl;
}
return max;
}


};
int main() {
SuperPrime sp(10, 13);
Nature n = sp.max();
std::cout << "��󳬼�������" ;
n.show();

return 0;
}
SuperPrime sp(100, 999);
Nature n = sp.max();
sp.show();
std::cout << "���ij���������" << n.num;

return 0;
}