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
Absolute time would be most similar to :
mr Goodwill [35]
The absolute time would be most similar to a fact. It was theorised and in fact, approved by scientists as a scientific law which governs, according to Wikipedia, as a "<span>true and mathematical </span>time<span>, of itself, and from its own nature flows equably without regard to anything external, and by another name is called duration: relative, apparent and common </span><span>time."</span>
6 0
3 years ago
Which of the following generally does not apply to all seven domains of an IT infrastructure? Incident response Service level ag
almond37 [142]

Answer:

Option C, Disaster recovery plan

Explanation:

The seven domains of IT are

User Domain

System/Application Domain

LAN Domain

Remote Access Domain

WAN Domain

LAN-to-WAN Domain

Workstation Domain

To these seven domain, the Disaster recovery plan only applies to the LAN-to-WAN Domain as it is vulnerable to corruption of data and information and data. Also, it has  insecure Transmission Control Protocol/Internet Protocol. (TCP/IP) applications and it at the radar of Hackers and attackers

4 0
2 years ago
To edit the text in a SmartArt graphic, what must be done first?
chubhunter [2.5K]
You have to go to where you first began the SmartArt then click on it to correct it. Or just double tap it and it should let you
3 0
3 years ago
Which of the following can a cell contain?
vredina [299]
I think it could be All of these.....
5 0
3 years ago
Read 2 more answers
It is essential for a relay energized by alternating current to have
Murrr4er [49]
A key difference in an AC relay is the presence of a "shading coil". The purpose of this coil is to keep the relay on when the current drops to zero during phase change (note that in AC power the current and voltage are alternating in a sine wave around 50/60 times per second). The shading coil retains some magnetic energy and essentially holds the relay ON when the current drops to zero momentarily due to the AC waveform.
5 0
3 years ago
Other questions:
  • The MIQ inventory measures how much you value status. Which two measures are measures of status?
    11·1 answer
  • Mrs. Jackson wrote a newsletter to the customers of her housecleaning business that included some organizational tips they could
    8·2 answers
  • A network address is 131.247.160.0/19. The 19 implies that
    5·1 answer
  • HELP 11 pointsw to the person who helps me
    11·2 answers
  • One of the first signals that an organization is making progress in the development of its IR program, specifically in the devel
    6·1 answer
  • Items that are cut or copied are placed on the Clipboard.
    6·2 answers
  • What is a system unit
    7·1 answer
  • Trading stock or selling stock, selling real estate for profit, and selling other assets that gain value over time.
    13·1 answer
  • Use your editor to open the cc_data.js file and study the data stored in the staff object to become familiar with its contents a
    6·1 answer
  • CALLING ALL COMPUTER SCIENCE LOVERS!!!!!!!
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!