Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Stacks can be used to check whether a mathematical expression has balanced parentheses ( ), braces { }, and brackets [ ]. For example, a

Stacks can be used to check whether a mathematical expression has balanced parentheses ( ), braces { }, and brackets [ ]. For example, a balanced expression is "{(x + y), [x + (y + z)]}". An unbalanced expression: a) Closes an unopen parenthesis/brace/bracket; or b) Closes a parenthesis/brace/bracket before closing the latest open parenthesis/brace/bracket; or c) Ends before closing all open parentheses/braces/brackets. For example, unbalanced expressions are "[x + (y + z)" or "[x + (y + z])".

Write the function int isBalanced(char* s) in stackapp.cimage text in transcribed that returns 1 if the input expression is balanced, or 0 otherwise. Alternative solutions that produce correct results BUT DO NOT USE a stack will not receive credit.

Your function should read through the input string using the function nextChar(char* s), which has already been implemented for you. You may use the provided main function for testing your code.

Note that you would need to fix the data type in dynArray.h as #define TYPE char

the code of stackapp.c that needs to be fixed:

/* stackapp.c: Stack application. */

#include

#include

#include

#include "dynArray.h"

/* ***************************************************************

Using stack to check for unbalanced parentheses.

***************************************************************** */

/* Returns the next character of the string, once reaches end return '0'

(zero)

param: s pointer to a string

pre: s is not null

*/

char nextChar(char* s)

{

static int i = -1;

char c;

++i;

c = *(s+i);

if ( c == '\0' )

return '\0';

else

return c;

}

/* Checks whether the (), {}, and [] are balanced or not

param: s pointer to a string

pre: s is not null

post:

*/

int isBalanced(char* s)

{

char ch; /*stores the current character from the input string*/

char ts; /*stores the top element of the stack*/

int b=1; /*Boolean variable b=1 means balanced; b=0 means

unbalanced string*/

DynArr *stack;

stack=newDynArr(5);/*initialize stack with capacity = 5*/

if (s && strlen(s))

while(1)

{

ch=nextChar(s);

if(ch==0 || ch=='\0')

break;

if(ch=='(' || ch=='[' || ch=='{' )

pushDynArr(stack,ch);

else

{

/* FIXME: You will write this part of the function

*/

}

}

/* Free the memory allocated to stack, and return b=1 or b=0 */

/* FIXME: You will write this part of the function */

}

int main(int argc, char* argv[]){

char* s=argv[1];

/*

char s[]="()+x+r*{{{((--{{[()[]]}}))}}}";

*/

int res;

printf("Assignment 2 ");

if(argc==2)

{

res = isBalanced(s);

if (res)

printf("The string %s is balanced ",s);

else

printf("The string %s is not balanced ",s);

}

else

printf("Please enter a string to be evaluated. ");

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Records And Database Management

Authors: Jeffrey R Stewart Ed D, Judith S Greene, Judith A Hickey

4th Edition

0070614741, 9780070614741

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago