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
Alona [7]
3 years ago
9

Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and

square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid.
Computers and Technology
1 answer:
Phoenix [80]3 years ago
5 0

Answer:

Explanation:

The program is written in C++

/*

 C++ Program to check for balanced parentheses in an expression using stack.

 Given an expression as string comprising of opening and closing characters

 of parentheses - (), curly braces - {} and square brackets - [], we need to  

 check whether symbols are balanced or not.  

*/

#include<iostream>

#include<stack>

#include<string>

using namespace std;

// Function to check whether two characters are opening  

// and closing of same type.  

bool ArePair(char opening,char closing)

{

if(opening == '(' && closing == ')') return true;

else if(opening == '{' && closing == '}') return true;

else if(opening == '[' && closing == ']') return true;

return false;

}

bool AreParanthesesBalanced(string exp)

{

stack<char>  S;

for(int i =0;i<exp.length();i++)

{

 if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')

  S.push(exp[i]);

 else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')

 {

  if(S.empty() || !ArePair(S.top(),exp[i]))

   return false;

  else

   S.pop();

 }

}

return S.empty() ? true:false;

}

int main()

{

/*Code to test the function AreParanthesesBalanced*/

string expression;

cout<<"Enter an expression:  "; // input expression from STDIN/Console

cin>>expression;

if(AreParanthesesBalanced(expression))

 cout<<"Balanced\n";

else

 cout<<"Not Balanced\n";

}

You might be interested in
What is the value of the variable result after these lines of code are executed?
Crank

Answer:

18

Explanation:

If you take the formula, and you substitute the values of the variables, it will be:

    10 * 2 - 10 / 5

Then if you remember the order of math operations, it will be:

    (10 * 2) - (10 / 5)

Which reduces to:

    20 - 2 = 18

4 0
3 years ago
By incorporating video games into classwork, teachers would help students become more confident decision makers.
dangina [55]

teachers would help students become more confident decision makers.

7 0
4 years ago
Read 2 more answers
I need help under standing an assignment, I've contacted my teacher but I want to finish this class already. Here's the directio
Verizon [17]

Answer: So have you done assignment 5? and have you read back the previous one?

8 0
4 years ago
Need help ASAPppppppppp
Darina [25.2K]

Answer:

Below picture should give you an indication. On different PC cases, the position of the connectors can be different, so you have to look at the shape.

5 0
3 years ago
Your company has just purchased an iSCSI storage array. You have installed a new iSCSI device onto your Ethernet network and you
Mars2501 [29]

Answer:

D) ISCSI Initiator

Explanation:

5 0
4 years ago
Other questions:
  • The technology that identifies people through bodily characteristics is known as
    10·1 answer
  • Which unique address is a 128-bit address written in hexadecimal?
    13·1 answer
  • What panel in the unity Editor shows the objects in the scene
    11·1 answer
  • Your company has just opened an office in another state and you need the computers to communicate between the offices. What type
    13·1 answer
  • explain the following with regard to a microcomputer. 1)program 2)stored program concept 3)instruction decoder
    5·1 answer
  • What do workspaces allow a company to do?
    9·1 answer
  • Northern Trail Outfitters stores Last Name in a data extension. How should the text file be defined?
    5·1 answer
  • A system analyst generally needs to have a number of skills. For example, technical and analytical skills are required for this
    7·1 answer
  • A time management tool in a help desk software package probably has the greatest impact on the productivity of _____.
    8·1 answer
  • What is a use case of factorization in quantum computing?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!