Question

In: Computer Science

Using STL stack class, implement in C++ the following pseudocodefunctioncheckBraces(aString: string) that checks for...

Using STL stack class, implement in C++ the following pseudocode function

checkBraces(aString: string) that checks for balanced braces { } in a given string /

arithmetic expressions. Write a main() program to test the function.

// Checks the string aString to verify that braces match.

// Returns true if aString contains matching braces, false otherwise.

checkBraces(aString: string): boolean{

aStack = a new empty stack
balancedSoFar = true
i =0
// Tracks character position in string
while (balancedSoFar and i < length of aString) {

ch = character at position i in aString i++
// Push an open brace
if (ch is a '{')

aStack.push('{')

// Close brace

else if (ch is a '}')

{ if (!aStack.isEmpty())
aStack.pop() // Pop a matching open brace

else
balancedSoFar = false

}

// No matching open brace // Ignore all characters other than braces

}
if (balancedSoFar and aStack.isEmpty())

aString has balanced braces else

aString does not have balanced braces

}

Solutions

Expert Solution

#include

#include

using namespace std;

//required function to check balanced braces

//acc to pseudocode

bool checkBraces(string aString){

  stack aStack;

  bool balancedSoFar = true;

  int i = 0;

  while(balancedSoFar && i < aString.size()){

      char ch = aString[i];

      i++;

      if(ch == '{'){

          aStack.push(ch);

      }

      else if(ch == '}'){

          if(!aStack.empty()){

              aStack.pop();

          }

          else balancedSoFar = false;

      }

  }

  if(balancedSoFar && aStack.empty()){

      return true;

  }

  else return false;

}

int main(){

  string aString;

  cout << "Enter the string to check for matching braces: ";

  cin >> aString;

  if(checkBraces(aString))cout << "balanced braces" << endl;

  else cout << "unbalanced braces" << endl;

}


Sample IO:


Related Solutions

Using STL stack class, implement in C++ a function that checks for balanced braces { },...
Using STL stack class, implement in C++ a function that checks for balanced braces { }, in a given string / arithmetic expressions.
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
Write a C function to implement operation of a stack using the following format: /** *...
Write a C function to implement operation of a stack using the following format: /** * function: *       push * * expects: *       pointer to the stack *       pointer to the size *       the value to push * * returns: *     true when value has been pushed *       false otherwise * * The push function push a value to the passed in stack */ bool push(int *stack, int *size, int max_size, int to_push) {...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked...
Description( IN C++)!! The purpose of this challenge is to implement a stack using a Linked List as a backing data structure Requirements Write the following structs struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; }; Create a class called Stack. In this class, create a private variable VisitNode * head. This will keep track of the location of the head node. Add the following private function. This function will be used...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that...
Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as the...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class...
StackBox Implement our own stack class patterned after Java's Stack class. Start with a generic class that uses an ArrayList for storage of the elements: public class StackBox<E> { ArrayList<E> stack = new ArrayList<E>(); } Implement the following methods in StackBox: boolean empty() Tests if this stack is empty. E push(E item) Pushes an item onto the top of this stack. Returns item pushed. E pop() Removes the object at the top of this stack and returns that object as...
In c++, using stack structure, write a program that will take a sequence of characters (string)...
In c++, using stack structure, write a program that will take a sequence of characters (string) and determine whether it is a palindrome. Use the linked version of the stack.
c++ problem: Use an STL stack to reverse a line of input characters that are read...
c++ problem: Use an STL stack to reverse a line of input characters that are read into a string. Your stack should contain the characters of the user's string. Use getline() for input – it needs to be part of your C++ tool inventory. A note on getline: Suppose I am doing the following - This program reverses a string using the STL stack Enter your string of less than 80 characters followed by an ENTER a string input Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT