Question

Problem Implement (in C) an algorithm that uses a stack to check if a parentheses sequence is balanced. Note that a parenthes

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<stdio.h>
#include<string.h>
#define MAX 20

int top = -1;
int stack[MAX];

char push(char item)
{
        if(top == (MAX-1))
                printf("Stack Overflow\n");
        else
        {
                top=top+1;
                stack[top] = item;
        }
}

char pop()
{
        if(top == -1)
                printf("Stack Underflow\n");
        else
                return(stack[top--]);
}


void main()
{
        
        char gp[MAX],t;
        int i,c=1;
        
        printf("Enter an Expression : ");
        gets(gp);
        
        for(i=0;i<strlen(gp);i++)
        {
                if(gp[i]=='(' || gp[i]=='{' || gp[i]=='[')
                        push( gp[i] );
                if(gp[i]==')' || gp[i]=='}' || gp[i]==']')
                        if(top == -1)
                                c=0;
                        else
                        {
                                t=pop();
                                if( gp[i]==')' && (t=='{' || t=='[') )
                                        c=0;
                                if( gp[i]=='}' && (t=='(' || t=='[') )
                                        c=0;
                                if( gp[i]==']' && (t=='(' || t=='{') )
                                        c=0;
                        }
        }
        
        if(top>=0)
                c=0;

        if( c==1)
                printf("This is Valid Expression\n");

        else
                printf("This is Invalid Expression\n");
                
}
Add a comment
Know the answer?
Add Answer to:
Problem Implement (in C) an algorithm that uses a stack to check if a parentheses sequence is balanced. Note that a par...
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 solve the following problem with programming using proper data structures. (Programming Language: Python) A similar...

    Please solve the following problem with programming using proper data structures. (Programming Language: Python) A similar application to the parentheses matching problem comes from hypertext markup language (HTML). In HTML, tags exist in both opening and closing forms and must be balanced to properly describe a web document. This very simple HTML document: Example> Hello, world is intended only to show the matching and nesting structure for tags in the language. Write a program that can check an HTML document...

  • 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...

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

  • Here is the code I have so far. I'm trying to figure out how to implement...

    Here is the code I have so far. I'm trying to figure out how to implement a boolean and use precedence. The 2nd expression should be 14 but it comes out as 28 so I'm definitely not understanding. #include <stack> #include <iostream> #include <string> using namespace std; // Function to find precedence of // operators. int precedence(char op) {    if (op == '+' || op == '-')        return 1;    if (op == '*' || op ==...

  • The last 3 cases are tests .cpp files to test the code. The language of this...

    The last 3 cases are tests .cpp files to test the code. The language of this code must be C++ because that is the only I am familiar with. Soreland, a software company, is planning on releasing its first C++ compiler with the hopes of putting MiniSoft's Visual C++ out of business (that reminds me of another story). Consequently, Soreland has contracted with the Fruugle Corporation to design and build part of their compiler. Of course, since Fruugle gets all...

  • Write a c++ program in that file to perform a “Search and Replace All” operation. This...

    Write a c++ program in that file to perform a “Search and Replace All” operation. This operation consists of performing a case-sensitive search for all occurrences of an arbitrary sequence of characters within a file and substituting another arbitrary sequence in place of them. Please note: One of the biggest problems some students have with this exercise occurs simply because they don’t read the command line information in the course document titled “Using the Compiler’s IDE”. Your program: 1. must...

  • Rules of implementation!: You may NOT modify any of the files except Expression.java in ANY way....

    Rules of implementation!: You may NOT modify any of the files except Expression.java in ANY way. You may NOT make ANY modifications to Expression.java EXCEPT: Write in the bodies of the methods you are asked to implement, Add private helper methods as needed (including the recursive evaluate method discussed below.) Note that the java.io.*, java.util.*, and java.util.regex.* import statements at the top of the file allow for using ANY class in java.io, java.util, and java.util.regex without additional specification or qualification....

  • C Program Please! Thanks! Spring 2019 ECE 103 Engineering Programming Problem Statement A standard deck of playi...

    C Program Please! Thanks! Spring 2019 ECE 103 Engineering Programming Problem Statement A standard deck of playing cards contains 52 cards. There are four suits (heart, diamond club +, spade+) and thirteen ranks (ace, 2 thru 10,jack, queen, king) per suit. Rank Numeric Value 10 10 10 10 gueen 10 Write a program that does the following: I. Simulates a randomized shuffling of a 52 card deck 2. Determines how many combinations of 21 exist in the deck by following...

  • This assignment is comprised of 3 parts: ​All files needed are located at the end of...

    This assignment is comprised of 3 parts: ​All files needed are located at the end of the directions. Part 1: Implementation of Dynamic Array, Stack, and Bag First, complete the Worksheets 14 (Dynamic Array), 15 (Dynamic Array Amortized Execution Time Analysis), 16 (Dynamic Array Stack), and 21 (Dynamic Array Bag). These worksheets will get you started on the implementations, but you will NOT turn them in. ​Do Not Worry about these, they are completed. Next, complete the dynamic array and...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

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