Thread: balancing parentheses????

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    2

    balancing parentheses????

    Problem description:A balanced string of parentheses is one where every opening parentheses has exactly one corresponding closing parentheses. In this assignment, you have to determine whether a string of parentheses is balanced or not. In case you are not sure how to do it, here is the algorithm.
    initialise an empty stack
    while there is item to read from the string, do
    read next item in the string
    if item is an opening parentheses, then
    push it into the stack
    else if item is a closing parentheses, then
    if stack is empty, then
    string is not balanced
    else
    pop from stack
    if popped item does not corresponds to the closing parentheses, then
    string is not balanced
    if stack is non-empty, then
    string is not balanced
    else string is balanced
    Input specification: The input begins with a positive integer N. In the next N lines, N strings of parentheses are provided (one string in each line). Strings can contain parentheses, curly braces and square braces. If the strings contain other characters, you should ignore those characters. The input has to be stored in a text file (parentheses.txt) and your program will read the input from the text file.
    Output specification: For each string of parentheses, you have to determine if the string is balanced or not. If it is balanced, you have to print “Balanced” and “Not balanced” otherwise. Your output should be written in a text file as well (report.txt).
    Sample input:
    4
    ()()(()))
    {()[()]}
    {(()())([])}
    ({}]
    Sample output:
    Not balanced
    Balanced
    Balanced
    Not balanced
    Code:
    StringStack.CPP
    #include "StringStack.h"
    #include "ItemType.h"
        StringStack::StringStack()
    {
      top = -1;
    }
    
    bool StringStack::IsEmpty()
    {
      return (top == -1);
    }
    
    bool StringStack::IsFull()
    {
      return (top == MAX_ITEMS - 1);
    }
    
    ItemType StringStack::Top()
    {
      if (IsEmpty())
        throw EmptyStack();
      return items[top];
    }
    
    void StringStack::Push(ItemType newItem)
    {
      if (IsFull())
        throw FullStack();
      top++;
      items[top] = newItem;
    }
    
    void StringStack::Pop()
    {
      if (IsEmpty())
        throw EmptyStack();
      top--;
    }
    
    
    #include <iostream>
    using namespace std;
    
    #ifndef StringStack_h
    #define StringStack_h
    
    
    #include "ItemType.h"
    
    class FullStack
    // Exception class used by Push when stack is full.
    {
    };
    
    class EmptyStack
    // Exception class used by Pop and Top when stack is empty.
    {
    };
    
    StringStack.h class StringStack {
    public:
      StringStack();
      bool IsEmpty();
      bool IsFull();
      void Push(ItemType item);
      void Pop();
      ItemType Top();
    private:
      int top;
      ItemType items[MAX_ITEMS];
    };
    
    #endif
    main.cpp Dont know what to
    do
    ? ?
    Last edited by Salem; 02-28-2014 at 05:44 AM. Reason: removed all the font and size abuse

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The way your exercise is worded, there is not much that anyone here could do to help other than write the required code for you.

    And that would be a breach of this site's homework policy (at this link).

    As such, put in some effort to actually do your own homework. That means more effort than simply posting the question, copying code you have obviously been given or found somewhere, and stating you don't know what to do. This is not a pre school where people will do what you need to do.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Location
    Norway
    Posts
    40
    What do you think you ought to do? .. You have the clearly stated algorithm and people are not gonna write the code for you.. Do something and ofcourse you will receive help.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    2
    Quote Originally Posted by grumpy View Post
    The way your exercise is worded, there is not much that anyone here could do to help other than write the required code for you.

    And that would be a breach of this site's homework policy (at this link).

    As such, put in some effort to actually do your own homework. That means more effort than simply posting the question, copying code you have obviously been given or found somewhere, and stating you don't know what to do. This is not a pre school where people will do what you need to do.
    Bro Look this is my code writing style ---

    Code:
    #include<iostream>
    #include <Stack>
    using namespace std;
    
    bool bracketsame(char opening,char closing) 
    {
    if(opening == '(' && closing == ')') return true;
    else if(opening == '{' && closing == '}') return true;
    else if(opening == '[' && closing == ']') return true;
    return false;
    }
    bool balancedparantheses(string exp) 
    {                                    
    stack<char> S;
    for(int i =0;i<exp.length();i++)
    {
    if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
    S.push(exp[i]);
    else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
    {
    if(S.empty() || !bracketsame(S.top(),exp[i]))
    return false;
    else
    S.pop();
    }
    }
    return S.empty() ? true:false;
    }
    
    int main()
    {
    string expression;
    
    cout<<"Enter an expression to check: ";
    cin>>expression;
    
    if(balancedparantheses(expression))
    cout<<"Balanced expression \n";
    
    else
    cout<<"Expression is not balanced \n";
    }
    problem is I have been given the above .cpp and .h file and has been asked to write this CODE based on that, now I never written a code like this so i m confused and there is a ItemType.h header file that's been included which is not in C++ library so where did that come from I m confused with this prob

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,068
    Do to the fact that double quotes are in the #include instead of <> you either have the "StringStack.h" and "ItemType.h" supplied to you.
    Or, you are expected to write them.

    The use of <> would have implied compiler supplied or library supplied header. Note, this is NOT a real solid rule; but, is the standard C/C++ way of doing things.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parentheses Checker
    By whiteboyfly in forum C Programming
    Replies: 7
    Last Post: 02-23-2014, 09:50 PM
  2. parentheses solver
    By yangss in forum C Programming
    Replies: 27
    Last Post: 09-01-2010, 02:41 PM
  3. Avl Tree Balancing
    By Jajo in forum C Programming
    Replies: 0
    Last Post: 08-06-2010, 04:48 AM
  4. Parentheses preference
    By brewbuck in forum Tech Board
    Replies: 22
    Last Post: 07-24-2010, 01:53 PM
  5. btree balancing
    By ender in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 02:22 PM