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

C++ help. Write a program that takes as its input a text file that contains some

ID: 3679442 • Letter: C

Question

C++ help.

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.

Simplifying assumptions

1. Only round brackets (), square brackets [], and curly braces {} need to be matched.

2. 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).

3. Comments will be indicated only with // (not with /* ... */).

4. Strings within quotes will be confined to one line.

HERE IS THE SKELETON CODE, PLEASE MODIFY IT TO WORK WITH THE REQUIREMENTS

// brackets_check.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#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);
}

The output should look like these examples.

Explanation / Answer

// brackets_check.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <stack>
#include <string>
#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);
}

/*
Notes and observations before starting:
Refer to 5.1.7 for major major help with this project.
Use the #<stack> STL library, will make things a ton easier.
Run the Parentheses Matching in the while loop.
There should be four if statements.
IF there is a opening grouping character, push that into the stack.
ELIF there is a CLOSING grouping charcter(ONLY a closing, at the beginning)
   ex. }{()}
   THEN -> return invalid.
ELIF there is a wrong type.
   ex {()( })}
   Then -> return invalid.
IF the stack is empty
   return true (Meaning, there is no extra Parentheses in the stack.)
   Everything has been matched.
else
   return false. (Meaning, there is a extra parentheses or two in the stack. They weren't grouped.)
  
*/

example.cpp
//#include "stdafx.h"

#include <cassert>
#include <iostream>
#include <list>
#include <map>

using namespace std;

// one million
const int MILLION = 1000 * 1000;

// maximum size of internal data structures
const int MAX_ACCOUNTS = 40 * MILLION;
const int MAX_UNPAID = 1 * MILLION;
const int MAX_VIOLATIONS = 2 * MILLION;

// Toll Roads policies
const int ACCOUNT_MAINTENANCE_FEE_CENTS = 200; // $2 in units of cents
const int VIOLATION_PENALTY_CENTS = 5750; // $57.50 in units of cents
const int GRACE_PERIOD_DAYS = 5;

// Days of the week.
enum DayOfWeek {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};

// Represents a point in time, including:
// - Unix time (whole seconds since midnight 1/1/1970)
// - day of the week
// - hour (using military time, so this must be 1 through 23)
// - minute (must be 0 through 59)
class Timestamp {
public:
    Timestamp(int unix_time, DayOfWeek day_of_week, int hour, int minute) {
        assert(unix_time >= 0);
        assert((hour >= 0) && (hour <= 23));
        assert((minute >= 0) && (minute <= 59));
        _unix_time = unix_time;
        _day_of_week = day_of_week;
        _hour = hour;
        _minute = minute;
    }

    // Default constructor, leaving all fields uninitialized.
    Timestamp() { }

    // Accessors.
    int unix_time() { return _unix_time; }
    DayOfWeek day_of_week() { return _day_of_week; }
    int hour() { return _hour; }
    int minute() { return _minute; }

    // Comparison operator. This is needed by some of the unit tests in main().
    bool operator==(const Timestamp& right) const {
        return ((_day_of_week == right._day_of_week) &&
            (_unix_time == right._unix_time) &&
            (_hour == right._hour) &&
            (_minute == right._minute));
    }

private:
    DayOfWeek _day_of_week;
    int _unix_time, _hour, _minute;
};

example2.cpp
//#include "stdafx.h"

#include <cassert>
#include <iostream>
#include <list>
#include <map>

using namespace std;

// one million
const int MILLION = 1000 * 1000;

// maximum size of internal data structures
const int MAX_ACCOUNTS = 40 * MILLION;
const int MAX_UNPAID = 1 * MILLION;
const int MAX_VIOLATIONS = 2 * MILLION;

// Toll Roads policies
const int ACCOUNT_MAINTENANCE_FEE_CENTS = 200; // $2 in units of cents
const int VIOLATION_PENALTY_CENTS = 5750; // $57.50 in units of cents
const int GRACE_PERIOD_DAYS = 5;

// Days of the week.
enum DayOfWeek {
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY,
    SUNDAY
};

// Represents a point in time, including:
// - Unix time (whole seconds since midnight 1/1/1970)
// - day of the week
// - hour (using military time, so this must be 1 through 23)
// - minute (must be 0 through 59)
class Timestamp {
public:
    Timestamp(int unix_time, DayOfWeek day_of_week, int hour, int minute) {
        assert(unix_time >= 0);
        assert((hour >= 0) && (hour <= 23));
        assert((minute >= 0) && (minute <= 59));
        _unix_time = unix_time;
        _day_of_week = day_of_week;
        _hour = hour;
        _minute = minute;
    }

    // Default constructor, leaving all fields uninitialized.
    Timestamp() { }

    // Accessors.
    int unix_time() { return _unix_time; }
    DayOfWeek day_of_week() { return _day_of_week; }
    int hour() { return _hour; }
    int minute() { return _minute; }

    // Comparison operator. This is needed by some of the unit tests in main().
    bool operator==(const Timestamp& right) const {
        return ((_day_of_week == right._day_of_week) &&
            (_unix_time == right._unix_time) &&
            (_hour == right._hour) &&
            (_minute == right._minute)));
    }

private:
    DayOfWeek _day_of_week;
    int _unix_time, _hour, _minute;
};


sample output


example2.cpp: In member function 'bool Timestamp::operator==(const Timestamp&) const':                                                                      
example2.cpp:65:40: error: expected ';' before ')' token                                                                                                    
             (_minute == right._minute)));                                                                                                                  
                                        ^                                                                                                                   
example2.cpp:65:40: error: expected primary-expression before ')' token                                                                                     

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote