-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 7.cpp
More file actions
84 lines (82 loc) · 1.3 KB
/
Assignment 7.cpp
File metadata and controls
84 lines (82 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//============================================================================
// Name : Assignment 7.cpp
// Author : 21258
// Version :
// Copyright : Your copyright notice
// Description : Paranthesis expression check using stack
//============================================================================
#include <iostream>
#include<string.h>
using namespace std;
class node{
public:
char s;
node *next;
node()
{
s=' ';
next=NULL;
}
};
class Stack{
public:
node *top;
Stack()
{
top=NULL;
}
bool isempty()
{
if(top==NULL)
return 1;
else
return 0;
}
void push(char c)
{
if(!isempty())
{
node *t=new node();
t->s=c;
t->next=top;
top=t;
}
else
{
top=new node();
top->s=c;
}
}
void pop()
{
node *t=top;
top=top->next;
delete t;
}
void check(int l,string r)
{
for(int i=0;i<l;i++)
{
if(r[i]=='('||r[i]=='{'||r[i]=='[')
push(r[i]);
else if((r[i]==')'&& top->s=='(')||(r[i]=='}' && top->s=='{')||(r[i]==']' && top->s=='['))
pop();
else
continue;
}
}
};
int main() {
int l;
Stack s;
string r;
cout<<"Enter the expression : ";
cin>r;
l=r.length();
s.check(l,r);
if(s.isempty())
cout<<"The expression is well parenthesized";
else
cout<<"The expression is not well parenthesized";
return 0;
}