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

Q: Write a program which check the validity of a mathematical expression using s

ID: 3620758 • Letter: Q

Question

Q:
Write a program which check the validity of a mathematical expression using stacks.what i am saying is :
e.g
(a+[(b-c)-{d-a}])
is a valid expression because it has equal number of opening and closing parenthesis.
what you have to do is input a string which is actually a mathematical expression as one the above is and whenever an opening bracket comes in the string it should push it to the stack and when a closing bracket comes it should pop the top bracket from the stack if it is the right opening bracket against that of closing bracket of the string.
if the expression is valid show message "Valid Expressing " and if the expression is invalid show message " invalid Expression ".
if top of the stack has '(' and the string character is '}' then the expression is wrong.
check these examples for insurance that it is working fine .
1). (a+b)
2). (((a-b)
3). ()))
4). ]
5). [a-{b+(c-d)}}]

Explanation / Answer

please rate - thanks #include<iostream>
using namespace std;
char pop(char[], int& );
void push(char[],int&,int, char) ;
bool checkinput(string,int,char[]);
int main()
{ int maxsize=20;
char stack[maxsize];
string input;
bool good;
cout<<"Enter the string who's parenthesis you want checked: ";
cin>>input;
good=checkinput(input,maxsize,stack);
if(good)
    cout<<"valid parenthesis ";
else
     cout<<"invalid parenthesis ";
system("pause");
return 0;
}
bool checkinput(string input,int maxsize,char stack[])
{char m=' ';
char opens[3]={'[','{','('};
char closes[3]={']','}',')'};
int n=0,i=0,j;
while(input[i]!='')
   {for(j=0;j<3;j++)
       {if(input[i]==opens[j])
            push(stack,n,maxsize,input[i]);
        else
              if(input[i]==closes[j])
                 { m=pop(stack,n);
                     if(m!=opens[j])
                        return false;
                     
                  }
      }
    i++;
    }
if(n==0)
   return true;
else
    return false;
}  
char pop(char a[], int& n)
{if (n <= 0)
        {cout<<"Stack underflow - program aborted ";
          system("pause");
          system("exit");
          }
   n--;
   return a[n];
}
void push(char a[], int& n, int maxsize, char val)
{if (n >= maxsize)
        {cout<<"Stack overflow - program aborted ";
          system("pause");
          system("exit");
          }
   a[n] = val;
   n++;
   return;
}