-
Notifications
You must be signed in to change notification settings - Fork 0
/
prime.cpp
109 lines (82 loc) · 2.19 KB
/
prime.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
#include <iostream>
#include<cstdio>
#include<cstdlib>
#include <fstream>
#include <cstring>
#include<strings.h>
#define MAX 999
#define LIMIT 10000000000000
using namespace std;
int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101};
typedef struct sortedLetters {
unsigned long long int value;
const char* word;
} sortedLetters;
int compare(const void * left, const void * right) {
const sortedLetters * a = (const sortedLetters *) left;
const sortedLetters * b = (const sortedLetters *) right;
if (a->value > b->value) {
return -1;
} else if (a->value < b->value) {
return 1;
} else {
return 0;
}
}
struct sortedLetters letters[MAX];
static long linesOfFile = 0;
class ReadFile {
public:
char words[MAX][MAX];
unsigned long long product;
ReadFile() {
product = 1;
linesOfFile = 0;
}
long getFileSize() {
return linesOfFile;
}
bool checkInput(char character) {
if((65 <= character && character <= 90) || (97 <= character && character <= 122)) {
return true;
} else {
return false;
}
}
void MapFile() {
ifstream myfile ("C:\\Users\\test\\Desktop\\code\\wordList.dat");
string line;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
linesOfFile++;
for(int index = 0; index < line.length(); index++) {
if(checkInput(line[index])) {
product *= primes[line[index]-97];
}
}
//cout << line << "\t" << product <<endl;
char * a = new char[line.size()+1];
a[line.size()]=0;
memcpy(a,line.c_str(),line.size());
//harsha's code
letters[linesOfFile].word = a;
letters[linesOfFile].value = product;
//cout << letters[linesOfFile].word << "\t" << letters[linesOfFile].value <<endl;
}
}
else
cout << "Unable to open file";
}
};
int main ()
{
ReadFile crossword;
crossword.MapFile();
//long size = crossword.getFileSize();
//qsort (letters, linesOfFile, sizeof(unsigned long long int), compare);
for(int i=0;i<linesOfFile; i++) {
cout << letters[i].word <<"\t" << letters[i].value <<endl;
}
}