6 Replies - 13240 Views - Last Post: 15 February 2008 - 05:13 AM Rate Topic: -----

#1 dms02x  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 08-February 08

parentheses check help!

Posted 08 February 2008 - 04:10 AM

>.< 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 :)




#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;
}


Is This A Good Question/Topic? 1

Replies To: parentheses check help!

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: parentheses check help!

Posted 08 February 2008 - 04:53 AM

// 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: .

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: 08 February 2008 - 04:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 selloorhari  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 68
  • Joined: 07-February 08

Re: parentheses check help!

Posted 08 February 2008 - 07:08 AM

Plz Don't put codes yaer..
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: parentheses check help!

Posted 08 February 2008 - 03:43 PM

View Postselloorhari, on 8 Feb, 2008 - 06:08 AM, said:

Plz Don't put codes yaer..

Please do not post replies that make no sense and have nothing to do with the topic.
Was This Post Helpful? 0
  • +
  • -

#5 dms02x  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 08-February 08

Re: parentheses check help!

Posted 14 February 2008 - 03:36 PM

Thnx for The Tip, But Im Still stuck
:P 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;
}
Was This Post Helpful? 0
  • +
  • -

#6 dms02x  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 08-February 08

Re: parentheses check help!

Posted 15 February 2008 - 03:44 AM

BUZZ!!
Was This Post Helpful? 0
  • +
  • -

#7 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: parentheses check help!

Posted 15 February 2008 - 05:13 AM

View Postdms02x, on 15 Feb, 2008 - 04:14 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1