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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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 <stdio.h>
#include <stdlib.h>

#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;
}
}
Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Heapsort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education