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 { scorch *str; int freq; struct word *pNext; } Word; /the code is gives the counter to figure recurrence and line numbers// int getNextWord(FILE *fp, scorch *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 document in a twofold hunt 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 principle() { 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 document; } while (inFile word) { rootPtr = Insert(rootPtr,word.substr(0,10)); on the off chance that (word == #) break; } /an exemption emerges when a word isn't find in the record// attempt { Record * fp; fp = fopen(wordtracker.txt,r);/opening the record if(!file.good()) toss 56; else { while (getline(file,s)) { assuming (bogus) { bring valid back; } } /to discover the looked through word in the content record// cout Enter the word you need to scan for: ; cin SearchWord; cout nn; cout Enter the name of record you need to open: ; cin FileName; ifstream FileSearch(FileName); /to look and print the line number of the word found in record 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) { in the event 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 figure the recurrence of each word found in the document scorch 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 remarkable wordsn, uniquecount);/* Print exceptional 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 document in a parallel inquiry tree To look for a word in the document To show the line quantities of words in the document To raise a special case if the word ID not found in document To sort the words in the given document one after another in order To compute the word recurrence of every word in the document. References Gilberg, R. what's more, 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.