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.
I am trying to create a program That works with two other programs in c++ and...
I am trying to create a program That works with two other programs in c++ and a makefile. Only Shape.cpp can be modified. and it needs to work on a unix machine. It isn't running on my machine. And gives me an error message that it doesn't recomize cin and endl The program will accept a character and an X and Y coordinate. Dependign on the Charactor, It will then tell you what Cells that shape occupies. I almost have...
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...
Hello, I have this question that I have been trying to solve for days. I am...
Hello, I have this question that I have been trying to solve for days. I am always able to solve the first part of the question, but I am never able to get the second part. Please help me understand how to set up this problem so I am able to solve it! Q: Let's revisit the banked curves from earlier to see another reason they are useful.We are building a road, and at one place we need to make...
Hello, I am attempting to write a program which calculates the amount of fence pieces needed...
Hello, I am attempting to write a program which calculates the amount of fence pieces needed to achieve a certain distance. The program should take in the amount of fencing needed from the user, then ask them the size of the smaller piece. The fencing is to be made up of two sizes of fencing, the smaller being two feet less than the larger. Then, the program will tell the user how many of each piece is needed. However, I...
I am trying to create a basic shell program in C that runs 10 commands and...
I am trying to create a basic shell program in C that runs 10 commands and then quits. Only one word commands are required, like: cal, date, ls, ps, pwd, who, quit The part I am struggling with is passing the scanned command into my array in the child process to be executed using execvp(). Here is my code: #include <stdio.h> #include <string.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/wait.h> #include<readline/readline.h> #include<readline/history.h> #define MAX_CMD_NUMBER 10 int main() {    int i;    char...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT