Write a program that takes as its input a text file that contains some C++ code.
ID: 3679922 • Letter: W
Question
Write a program that takes as its input a text file that contains some C++ code. Your program should determine if all brackets within the text file are nested with the exception of those appearing within comments and quoted strings. When your program detects a mismatched pair, it should print a message indicating the location (line numbers and column numbers) of the mismatch. If all pairs are matched, the program should indicate that at the end.
You can write your own implementation of a data structure, use an implementation developed in class, or a container from the C++ Standard Library
Your program should read the input text from a file. The skeleton program that is included here has code to read a text file line by line. This code can take the file name from the command line or ask the user. Note that code running within Visual Studio considers the project directory as the default directory in which to open files.
Only round brackets (), square brackets [], and curly braces {} need to be matched. The maximum depth of nesting is 80, i.e., there could be at most 80 pairs contained one within the other (note that there is no limit to the total number of brackets). Comments will be indicated only with // (not with /* ... */). Strings within quotes will be confined to one line.
You have been supplied with a source file. The file is complete and should compile cleanly. This file has code to read a text file and can be used as a starting point for your code.
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) {
string filename;
if (argc >= 2) {
filename = argv[1]; // command line arguments are given. The first argument is the filename
}
else {
cout << "Enter name of file to check: ";
cin >> filename;
}
cout << "File to check:" << filename << endl;
ifstream infile(filename);
if (!infile) {
cout << "That file does not exist" << endl;
return (-1); // end the program
}
string line;
while (getline(infile, line)) {
cout << line << endl;
}
infile.close();
system("pause");
return(0);
}
Explanation / Answer
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <stack>
#include <string>
typedef std::stack<char> Brackets;
//function initialization for opening brackets and store it
void pushOpeningBrackets(Brackets& opening, char ch)
{
if (ch == '{')
opening.push('{');
else if (ch == '(')
opening.push('(');
else if (ch == '[')
opening.push('[');
}
bool errorsFound(Brackets& stack, char openingBracket, char closingBracket)
{
// unmatched?
if (stack.empty())
{
std::cerr << "Unmatched " << closingBracket;
return true;
}
char topBracket = stack.top();
stack.pop();
// not a match?
if (topBracket != openingBracket)
{
if (topBracket == '{')
std::cerr << "Expected } but found " << closingBracket;
else if (topBracket == '(')
std::cerr << "Expected ) but found " << closingBracket;
else if (topBracket == '[')
std::cerr << "Expected ] but found " << closingBracket;
return true;
}
return false;
}
int main(int argc, char *argv[]) {
string filename;
if (argc >= 2) {
filename = argv[1]; // command line arguments are given. The first argument is the filename
}
else {
cout << "Enter name of file to check: ";
cin >> filename;
}
cout << "File to check:" << filename << endl;
ifstream infile(filename);
if (!infile) {
cout << "That file does not exist" << endl;
return (-1); // end the program
}
Brackets stack;
std::string fileLine;
while (inFile >> fileLine)
{
for (char ch : fileLine)
{
pushOpeningBrackets(stack, ch);
if (ch == '}')
{
if (errorsFound(stack, '{', '}'))
{
return EXIT_SUCCESS;
}
}
else if (ch == ')')
{
if (errorsFound(stack, '(', ')'))
{
return EXIT_SUCCESS;
}
}
else if (ch == ']')
{
if (errorsFound(stack, '[', ']'))
{
return EXIT_SUCCESS;
}
}
}
}
// checks for missing bracket or full match
if (!stack.empty())
{
char topBracket = stack.top();
stack.pop();
if ('{' == topBracket)
std::cerr << "Missing }";
else if ('(' == topBracket)
std::cerr << "Missing )";
else if ('[' == topBracket)
std::cerr << "Missing ]";
}
else
std::cout << "All brackets match!";
infile.close();
system("pause");
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.