Question

c++ program to implement a simple program that verifies whether an expression contains balanced braces

c++ program to implement a simple program that verifies whether an expression contains balanced braces

Homework Answers

Answer #1

Code:

#include <bits/stdc++.h>
using namespace std;

bool BalancedBraces(string expr) // Function to check if braces are balanced
{
   stack<char> s;
   for (int i = 0; i < expr.length(); i++)     // Traversing the Expression
   {
       if (expr[i] == '{')
           s.push(expr[i]); // Push '{' in the stack
       else if (expr[i] == '}')
           s.pop(); // Pop '{' from the stack
   }
if(s.empty())
return true;
else
return false;
}

int main()
{
string expr;
cout << "Enter the expression : ";
cin >> expr;
   if (BalancedBraces(expr))
       cout << "Braces are balanced.";
   else
       cout << "Braces are not balanced.";
   return 0;
}

Please refer to the screenshot of the code to understand the indentation of the code:

Output:

1.

2.

For any doubts or questions comment below.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement a C++ program to develop a simple Library. Library contains pile of books and each...
Implement a C++ program to develop a simple Library. Library contains pile of books and each book comprises of ISBN (cannot be changed), Name, Author Name, Publisher Name, Issue Date, Return Date. [using Structures]
1) Write the assembly code to implement expression A = B + C * ((D -...
1) Write the assembly code to implement expression A = B + C * ((D - E) * F) on 3-, 2-, 1-, and 0- address machines. Do not rearrange the expression. In accordance with "good programming", computing the expression should not change the values of its operands. Feel free to use a temporary variable, perhaps called T, if you feel you need one. 2) For the four code implementations of (1), compute the total size of the program, assuming:...
Write a program to implement a Distributed chat server using TCP sockets in ‘C’. Run the...
Write a program to implement a Distributed chat server using TCP sockets in ‘C’. Run the program and screen shot the Output Please.
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code
Implement a simple program to illustrate one producer and one consumer problem. The producer will produce...
Implement a simple program to illustrate one producer and one consumer problem. The producer will produce 100 messages for the consumer to consume. Use an array of 10 slots to hold the contents of the buffer. The producer should send a sequence of integers to the consumer. Specifically, it should send the sequence 1, 2, 3, ..., 100, in that order. The consumer should receive them and check that it has indeed received exactly that sequence, in that order. Implement...
Goal: Design and build a simple cash register program in C++ using the programming constructs included...
Goal: Design and build a simple cash register program in C++ using the programming constructs included (simple expressions, a loop, and if statements). Problem: A company needs a cash register program to work on their new computer system. The program should first ask whether the payment is in cash and credit card. If it is a credit card transaction, you must get the card pin. The program will then read in a number of prices to read, finally it must...
An array of characters contains a few letters. Write a complete C program that will display...
An array of characters contains a few letters. Write a complete C program that will display the output as shown below. Lets assume the array contains =”abcde”. The program should be able to work with any array size, configurable in the program. Expected output Original array = [ a b c d e] a a b a b c a b c d a b c d e
Let s be a string that contains a simple mathematical expression, e.g., s = '1.5 +...
Let s be a string that contains a simple mathematical expression, e.g., s = '1.5 + 2.1-3' s = '10.0-1.6 + 3 - 1.4' The expression will have multiple operands. We don't know exactly how many operands there are in s. The operator will be one of the following: +, -. Write a function called plus_minus() that takes s as the input, interpret the expression, then evaluate and return the output. Note: The use of the eval() is not allowed...
The code needs to be in C #. Write a simple program that reads from the...
The code needs to be in C #. Write a simple program that reads from the console a set of numbers and prints back onto the console the smallest number from the collection
Your assignment is to implement a computer program in C++ to implement the below application. According...
Your assignment is to implement a computer program in C++ to implement the below application. According to Dummies.com the following algorithm determines the amount of paint you need to paint the walls of a four-sided room: 1. Add together the length of each wall. (For example, if each of the four walls are 14, 20, 14, 20 feet respectively then the total length is 14 + 20 + 14 + 20 = 68 feet) 2. Multiply the sum by the...