Created
March 30, 2012 11:26
-
-
Save anonymous/2250912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <assert.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include "stack.h" | |
int check(char* expression){ | |
int i; | |
for(i=0; i<= strlen(expression); i++){ | |
if(expression[i]=='(') | |
{ | |
push('('); | |
} | |
else if(expression[i]==')') | |
{ | |
is_empty(); | |
pop('('); | |
} | |
if(expression[i]=='{') | |
{ | |
push('{'); | |
} | |
else if(expression[i]=='}') | |
{ | |
is_empty(); | |
pop('{'); | |
} | |
if(expression[i]=='[') | |
{ | |
push('['); | |
} | |
else if(expression[i]==']') | |
{ | |
is_empty(); | |
pop('['); | |
} | |
} | |
is_empty(); | |
} | |
void test(void){ | |
assert(check("")); | |
assert(check("()")); | |
assert(check("()()")); | |
assert(check("(())")); | |
assert(check("{}")); | |
assert(check("{}{}")); | |
assert(check("{{}}")); | |
assert(check("[]")); | |
assert(check("[][]")); | |
assert(check("[[]]")); | |
assert(check("{[]}")); | |
assert(check("([{}])")); | |
assert(check("[{{{()}}}]")); | |
assert(check("([{()}])")); | |
assert(!check(")(")); | |
assert(!check("(")); | |
assert(!check(")")); | |
assert(!check("}{")); | |
assert(!check("{")); | |
assert(!check("}")); | |
assert(!check("][")); | |
assert(!check("[")); | |
assert(!check("]")); | |
assert(!check("(()")); | |
assert(!check("())")); | |
assert(!check("{{}")); | |
assert(!check("{}}")); | |
assert(!check("[[]")); | |
assert(!check("[]]")); | |
assert(!check("{)")); | |
assert(!check("({])")); | |
assert(!check("{[({)]}")); | |
assert(!check("[({)]")); | |
assert(!check("[(}]")); | |
assert(!check("{{[}}")); | |
} | |
int main(void){ | |
test(); | |
clean_stack(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <assert.h> | |
char values[100]; | |
int index=0; | |
int push(char c){ | |
c = values[index++]; | |
} | |
int pop(char c){ | |
if(index>0) | |
{ | |
c = values[--index]; | |
} | |
else if(index<0) | |
{ | |
return 0; | |
} | |
} | |
int is_empty(void){ | |
return index==0; | |
} | |
int clean_stack(void){ | |
index=0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
int push(char c); | |
int pop(char c); | |
int is_empty(void); | |
int clean_stack(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment