-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsafe-version.cpp
159 lines (136 loc) · 3.51 KB
/
unsafe-version.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
156
157
158
159
#include <iostream>
#include <fstream>
#include <pthread.h>
#include <vector>
#include <cstdlib> // for atoi()
#include <cmath> // for sqrt(num)
using namespace std;
int numOfPrime = 0;
int numOfPalindrome = 0;
int numOfPalindromicPrime = 0;
int totalNums = 0;
vector<int> PrimeList;
vector<int> PalindromeList;
vector<int> PalindromicPrimeList;
struct threadsInfo {
int threadID;
int startNum;
int endNum;
bool TisWorking = true;
};
void* threadsFun(void* arg) {
threadsInfo* threadsArg = (threadsInfo*)arg;
if (!threadsArg->TisWorking) {
cout << "threadID=" << threadsArg->threadID << ", I have nothing to do!\n";
}
else {
cout << "threadID=" << threadsArg->threadID
<< ", startNum=" << threadsArg->startNum
<< ", endNum=" << threadsArg->endNum << endl;
for (int num = threadsArg->startNum; num < threadsArg->endNum; num++) {
totalNums++;
// check if prime
bool primeCheck = true;
if (num <= 1)
primeCheck = false;
else {
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
primeCheck = false;
break; }
}
}
// check if palindrome
bool palindromeCheck;
int orgNum = num;
int revNum = 0;
while (orgNum > 0) {
int digit = orgNum % 10;
revNum = revNum * 10 + digit;
orgNum /= 10;
}
palindromeCheck = (num == revNum);
if (primeCheck) {
PrimeList.push_back(num);
numOfPrime++;
}
if (palindromeCheck) {
PalindromeList.push_back(num);
numOfPalindrome++;
}
if (primeCheck && palindromeCheck) {
PalindromicPrimeList.push_back(num);
numOfPalindromicPrime++;
}
}
}
pthread_exit(NULL);
}
int main(int argc, char* argv[]) {
// store the number of threads (T)
if (argc != 2 || atoi(argv[1]) <= 0) {
cerr << "Unaccepted number of threads!\n";
return 1;
}
int T = atoi(argv[1]); // ASCII to int
// read the range of numbers
int rangeStart, rangeEnd;
ifstream infile("in.txt");
if (!infile) {
cerr << "Input file couldn't open.\n";
return 1;
}
infile >> rangeStart >> rangeEnd;
infile.close();
// divide the range over threads
int rangeSize = rangeEnd - rangeStart;
int newT = T;
if (rangeSize % T == 0) {
rangeSize /= T;
}
else { // if (T > rangeSize)
newT = rangeSize;
rangeSize = 1;
}
// create worker threads
pthread_t* threads = new pthread_t[T];
threadsInfo* threadsArg = new threadsInfo[T];
for (int i = 0; i < T; i++) {
threadsArg[i].threadID = i;
if (i < newT) {
threadsArg[i].startNum = rangeStart + (i * rangeSize);
threadsArg[i].endNum = threadsArg[i].startNum + rangeSize;
}
else // if (i > newT)
threadsArg[i].TisWorking = false;
pthread_create(&threads[i], NULL, threadsFun, (void*)&threadsArg[i]);
}
// make main wait until all threads finish
for (int i = 0; i < T; i++) {
pthread_join(threads[i], NULL);
}
// main thread will print
cout << "totalNums=" << totalNums
<< ", numOfPrime=" << numOfPrime
<< ", numOfPalindrome=" << numOfPalindrome
<< ", numOfPalindromicPrime=" << numOfPalindromicPrime << endl;
// store arrays of numbers
ofstream outfile("out.txt");
if (!outfile) {
cerr << "Output file couldn't open.\n";
return 1;
}
outfile << "The prime numbers are:\n";
for (int i = 0; i < numOfPrime; ++i) {
outfile << PrimeList[i] << endl;
}
outfile << "The palindrome numbers are:\n";
for (int i = 0; i < numOfPalindrome; ++i) {
outfile << PalindromeList[i] << endl;
}
outfile << "The palindromicPrime numbers are:\n";
for (int i = 0; i < numOfPalindromicPrime; ++i) {
outfile << PalindromicPrimeList[i] << endl;
}
return 0;
}