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: 3679491 • 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);
}

Use this example code for the text file

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

The output should look like these examples.

Explanation / Answer

Here you go :

#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;
stack<char> S;
while (getline(infile, line)) {
cout << line << endl;
AreParanthesesBalanced(line, S);
}
if (S.empty()) {
cout << "parentheses matched" << endl;
}
infile.close();
system("pause");
return(0);
}

bool ArePair(char opening,char closing)
{
   if(opening == '(' && closing == ')') return true;
   else if(opening == '{' && closing == '}') return true;
   else if(opening == '[' && closing == ']') return true;
   return false;
}
void AreParanthesesBalanced(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() || !ArePair(S.top(),exp[i]))
               cout << "Parentheses not matched" << endl;
           else
               S.pop();
       } else if (exp[i] == '/' && exp[i+1] == '/')
       {
       return;
       }
   }
}

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