Exemple #1
0
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;
}
    void moveSingle(SimpleStack<size_t> & from, SimpleStack<size_t> & to) {
        assert(!from.isEmpty());
        assert(from.peek() < to.peek());

        to.Push(from.Pop());

        if(print_states) {
            printState();
        }
    }
 void printStack(SimpleStack<size_t> & stack) {
     std::vector<size_t> vec = stack.ToVector();
     for (auto itr = vec.rbegin(); itr != vec.rend(); ++itr) {
         std::cout << *itr << " ";
     }
     std::cout << std::endl;
 }
Exemple #4
0
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 main() {
	SimpleStack<string> stack;

	stack.add("abc");
	stack.add("def");
	stack.add("ghi");
	stack.add("jkl");
	stack.add("mno");
	stack.add("pqr");
	stack.add("stu");

	cout << "item 7 is " << stack.get(7) << endl;

	try {
		UnmodifiableStack<string> safeStack(stack);

		untrustedCode(safeStack);
	} catch(char *msg) {
		cout << "caught exception: " << msg << endl;
	}
}
Exemple #6
0
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;
}
Exemple #7
0
int main()
{
	
	SimpleStack<float> s;
	s.Push(12.4f);
	s.Push(3.14f);
	s.Print();
	cout<<s.IsEmpty()<<endl;
	cout<<s.Size()<<endl;
	cout<<s.Pop()<<endl;
	cout<<s.Pop()<<endl;
	cout<<s.Pop()<<endl;
	cout<<s.IsEmpty()<<endl;
	s.Push(0.618f);
	s.Print();
	cout<<s.Size()<<endl;
	return 0;
}