Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment involves the stack data structure and file processing. A common

ID: 651247 • Letter: T

Question

This assignment involves the stack data structure and file processing. A common application of stacks is the parsing and evaluation of arithmetic expressions. Compilers use stacks to check if the brackets in an arithmetic expression are balanced. For example, the expression [(a-b)*(5+4)]/(g/h) is balanced in the sense that for each matching left bracket there is a matching right bracket. On the other hand, the expression [(a-b))] is not balanced because the number of left 'C is not equal to die number of risht *)'- Also, an expression like {(a+x)) would be incorrect because die inner '(' should be closed with a ') first before outer '{' is closed with the'}'.Your task is to write a program that reads an input text file consisting of a list of arithmetic expressions, one expression on each line. Each arithmetic expression may consist of three kinds of brackets, namely. ( ), [ ] and { }. There could either be spaces or no space between the brackets and arithmetic operators and variables. As an example, your input text file could be the following: Your program should read each line and check if it is balanced with respect to the three symbols. For this, you should use a stack. You may use any of the three Stack classes (Stack as an array, Stack as an arraylist or Stack as a linked list) that was discussed in the lectures. Your program should report whether each line is correct or incorrect. The output for the above example is given below. It would be displayed on the screen, not to a file. Note: You need not check the validity of the expression with respect to other parameters, such as addition, subtraction, multiplication, division, etc. Just check if the brackets are balanced. Submit the following: Source code of the program. Stack class source code. Sample text input file. Output

Explanation / Answer


void push(char);
void pop(void);
int stackempty(void);
void balanced(char str[]);
char str[20];
int low=-1;

main()
{
   printf("enter a string:");
   gets(str);
  
   balanced(str);
}

void balanced(char str[])
{
   char s;
   int i=0;
   while(str[i]!='')
   {
       if(str[i]=='('||str[i]=='{'||str[i]=='[')
       {
           push(str[i]);
       }
       if(str[i]==')'||str[i]=='}'||str[i]==']')
       {
           pop();
       }
       i++;
   }
   if(stackempty()==1)
   {
       printf("string is Balanced. Good Day!");
   }
   else
   {
       printf("string is not Balanced. Still, Good Day! :) ");
   }
}

void push(char t)
{
   low++;
   str[low]=t;
}

void pop(void)
{
  
   str[low]='';
   low--;
}
int stackempty(void)
{
   if(low==-1)
   {
       return(1);
   }
   else
   {
       return(0);
      
   }
}

// All The Best. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote