示例#1
0
int main()
{
  char symbol;
  StackType stack;
  bool balanced = true;
  char openSymbol;
  
  cout << "Enter an expression and press return." << endl;
  cin.get(symbol); 

  while (symbol != '\n' && balanced)
  {
    if (IsOpen(symbol))
      stack.Push(symbol);

	else if (IsClosed(symbol))
    {
      if (stack.IsEmpty())
        balanced = false;
      else
      {
        openSymbol = stack.Top();
        stack.Pop();
        balanced = Matches(symbol, openSymbol);
      }
    }
    cin.get(symbol);
  }

  if (balanced)
    cout << "Expression is well formed." << endl;
  else
    cout << "Expression is not well formed."  << endl;
  return 0;
}
示例#2
0
int main()
{
  bool palindrome = true;
  char character;
  StackType stack;
  QueType queue(40);
  char stackChar;
  char queChar;
  cout << "Enter a string; press return." << endl;
  cin.get(character);
  while (character != '\n')
  {
    stack.Push(character);
    queue.Enqueue(character);
    cin.get(character);
  }
  
  while (palindrome && !queue.IsEmpty())
  {
    stackChar = stack.Top();
    stack.Pop();
    queue.Dequeue(queChar);
   
    if (stackChar != queChar)
      palindrome = false;
  }
  
  if (palindrome)
    cout << "String is a palindrome" << endl;
  else
    cout << "String is not a palindrome" << endl;
  return 0;
}