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
The following pseudocode is an example of ____.do stepAdo stepBif conditionC is true thendo stepDelsedo stepEendifwhile conditio
rewona [7]

Answer:

Option d pretest

Explanation:

Given the pseudocode:

  1. do stepA
  2. do stepB
  3. if conditionC is true
  4. then do stepD
  5. else
  6. do stepE
  7. end if
  8. while conditionF is true
  9. do stepG
  10. end while

The pseudocode above shows that there is a pretest before some codes are executed. For example, line 3 check if condition is true then only execute stepD otherwise execute stepE. Line 8 check if conditionF is true then repeatedly execute stepG. These are examples of pretest a condition will must be met (pretest passed) before a block of codes can be executed. This pretest can be seen in if-else statements and also the while condition.  

6 0
3 years ago
What is the part of a file, the .pptx, or .txt etc called?
Lunna [17]
It is called the file extension or filename extension. It is a suffix that indicates the file format.
7 0
3 years ago
Read 2 more answers
Terrence smiles at his customers, helps his coworkers, and stays late when needed. What personal skill does Terrence demonstrate
Salsk061 [2.6K]
Friendliness or it shows that he follows the rules
6 0
3 years ago
Read 2 more answers
2. Give technical terms for
solmaris [256]

Answer:

Cccccccccccccccccccccccccccccccccc

4 0
3 years ago
A general rule for adding text to a slide is ____.
KiRa [710]

A general rule for adding text to  a slide is to use not more than two fonts in a presentation and vary the font  size. Besides adding text you can also import text, photographs, numerical data, and facts from files created in programs such as Microsoft word, excel and access.

5 0
3 years ago
Read 2 more answers
Other questions:
  • What does computer means?
    13·2 answers
  • If you're sending an email from your laptop to a classmate the laptop is the
    11·1 answer
  • Enter the number 2568 into the box below
    14·1 answer
  • Put the following five steps in the order in which you would perform them to use the Paste Special function. 1. Select and copy
    6·1 answer
  • Microsoft's
    8·1 answer
  • The computer output for integer programs in the textbook does not include reduced costs, dual values, or sensitivity ranges beca
    14·1 answer
  • What happens to a message when it is deleted?
    11·1 answer
  • Which influence on spending deals with the motivation to<br> purchase a product?
    12·1 answer
  • Does "remainder of x from y" mean xmody?
    5·1 answer
  • Given a number n, for each integer i in the range from 1 to n inclusive, print one value per line as follows:
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!