char * searchCode(char * word, WordCode ** array, int start, int end) { if (strcmp(word, "\n") == 0) { return "\n"; } char * code; int mid = (start + end) / 2; char * mid_word = array[mid]->word; if (start > end) { //printf("couldn't find word\n"); return "error"; } //found the word if (strcmp(word, mid_word) == 0) { code = array[mid]->code; } //go left else if (strcmp(word, mid_word) < 0) { code = searchCode(word, array, start, mid-1); } //go right else if (strcmp(word, mid_word) > 0) { code = searchCode(word, array, mid+1, end); } return code; }
void Functionality :: getTimeOrDate(string tempCode, string &tempString, int getFlag) { int vectorPosition=0; EventStorage tempEvent; int checkCode=searchCode(tempEvent, tempCode, vectorPosition); if(getFlag==0) tempString = tempEvent.getDate(); else tempString = tempEvent.getTime(); }
void doRules(char **codes, char *name) { int count,i, soundCode, prevSoundCode = -1; for(i = 0, count = 0; count < LENGTH && name[i] != '\0'; i++) { soundCode = searchCode(codes, name, i, 10, prevSoundCode); if (soundCode != prevSoundCode && soundCode >= 0 ) { if (count == 0) { putchar(name[i]); count++; } else { switch (soundCode) { case 1: printf("1"); break; case 2: printf("2"); break; case 3: printf("3"); break; case 4: printf("4"); break; case 5: printf("5"); break; case 6: printf("6"); break; default: break; } if (soundCode > 0) { count++; } } prevSoundCode = soundCode; } } if (count < 4) { extraChar(count); } pNL(); }
int searchCode(char **codes, char *name, int letter,int size, int prevSoundCode) { int row, col = 1; for (row = 0; row < size; row++) { for (col = 0; codes[row][col] != '\0'; col++) { if (toupper(name[letter]) == codes[row][col]) { if (row != prevSoundCode) { if (toupper(name[letter - 1]) == 'H' || toupper(name[letter - 1] == 'W')) { return(searchCode(codes,name,letter-2,size,row)); } return row; } else { return -1; } } } } return -1; }
void Functionality::removeTask(string tempCode) { unsigned i; int vectorPosition=0; EventStorage tempEvent; int checkCode=searchCode(tempEvent, tempCode, vectorPosition); if(checkCode==1) { string tempString=tempEvent.getPriority(); if(tempString[0]=='1') { for(i=vectorPosition;i<highPriority.size()-1;i++) { highPriority[i]=highPriority[i+1]; } highPriority.pop_back(); undoHighVector.push_back(highPriority); undoCount.push_back(0); } else if(tempString[0]=='2') { for(i=vectorPosition;i<normalPriority.size()-1;i++) { normalPriority[i]=normalPriority[i+1]; } normalPriority.pop_back(); undoNormalVector.push_back(normalPriority); undoCount.push_back(1); } redoCount.clear(); redoHighVector.clear(); redoNormalVector.clear(); callSort(); updateFile(); notificationFile(); } }
void Functionality::editTask(vector<string> userInput, string eventCode) { EventStorage tempEvent; int tempPosition = 0; int editCheck = 0; int priorityCheck=0; int tempCheck = 0; int checkCode = searchCode(tempEvent, eventCode, tempPosition); if(checkCode==0) cout<<"\nThe event code that you have entered is invalid\n"; else { if(userInput[0].size()!=0) tempEvent.writeTitle(userInput[0]); else editCheck++; if(userInput[1].size()!=0) tempEvent.writeDate(userInput[1]); else editCheck++; if(userInput[2].size()!=0) tempEvent.writeTime(userInput[2]); else editCheck++; if(userInput[3].size()!=0) { priorityCheck=1; if(tempEvent.getPriority()!=userInput[3]) { tempEvent.writePriority(userInput[3]); tempCheck = 1; } removeTask(tempEvent.getCode()); if(tempEvent.getPriority()=="1") { highPriority.push_back(tempEvent); if(tempCheck == 1) { undoNormalVector.pop_back(); undoCount.pop_back(); } else { undoHighVector.pop_back(); undoCount.pop_back(); } } else { normalPriority.push_back(tempEvent); if(tempCheck==1) { undoHighVector.pop_back(); undoCount.pop_back(); } else { undoNormalVector.pop_back(); undoCount.pop_back(); } } } else editCheck++; if(priorityCheck == 0) { removeTask(tempEvent.getCode()); if(tempEvent.getPriority()=="1") { undoHighVector.pop_back(); undoCount.pop_back(); highPriority.push_back(tempEvent); undoHighVector.push_back(highPriority); undoCount.push_back(0); } else if(tempEvent.getPriority()=="2") { undoNormalVector.pop_back(); undoCount.pop_back(); normalPriority.push_back(tempEvent); undoNormalVector.push_back(normalPriority); undoCount.push_back(1); } } else { undoHighVector.push_back(highPriority); undoNormalVector.push_back(normalPriority); undoCount.push_back(2); } if(editCheck==4) { cout<<"\nInvalid input since no event detail has been mentioned\n"; Sleep(700); } else { cout<<"\nEvent successfully edited\n"; Sleep(700); } } redoCount.clear(); redoHighVector.clear(); redoNormalVector.clear(); callSort(); updateFile(); notificationFile(); }
void outputHuffmanFile(WordData * huffman_tree, char * reviews_path, int word_count) { int i; WordCode ** code_array = createCodeArray(huffman_tree, word_count); /*printf("testing the wordCode array: contains %d words\n", num_words); for (i = 0; i < num_words; i++) { printf("%d) word: (%s) code: %s\n", i, code_array[i]->word, code_array[i]->code); }*/ //printTree(huffman_tree); //using the array of WordCodes output to a file FILE * reviews_stream = fopen(reviews_path, "r"); FILE * output = fopen("huffed.txt", "w"); int word_buffer_position = 0; int max_word_size = 50; char * word_buffer = malloc(max_word_size * sizeof(char)); char * output_code; while(!feof(reviews_stream)) { int current = fgetc(reviews_stream); //Test: is current a ' ', '\t' or '\n' (word separators): insert the buffer, then insert the current character if (current == ' ' || current == '\t' || current == '\n' || current == '.' || current == ',' || current == '(' || current == ')') { //insert word up untill current character unless the last word was a word separator if (word_buffer[0] != ' ' && word_buffer[0] != '\t' && word_buffer[0] != '\n' && word_buffer[0] != '.' && word_buffer[0] != ',' && word_buffer[0] != '(' && word_buffer[0] != ')') { output_code = searchCode(word_buffer, code_array, 0, word_count-1); fputs(output_code, output); //printf("code written: (%s)\n", s_output_code); } //reset the buffer position, add current character to the buffer, and insert word word_buffer_position = 0; word_buffer[word_buffer_position] = (char)current; word_buffer[word_buffer_position+1] = '\0'; output_code = searchCode(word_buffer, code_array, 0, word_count-1); fputs(output_code, output); //printf("code written: (%s)\n", s_output_code); } else { //ensure the word buffer can handle it if (word_buffer_position >= max_word_size-1) { //printf("had to realloc\n"); max_word_size *= 2; word_buffer = realloc(word_buffer, max_word_size * sizeof(char)); } word_buffer[word_buffer_position] = (char)current; word_buffer[word_buffer_position+1] = '\0'; word_buffer_position++; } i++; } free(word_buffer); fclose(reviews_stream); fclose(output); freeCodeArray(code_array, word_count); return; }