Question

In: Computer Science

Write a program to validate parenthesis of any given equation by using the following criteria (you...

Write a program to validate parenthesis of any given equation by using the following criteria (you MUST use stacks); Number of open parenthesis “(“must be same as number of close parentheses “)” For example if I input “3 + 4 * (98+34*(34+8)*34*(3+x)” the program should display an error message, because number of open parenthesis “(“is 3 and number of closed “)” parenthesis is 2.

Stack-Driver.cpp:

#include "stack.h"
#include "stack.cpp"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>


int main() {
   stack<int> my_stack(100);
   srand(time(NULL));
   for (int i = 0; i < 10; i++) {
       my_stack.push(rand() % 10 + 1);
   }
   cout << "stack size is: " << my_stack.getSize() << endl;
   cout << "top of the stack: " << my_stack.peek() << endl;
   while (!my_stack.isEmpty())
       cout << my_stack.pop() << " - ";
   cout << endl;
   //my_stack.~stack();

}

Stack.h:

#pragma once
#ifndef STACK_H
#define STACK_H
#include<iostream>
using namespace std;
template<class T>
class stack
{
public:
   stack(int ); // constructor
   ~stack();
   T peek(); // returns the top item from stack without removing
   void push(T anItem);
   T pop(); // remove top element and return it
   bool isEmpty(); // check if teh stack is empty
   //void print_the_stack();
   int getSize(); // return size of stack
private:
   T* a;
   int size;// count howmany items are in the stack
   int capacity;// maximum capacity of the stack

};
#endif // !STACK_H

Stack.cpp:

#include "stack.h"
template<class T>
stack<T>::stack(int capacity) {

   size = 0;
   this->capacity = capacity;
   a = new T[capacity];
}
template<class T>
T stack<T>::peek() {
   return a[size-1];
}

template<class T>
void stack<T>::push(T anItem) {
   a[size] = anItem;
   size++;
}
template<class T>
T stack<T>::pop() {
   if (size >0) {
       size--;
       return a[size];
   }
}

template<class T>
int stack<T>::getSize() {
   return size;
}
template<class T>
bool stack<T>::isEmpty() {
   return size > 0 ? false : true;
}
template<class T>
stack<T>::~stack() {
   delete[] a;
}

Solutions

Expert Solution

C++ program for the provided problem statement

// c++ program to validate the parenthesis in the expression
// using stack
#include<iostream>
#include<stack>
#include<string>
using namespace std;

// this function will check if the opening and closing parenthesis
// are of the same type
bool isParenthesisPair(char openingParenthesis, char closingParenthesis)
{
   if(openingParenthesis == '(' && closingParenthesis == ')'){
   return true;
   }else if(openingParenthesis == '{' && closingParenthesis == '}'){
   return true;
   }else if(openingParenthesis == '[' && closingParenthesis == ']'){
   return true;
   } else{
   return false;
   }
}

// this method will match the opening and closing parenthesis respectively
// if no. of opening parenthesis == no. of the closing parenthesis
// then it will return true otherwise false
bool isExpBalanced(string expn)
{
// initialize an stack
   stack<char> S;
  
   // iterate the complete expression
   for(int i =0;i<expn.length();i++)
   {
   // if opening parenthesis appears, push it to the stack
       if(expn[i] == '(' || expn[i] == '{' || expn[i] == '['){
       S.push(expn[i]);
       }
       // if closing parenthesis appears then check the stack to
       // find the appropriate pair
       // to find the pair, call a helper function isParenthesisPair()
       else if(expn[i] == ')' || expn[i] == '}' || expn[i] == ']')
       {
       // for a closing parenthesis
       // if stack is empty and there is no pair then return false
       // else pop the top of the stack
           if(S.empty() || !isParenthesisPair(S.top(),expn[i])){
               return false;
       }else{
       S.pop();
       }
       }
   }
   // at the end, if all parenthesis are matched and stack is empty
   // then return true else return false
   return S.empty() ? true:false;
}

// driver function
int main()
{
// provide test cases
   int testCases = 0;
   cout<<"Enter number of test cases: ";
   cin>>testCases;
  
   while(testCases-- > 0){
  
   // input expression
   string expn;
   cout<<"\nEnter an expression: ";
   cin>>expn;
  
   bool result = isExpBalanced(expn);
  
   if(result){
   cout<<"It is a balanced expression !!\n";
   }else{
   cout<<"It is not a balanced expression !!\n";
   }
   }
}

C++ program Screenshots

C++ program Output Screenshot


Related Solutions

Write a program, using any language you want, to sort the following using QuickSort: {3, 4,...
Write a program, using any language you want, to sort the following using QuickSort: {3, 4, 5,1, 2, 6, 7, 8}
[JAVA] You will write a program to validate passwords for users, making sure they meet the...
[JAVA] You will write a program to validate passwords for users, making sure they meet the following criteria: Rules: Passwords must be at least 8 characters long Passwords can only contain alpha numeric characters (no spaces or special characters) Passwords must contain at least 1 uppercase character Passwords must contain at least 1 lowercase character Passwords must contain at least 1 numeric character (0-9) Passwords cannot contain the word “password” A password that does not meet all of these rules...
Write a program for hotel booking system using C++ Program Requirement 1. You can write any...
Write a program for hotel booking system using C++ Program Requirement 1. You can write any program based on the title assigned. 2. The program must fulfill ALL the requirements below. The requirements listed below are the MINIMUM requirement. Your program may extend beyond the requirements if needed. a) Create at least one (1) base class. b) Create at least two (2) derived classes that inherit from the base class created in 2(a). c) Create at least one (1) object...
Write a program, using any language you want, and any sorting algorithm discussed in this unit...
Write a program, using any language you want, and any sorting algorithm discussed in this unit to sort the following : 243, 1, 4, 6, 234, 33, 674, 32, 3333 Note: Can you write it in quick sort
Write a program to sort the following using HeapSort. Use any language you want. A=[10, 8,...
Write a program to sort the following using HeapSort. Use any language you want. A=[10, 8, 7, 9,16, 14, 3, 2, 4, 1]
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar...
PYTHON PROGRAM: Write a program that determines the day of the week for any given calendar date after January 1, 1900, which was a Monday. This program will need to account for leap years, which occur in every year that is divisible by 4, except for years that are divisible by 100 but are not divisible by 400. For example, 1900 was not a leap year, but 2000 was a leap year.
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
ASSIGNMENT: Write a program to ask for the name and test score for 8 students, validate...
ASSIGNMENT: Write a program to ask for the name and test score for 8 students, validate that the test score is between 0.0 and 100.0 inclusive, and store the names and test scores in 2 parallel arrays. Then, calculate and display the average of the test scores. Finally, display all the students who have test scores above the average. Example Run #1: (bold type is what is entered by the user) Enter student name #1: George Enter the score for...
Write a 700 word summary in which you articulate elements of leadership using the following criteria:...
Write a 700 word summary in which you articulate elements of leadership using the following criteria: Differentiate between leadership and management roles, and provide specific examples from the text, literature, or personal example. Cite at least one peer-reviewed source in addition to the course text (be certain to include the web link for your test in your citations). Format your paper consistent with APA guidelines.
Create a Java application called ValidatePassword to validate a user’s password. Write a program that asks...
Create a Java application called ValidatePassword to validate a user’s password. Write a program that asks for a password, then asks again to confirm it. If the passwords don’t match or the rules are not fulfilled, prompt again. Your program should include a method that checks whether a password is valid. From that method, call a different method to validate the uppercase, lowercase, and digit requirements for a valid password. Your program should contain at least four methods in addition...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT