void Count::processWord(std::string& word) { std::string temp; temp = word; int flag; for (unsigned int i=0;i<word.length();i++) { flag = 0; if(!isalnum(word[i])) { flag = 1; if (i==0) { word = word.erase(0, 1); processWord(word); break; } else if (i==word.length()-1) { word = word.erase(i, std::string::npos); result[word]++; //no need for break since this is the end of loop //break; } // punctiation is in the middle of the word else { temp = temp.erase(0, i+1); word = word.erase(i, std::string::npos); result[word]++; processWord(temp); break; } } else if (isupper(word[i])) word[i] = tolower(word[i]); } if (flag==0 && word.length()) result[word]++; }
int main() { char **unique, filename[80], word[80]; int counts[31], size = 10, i, count = 0; FILE *fp; unique = malloc(10 * sizeof(char*)); printf("Filename: "); scanf("%s", filename); if((fp = fopen(filename, "r")) == NULL) { printf("Unable to read %s.\n", filename); return -1; } // if not able to open file for(i = 0; i <= 30; i++) counts[i] = 0; while(fscanf(fp, "%s", word) != EOF) unique = processWord(unique, counts, &size, &count, word); show_results(counts, count, size); free_unique(unique, count); return 0; } // main()
// returns non-zero when solve fails. int Count::solve() { std::ifstream fileStream; std::string word; try { fileStream.open(filepath.c_str()); } // catch fstream failure exception and return 1 catch (std::fstream::failure e) { std::cerr << "Error in openning file " << filepath << ", counting failed." << std::endl; return 1; } struct stat fileStat; if(stat(filepath.c_str(),&fileStat) < 0) { // file reading error std::cerr << "Error in openning file " << filepath << ", counting failed." << std::endl; return 2; } if (S_ISDIR(fileStat.st_mode)) { // fstream open is successful but input is a directory not a file std::cerr << "Input is a directory, not a file, " << filepath << ", counting failed." << std::endl; return 3; } while (fileStream >> word) { processWord(word); } fileStream.close(); return 0; }
void StyleSheetParser::parse(const char *text, int len) { const char *start = text; const char *end = text + len; for (const char *ptr = start; ptr != end; ++ptr) { if (isspace(*ptr)) { if (start != ptr) { myWord.append(start, ptr - start); } processWord(myWord); myWord.erase(); start = ptr + 1; } else if (isControlSymbol(*ptr)) { if (start != ptr) { myWord.append(start, ptr - start); } processWord(myWord); myWord.erase(); processControl(*ptr); start = ptr + 1; } } }
QString OOoReportBuilder::processString(const QString source) { if (source.isEmpty()) return QString(); QString text = source; QRegExp rx("\\{\\w+:?\\w+\\}"); int pos = 0; while ((pos = rx.indexIn(text,pos)) != -1) { QString replaced = processWord(text.mid(pos,rx.matchedLength())); text.replace(pos,rx.matchedLength(),replaced); pos += qMin(rx.matchedLength(),replaced.length()); } //qDebug() << text; return text; }
/* reverse Polish calculator */ int main() { printf("\tA Postfix Calculator.\nPlease use single capital characters as variables.\n"); char s[MAXOP]; char line[MAXLINE]; int len; // For each line to be evaluated while ((len = getline(line, MAXLINE)) > 0) { int i = 0; int state = 0; int j = 0; while (i < len) { if (line[i] == ' ' || (i+1 == len)) { if (i + 1 == len) s[j++] = line[i]; s[j] = '\0'; processWord(s); state = 0; j = 0; } else state = 1; if (state) s[j++] = line[i]; ++i; } if (sp > 0) { // Store the result recent = val[sp - 1]; printf("\t%.8g\n", pop()); // Print the answer } } printf(">>> Testing the stack functions.\n"); printf("Clearing stack.\n"); clear(); printf("Attempt to pop. Expect an error.\n"); pop(); printf("Push on a single value of 42.\n"); push(42.f); printf("Get what is on top of the stack.\n"); top(); printf("Popping %g off.\n", pop()); printf("Now pushing on 3.14 and 1.984.\n"); push(3.14); push(1.984); printf("Get what is on top of the stack.\n"); top(); printf("Swapping top two elements.\n"); topSwap(); printf("Get what is on top of the stack.\n"); top(); printf("Popping %g off.\n", pop()); printf("Get what is on top of the stack.\n"); top(); printf("Pushing on 1f through 9f inclusive.\n"); for (double d = 1.f; d < 10.f; ++d) push(d); printf("Print all elements of the original stack.\n"); for (int i = sp - 1; i >= 0; --i) printf("%g\n", val[i]); printf("Copying stack.\n"); duplicateStack(); printf("Clearing current stack.\n"); clear(); printf("Print all elements of the copy to know it is still intact.\n"); for (int i = spCopy - 1; i >= 0; --i) printf("%g\n", valCopy[i]); getch(); return 0; }