Saturday, May 2, 2020

C++ Programming with Data Structures

Question: Explain a Programming by C++ with Data Structures? Answer: The Command Line #include #include #include #include #include #define MAX_WORD 32 #define MAX_TEXT_LENGTH 10000 typedef struct word { char *str; int freq; struct word *pNext; } Word; //the code is provides with the counter to calculate frequency and line numbers// int getNextWord(FILE *fp, char *buf, int bufsize); void addWord(char *pWord); void show(Word *pWordcounter); Word* createWordCounter(char *word); Word *pStart = NULL; int totalcount = 0; int uniquecount = 0; //loading of words from the file in a binary search tree struct Node{ string data; Node* left; Node* right; }; Node* GetNewNode(string data){ Node* newNode = new Node(); newNode-data=data; newNode-left = newNode-right = NULL; } Node* Insert(Node* rootPtr,string data){ if(rootPtr == NULL){ rootPtr = GetNewNode(data); return rootPtr; } else if(data= rootPtr-data){ rootPtr-left = Insert(rootPtr-left,data); } else { rootPtr-right = Insert(rootPtr-right,data); } return rootPtr; } int main() { string word; ifstream inFile; Node* rootPtr = NULL; // Pointer to the root node inFile.open("wordtracker.txt"); if (!inFile) { cout "Unable to open text file"; } while (inFile word) { rootPtr = Insert(rootPtr,word.substr(0,10)); if (word == "#") break; } //an exception arises when a word is not find in the file// try { FILE * fp; fp = fopen(wordtracker.txt","r"); // opening the file if(!file.good()) throw 56; else { while (getline(file,s)) { if (false) { return true; } } // to find the searched word in the text file// cout "Enter the word you want to search for: "; cin SearchWord; cout "nn"; cout "Enter the name of file you want to open: "; cin FileName; ifstream FileSearch(FileName); // to search and print the the line number of the word found in file while(!FileSearch.eof()) { co++; string temp; FileSearch temp; if(temp == SearchWord) { cout SearchWord " found on line: " co "nn"; else coutWord not found; break; } } //comparing all the words in the file to sort it alphabetically for (list *i = first; i != NULL; i = i-next) { cout i-s " "; } cout endl; bool ok = false; while (!ok) { ok = true; prev-next = first; for (list *i = first; i != last; i = i-next) { if (i-s.compare(i-next-s) 0) { prev-next = i-next; i-next = i-next-next; prev-next-next = i; ok = false; } prev = prev-next; } } cout "Sorted List: n"; for (list *i = first; i != NULL; i = i-next) { cout i-s " "; } cout endl; } } // to calculate the frequency of each word found in the file char buf[MAX_WORD]; int size = MAX_TEXT_LENGTH; Word *pCounter = NULL; while (getNextWord(fp, buf, size)) { addWord(buf); totalcount++; return false; } } pCounter = pStart; while(pCounter != NULL) { uniquecount++; pCounter = pCounter-pNext; } printf("nSUMMARY:nn"); printf(" %d wordsn", totalcount); /* Print total words */ printf(" %d unique wordsn", uniquecount); /* Print unique words */ pCounter = pStart; while(pCounter != NULL) { show(pCounter); pCounter = pCounter-pNext; } printf("n"); pCounter = pStart; while(pCounter != NULL) { free(pCounter-str); pStart = pCounter; pCounter = pCounter-pNext; free(pStart); } fclose(fp); catch(int e) { cout"Error number " } } return 0; } void show(Word *pWordcounter) { printf("n%-30s %5d", pWordcounter-str,pWordcounter-freq); } void addWord(char *word) { Word *pCounter = NULL; Word *pLast = NULL; if(pStart == NULL) { pStart = createWordCounter(word); return; } pCounter = pStart; while(pCounter != NULL) { if(strcmp(word, pCounter-str) == 0) { ++pCounter-freq; return; } pLast = pCounter; pCounter = pCounter-pNext; } pLast-pNext = createWordCounter(word); } Word* createWordCounter(char *word) { Word *pCounter = NULL; pCounter = (Word*)malloc(sizeof(Word)); pCounter-str = (char*)malloc(strlen(word)+1); strcpy(pCounter-str, word); pCounter-freq = 1; pCounter-pNext = NULL; return pCounter; } int getNextWord(FILE *fp, char *buf, int bufsize) { char *p = buf; char c; do { c = fgetc(fp); if (c == EOF) return 0; } while (!isalpha(c)); do { if (p - buf bufsize - 1) *p++ = tolower(c); c = fgetc(fp); } while (isalpha(c)); *p = 'à ¯Ã‚ ¿Ã‚ ½'; return 1; } Preparing a report of the file To open the file using a link list comment To load the words of the file in a binary search tree To search for a word in the file To display the line numbers of words in the file To raise an exception if the word ID not found in file To sort the words in the given file alphabetically To calculate the word frequency of each words in the file. References Gilberg, R. and Forouzan, B. (2001).Data structures. Pacific Grove, CA: Brooks/Cole. Smith, P. (2004).Applied data structures with C++. Sudbury, Mass.: Jones and Bartlett Publishers.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.