Print keywords and function names in the C ++ source file of an application for hash search

Source: Internet
Author: User

# Include <iostream>
# Include <string>
# Include <vector>
# Include <fstream>
# Include <sstream>
# Include <set>

Using namespace STD;

/* Unsigned int bkdrhash (const string Str)
{
Unsigned int seed = 131;
Unsigned int hash = 0;
String: const_iterator it;
Cout <sizeof (unsigned INT) <Endl;
For (IT = Str. Begin (); it! = Str. End (); It ++)
{
Hash = hash * seed + * it;
}
Return hash & 0x7fffffff;
}
*/

// List of c ++ keywords
Const unsigned int M = 66; // Number of keywords
Const string keylist [m] = {"ASM", "Auto", "bad_cast", "const_cast", "dynamic_cast", "reinterpret_cast", "static_cast ",
"Bad_typeid", "bool", "break", "case", "catch", "Throw", "try", "char", "class", "type ", "const", "continue", "delete ",
"Do", "double", "else", "Enum", "Explicit", "Export", "extern", "false", "true", "float ", "for", "friend", "Goto", "if"
, "Inliine", "int", "long", "mutable", "namespace", "new", "operator", "private", "protected ", "public", "register ",
"Return", "short", "Signed", "unsigned", "static", "struct", "Switch", "template", "typedef", "typeid ", "typename ",
"Union", "using", "Void", "volatile", "wchar_t", "while", "this", "for", "cout ", "Cin "};

Vector <string> kl (keylist, keylist + M );

Typedef struct hashnode // hash Node
{
String key;
Struct hashnode * link;
};

Const unsigned int n = 26;
Typedef hashnode * array [N]; // hash Array

Class hashofalphabet
{
PRIVATE:
Array hashlist;
Unsigned int hashfuction (const string & Str); // Hash Function
Public:
Hashofalphabet ();
Hashnode * locate (const string & Str); // search for keywords
Bool insert (const string & Str); // insert a keyword
Bool create (const vector <string> & keylist); // create a hash table
Void show () const;
};

Hashofalphabet: hashofalphabet ()
{
Int I;
For (I = 0; I <n; I ++)
{
This-> hashlist [I] = 0; // each pointer is empty during initialization.
}
}

Bool hashofalphabet: Create (const vector <string> & keylist)
{
Vector <string >:: const_iterator it;
For (IT = keylist. Begin (); it! = Keylist. End (); It ++)
{
If (! This-> insert (* It ))
{
Cerr <"insufficient memory, insertion failed! "<Endl;
Return false;
}
}
Return true;
}

Unsigned int hashofalphabet: hashfuction (const string & Str)
{
Return static_cast <int> (STR [0]-'A ');
}

Hashnode * hashofalphabet: locate (const string & Str)
{
Unsigned int Index = This-> hashfuction (STR );
If (index <0 | index> = N) return 0; // The subscript exceeds the valid range.
Hashnode * P = This-> hashlist [Index];
While (P)
{
If (p-> key = Str) return P;
P = p-> link;
}
Return 0;
}

Bool hashofalphabet: insert (const string & Str)
{
Hashnode * P = new hashnode;
If (! P) return false;
P-> key = STR;
P-> link = 0;
Unsigned int Pos = This-> hashfuction (STR );
P-> link = This-> hashlist [POS]; // use the prefix
This-> hashlist [POS] = P;
Return true;
}

Void hashofalphabet: Show () const
{
Int I;
Hashnode * P;
For (I = 0; I <n; I ++)
{
Cout <"the hash value is" <I <:";
For (P = This-> hashlist [I]; P = p-> link)
{
Cout <p-> key <"";
}
Cout <Endl;
}
}

Class filenotopen // the file cannot be opened. exception class
{
Public:
String what ()
{
String STR = "the file cannot be opened. This file or directory is not available! ";
Return STR;
}
};
 
Class convert // conversion class
{
PRIVATE:
Bool haskuohao (const string & Str); // determines whether the string contains parentheses.
Bool hasjiankuohao (const string & Str); // determines whether the string contains angle brackets.
Public:
Void readfile (const string & filename, vector <string> & TMP); // read the file content into the vector TMP

};
 
Bool convert: haskuohao (const string & Str)
{
String: const_iterator it;
For (IT = Str. Begin (); it! = Str. End (); It ++)
{
If (* It = '(') return true;
}
Return false;
}

Bool convert: hasjiankuohao (const string & Str)
{
String: const_iterator it;
For (IT = Str. Begin (); it! = Str. End (); It ++)
{
If (* It = '<') return true;
}
Return false;
}

