-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmetic-expression-validity-checking.cpp
More file actions
38 lines (36 loc) · 1.05 KB
/
arithmetic-expression-validity-checking.cpp
File metadata and controls
38 lines (36 loc) · 1.05 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
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Enter your expression:\n ";
string expression;
cin >> expression;
stack<int> Stack;
int length = expression.size();
bool result = false;
for(int i = 0; i<length; i++){
char character = expression[i];
if(character=='(' || character=='{' || character=='['){
Stack.push(character);
}
else if (character==')' || character=='}' || character==']'){
if(Stack.empty()) {
result = true;
break;
}
else if((character==')'&&Stack.top()=='(') || (character=='}'&&Stack.top()=='{') || (character==']'&&Stack.top()=='[') ){
Stack.pop();
}
}
}
if(result==true) {
cout << "Opening bracket missing. Invalid arithmetic expression \n";
}
else if(!Stack.empty()) {
cout << "Closing bracket missing. Invalid arithmetic expression \n";
}
else {
cout << "Valid arithmetic expression \n";
}
return 0;
}