In: Computer Science
"balancing.cpp "
-----------------------------------------------------------
#include "stack.hpp"
using namespace std;
int main(){
freopen("input_balanced.txt", "r", stdin);
string s,r;
int line_counter;
while(cin >> r){
cin>>s;
Stack<char> stack;
bool isBalanced = true;
bool solution;
if(r[0] == 'Y' || r[0] == 'y'){
solution = true;
}else{
solution = false;
}
// The input file is in the format
"expected_solution expression"
// so variable solution tells you whether
'expression' is balanced or not
for(int i=0; i<s.length(); ++i){
// WRITE CODE HERE so that
isBalanced indicates whether 's' is balanced
}
// checking if you stored in isBalanced the
correct value
if(isBalanced == solution){
cout << "line " <<
line_counter << ": OK [" << solution << " "
<< isBalanced << "]" << endl;
}else{
cout << "line " <<
line_counter << ": ERROR [" << solution << " "
<< isBalanced << "]" << endl;
}
line_counter++;
}
}
____________________________________________________________________________
"input_balanced.txt"
yes [(a+b)(b+c)]
no [)(a+b)]
yes [{a+(b+c(3+4))}]
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
"stack.hpp"
#include "stack.hpp"
using namespace std;
template<class T>
void Stack<T>::push(T c){
if(topIndex > MAXSIZE-1){
cout<<"Stack overflow"<<endl;
return;
}
arr[topIndex + 1] = c;
topIndex++;
}
template<class T>
T Stack<T>::pop(){
if(topIndex < 0){
cout<<"Cannot delete. Stack
empty."<<endl;
}
return arr[topIndex--];
}
template<class T>
T Stack<T>::peek(){
if(topIndex < 0){
cout<<"Cannot peek. Stack
empty."<<endl;
}
return arr[topIndex];
}
template<class T>
int Stack<T>::size(){
return topIndex+1;
}
template<class T>
void Stack<T>::display(){
for(int i=topIndex; i>=0; --i){
cout<<arr[i]<<"\t";
}
cout<<endl;
}
template class Stack<char>;
template class Stack<int>;
_______________________________________________________________
can you make the code compile
balancing.cpp
#include "stack.hpp"
using namespace std;
int main() {
freopen("input_balanced.txt", "r", stdin);
string s,r;
int line_counter=1;
while(cin >> r) {
cin>>s;
Stack<char> stack;
bool isBalanced = true;
bool solution;
if(r[0] == 'Y' || r[0] == 'y') {
solution = true;
}else{
solution = false;
}
// Loop to check whether s is balanced or not
for(int i=0; i<s.length(); ++i) {
// If s[i] is opening symbol then push it into the stack
if(s[i] == '[' || s[i] == '(' || s[i] == '{')
stack.push(s[i]);
// if it is any of closing symbol then peek of the stack should
be
// the opening symbol of same kind
// else make isBalanced false and break the loop
else if (s[i] == ']') {
if(stack.peek() == '[')
stack.pop();
else {
isBalanced = false;
break;
}
}
else if (s[i] == ')') {
if(stack.peek() == '(')
stack.pop();
else {
isBalanced = false;
break;
}
}
else if (s[i] == '}') {
if(stack.peek() == '{')
stack.pop();
else {
isBalanced = false;
break;
}
}
}
if(isBalanced == solution) {
cout << "line " << line_counter << ": OK ["
<< solution << " " << isBalanced << "]"
<<endl;
}else{
cout << "line " << line_counter << ": ERROR ["
<< solution << " " << isBalanced << "]"
<<endl;
}
line_counter++;
}
}
This is the Solution of your Question.