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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "WordStat.h"
#define HASHMAX 190271 /* prime number for size of hash table*/
#define MULT 31 /* hash multiplier */
FILE *fp; /* file pointer */
Node *bin[HASHMAX]; /* creates link-list locations for hash table*/
int main(int argc, char **argv){
//makeDict();
//fp=fopen(*argv, "r");
Stack();
//fclose(fp);
//printf("%d",argc);
return 0;
}
void makeDict(){ /* creates a hash table of files from dict.txt */
int i;
char newWord[23]; /* longest word in dict is 23 chars*/
for (i=0; i<HASHMAX; i++)
bin[i]=NULL;
fp=fopen ("dict.txt", "r");
if(fp == NULL){
puts("Error : Cannot Find Dictionary File");
exit(1);
}
while(fgets(newWord, 23, fp)!=NULL){
addWord(newWord);
}
fclose(fp);
}
void addWord(char *s){ /* adds word to hash table */
Node * p;
int h=hash(s);
for(p=bin[h]; p!=NULL; p=p->next){
}
p=(Node *)malloc(sizeof(node));
if(p == NULL){
puts("Error : Cannot Allocate Memory");
exit(1);
}
p->word=(char *)malloc(strlen(s)+1);
strcpy(p->word, s);
p->next=bin[h];
bin[h]=p;
}
unsigned int hash(char *p){ /* creates hash value for word */
unsigned int h=0;
for(; *p; p++)
h=MULT * h + *p;
return h % HASHMAX;
}
//void del(struct wordList *s){
// s->current->word=NULL;
//}
void Stack(){ /* creates a stack that holds one word at a time*/
struct stacking let;
struct wordList list;
// del(&list);
char c;
fp=fopen ("test.txt", "r");
while (c!=EOF){
unsigned int stackCount=0; /* counts length of current stack */
clearStack(&let);
c=toupper(getc(fp));
while (isalpha(c)){
push(&let,c);
stackCount++;
c=toupper(getc(fp));
}
char newWord[50];
if (stackCount!=0)
newWord[stackCount]='\0';
if(stackCount>50){
puts("Error : All Words Must Be Under 51 Characters");
exit(1);
}
while (stackCount!=0){
c=pop(&let);
stackCount--;
newWord[stackCount]=c;
}
list=alphab(newWord,&list);
}
fclose(fp);
// while(list->current!=NULL){
// printf("%s %d\t",list->current->word, list->current->count);
// list->current=list->current->next;
// }
prant(&list);
}
void prant(struct wordList *gogogo){
while(gogogo->current!=NULL){
printf("%s %d\t",gogogo->current->word, gogogo->current->count);
gogogo->current=gogogo->current->next;
}
}
//int countLets(FILE *f){ /* returns length of the longest word */
// char c;
// int i,maxLen=0;
// while (c!=EOF){
// i=0;
// c=getc(f);
// while (isalpha(c)){
// i++;
// c=getc(f);
// }
// if (i>maxLen)
// maxLen=i;
// }
// printf("\n%d",maxLen);
// return maxLen;
//}
void clearStack(struct stacking *clear){ /* erases stack */
clear->top=NULL;
clear->current=NULL;
}
struct stacking push(struct stacking *stack,char c){/* adds element to stack*/
stack->current=malloc(sizeof(struct node));
if(stack->current == NULL){
puts("Error : Cannot Allocate Memory");
exit(1);
}
stack->current->let=c;
stack->current->next=stack->top;
stack->top=stack->current;
return *stack;
}
char pop(struct stacking *stack){ /* removes element from stack */
if (stack->top==NULL){
puts("Error : Stack Is Empty");
exit(0);
}
char popped=stack->top->let;
stack->current=stack->top;
stack->top=stack->top->next;
free(stack->current);
return popped;
}
struct wordList alphab(char *s,struct wordList *list){ /* adds word to linked-list alphabetically */
Node * p,* first;
first=list->current;
p=(Node *)malloc(sizeof(node));
if(p == NULL){
puts("Error : Cannot Allocate Memory");
exit(1);
}
p->word=(char *)malloc(strlen(s)+1);
strcpy(p->word, s);
list->current=p;
if (first->count==3133){
first=list->current;
list->current->count=1;
}else{
for(p=first;p!=NULL;p=p->next){
switch(strcmp(list->current->word,p->word)){
case 0:
list->current->count++;
break;
case -1:
list->current->count=1;
if(p==first){
list->current->next=first;
first=list->current;
}else{
list->current->next=p;
Node * temp=p;
p=list->current;
list->current=temp;
}
p=NULL;
break;
case 1:
if(p->next==NULL){
p->next=list->current;
list->current->count=1;
}
break;
default:
puts("Error : Incorrect Input");
exit(1);
}
}
}
// while(list!=NULL){
// printf("%s %d\t",list->current->word, list->current->count);
// list->current=list->current->next;
// }
return *list;
}
|