/* gg.h */
/* another file to define macros */
#ifndef GG_H
#define GG_H
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
using namespace std;
/* function declaration */
string readFile(char* filename); // deal with files in C++ methds
void startprint();
void endprint();
vector<string> findwords(const char* str); // calculate words number
void statistics(vector<string> strstr);
void numscore();
// void lastmodified(); // complete the last invoked for main function
#define LENGTH(x) (sizeof(x) / sizeof(*(x))) // the length of array...
typedef struct code{
char num[20];
int nnm;
}NUMCODE;
#endif
#include "gg.h"
#include <cstdio>
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
/* define globa variable */
NUMCODE array[]={
{ "sy_if", 0 },
{ "sy_then", 1},
{ "sy_else", 2},
{ "sy_while", 3},
{ "sy_begin", 4},
{ "sy_do", 5},
{ "sy_end", 6},
{ "a", 7},
{ "semicolon", 8},
{ "e", 9},
{ "jinghao", 10},
{ "S", 11},
{ "L", 12},
{ "tempsy", 13},
{ "EA", 18},
{ "EO", 19},
{ "#", 30},
{ "/", 31},
{ "*", 32},
{ "-", 33},
{ "+", 34},
{ "times", 36},
{ "becomes", 38},
{ "Op_and", 39},
{ "Op_or", 40},
{ "Op_not", 41},
{ "rop", 42},
{ "(", 48},
{ ")", 49},
{ "ident", 56},
{ "intconst", 57}
};
int linecount =0;
int element = 0;
void startprint(){
puts("--------------START---------------");
}
void endprint(){
puts("--------------END--------------");
}
/* a function read a file and puts its words to a container
* using C++ standard library */
string readFile(char *filename){
vector<string> words;
string word;
string strtext;
ifstream in(filename);
while(getline(in, word)){
strtext += word + '\n';
++linecount;
}
cout << strtext << endl;
return strtext;
}
/* searching for words and other print them */
vector<string> findwords(const char *str){
char *pf = const_cast<char*>(str); // type cast
char *ptemp;
int i = 0;
int j = 0; // calculate
bool isword = false;
bool redundance = false;
vector<string> strs;
vector<char> ch;
if( !isspace(*pf) )
isword = true;
while(*pf){
if(isword){
ptemp = pf;
++i; // the number of words
}
if( isalpha(*pf) || isdigit(*pf) || *pf == '_' \
&& *pf != '(' && *pf != ')' && *pf != ';' &&\
*pf != '+' && *pf != '-' && *pf != '*' && *pf != '/'){
isword = false;
j++;
}
if( isspace(*pf) || *pf == '(' || *pf ==')' || \
*pf == '[' || *pf == ']' || *pf == '+' || *pf == '-'\
|| *pf == '*'
|| *pf == '/' || *pf == '#'){ // || ispunct(*pf)){
// adds special characters
if(*pf == '(' || *pf == ')' || *pf == '#'\
|| *pf == '+' || *pf == '-' || *pf == '*'\
|| *pf == '/' || *pf == ':'){
string tempstr;
tempstr = *pf;
strs.push_back(tempstr);
redundance = true;
}
else
redundance = false;
if(redundance)
goto xxx;
ptemp[j] = '\0';
strs.push_back(ptemp);
isword = true;
j = 0;
ptemp = pf;
}
xxx: pf++;
}
return strs;
}
/* statistics the words which is in words table */
void statistics(vector<string> strstr){
int i;
int j;
bool isin = false;
cout << "\n the words can be recognized as follows" << endl;
cout << "==========================================\n";
for(i = 0; i < strstr.size(); i++){
for(j = 0; j < LENGTH(array); j++){
if( 0 == strcmp(strstr[i].data(), array[j].num)){
cout << "(\""<<strstr[i]<<"\","
<<array[j].nnm << " )" << endl;
++element;
continue;
}
else if (j ==
LENGTH(array)-1 && 0 != strcmp(strstr[i].data(), "\0")
&&\
0 != strcmp(strstr[i].data(), "+") && 0 !=
strcmp(strstr[i].data(), "-")&&\
0 != strcmp(strstr[i].data(), "*") && 0 !=
strcmp(strstr[i].data(), "/")&&\
0 != strcmp(strstr[i].data(), "(") && 0 !=
strcmp(strstr[i].data(), ")")){
cout << "(\"" << strstr[i] << "\",
" << 56 << ")"<< endl;
++element;
}
}
}
return ;
}
void numscore(){
cout << "----------------- numbers of elements ------------- "<< endl;
cout << "\tlines\t\telements" <<endl;
cout << "\t" << linecount << "\t\t" << element << endl;
}
//=====================================================
/* gg.cpp
* compiler: g++, c++
* abet standard C/C++ library
*/
#include "gg.h"
int main(char argc, char **args){
const char *filearray;
vector<string> dealstr;
string strname;
startprint();
if(argc == 1){
printf("error: please input file name.\n");
printf("dirpath: %s\n", args[argc-1]);
while(true){
cout << "Please input file name: ";
cin >> strname;
filearray = readFile((char*)strname.data()).data();
dealstr = findwords(filearray);
statistics(dealstr);
numscore();
break;
}
endprint();
system("PAUSE");
exit(0);
}else if(argc == 2){
printf("filename: %s\n", args[argc-1]);
puts("---text content----");
filearray = readFile(args[argc-1]).data();
endprint();
dealstr = findwords(filearray);
statistics(dealstr); numscore();
}
system("PAUSE");
}
// 里面的算法感觉都非常的笨,可是又想不出来好的,好的东西从哪里来...