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

You can use a simple array as your stack (you must have a stack). You can static

ID: 3756234 • Letter: Y

Question

 You can use a simple array as your stack (you must have a stack). You can statically declare the stack to be of size 100. You must have a PUSH  routine and a POP routine. The PUSH and POP routines must get the stack  passed to them and must return the modified stack to the main routine.  The algorithm is as follows:  1. Clear the stack (why?) 2. Open the data file (a C program) 3. Scan the data file    A. If you find a { push the stack    B. If you find a } pop the stack    C. If you find a new line character " "        a. Increment your line counter       b. Test the stack for left over { or } and           print error message if there are 4. If EOF, print appropriate message.  Your program MUST generate output as shown below.   Sample output: -------------- There are too many { in line 116 There are too many } in line 130. End of file.

Explanation / Answer

The Balanced Parenthesis:

#include<bits/stdc++.h>
#define LL long long int
#define s(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define ss(a) scanf("%s",a)
#define w(t) while(t--)
#define f(i,n) for(i=0;i<n;i++)
#define fd(i,n) for(i=n-1;i>=0;i--)
#define p(a) printf("%d",a)
#define pl(a) printf("%lld",a)
#define ps(a) printf("%s",a)
#define pc(a) printf("%c",a)
#define ent printf(" ")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin,s)
#define pb push_back
#define mp make_pair

using namespace std;

stack<LL> st;

int main()
{
   string s;
   cin>>s;
   LL len=s.length(),i;
   for(i=0;i<len;i++)
   {
       if(s[i]=='('||s[i]=='{'||s[i]=='[')
       st.push(s[i]);
       else
       {
           switch(s[i])
           {
               case ')': if(st.top()!='(') {cout<<"Invalid Expression "; return 0;} st.pop(); break;
               case '}': if(st.top()!='{') {cout<<"Invalid Expression "; return 0;} st.pop(); break;
               case ']': if(st.top()!='[') {cout<<"Invalid Expression "; return 0;} st.pop(); break;
           }
       }
   }
   if(st.empty())
   cout<<"Valid Expression ";
   else
   cout<<"Invalid Expression ";
}

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