Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Check Expression with Stack
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STACK_SIZE 100 #define MAX_STRING_SIZE 100 typedef int element; typedef struct { element *data; int capacity; int top; }StackType; void init_stack(StackType *s) { s->top = -1; s->capacity = 1; s->data = (element *) malloc(s->capacity * sizeof(element)); } int is_empty(StackType *s) { return (s->top == -1); } int is_full(StackType *s) { return(s->top == MAX_STACK_SIZE - 1 ); } void push(StackType *s,element item) { if(is_full(s)) { s->capacity *= 2; s->data = (element *) realloc(s->data,s->capacity * sizeof(element)); } s->data[++(s->top)] = item; } element pop(StackType *s) { if(is_empty(s)) { fprintf(stderr,"스택이 비었습니다."); exit(1); } else return s->data[(s->top)--]; } int check_matching(char *expr) { StackType *s; s = (StackType *) malloc(sizeof(StackType)); char ch,open_ch; int i, n = strlen(expr); init_stack(s); for(i = 0; i < n; i++){ ch = expr[i]; switch (ch) { case '(': case '{': case '[': push(s,ch); break; case ')': case '}' : case ']': if(is_empty(s)) return 0; else { open_ch = pop(s); if((open_ch == '(' && ch != ')') || (open_ch == '{' && ch != '}') || (open_ch == '[' && ch != ']')) { return 0; } break; } } } if (!is_empty(s)) return 0; return 1; } int main(void) { char *p = "{ A[(i+1))]=0; }"; if (check_matching(p) == 1) printf("%s 괄호검사성공\n",p); else printf("%s 괄호검사실패\n",p); return 0; }
run
|
edit
|
history
|
help
0
TBod
1
prestupny rok
scitani matic
TStack_array
TBod new
hrátky s ukazately ++ bonusy ++ profi swap
vyuziti struktury
Check Expression with Stack
insertionrandom