-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.cpp
More file actions
89 lines (75 loc) · 1.5 KB
/
1.cpp
File metadata and controls
89 lines (75 loc) · 1.5 KB
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
using namespace std;
#include "iostream"
#include <fstream>
#include <map>
class CStringComparator
{
public:
/*
A < B --> true
*/
bool operator()(const char* A, const char* B) const
{
while (true)
{
if (A[0] == B[0])
{
//A = B
if (!A[0])
return false;
A++;
B++;
}
else
{
return A[0] < B[0];
}
}
}
};
void fix_f(char *buffer){
for ( size_t i = 0; i < sizeof(buffer); i++){
if (ispunct(buffer[i])) buffer[i] = '0';
else if (isupper(buffer[i])) buffer[i] = tolower(buffer[i]);
}
}
void process(char* buffer){
map<const char*, size_t, CStringComparator> WMap;
const char* Words[] = { "Who", "Are", "You", "Who" };
const char* Word;
for (size_t i = 0; i < sizeof(Words) / sizeof(Words[0]); i++)
{
Word = Words[i];
auto It = WMap.find(Word);
if (It == WMap.end())
{
WMap.insert(make_pair(Word, 1));
}
else
{
It->second++;
}
}
fix_f(buffer);
printf(buffer);
for (auto Entry : WMap){
printf("Word %s, count %d\n", Entry.first, (int)Entry.second);
}
}
int main(int argc, char** args){
ifstream file("wp.txt", ios::binary | ios::ate);
streamsize size = file.tellg();
file.seekg(0, ios::beg);
char* buffer = new char[size+1];
buffer[size] = 0;
if (file.read(buffer, size)){
process(buffer);
printf("File read, %d\r\n", (int)size);
}
else{
printf("err");
}
delete[]buffer;
printf("Hello\r\n");
return 1;
}