#ifndef ROUTINES_H #define ROUTINES_H #include #include #include #include "basic.h" #include "global.h" /*****************************************************************************/ /* init_list_reserved_words */ /*****************************************************************************/ struct List * init_list_reserved_words(char *reserved_words_parameter_file) { FILE *fp; int input; struct List *reserved_words=(struct List *)malloc(sizeof(struct List)); char buffer[10]; int i,index; initializeList(reserved_words); if((fp=fopen(reserved_words_parameter_file,"r"))==NULL) { printf("Please check the directory and file name of the reserved words file!\n"); /* the reason to use a file to store the reserved words is to provide better compatibility for different c compilers and future extension */ exit(0); } for(i=0;i<10;i++) buffer[i]='\0'; index=0;/* the position of the current character in the buffer */ while((input=fgetc(fp))!=EOF) { if((input!=' ')&&(input!='\n')) { buffer[index]=(unsigned char)input; index++; } else { insertItem(reserved_words,buffer,index,reserved_words->count); for(i=0;i<10;i++) buffer[i]='\0'; index=0; } } fclose(fp); return reserved_words; } /*****************************************************************************/ /* init_list_operators */ /*****************************************************************************/ struct List * init_list_operators(char *operators_parameter_file) { FILE *fp; int input; struct List *operators=(struct List *)malloc(sizeof(struct List)); char buffer[10]; int i,index; initializeList(operators); if((fp=fopen(operators_parameter_file,"r"))==NULL) { printf("Please check the directory and file name of the operators file!\n"); /* the reason to use a file to store the operators is to provide better compatibility for different c compilers and future extension */ exit(0); } for(i=0;i<10;i++) buffer[i]='\0'; index=0;/* the position of the current character in the buffer */ while((input=fgetc(fp))!=EOF) { if((input!=' ')&&(input!='\n')) { buffer[index]=(unsigned char)input; index++; } else { insertItem(operators,buffer,index,operators->count); for(i=0;i<10;i++) buffer[i]='\0'; index=0; } } fclose(fp); return operators; } /*****************************************************************************/ /* init_list_punctuations */ /*****************************************************************************/ struct List * init_list_punctuations(char *punctuations_parameter_file) { FILE *fp; int input; struct List *punctuations=(struct List *)malloc(sizeof(struct List)); char buffer[10]; int i,index; initializeList(punctuations); if((fp=fopen(punctuations_parameter_file,"r"))==NULL) { printf("Please check the directory and file name of the operators file!\n"); /* the reason to use a file to store the operators is to provide better compatibility for different c compilers and future extension */ exit(0); } for(i=0;i<10;i++) buffer[i]='\0'; index=0;/* the position of the current character in the buffer */ while((input=fgetc(fp))!=EOF) { if((input!=' ')&&(input!='\n')) { buffer[index]=(unsigned char)input; index++; } else { insertItem(punctuations,buffer,index,punctuations->count); for(i=0;i<10;i++) buffer[i]='\0'; index=0; } } fclose(fp); return punctuations; } /****************************************************************************/ /* init_white_space */ /****************************************************************************/ struct List * init_white_space( ) { struct List *white_space=(struct List *)malloc(sizeof(struct List)); char buffer[2]; int i,index; initializeList(white_space); for(i=0;i<2;i++) buffer[i]='\0'; buffer[0]=' '; insertItem(white_space,buffer,2,white_space->count); return white_space; } /*****************************************************************************/ /* update_strings */ /*****************************************************************************/ void update_strings() { counter_string++; return; } /*****************************************************************************/ /* update_characters */ /*****************************************************************************/ void update_characters() { counter_char++; return; } /*****************************************************************************/ /* search_reserved_words */ /*****************************************************************************/ int search_reserved_words(struct List *reserved_words,char *buffer, int *idx) { struct Node *Item; int i; for(i=0;icount;i++) { Item=setPosition(reserved_words,i); if(strcmp(Item->words,buffer)==0) { *idx=i; return ON; } } return OFF; } /*****************************************************************************/ /* update_reserved_words */ /*****************************************************************************/ void update_reserved_words(struct List *reserved_words,int idx) { struct Node * Item; if(idx>reserved_words->count) { printf("something is wrong with the index for reserved_words\n"); exit(0); } Item=setPosition(reserved_words,idx); Item->count++; return; } /*****************************************************************************/ /* search_operators */ /*****************************************************************************/ int search_operators(struct List *operators,char *buffer, int *idx) { struct Node *Item; int i; for(i=0;icount;i++) { Item=setPosition(operators,i); if(strcmp(Item->words,buffer)==0) { *idx=i; return ON; } } return OFF; } /*****************************************************************************/ /* update_operators */ /*****************************************************************************/ void update_operators(struct List *operators,int idx) { struct Node * Item; if(idx>operators->count) { printf("something is wrong with the index for operators\n"); exit(0); } Item=setPosition(operators,idx); Item->count++; return; } /*****************************************************************************/ /* search_punctuations */ /*****************************************************************************/ int search_punctuations(struct List *punctuations,int value, int *idx) { struct Node *Item; int i; for(i=0;icount;i++) { Item=setPosition(punctuations,i); if(Item->words[0]==value) { *idx=i; return ON; } } return OFF; } /*****************************************************************************/ /* update_punctuations */ /*****************************************************************************/ void update_punctuations(struct List *punctuations,int idx) { struct Node * Item; if(idx>punctuations->count) { printf("something is wrong with the index for punctuations\n"); exit(0); } Item=setPosition(punctuations,idx); Item->count++; return; } /*****************************************************************************/ /* update_identifiers */ /*****************************************************************************/ void update_identifiers() { counter_identifier++; return; } /*****************************************************************************/ /* update_numbers */ /*****************************************************************************/ void update_numbers() { counter_number++; return; } /*****************************************************************************/ /* search_white_space */ /*****************************************************************************/ int search_white_space(struct List *white_space, int input, int *idx) { struct Node *Item; int i; for(i=0;icount;i++) { Item=setPosition(white_space,i); if(Item->words[0]==input) { *idx=i; return ON; } } return OFF; } /*****************************************************************************/ /* update_white_space */ /*****************************************************************************/ void update_white_space(struct List *white_space, int n) { struct Node * Item; if(n>white_space->count) { printf("something is wrong with the index for white space\n"); exit(0); } Item=setPosition(white_space,n); Item->count++; return; } /*****************************************************************************/ /* update_comments */ /*****************************************************************************/ void update_comments() { counter_comment++; return; } /*****************************************************************************/ /* end_list_reserved_words */ /*****************************************************************************/ void end_list_reserved_words(struct List *reserved_words) { while(reserved_words->count>0) deleteNode(reserved_words,reserved_words->count-1); free(reserved_words); } /*****************************************************************************/ /* end_list_operators */ /*****************************************************************************/ void end_list_operators(struct List *operators) { while(operators->count>0) deleteNode(operators,operators->count-1); free(operators); } /*****************************************************************************/ /* end_list_punctuations */ /*****************************************************************************/ void end_list_punctuations(struct List *punctuations) { while(punctuations->count>0) deleteNode(punctuations,punctuations->count-1); free(punctuations); } /****************************************************************************/ /* end_white_space */ /****************************************************************************/ void end_white_space(struct List *white_space ) { while(white_space->count>0) deleteNode(white_space,white_space->count-1); free(white_space); } /*****************************************************************************/ /* calculate_and_print_results */ /*****************************************************************************/ void calculate_and_print_results() { int summation=0,i,is_reserved_word,idx; int counter_reserved_words,counter_operators,counter_punctuations,counter_white_space; struct Node *tmp; float frequency; /* update relative counters if necessary */ if(position!=0) { if(flag_number==ON) { flag_number=OFF; update_numbers(); } else if(flag_letter==ON) { is_reserved_word=search_reserved_words(reserved_words,buffer,&idx); if(is_reserved_word==ON) update_reserved_words(reserved_words,idx); else update_identifiers(); flag_letter=OFF; } } /* first let's calculate the summation of all the symbols in the file */ summation+=counter_string+counter_char+counter_identifier+counter_number+counter_comment; tmp=reserved_words->headPtr; for(i=0;icount;i++) { summation+=tmp->count; tmp=tmp->nextPtr; } tmp=operators->headPtr; for(i=0;icount;i++) { summation+=tmp->count; tmp=tmp->nextPtr; } tmp=punctuations->headPtr; for(i=0;icount;i++) { summation+=tmp->count; tmp=tmp->nextPtr; } tmp=white_space->headPtr; for(i=0;icount;i++) { summation+=tmp->count; tmp=tmp->nextPtr; } printf("the total number of symbols is:%d\n",summation); printf("the estimated probability of strings is:%f\n",(float)counter_string/summation); printf("the estimated probability of characters is:%f\n",(float)counter_char/summation); printf("the estimated probability of identifiers is:%f\n",(float)counter_identifier/summation); printf("the estimated probability of numbers is:%f\n",(float)counter_number/summation); /* printf("the estimated probability of white spaces are:%f\n",counter_white_space/summation);*/ /* printf("the estimated probability of comment is:%f\n",(float)counter_comment/summation);*/ tmp=reserved_words->headPtr; for(i=0;icount;i++) { printf("the estimated probablity of %s is:%f\n",tmp->words,(float)tmp->count/summation); tmp=tmp->nextPtr; } tmp=operators->headPtr; for(i=0;icount;i++) { printf("the estimated probablity of %s is:%f\n",tmp->words,(float)tmp->count/summation); tmp=tmp->nextPtr; } tmp=punctuations->headPtr; for(i=0;icount;i++) { printf("the estimated probablity of %s is:%f\n",tmp->words,(float)tmp->count/summation); tmp=tmp->nextPtr; } tmp=white_space->headPtr; for(i=0;icount;i++) { printf("the estimated probablity of %s(white space) is:%f\n",tmp->words,(float)tmp->count/summation); tmp=tmp->nextPtr; } } int is_operator_char(int input) /* this function is to judge whether the input is a operator or not */ { if((input==33)||(input==35)) return(1); else if((input==37)||(input==38)) return(1); else if((input==42)||(input==43)) return(1); else if((input==45)||(input==47)) return(1); else if((input>=60)&&(input<=63)) return(1); if(input==126) return(1); else return(0); } #endif