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

In C++. Please use only push, pop and back functions. Write a C++ program to rea

ID: 3871082 • Letter: I

Question

In C++. Please use only push, pop and back functions.

Write a C++ program to read string (sequences of characters) from a text file saved on you hard drive, assume that each string is on a single line. After that, your program will parse the string and determine whether each sequence is parentheses, braces, and curly braces are "balanced." If not, it will display a message that indicates what are the missing characters from the sequence, Your program can only use stack(s) data structure. - You can only use implement your stack using the vector structure from the STL - Only stack operations can be used for inserting and removing elements from your stack. - With the assignment, you should find an example written in C+ on how to read a text file line by line, feel free to adapt the code in that file to solve the problem - You should also find a text file with a few expressions to test your code. Hint: for left delimiters, push onto stack; for right delimiters, pop from stack and check whether popped element matches right delimiter. For example, you can create a text file with the following expressions to test your code: (a+b) c/d*5 (7-2-ab) -[a/2 (5+al)]

Explanation / Answer

#include<iostream>
#include<vector>
#include<string>
#include<fstream>

using namespace std;

int main(){

   string name;
   string text;
   ifstream fin;
   vector<string> input;
   vector<char> stack;
   vector<char> missing;
   int found;

   cout << "Enter file name:" << endl;
   cin >> name;
   fin.open(name.c_str());
   if (!fin){
      cout << "Error opening file ";
      return 0;
   }
   else {
       while (fin >> text){
           input.push_back(text);
       }
       for (int i = 0; i<input.size(); i++){

           for (int j = 0; j<input[i].size(); j++){
              if (input[i][j] == ']') {
                 found = 0;
                 int n = stack.size();
                
                 for (int k = 0; k<n; k++){
                    
                     if (stack.back() == '['){
                        found = 1;
                        stack.pop_back();
                        break;
                     }
                    
                     stack.pop_back();
                 }
                 if (found == 0)
                    missing.push_back('[');
                 continue;
              }
              if (input[i][j] == '}') {
                 found = 0;
                 int n = stack.size();
                 for (int k = 0; k<n; k++){
                     if (stack.back() == '{'){
                        found = 1;
                        stack.pop_back();
                        break;
                     }
                    
                     stack.pop_back();

                 }
                 if (found == 0)
                    missing.push_back('{');
                 continue;
              }
               if (input[i][j] == ')') {
                 found = 0;
                 int n = stack.size();
                 for (int k = 0; k<n; k++){
                     if (stack.back() == '('){
                        found = 1;
                        stack.pop_back();
                        break;
                     }
                    
                     stack.pop_back();

                 }
                 if (found == 0)
                    missing.push_back('(');
                 continue;
              }
              stack.push_back(input[i][j]);

           }
           cout << input[i] << endl;
           cout << "Missing characters: ";
           for (int i = 0; i<stack.size(); i++){
               if (stack[i] == '(')
                   cout << ')';
               if (stack[i] == '{')
                   cout << '}';
               if (stack[i] == '[')
                   cout << ']';
           }   
           for (int i = 0; i<missing.size(); i++)
               if (missing[i] == '(' || missing[i] == '{' || missing[i] == '[')
                   cout << missing[i];
           stack.clear();
           missing.clear();
           cout << " ";
       }
   }
}