I need help fill in the missing parts to finish the code // balance.cpp // file
ID: 3639479 • Letter: I
Question
I need help fill in the missing parts to finish the code // balance.cpp // file to determine if (), [], {} are balanced and matched #include <iostream> #include <fstream> #include <cstdlib> #include <stack> #include <string> using namespace std; int main() { ifstream fin; // input file // use s to check for symbol balance stack<char> s; char c; // character to read from file string fileName; // name of input file // initially assume the file is balanced bool balanced = true; char trash; cout << "Enter the file name: "; cin >> fileName; cin >> trash; fin.open(fileName.c_str()); if (!fin) { cerr << "Cannot open " << fileName << endl; exit(1); } // continue in loop while there are characters to read // and as long as file remains balanced while (fin.get(c) && balanced) { // only consider "([{)]}" switch(c) { // ADD YOUR LOGIC HERE // you need to add logic for also matching [ ] and { } case '(': // push symbol on stack s.push(c); break; case ')': // verify that stack is not empty if (!s.empty()) if (s.top() == '(') s.pop(); else balanced = false; else balanced = false; break; } // end switch } fin.close(); // the stack should be empty if (!s.empty()) balanced = false; // output the result if (balanced) cout << "The symbols in " << fileName << " are balanced." << endl; else cout << "The symbols in " << fileName << " are not balanced." << endl; cin >> trash; return 0; } I need help fill in the missing parts to finish the code // balance.cpp // file to determine if (), [], {} are balanced and matched #include <iostream> #include <fstream> #include <cstdlib> #include <stack> #include <string> using namespace std; int main() { ifstream fin; // input file // use s to check for symbol balance stack<char> s; char c; // character to read from file string fileName; // name of input file // initially assume the file is balanced bool balanced = true; char trash; cout << "Enter the file name: "; cin >> fileName; cin >> trash; fin.open(fileName.c_str()); if (!fin) { cerr << "Cannot open " << fileName << endl; exit(1); } // continue in loop while there are characters to read // and as long as file remains balanced while (fin.get(c) && balanced) { // only consider "([{)]}" switch(c) { // ADD YOUR LOGIC HERE // you need to add logic for also matching [ ] and { } case '(': // push symbol on stack s.push(c); break; case ')': // verify that stack is not empty if (!s.empty()) if (s.top() == '(') s.pop(); else balanced = false; else balanced = false; break; } // end switch } fin.close(); // the stack should be empty if (!s.empty()) balanced = false; // output the result if (balanced) cout << "The symbols in " << fileName << " are balanced." << endl; else cout << "The symbols in " << fileName << " are not balanced." << endl; cin >> trash; return 0; }Explanation / Answer
#include <iostream> #include <fstream> #include <cstdlib> #include <stack> #include <string> using namespace std; int main() { ifstream fin; // input file // use s to check for symbol balance stack<char> s; char c; // character to read from file string fileName; // name of input file // initially assume the file is balanced bool balanced = true; char trash; cout << "Enter the file name: "; cin >> fileName; //cin >> trash; fin.open(fileName.c_str()); if (!fin) { cerr << "Cannot open " << fileName << endl; exit(1); } // continue in loop while there are characters to read // and as long as file remains balanced while (fin.get(c) && balanced) { // only consider "([{)]}" switch(c) { // ADD YOUR LOGIC HERE // you need to add logic for also matching [ ] and { } case '(': case '[': case '{':// push symbol on stack s.push(c); break; case ')': case ']': case '}': // verify that stack is not empty if (!s.empty()) if (s.top() == '(' || s.top() == '[' || s.top() == '{') s.pop(); else balanced = false; else balanced = false; break; default:break; } // end switch } fin.close(); // the stack should be empty if (!s.empty()) balanced = false; // output the result if (balanced) cout << "The symbols in " << fileName << " are balanced." << endl; else cout << "The symbols in " << fileName << " are not balanced." << endl; getchar(); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.