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
Which part of color theory deals with how colors on a web page relate to each other?
Elena L [17]

Answer:

C) Matching

Explanation:

5 0
4 years ago
Read 2 more answers
The ____ keyword specifies that a function or method does not return a value.
dedylja [7]
The void keyword. Otherwise you use a type name, such as int or char or string.
4 0
3 years ago
What was a result of george washington's belief in the sovereignty of the people instead of monarchy?
Ilya [14]
<span>George Washington's belief in the sovereignty of the people instead of monarchy led him to reject a third term as president.</span>
8 0
3 years ago
What can you add to your presentation from the insert ribbon toolba?
stiks02 [169]

Answer:

anything you want

Explanation:

there are a lot of adons for powerpoint

7 0
3 years ago
Plz answer it’s timed
MAVERICK [17]

Answer: the third one

Explanation:

just trust me, the other ones dont mke sense to what I know about the subject, which is a lot

7 0
3 years ago
Other questions:
  • OBJECTIVE This project will familiarize you with the use of pthreads, pthread mutexes and pthread condition variables. Your prog
    9·1 answer
  • What is telepresence
    8·1 answer
  • These systems consist of interlinked knowledge resources, databases, human experts, and artificial knowledge agents that collect
    9·1 answer
  • I NEED HELP NOW PLEASE!!!!!!
    6·2 answers
  • 20 Points and Brainliest 5. What is the difference between a real-time light and a shadow map? Which of the two would you use if
    15·1 answer
  • Using javaFX components
    6·1 answer
  • The results of the SPEC CPU2006 bzip2 benchmark running on an AMD Barcelona has an instruction count of 2.389E12, an execution t
    6·1 answer
  • Consider the following code:
    13·1 answer
  • Anyone know the friends song by Marshmello? It's stuck in my head.
    12·2 answers
  • 3.4.6 colorful bracelet ?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!