Question: 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.cStacks can be used to check whether a mathematical expression has balanced 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!