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,
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...
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.
Implement a generic MyStack class using templates. A stack allows elements to be added and removed...
Implement a generic MyStack class using templates. A stack allows elements to be added and removed in a last-in, first-out (LIFO) manner. Stacks have an operation called push to place elements at the top of the stack, and another operation called pop to remove and return the element at the top of the stack. The only element on the stack that may be referenced is the one on the top. This means that if two elements are pushed onto the...
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be...
Implement the stack class (called Stack2) using queue, meaning that the only operations that can be used are the ones defined in the Queue class. class Queue: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def enqueue(self, item): self.items.insert(0, item) def dequeue(self): return self.items.pop() def size(self): return len(self.items) The codes to fill: """ 1. Stack2 class Implement stack data structure using queue """ class Stack2: def __init__(self): # Write your definition for __init__ here def isEmpty(self): #...
In Java: Design a class that checks if a String is made of tokens of the...
In Java: Design a class that checks if a String is made of tokens of the same data type (for this, you may only consider four data types: boolean, int, double, or char). This class has two instance variables: the String of data and its delimiter. Other than the constructor, you should include a method, checkTokens, that takes one parameter, representing the data type expected (for example, 0 could represent boolean, 1 could represent int, 2 could represent double, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT