SP1 cpp Defines the entry point for the console application include st

  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
// SP1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <cstdio>
#include <string>
#include <vector>
using namespace std;
#define BLOCKSIZE 262144
#define WORDSIZE 32
#define WORDSIZEPLUS1 33
struct st {
char word[WORDSIZEPLUS1];
};
vector<struct st> vec[WORDSIZE];
bool is_letter(char c) {
if ((c>='a' && c<='z')||(c>='A' && c<='Z')||(c>='0' && c<='9')) return true;
else return false;
}
void add_word_to_list(char cur[WORDSIZEPLUS1]) {
int vec_num=strlen(cur)-1;
struct st curst;
bool fresh = true;
for(int i=0; i<vec[vec_num].size(); i++)
{
if (!strcmp(vec[vec_num].at(i).word,cur))
{
fresh = false;
break;
}
}
if (fresh)
{
strcpy(curst.word, cur);
vec[vec_num].push_back(curst);
}
}
void make_word_list(FILE * work_file) {
bool word_exist=false;
char line[BLOCKSIZE];
char current_word[WORDSIZEPLUS1];
int i=0,j=0,len;
while (fgets(line, BLOCKSIZE, work_file)) {
//printf(".");
if (!word_exist) for(int k=0; k<WORDSIZEPLUS1; k++) current_word[k]='\0';
len=strlen(line);
for(i=0; i < len; i++) {
if (is_letter(line[i])) {
if (j<WORDSIZE) {
current_word[j]=line[i];
j++;
word_exist = true;
}
}
else if (word_exist) {
add_word_to_list(current_word);
j=0;
word_exist = false;
for(int k=0; k<WORDSIZEPLUS1; k++) current_word[k]='\0';
}
}
}
if (word_exist) add_word_to_list(current_word);
}
void print_list() {
for(int i = 0; i<WORDSIZE; i++) {
if (!vec[i].empty()) printf("\n%d: ", i+1);
while (!vec[i].empty())
{
printf("%s ", vec[i].back().word);
vec[i].pop_back();
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
FILE * work_file;
char file_name[64];
printf("\nEnter the path to the text file: ");
scanf("%s", file_name);
work_file=fopen(file_name, "r");
make_word_list(work_file);
print_list();
getchar();
getchar();
return 0;
}