Void convert: readfile (const string & filename, vector <string> & TMP)
{
Ifstream infile; // defines the input stream File
Try
{
Infile. Open (filename. c_str (), IOS: In); // read the file
If (! Infile) Throw filenotopen ();
}
Catch (filenotopen & E)
{
Cerr <E. What () <Endl;
Exit (-1 );
}
Catch (...)
{
Cerr <"something unexpected! "<Endl;
}
String S;
While (Getline (infile, S) // obtain each row
{
Stringstream line (s); // initialize the string Stream Object
String word;
While (line> word)
{
If (! This-> haskuohao (Word )&&! This-> hasjiankuohao (Word) TMP. push_back (Word); // This word does not contain left brackets
// It does not include <. brackets are directly placed in the TMP vector.
Else if (this-> haskuohao (Word) // contains left brackets
{
String prekuohao = ""; // content before brackets
String: iterator it = word. Begin ();
While (* It! = '(') Prekuohao ++ = * It ++;
TMP. push_back (prekuohao); // put the content before the left slogan
TMP. push_back ("(");
It ++;
String betweenkuohao = ""; // content between left and right brackets
While (it! = Word. End () & * It! = ') Betweenkuohao + = * It ++;
If (betweenkuohao. Empty () = false) // The content between the left and right brackets does have content
{
Stringstream SS (betweenkuohao );
String word;
While (SS> word)
{
TMP. push_back (Word); // place the content between the left and right brackets
}
}
If (* It = ') TMP. push_back (")");
}
Else // contains <parentheses
{
String prekuohao = ""; // content before brackets
String: iterator it = word. Begin ();
While (* It! = '<') Prekuohao ++ = * It ++;
TMP. push_back (prekuohao); // put the content before the left slogan
TMP. push_back ("<");
It ++;
String betweenkuohao = ""; // content between left and right brackets
While (it! = Word. End () & * It! = '>') Betweenkuohao + = * It ++;
If (betweenkuohao. Empty () = false) // The content between the left and right brackets does have content
{
Stringstream SS (betweenkuohao );
String word;
While (SS> word)
{
TMP. push_back (Word); // place the content between the left and right brackets
}
}
If (* It = '>') TMP. push_back ("> ");
}
}
}
Infile. Clear (); // clear the file stream
Infile. Close (); // close the file
}

Class print_filecontent // print the abstract class of the file content
{
Protected:
Vector <string> storevector; // The vector that stores the File Content
Convert C;
String filename;
Hashofalphabet hash; // hash object
Public:
Virtual void print () = 0;
};

Class print_keyword: Public print_filecontent
{
PRIVATE:
Bool occurance [m]; // indicates whether the keyword has already appeared. Duplicate keywords are removed during printing.
Unsigned int positionofkl (const string & Key); // position of the keyword in the KL Vector
Public:
Print_keyword ();
Void print ();
};

Print_keyword: print_keyword ()
{
Hash. Create (kL); // create a hash
Int I;
For (I = 0; I <m; I ++) occurance [I] = false; // indicates that the I-th keyword has not been shown
}

Unsigned int print_keyword: positionofkl (const string & Key)
{
Vector <string >:: const_iterator it;
For (IT = KL. Begin (); it! = KL. End (); It ++)
{
If (* It = key)
{
Return it-kl.begin ();
}
}
}
Void print_keyword: Print ()
{
Cout <"Enter the complete path of the C ++ source file (for example, c: \ study \ SS \ AA. cpp ):";
Cin> filename;
C. readfile (filename, storevector );
Vector <string >:: const_iterator it;
Cout <"the keyword of C ++ in this source file is:" <Endl;
Int Pos; // The Position of the keyword in occurance
For (IT = storevector. Begin (); it! = Storevector. End (); It ++)
{
If (hash. Locate (* It) // is the keyword
{
Pos = This-> positionofkl (* It); // locate the keyword
If (! This-> occurance [POS])
{
Cout <* It <Endl;
This-> occurance [POS] = true;
}
}
}
}

Class print_functionname: Public print_filecontent // print the function name class
{
PRIVATE:
Set <string> name; // stores printed file names.
Public:
Print_functionname ();
Void print ();
};

Print_functionname: print_functionname ()
{
This-> hash. Create (kL );
}

Void print_functionname: Print ()
{
Cout <"Enter the complete path of the C ++ source file (for example, c: \ study \ SS \ AA. cpp ):";
Cin> filename;
C. readfile (filename, storevector );
Vector <string >:: const_iterator it;
Cout <"the C ++ function in this source file is:" <Endl;
Int Pos; // The Position of the keyword in occurance
For (IT = storevector. Begin (); it! = Storevector. End (); It ++)
{
If (* It = "(" & it> storevector. Begin () // The function name may be before the left parenthesis.
{
If (hash. Locate (* (IT-1) break; // the keyword is before the left bracket.
Else
{
If (name. Count (* (IT-1) = 0)
{
Cout <* (IT-1) <Endl;
Name. insert (* (IT-1 ));
}
}
}
}
}

Void main ()
{
Print_keyword PK;
PK. Print ();
Print_functionname PF;
PF. Print ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.