Ejemplo n.º 1
0
Archivo: CL.cpp Proyecto: GarageInc/all
//Project Properties -> Configuration Properties -> Linker (General) -> Enable Incremental Linking -> "No (/INCREMENTAL:NO)" 
void Commands(char *com)//2аяПроверка - построить цепочку команд.
{
	int n;
	n=strlen(com);
	int k=1;
	//Вводится строка чётной длины!
	
	for(int i=0; i<n ;)
	{
		{
			if (com[i] && com[i]==k)
			{
				st.Push(k); st.Pop(); cout<<"s"<<"x"; i++; k++;
			}
			else if (i<n) break;}
		}
				
		{
			if (i<n && com[i]!=k)
			{
				st.Push(k); k++; cout<<"S";
			}
		}

		if (st.Empty()==false)
		{
			if (st.Top()==com[i])
			{
				st.Pop(); i++; k++; cout<<"X";
			}
		}
	}
void Preorder_Traversal(TreeNode Root)
{
	if (Root == NULL)
	{
		return ;
	}
	// use Stack ADT to store TreeNode
	Stack S;
	S = Stack_Initial(S);
	TreeNode Temp = Root;
	while(Temp != NULL || !S.Empty()) 
	{
		while (Temp != NULL)
		{	// Firstly, we deal with LeftChild
			visit(Temp);
			push(S, Temp);
			Temp = Temp->LeftChild;
		}

		if(!Empty(S)) 
		{	// traversal the previous TreeNode's RightChild
			Temp = pop(S);
			Temp = Temp->RightChild;
		}
	}
}
Ejemplo n.º 3
0
void Test()
{
	Stack<int> st;
	st.Push(1);
	st.Push(2);
	st.Push(3);
	st.Push(4);
	cout<<st.Top()<<endl;
	cout<<st.Size()<<endl;
	cout<<st.Empty()<<endl;
}
Ejemplo n.º 4
0
Archivo: CL.cpp Proyecto: GarageInc/all
void ProverkaPoSx(char *sx)//1аяПроверка - можно ли извлечь числа по заданному коду.
{
	int n=strlen(sx), k=1, l=0;

	for(int i=0; i<n; i++)
	{
		if (sx[i]=='s') st.Push(k); 
		else 
			{
				if (st.Empty()==false) cout<<st.Pop()<<" ";
				else {cout<<"..Error"; break;}
			}
	}
}
Ejemplo n.º 5
0
int main(int argc, char ** argv)
{
    Stack<string> strs;
    for (int i = 1; i < argc; ++i)
    {
        strs.Push(argv[i]);
    }

    while (strs.Empty() == false)
    {
        string name;
        strs.Pop(name);
        cout << name << endl;
    }
    return 0;
}
Ejemplo n.º 6
0
void test()
{
	Stack<int> s;
	for(size_t i = 1;i <= 5;i++)
	{
		s.Push(i);
	}
	cout<<"size = "<<s.Size()<<endl;
	cout<<"capacity = "<<s.Capacity()<<endl;

	while(!s.Empty())
	{
		cout<<s.Top()<<" ";
		s.Pop();
	}

	cout<<endl;
	cout<<"size = "<<s.Size()<<endl;
	cout<<"capacity = "<<s.Capacity()<<endl;
}
Ejemplo n.º 7
0
void TestMaze()
{
	int n = 10;
	int** ppMaze = CreateMaze(n);
	Pos entry = InitMaze(ppMaze, n);
	PrintMaze(ppMaze, n);

	Stack<Pos> path;
	Maze(ppMaze, n, entry, path);
	PrintMaze(ppMaze, n);

	cout<<"Path:";
	while (!path.Empty())
	{
		const Pos& pos = path.Top();
		cout<<"("<<pos.row<<","<<pos.col<<")"<<"<-";
		path.Pop();
	}

	cout<<"Entry"<<endl;
}
Ejemplo n.º 8
0
Archivo: CL.cpp Proyecto: GarageInc/all
void ProverkaPoChislam(char *chislo)//Проверка - можно ли извлечь заданные числа в заданном порядке. Происходит процесс извлечения.
{
	int n=strlen(chislo), k=0, l=0;

	for(int i=0; i<n; i++)
	{
		st.Push(k);
		{if (st.Top()==chislo[i]) 
			{
				while (st.Top()==chislo[i]) 
				{
				cout<<st.Pop()<<" ";
				i++;
				}
				i--;
			}
		}
		k++;
	}
	if  (st.Empty()==true) cout<<"Verno!";
		else cout<<"NO!";
}
Ejemplo n.º 9
0
bool CorrectBr(Stack& stack, char* str)
{
	if (!str)
	{
		return false;
	}

	for ( UI i = 0; i < strlen(str); ++i )
	{
		if ( ( str[i] == ')' ) && ( stack.Top() == '(' ) )
		{
			stack.PopBack();
		}
		else if ( ( str[i] == ']' ) && ( stack.Top() == '[' ) )
		{
			stack.PopBack();
		}
		else if ( ( str[i] == '}' ) && ( stack.Top() == '{' ) )
		{
			stack.PopBack();
		}
		else if (	( str[i] == ')' ) || ( str[i] == '(' ) 
					|| ( str[i] == ']' ) || ( str[i] == '[' ) 
					|| ( str[i] == '}' ) || ( str[i] == '{' ) )
		{
			stack.PushBack( str[i] );
		}
	}
	if ( stack.Empty() )
	{
		return true;
	}
	else
	{
		return false;
	}
}
Ejemplo n.º 10
0
Archivo: fix.C Proyecto: pete0877/wpi
FixTraverseMaze(Map &map, Point ptStart)
// Fixed Order(fix)-uses a fixed order in how it tries the four
// neighboring direction.  North, East, South, West is the order of
// priority.
{
 char step=CURRENT,count;
 Stack stack;
 int WrongStep=0,length;
 char ok;
 Point point,x,direction,tryd;

 stack.Push(ptStart);
 while (!stack.Empty()) {

	stack.Pop(point);
	if (map.GetPoint(point)==GOAL) {
		map.SetPoint(point,step);
		length=int(step-CURRENT+1);
		cout<<"\nPath was found of length "<<length<<" with ";
		cout<<WrongStep<<" wrong points visited.";
		cout<<"\n(Total of "<<2*WrongStep+length<<" moves, ";
		printf("algorythm efficiency: %.2f)\n",
		 (.0+length)/(2*WrongStep+length));
		map.Print();
		return(0);
	}
	map.SetPoint(point,step++);
	stack.Push(point);
	ok=0;

	x.row=point.row;
	x.col=point.col-1;
	if (map.GetPoint(x)==FREE || map.GetPoint(x)==GOAL)
	{
		stack.Push(x);
		ok=1;
	}
	x.row=point.row+1;
	x.col=point.col;
	if (map.GetPoint(x)==FREE || map.GetPoint(x)==GOAL)
	{
		stack.Push(x);
		ok=1;
	}
	x.row=point.row;
	x.col=point.col+1;
	if (map.GetPoint(x)==FREE || map.GetPoint(x)==GOAL)
	{
		stack.Push(x);
		ok=1;
	}
	x.row=point.row-1;
	x.col=point.col;
	if (map.GetPoint(x)==FREE || map.GetPoint(x)==GOAL)
	{
		stack.Push(x);
		ok=1;
	}
	if (!ok)
	{
		ok=0;
		do {
			if (stack.Pop(point)==-1) return(-1);
			if (map.GetPoint(point)>=CURRENT)
			{
				map.SetPoint(point,DEADEND);
				step--;
				WrongStep++;
			} else
			{
				stack.Push(point);
				ok=1;
			}
		} while (ok==0);
   }
 }
 cout<<"\n Could not find the Goal.\n";
 map.Print();
 return(0);
}
void Update(clock_t &tLast, short &charIndex, bool &newWord, char(&command)[MAX_CHARATERS_PER_LINE])
{

	command[charIndex] = GetCharacter(newWord);					//escuchamos la entrada de datos con un timeout para poder dedicar tiempo a los calculos posteriores

																//verificamos que tenemos caracter leido o nueva palabra empezada
	if (command[charIndex] != NULL || newWord != 0)
	{
		if (newWord == 0) { newWord = 1; }						//ponemos a true el boleano de palabra

																//cuando pulse enter el jugador, almacenamos la palabra en el stack y reseteamos el indice de nuestra palabra asi como el boleano 
																//en caso contrario aumentamos el indice para obtener una nueva letra

		if (command[charIndex] == '\n')
		{
			command[charIndex] = 0;
			if (charIndex > 0)					//si tan solo pulsa enter no necesitamos almacenarlo en la pila
			{
				newWord = 0;
				stackCommands.Push((std::string) command);
				command[0] = 0;
			}
			else
			{
				Prompt();
			}
			charIndex = 0;
		}
		else
		{
			++charIndex;
		}
	}

	//tratamiento acciones o comandos
	//Si la pila no esta vacia y el delay se cumple ejecutamos una nueva instruccion
	if (!stackCommands.Empty() && (clock() - tLast) > DELAY_BETWEEN_COMMANDS)
	{
		//La palabra reservada para ejecutar 'salir del juego' es especial ya que requiere de verificación. 
		if (LEAVE == Upcase(stackCommands.Consult()))
		{
			char a[MAX_CHARATERS_PER_LINE];	//para no obtener datos de la pila definios una nueva variable
			a[0] = 0;
			do				//para salir hay que verificar antes
			{
				printf(LEAVE_Q);
				scanf_s("%s", a, _countof(a));
			} while (!Compare(YES, Upcase(a)) && !Compare(NO, Upcase(a)));

			if (Compare(YES, Upcase(a)))  gameState = endLoop;
			else
			{
				Prompt();
				stackCommands.Pop();
			}
		}
		else
		ShowCommands();
		tLast = clock();
	}
}
Ejemplo n.º 12
0
bool Maze(int** ppMaze, int n, const Pos& entry, Stack<Pos>& path)
{
	assert(entry.col < n && entry.row < n);
	
	Pos cur = entry;
	ppMaze[cur.row][cur.col] = 2;
	path.Push(cur);

	while (!path.Empty())
	{
		 Pos next = path.Top();
		
		 //
		 // 出口在最后一行
		 //
		 if (next.row == n-1)
		 {
			 return true;
		 }

		 // 上
		 next.row -= 1;
		 if (CheckIsAccess(ppMaze, n, next))
		 {
			ppMaze[next.row][next.col] = 2;
			path.Push(next);
			continue;
		 }

		 // 右
		 next = path.Top();
		 next.col += 1;
		 if (CheckIsAccess(ppMaze, n, next))
		 {
			 ppMaze[next.row][next.col] = 2;
			 path.Push(next);
			 continue;
		 }

		 // 下
		 next = path.Top();
		 next.row += 1;
		 if (CheckIsAccess(ppMaze, n, next))
		 {
			 ppMaze[next.row][next.col] = 2;
			 path.Push(next);
			 continue;
		 }

		 // 左
		 next = path.Top();
		 next.col -= 1;
		 if (CheckIsAccess(ppMaze, n, next))
		 {
			 ppMaze[next.row][next.col] = 2;
			 path.Push(next);
			 continue;
		 }

		 Pos tmp = path.Top();
		 ppMaze[tmp.row][tmp.col] = 3;
		 path.Pop();
	}

	return false;
}