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
Por qué no es tan común el uso de energías mas amigables con el medio ambiente en el país donde vives?
Effectus [21]

Answer:

Explanation:

English:

All forms of electricity generation have an environmental impact on our air, water, and land, but it varies. Of the total energy consumed in the United States, about 40% is used to generate electricity, making electricity use an important part of each person’s environmental footprint.

Producing and using electricity more efficiently reduces both the amount of fuel needed to generate electricity and the number of greenhouse gases and other air pollution emitted as a result. Electricity from renewable resources such as solar, geothermal, and wind generally does not contribute to climate change or local air pollution since no fuels are combusted.

The fuel mix for U.S. electricity generation

The chart below shows that most of the electricity in the United States is generated using fossil fuels such as coal and natural gas. A small but growing percentage is generated using renewable resources such as solar and wind.

Spanish:

Todas las formas de generación de electricidad tienen un impacto ambiental en nuestro aire, agua y tierra, pero varía. Del total de energía consumida en los Estados Unidos, alrededor del 40% se usa para generar electricidad, lo que hace que el uso de electricidad sea una parte importante de la huella ambiental de cada persona.

Producir y usar electricidad de manera más eficiente reduce tanto la cantidad de combustible necesario para generar electricidad como la cantidad de gases de efecto invernadero y otra contaminación del aire emitida como resultado. La electricidad de recursos renovables como la energía solar, geotérmica y eólica generalmente no contribuye al cambio climático ni a la contaminación del aire local, ya que no se queman combustibles.

Combustible para la generación de electricidad en EE. UU.

El cuadro a continuación muestra que la mayor parte de la electricidad en los Estados Unidos se genera utilizando combustibles fósiles como el carbón y el gas natural. Un porcentaje pequeño pero creciente se genera utilizando recursos renovables como la solar y la eólica.

5 0
3 years ago
5
Tom [10]

Answer:

ما هو السؤال هنا؟

Explanation:

من فضلك أجب.

8 0
2 years ago
Read 2 more answers
Discovery of user requirements, existing system evaluation, and logical system design are part of the _____ phase of the Systems
natulia [17]

Answer:

analysis

Explanation:

4 0
2 years ago
Que es un algoritmo informático?
nasty-shy [4]

Answer:

An algorithm is a specific procedure for solving a well-defined computational problem. ... It requires an understanding of the alternatives available for solving a computational problem, including the hardware, networking, programming language, and performance constraints that accompany any particular solution.

4 0
2 years ago
Read 2 more answers
Q1) Convert the decimal number 67 to binary?​
Mashutka [201]

Answer:

1000011

Explanation:

4 0
2 years ago
Other questions:
  • In the Happy Valley School System, children are classified by age as follows: less than 2, ineligible 2, toddler 3-5, early chil
    6·1 answer
  • A program requires that you change your screen size from 1024 X 728 to 800 X 600. In which of the following locations should you
    7·2 answers
  • When a bank account pays compound interest, it pays interest not only on the principal amount that was deposited into the accoun
    15·1 answer
  • PLEASE HELP ASAP!!!
    7·1 answer
  • HELP PLZZZZZZZZ!!!!!!!!!!!
    14·1 answer
  • Pleaseeeeeeeee I will give a brainliest
    7·1 answer
  • Abby has always dreamed of having her own ice cream shop. Now as a young entrepreneur she has decided to pursue her dream, but s
    13·1 answer
  • Why can’t I message people? It doesn’t let me, please help.
    12·2 answers
  • Which of the following are acceptable to share? Check all of the boxes that apply.
    13·1 answer
  • ACTIVITY NO. 5 (DAY 5) FACT OR BLUFF. Write Fact if the statement is correct and write Bluff if it is incorrect.
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!