#include <stdio.h> #include <string.h> #include <stdlib.h> //CONSTANTS #define wrdlen [48] #define linelen [1024] // A struct representing a node in a binary search tree struct BSTnode { char word[wrdlen];// The contents of the node struct BSTnode* left;// Links to the node's left and right children struct BSTnode* right; }; // Adds a new node to the tree. Duplicates are disallowed. Returns 1 if a // new node was added, returns 0 if newdata was already in the tree int insert(struct BSTnode** root, char newword[wrdlen]) { // If we've reached the right place to insert, create a new node and add it in if( (*root) == NULL) { (*root) = (struct BSTnode*)malloc(sizeof(struct BSTnode)); strcpy((*root)->word,newword); (*root)->left = NULL; (*root)->right = NULL; return 1; } // Otherwise, search for the correct place to insert if(strcmp(newword,(*root)->word)<0) { return insert( &((*root)->left), newword); } else if(strcmp(newword,(*root)->word)>0) { return insert( &((*root)->right), newword); } // If the new data is neither less than nor greater than the the data at // the current node, it must be equal, and duplicates are not allowed else return 0; } // Returns 1 if target is in the tree and 0 otherwise int search(struct BSTnode* root, char target[wrdlen]) { // An empty tree contains nothing, much less target if(root == NULL) return 0; // If the current node is what we're looking for, we've found it if(strcmp(root->word,target) == 0) return 1; // If what we're looking for is smaller than this node, it must be in // the left subtree if it exists if(strcmp(target,root->word) < 0) return search(root->left, target); // Similarly, if the target is greater than this node, it can only be in // the right subtree else return search(root->right, target); } // An iterative version of the search algorithm int searchiterative(struct BSTnode* root, char target[wrdlen]) { struct BSTnode* crnt = root; // Keep descending through the tree until we reach the bottom or find what // we're looking for while(crnt != NULL && strcmp(crnt->word,target)!=0) { if(strcmp(target,crnt->word)>0) crnt = crnt->left; else crnt = crnt->right; } // If we reached the bottom of the tree, then the target isn't present, // otherwise we found what we're looking for if(crnt == NULL) return 0; else return 1; } void spellcheck(struct BSTnode* root, char *token, int line) { //capital letters are from 65 -> 90 //lowercase letters are from 97-122 if(search(root, token))//if you find it normally then return; else if(token[0] >= 65 && token[0] <= 90) { token[0] = token[0] + 32; if(search(root, token)) return; else { token[0] = token[0] - 32; printf("Line %d: %s\n", line, token); return; } } else { printf("Line %d: %s\n", line, token); return; } } int main(void) { FILE *ifp; //dictionary file FILE *scfp; //file to be spellchecked char infile[wrdlen]; //"words.txt"; char Tinfile[wrdlen]; //"test.txt"; //test file int valid = 0; struct BSTnode* root; char newword[wrdlen]; root = NULL; int i = 0; char str[linelen]; char delims[] = ("~!@#$%^&*()-_=+[]{}\\|;:\'\",.<>/?\n\r\t "); //ask the user for the dictionary file while(!valid) { printf("Please enter the name of the dictionary file you wish to access\n"); scanf("%s", infile); ifp = fopen(infile, "r"); if(ifp == NULL) printf("sorry, could not find that file!\n"); else { valid = 1; printf("Reading file now.........\n"); } } valid = 0; //read in all the words and place them into a BST while(!feof(ifp)) { fscanf(ifp, "%s ", newword); insert(&root, newword); } //ask the user for the file to be spellchecked while(!valid) { printf("what is the name of the file you would like to spellcheck?\n"); scanf("%s", Tinfile); scfp = fopen(Tinfile, "r"); if(scfp == NULL) printf("sorry, could not find that file!\n"); else { valid = 1; printf("Reading file now.........\n"); } } printf("The following words were not recognized: \n"); for(i = 1; !feof(scfp); i++) { fgets(str, linelen, scfp); char *token; token = strtok(str, delims); while( token != NULL ) { spellcheck(root, token, i); token = strtok( NULL, delims ); } } system("PAUSE"); return; }
#include <stdio.h> #include <string.h> #include <stdlib.h> //CONSTANTS #define wrdlen 48 #define linelen 1024 // A struct representing a node in a binary search tree struct BSTnode { char word[wrdlen];// The contents of the node struct BSTnode* left;// Links to the node's left and right children struct BSTnode* right; }; // Adds a new node to the tree. Duplicates are disallowed. Returns 1 if a // new node was added, returns 0 if newdata was already in the tree int insert(struct BSTnode** root, char newword[wrdlen]) { // If we've reached the right place to insert, create a new node and add it in if( (*root) == NULL) { (*root) = (struct BSTnode*)malloc(sizeof(struct BSTnode)); strcpy((*root)->word,newword); (*root)->left = NULL; (*root)->right = NULL; return 1; } // Otherwise, search for the correct place to insert if(strcmp(newword,(*root)->word)<0) { return insert( &((*root)->left), newword); } else if(strcmp(newword,(*root)->word)>0) { return insert( &((*root)->right), newword); } // If the new data is neither less than nor greater than the the data at // the current node, it must be equal, and duplicates are not allowed else return 0; } // Returns 1 if target is in the tree and 0 otherwise int search(struct BSTnode* root, char target[wrdlen]) { // An empty tree contains nothing, much less target if(root == NULL) return 0; // If the current node is what we're looking for, we've found it if(strcmp(root->word,target) == 0) return 1; // If what we're looking for is smaller than this node, it must be in // the left subtree if it exists if(strcmp(target,root->word) < 0) return search(root->left, target); // Similarly, if the target is greater than this node, it can only be in // the right subtree else return search(root->right, target); } // An iterative version of the search algorithm int searchiterative(struct BSTnode* root, char target[wrdlen]) { struct BSTnode* crnt = root; // Keep descending through the tree until we reach the bottom or find what // we're looking for while(crnt != NULL && strcmp(crnt->word,target)!=0) { if(strcmp(target,crnt->word)>0) crnt = crnt->left; else crnt = crnt->right; } // If we reached the bottom of the tree, then the target isn't present, // otherwise we found what we're looking for if(crnt == NULL) return 0; else return 1; } void spellcheck(struct BSTnode* root, char *token, int line) { //capital letters are from 65 -> 90 //lowercase letters are from 97-122 if(search(root, token))//if you find it normally then return; else if(token[0] >= 65 && token[0] <= 90) { token[0] = token[0] + 32; if(search(root, token)) return; else { token[0] = token[0] - 32; printf("Line %d: %s\n", line, token); return; } } else { printf("Line %d: %s\n", line, token); return; } } int main(void) { FILE *ifp; //dictionary file FILE *scfp; //file to be spellchecked char infile[wrdlen]; //"words.txt"; char Tinfile[wrdlen]; //"test.txt"; //test file int valid = 0; struct BSTnode* root; char newword[wrdlen]; int i = 0; char str[linelen]; char delims[] = {"~!@#$%^&*()-_=+[]{}\\|;:\'\",.<>/?\n\r\t ""}; char *token; //ask the user for the dictionary file while(!valid) { printf("Please enter the name of the dictionary file you wish to access\n"); scanf("%s", infile); ifp = fopen(infile, "r"); if(ifp == NULL) printf("sorry, could not find that file!\n"); else { valid = 1; printf("Reading file now.........\n"); } } valid = 0; //read in all the words and place them into a BST while(!feof(ifp)) { fscanf(ifp, "%s ", newword); insert(&root, newword); } //ask the user for the file to be spellchecked while(!valid) { printf("what is the name of the file you would like to spellcheck?\n"); scanf("%s", Tinfile); scfp = fopen(Tinfile, "r"); if(scfp == NULL) printf("sorry, could not find that file!\n"); else { valid = 1; printf("Reading file now.........\n"); } } printf("The following words were not recognized: \n"); for(i = 1; !feof(scfp); i++) { fgets(str, linelen, scfp); token = strtok(str, delims); while( token != NULL ) { spellcheck(root, token, i); token = strtok( NULL, delims ); } } system("PAUSE"); return 0; }
#include <stdio.h> #include <string.h> #include <stdlib.h> //CONSTANTS #define wrdlen 48 #define linelen 1024 // A struct representing a node in a binary search tree struct BSTnode { char word[wrdlen];// The contents of the node struct BSTnode* left;// Links to the node's left and right children struct BSTnode* right; }; // Adds a new node to the tree. Duplicates are disallowed. Returns 1 if a // new node was added, returns 0 if newdata was already in the tree int insert(struct BSTnode** root, char newword[wrdlen]) { // If we've reached the right place to insert, create a new node and add it in if( (*root) == NULL) { (*root) = (struct BSTnode*)malloc(sizeof(struct BSTnode)); strcpy((*root)->word,newword); (*root)->left = NULL; (*root)->right = NULL; return 1; } // Otherwise, search for the correct place to insert if(strcmp(newword,(*root)->word)<0) { return insert( &((*root)->left), newword); } else if(strcmp(newword,(*root)->word)>0) { return insert( &((*root)->right), newword); } // If the new data is neither less than nor greater than the the data at // the current node, it must be equal, and duplicates are not allowed else return 0; } // Returns 1 if target is in the tree and 0 otherwise int search(struct BSTnode* root, char target[wrdlen]) { // An empty tree contains nothing, much less target if(root == NULL) return 0; // If the current node is what we're looking for, we've found it if(strcmp(root->word,target) == 0) return 1; // If what we're looking for is smaller than this node, it must be in // the left subtree if it exists if(strcmp(target,root->word) < 0) return search(root->left, target); // Similarly, if the target is greater than this node, it can only be in // the right subtree else return search(root->right, target); } // An iterative version of the search algorithm int searchiterative(struct BSTnode* root, char target[wrdlen]) { struct BSTnode* crnt = root; // Keep descending through the tree until we reach the bottom or find what // we're looking for while(crnt != NULL && strcmp(crnt->word,target)!=0) { if(strcmp(target,crnt->word)>0) crnt = crnt->left; else crnt = crnt->right; } // If we reached the bottom of the tree, then the target isn't present, // otherwise we found what we're looking for if(crnt == NULL) return 0; else return 1; } void spellcheck(struct BSTnode* root, char *token, int line) { //capital letters are from 65 -> 90 //lowercase letters are from 97-122 if(search(root, token))//if you find it normally then return; else if(token[0] >= 65 && token[0] <= 90) { token[0] = token[0] + 32; if(search(root, token)) return; else { token[0] = token[0] - 32; printf("Line %d: %s\n", line, token); return; } } else { printf("Line %d: %s\n", line, token); return; } } int main(void) { FILE *ifp; //dictionary file FILE *scfp; //file to be spellchecked char infile[wrdlen]; //"words.txt"; char Tinfile[wrdlen]; //"test.txt"; //test file int valid = 0; struct BSTnode* root; char newword[wrdlen]; int i = 0; char str[linelen]; char delims[] = {"~!@#$%^&*()-_=+[]{}\\|;:\'\",.<>/?\n\r\t "}; char *token; //ask the user for the dictionary file while(!valid) { printf("Please enter the name of the dictionary file you wish to access\n"); scanf("%s", infile); ifp = fopen(infile, "r"); if(ifp == NULL) printf("sorry, could not find that file!\n"); else { valid = 1; printf("Reading file now.........\n"); } } valid = 0; //read in all the words and place them into a BST while(!feof(ifp)) { fscanf(ifp, "%s ", newword); insert(&root, newword); } //ask the user for the file to be spellchecked while(!valid) { printf("what is the name of the file you would like to spellcheck?\n"); scanf("%s", Tinfile); scfp = fopen(Tinfile, "r"); if(scfp == NULL) printf("sorry, could not find that file!\n"); else { valid = 1; printf("Reading file now.........\n"); } } printf("The following words were not recognized: \n"); for(i = 1; !feof(scfp); i++) { fgets(str, linelen, scfp); token = strtok(str, delims); while( token != NULL ) { spellcheck(root, token, i); token = strtok( NULL, delims ); } } system("PAUSE"); return 0; }
| DaniWeb Message | |
| Cancel Changes | |