1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Elan Coil [88]
2 years ago
5

Matching parentheses. An math expression may have a number of parentheses like (, ), [, ], { and }. Each openning parenthesis (,

or [, or { must be macthed by a corresponding closing parenthsis ), or ] or }. For example, 12 { 34 / [ 6 * ( 55 - 10 ) / ( 100 20 ) ] } has matched pairs of parentheses, while 12 { 34 / ( 6 * ) ] } does not. Write a function to check whether an input math expression has matched parentheses. The header of the function is given as follows:
bool match( const char exp [ ], const int s);
The input math express is store in the char array exp, and the size of exp is s. It returns true if all parentheses are matched or false otherwise.
Computers and Technology
1 answer:
Solnce55 [7]2 years ago
3 0
C++ Code

#include
using namespace std;

bool match(const char exp[],const int s)
{
// declare a character array to perform stack operations
char stack[s];

// declare top and initialize to -1 and flag to 0
int top=-1,i,flag=0;

// visit all characters in the expression string
for(i=0;i {
// if the character is [ or ( or { then push it into stack
if(exp[i]=='[' || exp[i]=='(' || exp[i]=='{')
{
top++;
stack[top]=exp[i];
}
// if the character is ] or ) or } then check conditions
else if(exp[i]==']' || exp[i]==')' || exp[i]=='}')
{
// check stack is empty or not
if(top!=-1)
{
// check all possible failure conditions
if(exp[i]==')' && (stack[top] == '{' || stack[top]=='['))
{
flag = 1;
break;
}
else if(exp[i]==']' && (stack[top] == '{' || stack[top]=='('))
{
flag = 1;
break;
}
else if(exp[i]=='}' && (stack[top] == '(' || stack[top]=='['))
{
flag = 1;
break;
}
top--;
}
else
{
flag=1;
break;
}
}
}
// after visiting all characters of expression string check if stack is not empty and flag is 1. if any one of the condition is true return false. otherwise return true

if(top>=0 || flag==1)
return false;
else
return true;
}

int main()
{

// declare character array to store expression
char exp[10000];
cout<<"Enter an Expression"<
// read expression from user
cin.getline(exp, 10000);
int s=0;

// find the length of the expression string
for(int i=0;exp[i]!='\0';i++)
{
s++;
}

// call the match function
bool status = match(exp,s);

// print the result based on value returned by match() function
if(status == 1)
cout<<"true"< else
cout<<"false"<
}


Sample Input/Output is attached

You might be interested in
What is the statement below an example of?
Arte-miy333 [17]
I would assume that your answer would be

E) job description

Hope I helped!!
6 0
4 years ago
Read 2 more answers
This is the full version
spayn [35]

i can't really see the numbers

6 0
3 years ago
A(n) _____ is a combination of a transmitter and a receiver in a single package that may share some circuits. modulator oscillat
erma4kov [3.2K]
The combination of a transmitter and a receiver in a single cell package is a transceiver 
4 0
3 years ago
The sugar-water mixture is a solution.<br><br> True<br> False
Mademuasel [1]
Although true, this is a very simple mixture.
7 0
3 years ago
When troubleshooting vpn connectivity, which windows and linux command can you use to verify your internal port?
Dmitrij [34]
The answer is ping = A; 
5 0
3 years ago
Other questions:
  • What is authentication?
    8·1 answer
  • Under the guise of justice, some less scrupulous administrators may be tempted to ____________________, or hack into a hacker’s
    12·2 answers
  • ¿Cómo es la onda percibida por un osciloscopio cuando hablamos de sonido? ¿Qué parámetros podemos observar en ella?
    15·1 answer
  • When you use the tilde operator (~ in a url for the attribute of a server control, it represents the _____ directory of the webs
    8·1 answer
  • In the insert table of figures dialog box which drop down menu do you use to choose what information is displayed in the table
    14·1 answer
  • Suppose you create a class Square to be a subclass of GeometricObject. Analyze the following code:
    8·1 answer
  • A ______________ is a way of taking a screenshot or a picture of your computer screen. ​
    14·1 answer
  • Write a formula that would return a TRUE result if the sum of the first five numbers in a column of data are negative
    7·1 answer
  • What are the difference among the whole note, half note and quarter note ? (this is music)
    12·2 answers
  • Determine whether the compound condition is True or False.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!