示例#1
0
void printHistory(LinkedStack<LinkedStack<Point> >& history, ostream& os = cout) {
	if (!history.empty()) {
		Point x = history.pop().peek();
		printHistory(history);
		os << x << endl;
	}
}
示例#2
0
void make_sum_bigger(int n, int m, LinkedStack<int>& x, LinkedStack<int>& y, LinkedStack<int>& z){
int data_f = 0;
  for(int i=0;i<m ;i++){                       //Събира двета числа до позицията на по-малко
    data_f += x.pop() + y.pop();               //
    if(data_f < 10){
        z.push(data_f);
        data_f = 0; }
    else {
        z.push(data_f%10);
        data_f = 1;
    }
}
    int result = data_f;                  // и прехвърля останалите цифри (+1 ако има едно на ум) от по-голямото число в стека с резултата
    while(!x.empty()){
    result += x.pop();
    if (result >= 10){
    z.push(result % 10);
    result = 1;
    }
    else {
    z.push(result);
    result = 0;
    }
}
}
示例#3
0
void printPath(LinkedStack<Point>& path, ostream& os = cout) {
	if (!path.empty()) {
		Point x = path.pop();
		printPath(path, os);
		os << x << endl;
	}
}
示例#4
0
void LinkedStack<T>::copy(LinkedStack<T> const& ls) {
	/* !!! неправилно !!!
	top = NULL;
	while(!ls.empty())
		push(ls.pop());
		*/
	if (ls.empty())
		top = NULL;
	else {
		// първа стъпка
		top = new StackElement<T>;
		top->data = ls.top->data;

		// подготовка за втората стъпка
		StackElement<T>* toCopy = ls.top->next;
		StackElement<T>* lastCopy = top;

		while (toCopy != NULL) {
			// копиране
			StackElement<T>* newCopy = new StackElement<T>;
			newCopy->data = toCopy->data;

			// завързване
			lastCopy->next = newCopy;

			// подготовка за следващата стъпка
			toCopy = toCopy->next;
			lastCopy = newCopy;
		}
		// затваряме веригата
		lastCopy->next = NULL;
	}
}
示例#5
0
void make_sum_small(int n, int m, LinkedStack<int>& x, LinkedStack<int>& y, LinkedStack<int>& z){
  //81094: Confusing bracket alignment within this function.
  int data = 0;
  for(int i=0;i<n;i++){
    data += x.pop() + y.pop();
    if(data < 10){
        z.push(data);
        data = 0; }
    else {
        z.push(data%10);
        data = 1;
    }
}   int result = data;
    while(!y.empty()){
    result += y.pop();
    if (result >= 10){
    z.push(result % 10);
    result = 1;
    }
    else {
    z.push(result);
    result = 0;
    }
}
}
示例#6
0
void expressionMain() {
    LinkedStack expressionNum;
    LinkedStack expressionOp;
    int choice = 1, num;
    string op, expression = "", numS;
    stringstream ss;
    cout << "Please enter your expression one character at a time." << endl;
    cout << "num:";
    num = enterNum();
    ss << num;
    ss >> numS;
    expression += numS;
    expressionNum.push(num);
    while (choice != 3) {
        switch (choice) {
            case 1: cout << "op:";
                    op = enterOp();
                    cout << "num:";
                    num = enterNum();
                    ss.clear();
                    ss << num;
                    ss >> numS;
                    expression += op + numS;
                    if (!expressionOp.empty() && opSize(expressionOp.top()) >= opSize(op)) {
                        eval(expressionOp,expressionNum);
                        if (!expressionOp.empty() && opSize(expressionOp.top()) >= opSize(op)) {
                            eval(expressionOp,expressionNum);
                        }
                        expressionOp.push(op);
                        expressionNum.push(num);
                    }
                    else {
                        expressionOp.push(op);
                        expressionNum.push(num);
                    }
                    cout << "Expression: " << expression << endl;
                    cout << "More(1),Quit(2)?: ";
                    cin >> choice;
                break;
            case 2: while (!expressionOp.empty())
                        eval(expressionOp,expressionNum);
                    cout << "Answer: " << expressionNum.top2() << endl;
                    choice = 3;
        }
    }
    
}
示例#7
0
void testPointStack() {
	LinkedStack<Point2D<int> > s;
	s.push(Point2D<int>(1, 2));
	s.push(Point2D<int>(2, 3));
	s.push(Point2D<int>(3, 4));
	while (!s.empty())
		cout << s.pop();
}
bool isHtmlMatched(const vector<string>& tags) {
  LinkedStack<string> S;
  typedef vector<string>::const_iterator Iter;

  for (Iter p = tags.begin(); p != tags.end(); ++p) {
    if (p->at(1) != '/')
      S.push(*p);
    else {
      if (S.empty()) return false;
      string open = S.top().substr(1);
      string close = p->substr(2);
      if (open.compare(close) != 0) return false;
      else S.pop();
    }
  }
  if (S.empty()) return true;
  else return false;
}
示例#9
0
void horse_stack(Point start, Point end) {
	LinkedStack<LinkedStack<Point> > history;
	LinkedStack<Point> startStack;
	ofstream clog("log.txt");
	startStack.push(start);
	history.push(startStack);
	while (!history.empty() &&
			(history.peek().empty() || history.peek().peek() != end)) {
		if (history.peek().empty()) {
			// Стъпка назад
			history.pop();
			if (!history.empty())
				history.peek().pop();
			clog << "Стъпка назад" << endl;
		}
		else {
			// стъпка напред
			Point current = history.peek().peek();
			clog << "Стъпка напред: " << current << endl;
			LinkedStack<Point> possibleMoves;
			board[current.first][current.second] = true;
			for(int dx = -2; dx <= 2; dx++)
				if (dx != 0)
					for(int sign = -1; sign <= 1; sign += 2) {
						int dy = sign * (3 - abs(dx));
						Point newstart(current.first + dx,
									   current.second + dy);
						if (inside_board(newstart)
							&&
							!board[newstart.first][newstart.second])
							possibleMoves.push(newstart);
					}
			LinkedStack<Point> pm(possibleMoves);
			clog << "---\n";
			printPath(pm,clog);
			clog << endl << "---\n";
			history.push(possibleMoves);
		}
	}
	if (!history.empty() && !history.peek().empty()) {
		cout << "Успех:\n";
		printHistory(history);
	}
}
示例#10
0
bool checkParentheses(char const* s) {
	LinkedStack<char> stack;
	char const* p = s;
	while (*p != '\0') {
		switch (*p) {
		case '(':
		case '[':
		case '{':
		case '<':
			stack.push(*p);
			break;
		case ')':
		case ']':
		case '}':
		case '>':
			if (stack.empty() ||
				!matchParentheses(stack.pop(), *p))
				return false;
			break;
		}
		p++;
	}
	return stack.empty();
}
示例#11
0
void horse_queue(Point start, Point end) {
	LinkedQueue<Position> q;
	q.enqueue(start);
	board[start.first][start.second] = true;
	Position current;
	LinkedStack<LinkedQueue<Position> > history;
	LinkedQueue<Position> level;
	int currentMove = 0;
	history.push(level);
	while (!q.empty() && (current = q.dequeue()).p != end) {
		if (current.move == currentMove) {
			history.peek().enqueue(current);
		} else {
			// current.move == currentMove + 1
			currentMove = current.move;
			LinkedQueue<Position> level;
			level.enqueue(current);
			history.push(level);
		}
		for(int dx = -2; dx <= 2; dx++)
			if (dx != 0)
				for(int sign = -1; sign <= 1; sign += 2) {
					int dy = sign * (3 - abs(dx));
					Point newstart(current.p.first + dx,
								   current.p.second + dy);
					Position newposition(newstart, current.move + 1);
					if (inside_board(newstart)
						&&
						!board[newstart.first][newstart.second]) {
						q.enqueue(newposition);
						clog << newposition << endl;
						board[newstart.first][newstart.second] = true;
					}

				}
	}
	// current == end -- успех
	// q.empty() -- неуспех
	clog << history;
	history.pop();
	if (current.p == end) {
		cout << "Успех за " << current.move << " хода!" << endl;
		LinkedStack<Point> path;
		path.push(current.p);
		while(!history.empty()) {
			// в history.peek() търсим първата позиция position,
			// за която isValidMove(current.p,position.p)
			Position position;
			while (!isValidMove(current.p,
					           (position = history.peek().dequeue()).p));
			// знаем, че isValidMove(current.p,position.p)
			// добавим position в пътя
			path.push(position.p);
			history.pop();
			current = position;
		}
		cout << path << endl;
	}
	else
		cout << "Неуспех!" << endl;
}