Home > Software engineering >  About the hash table in c source program read the file storage
About the hash table in c source program read the file storage

Time:01-13

Code on the Internet to find, should be able to run, but I run after the input file name is abnormal, ask counsel,

# include & lt; Iostream>
# include & lt; String>
# include & lt; Iomanip>
# include & lt; stdio.h>
using namespace std;
Const int TOTAL=32;//32 keyword
Const int MAXLEN=10;//key length
Const int HASHLEN=41;//length of hash table

int cont=0;//statistical key in the hash table number

Void Show (int key);
Int Read (char * filename);
Int isLetter (char ch);
Int isKeyWords word (char *);
Int FindHX keyword (char *);
Int CreatHX keyword (char *);
Int GetFreePos (int key);
Void ResetHX ();
Keyword int GetKey (char *);

Char KeyWords [TOTAL] [MAXLEN]=//construct two-dimensional array storage 32 keyword
{
"Auto", "break", "case", "char", "const", "continue,"
"Default" and "do", "double", "else", "enum" and "extern,"
"Float", "for", "goto", "if", "int", "long", "register",
"Return", "short", "signed", "sizeof", "static",
"Struct", "switch", "typedef", "union", "unsigned",
"Void", "volatile," "while,"
};
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Typedef struct HASH
{
Char keyword [MAXLEN];
Int count;//occurrences (frequency)
Int con;//number of conflict
} HASH HS [HASHLEN];

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
The class HASH//HASH table
{
Public:
Char keyword [MAXLEN];
Int count;//occurrences (frequency)
Int con;//number of conflict
};
HASH HS [HASHLEN];


Int main ()
{
Char filename [128], word [MAXLEN];
Int I, a key, the count;
ResetHX ();//to empty the hash table
Cout & lt; <"Please enter a file name to read (file must be in the same directory) :";
Cin & gt;> The filename.
Cout & lt; Read (filename);//read function read bytes from a file to a specified storage area, the length of the parameter determination of the number of bytes to read
For (I=0; iShow (I);
Cout & lt; <"Total number of keywords for:" & lt; Cout & lt; <"Please do you want to enter search keywords:";
Cin & gt;> The word;
Cout & lt; Show (FindHX (word));
Cout & lt; <"\ t conflict keyword list" & lt; Count=0;
For (I=0; i{
If (strlen (HS [I]. Keyword) & gt; 0)
{
Key=GetKey (HS [I]. Keyword).
If (key!=I)
{
count++;
Cout & lt; <[key words] : "\ t" & lt; Cout & lt; <"\ t [hash table current location] :" & lt; Cout & lt; <"\ t [number] conflict:" & lt; }
}
}
If (count==0)
Cout & lt; <"There is no conflict" & lt; The else
Cout & lt; <"Keyword \ t conflict altogether:" & lt; return 0;
}





Void Show (int key)//Show a keyword frequency
{
If (key<0 | | key & gt;=HASHLEN)
{
Cout & lt; <"Keyword does not exist!" return;
}
If (strlen (HS [key]. Keyword)==0)
{
Cout & lt; <"Hash table location:" & lt; return;
}
Cout & lt; <"Hash table location:" & lt; "<[key]. HS keyword & lt; <"Occurrences" & lt; cont++;
}



Int Read (char * filename)//Read the file
{
Char word [MAXLEN], ch.
int i;
The FILE * read;

If ((read=fopen (filename, "r"))==NULL)//read-only open a text file, only allowed to read data
{
Cout & lt; <"File does not exist, please confirm good input again!" return -1;
}
ResetHX ();//read the file before you empty the hash table
while (! Feof (read))//feof () is the end of file detection function, if there is no end, the return value is zero, the end is 1
{
i=0;
Ch=fgetc (read);//read a character
While (isLetter (ch)==0 & amp; & The feof (read)==0) ch=fgetc (read);
//if not letter then read
While (isLetter (ch)==1 & amp; & The feof (read)==0)
{
If (I==MAXLEN)
{
While (isLetter (ch)==1 & amp; & The feof (read)==0)
{
Ch=fgetc (read);
}
i=0;
break;
}
//over key length will skip the current recognition area, a word after reading
The else
{//to read the letters in the array in a row, to form a word
The word [i++]=ch;
Ch=fgetc (read);
}
}
The word [I]='\ 0';//end of an array of characters
If (isKeyWords (word))
{
CreatHX (word);
}
}
The fclose (read);
Cout & lt; <"Read success, file keyword has been deposited in the hash table, please continue to operate" & lt;
return 1;
}

Int isLetter (char ch)//judge whether letters, because the keywords are in English words
{
If ((ch & gt;='a' & amp; & Ch & lt;='z') | | (ch & gt;='A' & amp; & Ch & lt;='Z')) return 1;
The else return 0;
//is letter is returned 1, otherwise it returns 0 value
}

Int FindHX keyword (char *)//hash table lookup, block search
{
Int the key, find, tem=0;

if (! IsKeyWords (keyword)) return 1;
Key=GetKey (keyword);

If (STRCMP (HS [key]. Keyword, keyword)==0) return the key;

For (=find key + 1; Find{//linear probe method sequence in the hash table lookup whether existing keyword
Tem++;//number of statistics conflict
If (STRCMP (HS [the find]. Keyword, keyword)==0) {
The HS [the find]. Con=tem;
Return the find;
}
}

For (find=0; Find{
Tem++;
If (STRCMP (HS [the find]. Keyword, keyword)==0) {
The HS [the find]. Con=tem;
Return the find;
}
}
return -1; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related