Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone finish the program? I need to use Doubly Linked List. I need a C++ p

ID: 3915771 • Letter: C

Question

Can someone finish the program? I need to use Doubly Linked List. I need a C++ program that checks to see if parentheses (or brackets) in

expressions are valid or not. The program takes as input a text file that has a series

of expressions, one per line. An example input file could be:

[ ][ ][ ][ ]

( )

( try )

]

ok

{

((())) [][][]

[ ] [ ]

55 5 4 3 2 2 { ( [ (3 * 4 ) ] ( 8 + 1 ) ) }

)()(

hello

last

The program should read them in and determine if the brackets are arranged in a

valid way or not. The output should be a summary of the data file with the following

lines:

Statistics:

12 total lines.

8 valid lines.

4 invalid lines.

67% valid.

33% invalid.

As you can see from the example, a line is valid if it has no brackets, if it has brackets

of any kind, they must be nested properly, beginning with an open bracket ( or [ or {

and ending with a closed bracket } or ] or ) respectively. It is not enough to simply

count brackets as the following line is not valid:

( [ ) ]

despite it having the correct number of brackets, and the openings occurring before

the closings.

Here is my Code:

#include "l_char_stack.h"

using namespace std;

int main()
{
l_char_stack stack;
return 0;
}

-----------------------------

#include "l_char_stack.h"

l_char_stack::l_char_stack()

{

}

l_char_stack::l_char_stack(int mSize)

{

}

l_char_stack::~l_char_stack()

{

}

void l_char_stack::push(listObjType newItem)

{

}

listObjType l_char_stack::pop()

{

listObjType a;

a = listObjType();

return a;

}

listObjType l_char_stack::peek()

{

listObjType a;

a = listObjType();

return a;

}

bool l_char_stack::isEmpty()

{

return false;

}

bool l_char_stack::isFull()

{

return false;

}

void l_char_stack::clear()

{

}

int l_char_stack::depth()

{

return 0;

}

-------- l_char_stack.h --------

#include "char_linked_list.h"

class l_char_stack

{

private:

public:

l_char_stack();

l_char_stack(int mSize);

~l_char_stack();

void push(listObjType newItem);

listObjType pop();

listObjType peek();

bool isEmpty();

bool isFull();

void clear();

int depth();

};

----------- text input file---------------

()
[]
[[[]]]
a
b
c
d
e
)()()()(
(())(())(a)

Explanation / Answer

#include<iostream>
#include<stack>
#include<string>
#include<fstream>
using namespace std;
//below function validates is symbols are pair
bool isPair(char open,char clos)
{
   if(open == '(' && clos == ')') return true;
   else if(open == '[' && clos == ']') return true;
   else if(open == '{' && clos == '}') return true;
   return false;
}

//checks if an expression is balanced
bool isExpressionBalanced(string exp)
{
   stack<char> S;
   for(int i =0;i<exp.length();i++)
   {
       if(exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
           S.push(exp[i]);
       else if(exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
       {
           if(S.empty() || !isPair(S.top(),exp[i]))
               return false;
           else
               S.pop();
       }
   }
   return S.empty() ? true:false;
}

int main()
{
   string filename;
   cout<<"Enter filename: ";
   cin>>filename; //reads filename
   std::ifstream file(filename);
   if(!file){
        cout<< "Cannot open input file!" << endl;
       return 1;
   }
   std::string line;
   int totalLines=0;
   int validLines=0;
   int invalidLines =0;
   double validPercentage ;
   double invalidPercentage;
   while( std::getline(file,line,' ') ){
        totalLines++;
        if(isExpressionBalanced(line)){
            validLines++;
            cout<<"Balanced" <<" " <<line <<" ";
        }
        else{
            cout<<"Balanced" <<" " <<line <<" ";
        }
   }
   invalidLines = totalLines-validLines;
   validPercentage = validLines * 100.00 / totalLines;
   invalidPercentage = invalidLines * 100.00 / totalLines;
   cout << "Statistics : ";
   cout << totalLines << " total Lines ";
   cout << validLines << " valid lines ";
   cout << invalidLines <<" invalid lines ";
   cout << validPercentage << "% valid ";
   cout << invalidPercentage << "% invalid ";
  
  
}