C++ help So I\'m trying to write a program that takes as its input a text file t
ID: 3680193 • Letter: C
Question
C++ help
So I'm trying to 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.
So far I have this as my code. It is not working though. Please help me out. thanks!
#include "stdafx.h"
#include <stack>
#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
}
int lineNum=0;
string line;
stack<char> S;
while (getline(infile, line)) {
lineNum++;
size_t pos = 0;
while (string::npos != (pos = line.find_first_of("(){}[]", pos))) {
int colNum = pos + 1;
switch (line[pos]) {
case '(': S.push(')'); break;
case '{': S.push('}'); break;
case '[': S.push(']'); break;
case ']':
case '}':
case ')':
if (S.empty()) {
cout << "Mismatched " << line[pos]
<< " at line " << lineNum << ", col " << colNum
<< endl;
return false;
}
if (line[pos] != S.top()) {
cout << "Expected " << S.top()
<< ", found " << line[pos]
<< " at line " << lineNum << ", col " << colNum
<< endl;
return false;
}
S.pop();
}
pos = colNum;
}
}
if (S.empty()) {
cout << "parentheses matched" << endl;
}
infile.close();
system("pause");
return(0);
}
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.)
*/
example1.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;
};
example3.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;
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.