Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C++ Programming. String Check for the valid pair of brackets.

Status
Not open for further replies.

macer_0001

Member level 1
Joined
Aug 15, 2009
Messages
36
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Manila
Activity points
1,491
I am using is C++ programming language.
Could you please help me about the program I am working now?
The program is all about checking the string for valid pair or pairs of bracket/s.
{}[]<>()
Then printed "Valid" or "Invalid".

Here are the samples:

Input: Output:
{}dsafsdf Valid
jjhfsajd[] Valid
[sjj<df>] Valid
]sdafasd[ Invalid
(sfd<sf)> Invalid
(dsfsdfsd Invalid
sfsdfsdfsf) Invalid
(){}[]<> Valid
({<[]>}) Valid

Thank you in advance.
 

You may try "Regular Expressions" to check these patterns

Search "Regex in C++", "Regular Expressions in C++"
 

Try this:

Stack.h
Code:
// implementation file for a stack class

#include <iostream>
using namespace std;

const int stack_size = 1000;

class stack
{
      private:
         // data for the stack
         char data [stack_size];
         // the index of the top element of the stack
         int top;
      public:
         stack ();   // constructor creates an empty stack
         void push (char item);  // puts item on the top of the stack
         char pop ();   // removes and returns the item at the top of stack
         bool empty ();
         bool full ();
};

// initialize the stack to an empty stack
stack::stack ()
{
   top = -1;
}

// push adds a new element to the top of the stack
void stack::push (char item)
{
     // if the stack is full, error
     if (full ())
     {
        cout << "\n\nStack Error: pushing on a full stack";
        cout << "\nThe item being pushed is " << item;
     }
     else  // OK to add the element
     {
           top++;
           data [top] = item;
     }
}

// pop removes and returns the top element of the stack
char stack::pop ()
{
     // if the stack is empty, error
     if (empty ())
     {
        cout << "\n\nStack Error: Popping an empty stack";
        cout << "\nReturing a ?";
        return '?';
     } 
     else // OK to pop
     {
          top--;
          return data [top + 1];
     }
}

// empty returns true if the stack is empty, else it returns false
bool stack::empty ()
{
     return top == -1;
}

// full returns true if the stack is full, else it returns false
bool stack::full ()
{
     return top == stack_size - 1;
}

Here is the program:
Code:
/*
  
  Program reads an expression terminated by an s, and tells whether the
  parentheses (), [], and {} match correctly or not
*/

#include <cstdlib>
#include <iostream>
#include "stack.h"

using namespace std;

int main(int argc, char *argv[])
{
    stack equation;  // stack of open parentheses
    char ch;         // current character
    char popped;     // an element popped off the stack
    bool good;       // true if everything matches
    
    good = true;
    
    cout << "Enter an equation followed by an s:\n";
    cin >> ch;
    
    // continue reading an processing characters until an s is read
    while (ch != 's')
    {
          // if it is an open parenthesis, put on the stack
          if (ch == '{' || ch == '[' || ch == '(')
          {
                 equation.push (ch);
          }
          else
          // if it is a close parenthesis, pop the stack and match
          if (ch == '}' || ch == ']' || ch == ')')
          {
                 // stuff on the stack, ok to pop
                 if (! equation.empty ())
                 {
                    popped = equation.pop ();
                    // if the characters don't match
                    if (! (popped == '{' && ch == '}' ||
                        popped == '[' && ch == ']' ||
                            popped == '(' && ch == ')'))
                    good = false;
                 }
                 else // nothing on the stack
                      good = false;
          }
          cin >> ch;
    }
    if (good && equation.empty ())
       cout << "\n\nYes, it matched";
    else
        cout << "\n\nNo, it was bad!";
    
    
    cout << "\n\n";
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top