/*
 * Solution to Problem 3 (C++)
 * 14th Annual CS Programming Contest
 * WCU Dept of Math and CS
 * Copyright 2003 , All Rights Reserved
 *
 * Problem Statement:
 * Parentheses
 * Takes as input a string of symbols
 * Determines whether or not the string contains a balanced set
 * of parentheses
 * We can determine whether or not the parentheses are balanced
 * by keeping track of the difference between the number of 
 * left parens seen and the number of right parens seen.  If
 * this number falls below zero, it signifies we've seen more 
 * right than left and we should state that the () are not 
 * balanced.  If at the end of the processing the string, 
 * the number is not zero, then we haven't seen the same number 
 * of right and left, meaning that we haven't seen a balanced set.
 */

#include <iostream.h>
#include "apstring.h"
#include "apstring.cpp"


main()
{
  apstring toCheck; //toCheck is the input variable

  //Prompt for and accept input
  cout << "Enter a string to check." <<endl;
  cin >> toCheck;

  int moreLeft = 0; //number of left minus number of right
  int checkLen = toCheck.length();
  int i=0;     //i is the counter variable
  
  // Go through the string symbol by symbol; 
  // if there are ever more right parentheses
  // than left, the string is unbalanced.
  while (i<checkLen && moreLeft >=0)
  {
    if (toCheck.substr(i,1)=="(")
        moreLeft++; //we've seen one more left
    else if (toCheck.substr(i,1) == ")")
        moreLeft--; //we've seen one less left
    i++;
  }
  if (moreLeft != 0)  // If the number of left and right 
                      // parentheses is not the same,
    cout <<"Unbalanced parentheses." << endl;  //string is unbalanced
  else                // If we've reached this point & number of 
                      // parens is the same
    cout << "Balanced parentheses." << endl;  //string is balanced
}

