Question

In: Computer Science

Hello, I am trying to write a C++ program that will do the following: Use the...

Hello, I am trying to write a C++ program that will do the following:

  1. Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                        

Use the following strings to test your program.

  1. A+ B - C
  2. A * B / (C + 9)
  3. A * ((B / C) + D + (E - 5)
  4. A * (B / C) + D + (E - 9))

Clearly describe the algorithm you will use to determine if the string contains balanced parenthesis. Document the program so I can follow what is going on. The output should look like this:

          String: A + B – C                                No parenthesis

String: A * B / (C+9)                          Matching parenthesis

String: A * ((B / C) + D + (E - 5)          Parenthesis don’t match. Missing right parenthesis

String: A * (B / C) + D + (E - 9))          Parenthesis don’t match. Missing left

parenthesis

Solutions

Expert Solution

Solution:

The algorithm is pretty simple:

Step1: Create a flag equal to 0

Step2: Create a stack and read the expression

Step3: If there is an opening paranthesis set flag =1 and push the paranthesis into stack

Step4: If there is a closing paranthesis check if stack is empty.If stack is empty set flag=-1 and break or else pop the stack.

Step5: Continue untill all the characters are read.

Step6: After exiting the loop if stack is not empty missing right paranthesis else if flag==0 no paranthesis

else if flag==-1 missing right paranthesis else matching paranthesis.

Code:

#include<iostream>
#include<string>
#include<stack>
using namespace std;

int main()
{
        //Stack to check the balaning of paranthesis
        stack <char> paranthesis;
        //Expression for input
        string expression;
        cout<<"String: ";
        getline(cin,expression);
        //flag to check no paranthesis and missing right paranthesis
        int i=0,flag=0;
        //iterating the expression
        for(i=0;i<expression.length();i++)
        {
                //If opening paranthesis push into stack
                if(expression[i]=='(')
                {
                        //set flag to 1 if paranthesis appears
                        flag=1;
                        paranthesis.push(expression[i]);
                }
                //If closing paranthesis
                else if(expression[i]==')' )
                {
                        //If stack is empty missing left paranthesis set flag=-1 and break
                        if(paranthesis.empty())
                        {
                                flag=-1;
                                break;
                        }
                        //If stack is not empty pop
                        else
                                paranthesis.pop();
                                
                }
                
        }
        //If stack not empty missing right paranthesis
        if(!paranthesis.empty())
        {
                cout<<"\tParenthesis don't match. Missing right parenthesis";
        }
        //if flag is 0 then no paranthesis
        else if(flag==0)
        {
                cout<<"\tNo paranthesis";
        }
        //if flag =-1 missing left paranthesis
        else if(flag==-1)
        {
                cout<<"\tParenthesis don't match. Missing left parenthesis";              
        }
        //if none satisfies the matching paranthesis
        else 
        {
                cout<<"\tMatching paranthesis";
        }
}

the output of 4 sample runs are clubbed together

Output:


Related Solutions

I am trying to create a makefile for the following program in Unix. The C++ program...
I am trying to create a makefile for the following program in Unix. The C++ program I am trying to run is presented here. I was wondering if you could help me create a makefile for the following C++ file in Unix and show screenshots of the process? I am doing this all in blue on putty and not in Ubuntu, so i don't have the luxury of viewing the files on my computer, or I don't know how to...
I am trying to write a UDP socket program in which there is a server program...
I am trying to write a UDP socket program in which there is a server program and a client program. This is what I have but I can't figure out where I am going wrong. This is the input and expected output: > gcc udpclient.c –o clientout > gcc udpserver.c –o serverout > ./serverout 52007 ‘to celebrate’ & > ./clientout odin 52007 ‘Today is a good day’ Today is a good day to celebrate //UDP echo client program #include #include...
I am trying to write a java program that determines if an inputted year is a...
I am trying to write a java program that determines if an inputted year is a leap year or not, but I am not able to use if else statements how would I do it. I don't need the code just advice.
Hello Everyone, Can anyone tell me why my program will not run? I am trying to...
Hello Everyone, Can anyone tell me why my program will not run? I am trying to work on abstract base classes... not sure what is going on. import math from abc import ABC from abc import abstractmethod #TODO: convert this to an ABC class Shape(ABC): def __init__(self): self.name = "" def display(self): print("{} - {:.2f}".format(self.name, self.get_area())) #TODO: Add an abstractmethod here called get_area @abstractmethod def get_area(self): if self.name == "Circle": get_area()= 3.14 * radius * radius else: get_area() = self.length...
I am trying to write a code in C for a circuit board reads in a...
I am trying to write a code in C for a circuit board reads in a temperature from a sensor on the circuit board and reads it onto a 7-segment LED display. How exactly would you code a floating 2 or 3 digit reading onto the LED display? I am stuck on this part.
Hello, I have been trying to answer this question for the last hour and I am...
Hello, I have been trying to answer this question for the last hour and I am still struggling could someone help me? The deadline is in 1hour! Perform an analysis of variance on the following data set. Do this by answering the questions below. Group 1 Group 2 Group 3 82 87 97 91 90 99 93 91 104 94 99 105 94 101 106 95 115 109 99 118 110 101 114 103 117 105 121 106 121 106...
I am trying to write a program in Java: Pick 4 cards and display the cards...
I am trying to write a program in Java: Pick 4 cards and display the cards and their total value (Ace = 1, King = 13, Queen = 12, and Jack 11...etc. , BUT you should check that no card is a duplicate... there is only one "Ace of Spades" per deck for example. I need to utilize the displayMenu so the program continues to run for the user without breaking. My program is not running correctly. import java.util.Scanner; import...
Hello, I am working on a C-program that deals with memory mapping, Please show all code...
Hello, I am working on a C-program that deals with memory mapping, Please show all code (code must be in the C) and outputs. The instructions are as follows INSTRUCTIONS: You have two sample c codes, one for the client and one for the server. IF the client makes a change in the shared memory, those changes will show on the server side. Your task is to adapt the change strategy to develop two applications so they can send messages...
Hello! Html question. I am trying to add a background to a page. How would i...
Hello! Html question. I am trying to add a background to a page. How would i do this? I have tried using a jpg. The code is posted below. i used the background-image: url function. Any help of different ways to do this would be appreciated! Thank you! CODE: <!DOCTYPE html> <html> <head> <style> ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a { display: block; color: white; text-align:...
Write a C program with the following prompt *do not use gotostatement* "Write an interactive...
Write a C program with the following prompt *do not use goto statement* "Write an interactive program that implements a simple calculator. The program should allow the user to choose a binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT