GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Apr-2008, 18:24
fido_dido_do_it fido_dido_do_it is offline
New Member
 
Join Date: Feb 2008
Posts: 3
fido_dido_do_it is on a distinguished road

need help with brackets check using stacks


hey ppl,
i'm trying 2 make a parentheses, brackets and braces check but my only problem is how to use them in the int main() function to be able to execute the program.

i tried with scanf(), but i didnt knw wt to write in it.

i guess i've got a knick with the functions .

if it 'd make any diff. i run it under OpenSUSE 10.3

CPP / C++ / C Code:
#include <stdio.h>
#define OK          1
#define ERR       -1
#define FULL       1
#define EMPTY    0
#define MAX_STK 100

int check_paren( char data[] ){
   
   int i;
   
   for( i = 0 ; data[i] != NULL ; i++){
      switch( data[i] ){
   
      case'{':
      case'[':
      case'(':
         push( data[i] );
         break;
      case'}':
         if( empty() || pop() != '{' );
            return(ERR);
         break;
      case']':
         if( empty() || pop() != '[' );
            return(ERR);
         break;
      case')':
         if( empty() || pop() != '(' );
            return(ERR);
         break;
      }
   }

   if( empty() )
      return( ERR );
   break;
}
push( int new ){

   if( top+1 >= MAX_STK )
      return( FULL );

   stack[++top] = new ;
   return( Ok ) ;
}

pop(){
   if( top < 0)
      return( EMPTY );
   return( stack[top--] ) ;
}

}

int main(){}

if there's any corrections to my code please highlight it and explain it if it's not a prob.

thnx
Last edited by LuciWiz : 01-May-2008 at 10:37. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 02-May-2008, 05:15
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 53
mamntc02 has a spectacular aura about

Re: need help with brackets check using stacks


Hi,

You have some errors in your code:
  • stack and top aren't declared.
  • empty function is not declared and defined. You should also declare all other functions.
  • You cannot use reserved words (as new) as variables.
  • This code:
    CPP / C++ / C Code:
    if( empty() || pop() != '{' );
       return(ERR);
    has no sense, because it'll always return ERR. So, remove the semicolon (; )
  • This expression
    CPP / C++ / C Code:
    data[i]!=NULL
    is not correct, because data[i] is a char value and not a pointer. Although some compilers will not complain (depending how NULL is defined), is not correct.
Running your function is very simple, once you've corrected the code. For example:
CPP / C++ / C Code:
int main(){
	char test[]="(fg{[{}]11}fsdf)";

	printf("Ret val = %d", check_paren(test));
	return 0;
}
Regards
__________________
Please, correct me if I'm wrong, and sorry for my english ;)
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How Can I Check on Database asdfg Java Forum 6 01-Mar-2008 08:19
MFC combo check box does not work ktcm MS Visual C++ / MFC Forum 2 12-Aug-2007 09:18
Need help understand function prototypes! Blstretch CPP / C++ Forum 20 25-Oct-2005 14:14
firewall, check referer, no good dopee MySQL / PHP Forum 0 23-Dec-2004 09:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:30.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.