-
Notifications
You must be signed in to change notification settings - Fork 1
/
1000. The MyInteger class .cpp
155 lines (130 loc) · 2.45 KB
/
1000. The MyInteger class .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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<iostream>
#include<algorithm>
#include<cmath>
#include <stdlib.h>
using namespace std;
class MyInteger
{
private:
int value;
public:
MyInteger(int);
int getValue()const;
bool isEven()const;
bool isOdd()const;
bool isPrime()const;
static bool isEven(int);
static bool isOdd(int);
static bool isPrime(int);
static bool isEven(const MyInteger&);
static bool isOdd(const MyInteger&);
static bool isPrime(const MyInteger&);
bool equals(int)const;
bool equals(const MyInteger&)const;
static int parseInt(const string&);
};
MyInteger::MyInteger(int value)
{
this -> value = value;
}
int MyInteger::getValue()const
{
return value;
}
bool MyInteger::isEven()const
{
if(value%2 == 0) return 1;
return 0;
}
bool MyInteger::isOdd()const
{
if(value%2 != 0) return 1;
return 0;
}
bool MyInteger::isPrime()const
{
if(value<2) return 0;
if(value==2) return 1;
for(int i=2;i<=sqrt(value)+1;i++)
{
if(value%i==0) return 0;
}
return 1;
}
bool MyInteger::isEven(int val)
{
if(val%2 == 0) return 1;
return 0;
}
bool MyInteger::isOdd(int val)
{
if(val%2 != 0) return 1;
return 0;
}
bool MyInteger::isPrime(int val)
{
if(val<2) return 0;
if(val==2) return 1;
for(int i=2;i<=sqrt(val)+1;i++)
{
if(val%i==0) return 0;
}
return 1;
}
bool MyInteger::isEven(const MyInteger& another)
{
if(another.value%2 == 0) return 1;
return 0;
}
bool MyInteger::isOdd(const MyInteger& another)
{
if(another.value%2 != 0) return 1;
return 0;
}
bool MyInteger::isPrime(const MyInteger& another)
{
if(another.value<2) return 0;
if(another.value==2) return 1;
for(int i=2;i<=sqrt(another.value)+1;i++)
{
if(another.value%i==0) return 0;
}
return 1;
}
bool MyInteger::equals(int val)const
{
if(val==value) return 1;
return 0;
}
bool MyInteger::equals(const MyInteger& another)const
{
if(another.value==value) return 1;
return 0;
}
int MyInteger::parseInt(const string& str)
{
// int cvalue=0;
// bool flag=0;
// if(str[0]=='-') flag=1;
// for(int i=1;i<str.size();i++)
// {
// cvalue=cvalue*10+(str[i]-'0');
// }
// if(flag) cvalue=0-cvalue;
// return cvalue;
return atoi(str.c_str());
}
int main()
{
MyInteger a=2;
MyInteger b=-2;
cout<<a.isOdd()<<endl;
cout<<a.isOdd(-1)<<endl;
cout<<a.isEven()<<endl;
cout<<a.isEven(-1)<<endl;
cout<<a.isPrime()<<endl;
cout<<a.isPrime(b)<<endl;
cout<<a.equals(-1)<<endl;
cout<<a.equals(b)<<endl;
cout<<a.parseInt("-10")<<endl;
}