int main() { SimpleStack s; char c; printf("testing push/pop..."); if (!s.push('a')) goto fail; if (!s.push('b')) goto fail; if (!s.push('c')) goto fail; if (!s.pop(&c) || c != 'c') goto fail; if (!s.pop(&c) || c != 'b') goto fail; if (!s.pop(&c) || c != 'a') goto fail; printf("[ok]\n"); printf("testing empty..."); if (!s.empty()) goto fail; s.push('a'); if (s.empty()) goto fail; printf("[ok]\n"); return 0; fail: printf("[failed]\n"); return -1; }
int main() { // ein Stack für 100 int-Zahlen SimpleStack<int,100> einIntStack; // Stack füllen int i = 100; while(!einIntStack.full()) einIntStack.push(i++); cout << "Anzahl : " << einIntStack.size() << endl; // oberstes Element anzeigen cout << "oberstes Element: " << einIntStack.top() << endl; cout << "alle Elemente entnehmen und anzeigen: " << endl; while(!einIntStack.empty()) { i = einIntStack.top(); einIntStack.pop(); cout << i << '\t'; } cout << endl; }
int searchRecursive(const char *root, const char *allowed_extensions) { SimpleStack stack; stack.push(new_entry(root, false)); WIN32_FIND_DATAA ffd; HANDLE hfind; char path[NLF_MAXPATH]; char *path_w = path; while (stack.top != NULL)//stack.length != 0) { dir curdir = stack.top; // build a full path { // TODO: check path length! strcpy(path_w, curdir->name); strcat(path_w, DIRSEP_STR); strcat(path_w, "*"); int namelen = strlen(curdir->name); path_w += namelen + 1; } // try to open the path hfind = FindFirstFileA(path, &ffd); if (hfind == INVALID_HANDLE_VALUE) { stack.pop_all(); return error("failed to open directory"); } // traverse the current dir do { if ((ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { // do the job //printf("checking file %s, extensions: %s\n", ffd.cFileName, extensions); if (allowed_extensions[0] != '\0') { if (!checkExtension(ffd.cFileName, allowed_extensions)) continue; } strcpy(path_w, ffd.cFileName); if (ShowLogs) printf("%s: ", path); if (!processFile(path)) { if (ShowLogs) puts("error!"); if (!SkipErrors) { stack.pop_all(); return error("unable to open file"); } } else if (ShowLogs) puts("done"); } else if (strcmp(ffd.cFileName, ".") != 0 && strcmp(ffd.cFileName, "..") != 0) { dir newdir = new_entry(ffd.cFileName, stack.top == curdir); stack.push(newdir); } } while (FindNextFileA(hfind, &ffd) != 0); /*if (GetLastError() != ERROR_NO_MORE_FILES) { FindClose(hfind); return error("unknown reading error"); }*/ FindClose(hfind); if (stack.top == curdir) { path_w[-1] = '\0'; path_w = strrchr(path, DIRSEP) + 1; while (stack.top->first_in_level) { path_w[-1] = '\0'; path_w = strrchr(path, DIRSEP) + 1; stack.pop(); } stack.pop(); } } return 0; }