#include #include #define bool int #define SIZE 30 typedef struct stack { char symbol; struct stack *link; } stack_t; // Here are my function prototypes. void push (stack_t *new_node, char value); int pop (stack_t *top_node); bool Match_Pair (char group1, char group2); bool is_Balanced (char express[]); int main(void) { char expression[SIZE]; printf("Enter expression: "); scanf("%s", expression); if (is_Balanced(expression)) printf("Balanced\n"); else printf("Not Balanced\n"); return 0; } bool Match_Pair (char group1, char group2) // Used to check if the groupings match { if (group1=='(' && group2==')') return 1; elseif (group1=='{' && group2=='}') return 1; elseif (group1=='[' && group2==']') return 1; else return 0; } bool is_Balanced (char express[]) { int i = 0; stack_t *stack_ptr = NULL; while (express[i]) { if (express[i]=='{' || express[i]=='(' || express[i]=='[') push(stack_ptr, express[i]); if (express[i]=='}' || express[i]==')' || express[i]==']') { push(stack_ptr, express[i]); if (stack_ptr == NULL) return0; elseif (!Match_Pair(pop(stack_ptr), express[i])) return0; } i++; } if (stack_ptr == NULL) return1; // Proves that expression is balanced else return0; // Proves that expression is imbalanced } void push (stack_t *top, char symbol) // This is the push function { stack_t *new_node = (stack_t *)malloc(sizeof(stack_t)); if (new_node == NULL) { printf("Stack is full!\n"); getchar(); exit(0); } new_node -> symbol = symbol; new_node -> link = top; top = new_node; } int pop (stack_t *top) // This is the pop function { char symbol; stack_t *top_node; if (top == NULL) { printf("Stack is empty!\n"); getchar(); return (-1); } else { top_node = top; symbol = top_node -> symbol; top = top_node -> link; free(top_node); return symbol; } }
Sorry, but I'm try to figure out from a sample code on why I get "Not Balanced" readings whenever I try an example expression of "A-(C+B)/[12*D]" and "14*(6+5)", but can't seem to find where in my code that happens. Can you please help me spot the area that I need to debug? Thanks in advance.
Prompt:
Use a stack implementation, to check that a given an arithmetic expression, that uses braces “{“ & ”}” or parenthesis ”(“ &, ”)” or brackets ”[“ & ”]” as grouping symbols, is using them in a matching and balanced way.
The book has an example function, called isBalance( ), that takes the input string as an argument and returns 0 if it is unbalanced or 1 if it is balanced.
(Examples below are given only for illustrative purposes, you may come up with your own.)
Example outputs:
Input: exp = "[ ( ) ] { } { [ ( ) ( ) ] ( ) }"
Output: Balanced
Input: exp = "[ ( ] )"
Output: Not Balanced
Code:
Include necessary header files.
create a structure stack and declare structure variable st and top.
declare the push() and pop ().
In main () get the expression from the user and check the expression are balanced or not using for and if statement.
return 1 if it is balanced and return 0 if it is not balanced.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images