Friday, August 21, 2020

C++ Programming with Data Structures

Question: Clarify 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 { burn *str; int freq; struct word *pNext; } Word; /the code is furnishes with the counter to ascertain recurrence and line numbers// int getNextWord(FILE *fp, burn *buf, int bufsize); void addWord(char *pWord); void show(Word *pWordcounter); Word* createWordCounter(char *word); Word *pStart = NULL; int totalcount = 0; int uniquecount = 0; /stacking of words from the record in a double pursuit tree struct Node{ string information; 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 primary() { string word; ifstream inFile; Node* rootPtr = NULL;/Pointer to the root hub inFile.open(wordtracker.txt); on the off chance that (!inFile) { cout Incapable to open content record; } while (inFile word) { rootPtr = Insert(rootPtr,word.substr(0,10)); in the event that (word == #) break; } /a special case emerges when a word isn't find in the document// attempt { Document * fp; fp = fopen(wordtracker.txt,r);/opening the document if(!file.good()) toss 56; else { while (getline(file,s)) { assuming (bogus) { bring valid back; } } /to discover the looked through word in the content document// cout Enter the word you need to look for: ; cin SearchWord; cout nn; cout Enter the name of document you need to open: ; cin FileName; ifstream FileSearch(FileName); /to look and print the line number of the word found in document while(!FileSearch.eof()) { co++; string temp; FileSearch temp; if(temp == SearchWord) { cout SearchWord found on line: co nn; else coutWord not found; break; } } /looking at all the words in the document to sort it one after another in order for (list *i = first; I != NULL; I = I-next) { cout I-s ; } cout endl; bool alright = bogus; while (!alright) { alright = valid; prev-next = first; for (list *i = first; I != last; I = I-next) { on the off chance that (I-s.compare(i-next-s) 0) { prev-next = I-next; I-next = I-next-next; prev-next-next = I; alright = bogus; } prev = prev-next; } cout Arranged List: n; for (list *i = first; I != NULL; I = I-next) { cout I-s ; } cout endl; } } /to compute the recurrence of each word found in the document burn buf[MAX_WORD]; int size = MAX_TEXT_LENGTH; Word *pCounter = NULL; while (getNextWord(fp, buf, size)) { addWord(buf); totalcount++; return bogus; } } pCounter = pStart; while(pCounter != NULL) { uniquecount++; pCounter = pCounter-pNext; } printf(nSUMMARY:nn); printf( %d wordsn, totalcount);/* Print all out words */ printf( %d interesting wordsn, uniquecount);/* Print one of a kind 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) { coutError number } } bring 0 back; } 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, roast *buf, int bufsize) { roast *p = buf; roast c; do { c = fgetc(fp); on the off chance that (c == EOF) bring 0 back; } while (!isalpha(c)); do { on the off chance that (p - buf bufsize - 1) *p++ = tolower(c); c = fgetc(fp); } while (isalpha(c)); *p = 'à ¯Ã¢ ¿Ã¢ ½'; bring 1 back; } Setting up a report of the record To open the record utilizing a connection list remark To stack the expressions of the record in a paired hunt tree To scan for a word in the record To show the line quantities of words in the record To raise an exemption if the word ID not found in record To sort the words in the given record in order To ascertain the word recurrence of every word in the document. References Gilberg, R. furthermore, Forouzan, B. (2001).Data structures. Pacific Grove, CA: Brooks/Cole. Smith, P. (2004).Applied information 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.