Question
C++
Include a Stack class but DO NOT use STL stack, do not sumbit code that is similar to www.cplusplus.com/forum/beginner/192479/
Parenthesis Balance Problem Description Compilers require parenthesis, braces, brackets, and angled brackets to be balanced.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ program :-


#include<bits/stdc++.h>
using namespace std;
  
bool is_expr_balanced(string expr)
{
stack<char> s;
char x;
  
// Traversing the Expression
for (int i=0; i<expr.length(); i++)
{
if (expr[i]=='('||expr[i]=='['||expr[i]=='{'||expr[i]=='<')
{
// Push the element in the stack
cout<<expr[i];
s.push(expr[i]);
continue;
}
  
switch (expr[i])
{
case ')':
// Store the top element in a
if (s.empty()){
cout<<" Too many closes "<<expr[i];
return false;
}
x = s.top();
s.pop();
if (x=='{' || x=='[' || x == '<') {
cout<<" No close "<<expr[i]<<endl;
return false;
}
else
cout<<expr[i];
break;
  
case '}':
// Store the top element in b
if (s.empty()){
cout<<" Too many closes "<<expr[i];
return false;
}
x = s.top();
s.pop();
if (x=='(' || x=='[' || x=='<'){
cout<<" No close "<<expr[i]<<endl;
return false;
}
else
cout<<expr[i];
break;
  
case ']':
// Store the top element in c
if (s.empty()){
cout<<" Too many closes "<<expr[i];
return false;
}
x = s.top();
s.pop();
if (x =='(' || x == '{' || x=='<') {
cout<<" No close "<<expr[i]<<endl;
return false;
}
else
cout<<expr[i];
break;
case '>':
// Store the top element in c
if (s.empty()){
cout<<" Too many closes "<<expr[i];
return false;
}
x = s.top();
s.pop();
if (x =='(' || x == '{' || x == '[') {
cout<<" No close "<<expr[i]<<endl;
return false;
}
else
cout<<expr[i];
break;
}
}
  
  
// Check Empty Stack
return (s.empty());
}
  
// Driver program to test above function
int main()
{
string expr;
  
cout<<"Enter set : ";
getline (cin, expr);
cout<<endl;
  
if(expr[0] == '0'){
cout<<"Exiting"<<endl;
return 0;
}
  
if (is_expr_balanced(expr))
cout << " This set is balanced";

return 0;
}

Output :-

Stdin Inputs... Result... CPU Time: 8.8 sec(s), Memory: 3272 kiLobyte(s) Enter set: ◇[ No close >

Stdin Inputs... Result... CPU Time: 8.88 sec(s), Memory: 3276 kilobyte(s) Enter set: [1 Too many closes)

Stdin Inputs... Result... CPU Time: θ.ee sec(s), Memory: 3256 kilobyte(s) Enter set: (()()) This set is balanced

Stdin Inputs... Result... CPU Time: 8.88 sec(s), Memory: 3188 kilobyte(s) Enter set: Exiting

Add a comment
Know the answer?
Add Answer to:
C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please give me that correct code for this by using C++ language. and i hope you...

    Please give me that correct code for this by using C++ language. and i hope you use Visual stduio Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }.  For example, the string  {a(b+ac)d[xy]g} and  kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match...

  • USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class...

    USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class StaticStack { private: int *stack; int capacity; int top; // index of the top element public: StaticStack(int size); // constructor ~StaticStack(); bool isFull(); bool isEmpty(); void push(int); int pop(); }; #include "StaticStack.h" #include <iostream> using namespace std; StaticStack::StaticStack(int size) { stack = new int[size]; // constructing a size sized array capacity = size; top = -1; // empty stack    } StaticStack::~StaticStack() { delete...

  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

  • This lab will give you a practice with both queue and stack ADTs. In this work,...

    This lab will give you a practice with both queue and stack ADTs. In this work, you are to determine if an input string is a palindrome. A string of characters is a palindrome if and only if it reads the same forward and backward. Examples: eye, abba, civic, radar, and so on. Sample Output: Please enter a string of characters: abba The given string is a palindrome. Want to examine another string? (y/n): y Please enter a string of...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

  • Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty...

    Implement the Stack Class with an ArrayList instead of an array, including the following functions: -empty -push -peek -pop -overrided toString() function which returns all of the stack's contents Things to note: -You no longer need a size. -You no longer need to define a constant DEFAULT_CAPACITY. since ArrayLists grow dynamically. -Whenever possible, use ArrayList functions instead of the [ ] (index operator) to implement your stack functions Then write a driver program to do palindrome check. A string is...

  • JAVA PROBLEM! PLEASE DO IT ASAP!! REALLY URGENT! PLEASE DO NOT POST INCOMPLETE OR INCORRECT CODE...

    JAVA PROBLEM! PLEASE DO IT ASAP!! REALLY URGENT! PLEASE DO NOT POST INCOMPLETE OR INCORRECT CODE Below is the program -- fill the code as per the instructions commented: //---------------------------------------------------------------------------------------------------------------------------------------------------------------// /* The idea of this HW is as follows : A password must meet special requirements, for instance , it has to be of specific length, contains at least 2 capital letters , 2 lowercase letters, 2 symbols and 2 digits. A customer is hiring you to create a class...

  • A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here...

    A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT