Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,470 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,952 people online right now. Registration is fast and FREE... Join Now!




parentheses check help!

 
Reply to this topicStart new topic

parentheses check help!

dms02x
8 Feb, 2008 - 03:10 AM
Post #1

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 18

anime2.gif my 1st post.... hehe im sorry for myself becouse i cant figure out one
part of this program..

im taking datastuctures now and my professor is in Java and my previous
csc was in c++ and im really working my shit out coz im having a hard time
deciphering Java to C++

i know you guys don't want assignments and all in this treand..

tips, hints,revise or samples would be nice...

my program lacks only one part and thats the checking the parentheses

the output should see if the parentheses is balance or not

Plz Guys point me in the right direction!! haha smile.gif




CODE

#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<malloc.h>

//   Creating a NODE Structure
struct node
{
   int data;
   struct node *next;
};

// Creating a class STACK
class stack
{
   struct node *top;
   public:
      stack() // constructure
      {
     top=NULL;
      }
      void push(); // to insert an element
      void pop();  // to delete an element
      void show(); // to show the stack
      void balance(); //balance the parenthesis
};
// PUSH Operation
void stack::push()
{
   int value;
   struct node *ptr;
   cout<<"\nPUSH Operation\n";
   cout<<"Enter a number to insert: ";
   cin>>value;
   ptr=new node;
   ptr->data=value;
   ptr->next=NULL;
   if(top!=NULL)
      ptr->next=top;
   top=ptr;
   cout<<"\nNew item is inserted to the stack!!!";
   getch();
}

// POP Operation
void stack::pop()
{
   struct node *temp;
   if(top==NULL)
   {
      cout<<"\nThe stack is empty!!!";
      getch();
      return;
   }
   temp=top;
   top=top->next;
   cout<<"\nPOP Operation........\nPoped value is "<<temp->data;
   delete temp;
   getch();
}

// Balance parenthesis
void stack::balance()
{  
    
     balanced cs = bal 0 cs
     where bal n ""       = n == 0
      bal (-1) _     = False
      bal n ('(':cs) = bal (n+1) cs
      bal n (')':cs) = bal (n-1) cs
      bal n (_:cs)   = bal n cs
  
}

// Show stack
void stack::show()
{
   struct node *ptr1=top;
   cout<<"\nThe stack is\n";
   while(ptr1!=NULL)
   {
      cout<<ptr1->data<<" ->";
      ptr1=ptr1->next;
   }
   cout<<"NULL\n";
   getch();
}

// Main function
int main()
{

   stack s;
   int choice;
   while(1)
   {
      cout<<"\n-----------------------------------------------------------";
      cout<<"\n\t\tSTACK USING LINKED LIST\n\n";
      cout<<"1:PUSH\n2:POP\n3:BALANCE PARENTHESES\n4:DISPLAY STACK\n5:EXIT";
      cout<<"\nEnter your choice(1-4): ";
      cin>>choice;
      switch(choice)
      {
       case 1:
      s.push();
      break;
       case 2:
      s.pop();
      break;
       case 3:
      s.balance();
      break;
       case 4:
      s.show();
      break;
       case 5:
      exit(0);
      break;
       default:
      cout<<"Please enter correct choice(1-5)!!";
      getch();
      break;
       }
   }
   return 0;
}

User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Parentheses Check Help!
8 Feb, 2008 - 03:53 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,153



Thanked: 44 times
Dream Kudos: 125
My Contributions
CODE

// Balance parenthesis
void stack::balance()
{

balanced cs = bal 0 cs
where bal n "" = n == 0
bal (-1) _ = False
bal n ('(':cs) = bal (n+1) cs
bal n (')':cs) = bal (n-1) cs
bal n (_:cs) = bal n cs

}



What was that? Other things are looking like Stack implementation assignment and this code seems just boxed inside that. no connection in logic. If you are supposed to verify whether the parenthesis are balanced then why are you taking only numbers? Looks like either there are too many mistakes or you are trying a tremendous amount of code reuse blink.gif .

Make it clear that what exactly you want to do first. Then start with the coding, of course you will use the stack here. So take a complete string from user. traverse it character by character. if you get any opening parenthesis then push it. if you get any closing one then pop. and the end of string traversal, stack should be empty. If yes then it's balanced, else it's not. [I am considering that you are going to get only one type of brackets here and that's () ].

That's all. Now work again on it. and tell me whether it works.

This post has been edited by AmitTheInfinity: 8 Feb, 2008 - 03:54 AM
User is offlineProfile CardPM
+Quote Post

selloorhari
RE: Parentheses Check Help!
8 Feb, 2008 - 06:08 AM
Post #3

D.I.C Head
**

Joined: 7 Feb, 2008
Posts: 66


My Contributions
Plz Don't put codes yaer..


User is offlineProfile CardPM
+Quote Post

Jayman
RE: Parentheses Check Help!
8 Feb, 2008 - 02:43 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,301



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
QUOTE(selloorhari @ 8 Feb, 2008 - 06:08 AM) *

Plz Don't put codes yaer..

Please do not post replies that make no sense and have nothing to do with the topic.
User is online!Profile CardPM
+Quote Post

dms02x
RE: Parentheses Check Help!
14 Feb, 2008 - 02:36 PM
Post #5

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 18

Thnx for The Tip, But Im Still stuck
tongue.gif hehehe




#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<malloc.h>

// Creating a NODE Structure
struct node
{
int data;
struct node *next;
};

// Creating a class STACK
class stack
{
struct node *top;
public:
stack() // constructure
{
top=NULL;
}
void push(); // to insert an element
void pop(); // to delete an element
void show(); // to show the stack
void balance(); //balance the parenthesis
bool isEmpty(const stack& s);
};
// PUSH Operation
void stack::push()
{
int value;
struct node *ptr;
cout<<"\nPUSH Operation\n";
cout<<"Enter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";
getch();
}

// POP Operation
void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
getch();
return;
}
temp=top;
top=top->next;
cout<<"\nPOP Operation........\nPoped value is "<<temp->data;
delete temp;
getch();
}

// Balance parenthesis
void stack::balance()
{
char leftparen = '(';
char rightparen = ')';
bool error = false;
char ch;

int value;
struct node *ptr;

cout<<"Enter Stack: ";
cin>>ch;

while (( ch != '\n') && (!error) )
{
if (ch == leftparen)
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if (ch == rightparen)
if (isEmpty(s))
error = true;
else
temp=top;
top=top->next;
cin.get(ch);
}
if ((!error) && isEmpty(s))
cout<<"Valid expression"<<endl;
else
cout <<"Invalid Expression"<<endl;


}

// Show stack
void stack::show()
{
struct node *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
getch();
}

// Main function
int main()
{

stack s;
int choice;
while(1)
{
cout<<"\n-----------------------------------------------------------";
cout<<"\n\t\tSTACK USING LINKED LIST\n\n";
cout<<"1:PUSH\n2:POP\n3:BALANCE\n4:DISPLAY STACK\n5:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.balance();
break;
case 4:
s.show();
break;
case 5:
exit(0);
break;
default:
cout<<"Please enter correct choice(1-4)!!";
getch();
break;
}
}
return 0;
}

User is offlineProfile CardPM
+Quote Post

dms02x
RE: Parentheses Check Help!
15 Feb, 2008 - 02:44 AM
Post #6

New D.I.C Head
*

Joined: 8 Feb, 2008
Posts: 18

BUZZ!!
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Parentheses Check Help!
15 Feb, 2008 - 04:13 AM
Post #7

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,153



Thanked: 44 times
Dream Kudos: 125
My Contributions
QUOTE(dms02x @ 15 Feb, 2008 - 04:14 PM) *

BUZZ!!



I am really sorry to say that I am still not able to understand what exactly you are doing in that code [either the code is done by a genius or I am stupid]. You are just trying to stuff in your function [which in turn is all mess and does nothing] in some ready made code. Please understand that you can not use that stack program directly, you have to make many modifications in that.


try to do the things that I told you last time and then come up with some errors you are facing. You can not get help by just pasting some crap here. Try to do something yourself.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 02:28PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month