#include #include #include #include #include using namespace std; struct node{ char word[20]; struct node *next; struct node *prev; int linenum; //line the word is on int wordnum; //count of occurances }; typedef struct node *WORDPTR; struct node2{//tree char word[20]; WORDPTR wordlist;//pointer to word list int wordnum;//number of occurancce struct node2 *left; struct node2 *right; }; typedef struct node2 *TREEPTR; WORDPTR getnode(void){ WORDPTR tmpword; tmpword = (WORDPTR)malloc(sizeof(struct node)); tmpword->next = NULL; return(tmpword); } TREEPTR gettreenode(){ TREEPTR temptree; temptree = (TREEPTR)malloc(sizeof(struct node2)); return(temptree); } TREEPTR insert(TREEPTR,char[]); TREEPTR maketree(char[]); TREEPTR insertree(TREEPTR,char[]); void printaword(WORDPTR); int makeaword(); void makealine(); void storealine(); void printTree(TREEPTR); void insafter(WORDPTR,char[]); int i; char word[20]; int linenumber=1; int linepos=0; FILE * pfile = fopen ("txtinput","r");//the text file char line[1000];//store a whole line in this TREEPTR tree; int main() { storealine(); storealine(); printTree(tree); system("pause"); free(tree); return(0); } void makealine() //gets a line { char temper; i=0; fscanf (pfile,"%c",&temper); //scan first character do { line[i] = temper;//copy temp cahr into line i++;//move along line line[i] = '\0'; fscanf (pfile,"%c",&temper); }while((temper!='\n'));//while it's not the new line character , keep goin } void storealine() { makealine(); //get initial line int flag = makeaword(); //get a word from it tree = insert(tree,word);//add it to tree flag = 0;//reset flag do { flag = makeaword();//get a word if (flag == 1)//if it is a word, add it to tree { //printf("%s ",word); tree=insert(tree,word);//insert it to tree } if(linepos+2 > strlen(line)) { linepos=0;// if it's at the end of the line, reset the line, increment linenumber++;}//line number }while(linepos !=0); printf("\n"); } int makeaword(){ char tmp[20]; int k =0,l=0; int flag = 0; while (line[linepos] != '\0' && line[linepos] != ' ' && line[linepos] != '.' && line[linepos]!=',' && line[linepos]!='EOF') { word[k]=tolower(line[linepos]); //while it's not end of the word, a space,a period, a comma, copy the character word[k+1] = '\0'; k++, linepos++; flag = 1; }; if (line[linepos] == '.' || line[linepos] == ' ' || line[linepos]==',') linepos++;//advance if it is one of those characters // if it's at the end go to next line to be inserted return(flag); } void printTree(TREEPTR treep) { if (treep == NULL) return;//print tree inorder printTree(treep->left); printf("%s",*(treep->wordlist->word); printTree(treep->right); } void insafter(WORDPTR tempword,char x[]) { WORDPTR temp ; temp = getnode(); if (tempword == NULL){ printf("void insertion"); exit(1); } for (i = 0; i<=strlen(x); i++) temp->word[i] =(x[i]); temp->linenum = linenumber; temp->wordnum = linepos-(strlen(x));//copy over relevant information temp->next = tempword; tempword= temp; } void printaword(WORDPTR get) { printf("%s %d %d \n",get->word,get->linenum,get->wordnum); } TREEPTR maketree(char wd[])//make a treenode { TREEPTR tmptree; tmptree = gettreenode(); for (i = 0; i<=strlen(wd); i++) tmptree->word[i] =(wd[i]);//copy over word tmptree->left = NULL; tmptree->right = NULL; if (tmptree->wordlist == NULL) {//if the wordlist for the point hasn't been tmptree->wordlist=getnode();//initialed do so } else insafter(tmptree->wordlist,wd);//else, insert it return(tmptree); } TREEPTR insert(TREEPTR tp, char wd[]) { if (tp == NULL) { return(maketree(wd)); } if ((strcmp(wd,tp->word)==0)){ insafter(tp->wordlist,wd); return(tp);} else { if ((strcmp(wd,tp->word)<0)) //recursively go through tree to find the right tp->left = insert(tp->left,wd);//position else tp->right = insert(tp->right,wd); return(tp); } }