C++ help. Write a program that takes as its input a text file that contains some
ID: 3679598 • 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
#include "stdafx.h"
#include <string>
#include <cassert>
#include <iostream>
#include <list>
#include <map>
using namespace std;
const int MILLION = 1000 * 1000;
const int MAX_ACCOUNTS = 40 * MILLION;
const int MAX_UNPAID = 1 * MILLION;
const int MAX_VIOLATIONS = 2 * MILLION;
const int ACCOUNT_MAINTENANCE_FEE_CENTS = 200;
const int VIOLATION_PENALTY_CENTS = 5750;
const int GRACE_PERIOD_DAYS = 5;
enum DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
};
enum Onramp {
> > > > > > > > > > > > > > > > > > };
enum Offramp {
OFFRAMP_UNKNOWN_NORTHBOUND = 0,
OFFRAMP_405 = 1,
OFFRAMP_BEAR = 2,
OFFRAMP_55 = 3,
OFFRAMP_IRVINE = 4,
OFFRAMP_JAMBOREE = 5,
OFFRAMP_UNIVERSITY = 6,
OFFRAMP_MACARTHUR = 7,
OFFRAMP_BISON = 8,
OFFRAMP_BONITA_CANYON = 9,
OFFRAMP_NEWPORT_COAST = 10,
OFFRAMP_EL_TORO = 11,
OFFRAMP_GLENWOOD = 12,
OFFRAMP_ALISO_CREEK = 13,
OFFRAMP_LA_PAZ = 14,
OFFRAMP_GREENFIELD = 15,
OFFRAMP_5 = 16,
OFFRAMP_UNKNOWN_SOUTHBOUND = 17,
};
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;
}
Timestamp() { }
int unix_time() { return _unix_time; }
DayOfWeek day_of_week() { return _day_of_week; }
int hour() { return _hour; }
int minute() { return _minute; }
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;
};
int calculate_toll(bool is_fastrak, Timestamp& time, Onramp start, Offramp end) {
if (start == end)
{
return 0;
}
else if (start < 0 || start >17 || end < 0 || end > 17)
{
return 0;
}
else if (start==0 && end==17|| start == 17 && end == 0 ||end == 4 || end == 5 || end == 6|| start ==5 || start < 9 && end < 9 || start== 12 && end>12 || end==12 && start > 12|| start>14 && end>14)
{
return 0;
}
int moneyCharged = 0;
bool isHeadingNorth;
if (start < end)
{
isHeadingNorth = false;
}
else
{
isHeadingNorth = true;
}
if (is_fastrak)
{
if (start >= 1 && start <= 4 || start >= 6 && start <= 8)
{
if (end == 9)
{
moneyCharged += 102;
}
else if (end == 10)
{
moneyCharged += 231;
}
else if (end >= 11 && end <= 16)
{
moneyCharged += 525;
}
}
else if (start == 9)
{
if (end >= 1 && end && end <= 3 || end >= 7 && end <= 8)
{
moneyCharged += 102;
}
else if (end == 10)
{
moneyCharged += 231;
}
else if (end > 10)
{
moneyCharged += 525;
}
}
else if (start == 10)
{
if (end >= 1 && end && end <= 3 || end >= 7 && end <= 9)
{
moneyCharged += 231;
}
else if (end > 10)
{
moneyCharged += 525;
}
}
else if (start >= 11 && start <= 16 && end >= 1 && end <= 11)
{
if (end >= 1 && end <= 10)
{
moneyCharged += 525;
}
else if (end == 11)
{
moneyCharged += 257;
}
}
else if (start == 11 && end >= 13 && end <= 16)
{
moneyCharged += 257;
}
else if (start == 13 && end >= 14 && end <= 16) {
moneyCharged += 195;
}
else if (start == 14 && end >= 13 && end <= 16)
{
if (end == 13)
{
moneyCharged += 195;
}
else if (end == 15 || end == 16)
{
moneyCharged += 137;
}
}
else if (start <= 16 && start >= 15 && end <= 14 && end >= 13)
{
if (end == 13)
{
moneyCharged += 195;
}
else if (end == 14)
{
moneyCharged += 137;
}
}
if (moneyCharged > 300)
{
if (time.day_of_week() == SATURDAY || time.day_of_week() == SUNDAY)
{
moneyCharged -= 25;
}
else
{
if (isHeadingNorth)
{
if (time.hour() >= 8 && time.hour() < 9)
{
moneyCharged += 153;
}
else if (time.hour() >= 7 && time.hour() < 8 || time.hour() >= 9 && time.hour() < 10)
{
moneyCharged += 123;
}
}
else if (!isHeadingNorth)
{
if (time.hour() >= 17 && time.hour() < 18)
{
moneyCharged += 153;
}
else if (time.hour() >= 15 && time.hour() < 17 || time.hour() >= 18 && time.hour() < 19)
{
moneyCharged += 123;
}
}
}
}
}
else
{
if (start >= 1 && start <= 4 || start >= 6 && start <= 8)
{
if (end == 9)
{
moneyCharged += 202;
}
else if (end == 10)
{
moneyCharged += 331;
}
else if (end >= 11)
{
moneyCharged += 748;
}
}
else if (start == 9)
{
if (end >= 0 && end <= 3)
{
moneyCharged += 202;
}
else if (end >= 7 && end <= 8)
{
moneyCharged += 202;
}
else if (end == 10)
{
moneyCharged += 331;
}
else if (end >= 11)
{
moneyCharged += 748;
}
}
else if (start == 10)
{
if (end >= 0 && end <= 3)
{
moneyCharged += 331;
}
else if (end >= 7 && end <= 9)
{
moneyCharged += 331;
}
else if (end >= 11)
{
moneyCharged += 748;
}
}
else if (start >= 11 && end >= 0 && end <= 11)
{
if (end >= 0 && end <= 3)
{
moneyCharged += 748;
}
else if (end >= 7 && end <= 10)
{
moneyCharged += 748;
}
else if (end == 11)
{
moneyCharged += 357;
}
}
else if (start == 11 && end > 12) {
moneyCharged += 357;
}
else if (start >= 14 && start <= 17 && end == 15)
{
moneyCharged += 295;
}
else if (start == 13 && end >= 14 && end <= 17)
{
moneyCharged += 295;
}
else if(start == 14 && end >= 15 && end <= 17)
{
moneyCharged += 237;
}
else if (start >= 15 && start <= 17 && end == 14)
{
moneyCharged += 237;
}
}
if(start == 17 && end == 11)
{
int blah = 0;
}
return moneyCharged;
}
class TransponderTrip
{
public:
TransponderTrip(Timestamp time, Onramp start, Offramp end, int transponder_id)
{
_time = time;
_start = start;
_end = end;
_transponder_id = transponder_id;
}
Timestamp& time() { return _time; }
Onramp start() { return _start; }
Offramp end() { return _end; }
int transponder_id() { return _transponder_id; }
private:
Timestamp _time;
Onramp _start;
Offramp _end;
int _transponder_id;
};
class LicenseTrip {
public:
LicenseTrip(){}
LicenseTrip(Timestamp time, Onramp start, Offramp end, string& plate_number) {
assert(!plate_number.empty());
_time = time;
_start = start;
_end = end;
_plate_number = plate_number;
}
Timestamp& time() { return _time; }
Onramp start() { return _start; }
Offramp end() { return _end; }
string& plate_number() { return _plate_number; }
int toll(bool is_fastrack, Timestamp& time, Onramp start, Offramp end) {
int tollTotal = calculate_toll(is_fastrack, time, start, end);
return tollTotal;
}
private:
Timestamp _time;
Onramp _start;
Offramp _end;
string _plate_number;
};
class FastTrakAccount {
public:
FastTrakAccount() {
}
FastTrakAccount(int account_number, int balance, int transponder_id, string& plate_number) {
assert(!plate_number.empty());
AccountNumber = account_number;
Balance = balance;
TransponderId = transponder_id;
PlateNumber = plate_number;
}
int AccountNumber;
int Balance;
int TransponderId;
string PlateNumber;
int account_number() { return AccountNumber; }
int balance() { return Balance; }
int transponder_id() { return TransponderId; }
string& plate_number() { return PlateNumber; }
void setBalance(int newBalance)
{
Balance = newBalance;
}
void debit(int cents, FastTrakAccount accountToUpdate) {
if ( cents > 0)
{
accountToUpdate.Balance -= cents;
setBalance(accountToUpdate.Balance);
}
}
void credit(int cents, FastTrakAccount accountToUpdate) {
if ( cents > 0)
{
accountToUpdate.Balance += cents;
setBalance(accountToUpdate.Balance);
int blah = 0;
}
}
private:
int _account_number, _balance, _transponder_id;
string _plate_number;
};
class Violation {
public:
Violation(){}
Violation(string& plate_number, int balance) {
assert(!plate_number.empty());
assert(balance > 0);
_plate_number = plate_number;
_balance = balance;
}
string& plate_number() { return _plate_number; }
int balance() { return _balance; }
private:
string _plate_number;
int _balance;
};
template <typename ELT>
class DoublyLinkedNode {
public:
DoublyLinkedNode(DoublyLinkedNode<ELT>* prev, ELT element, DoublyLinkedNode<ELT>* next) {
_prev = prev;
_element = element;
_next = next;
}
DoublyLinkedNode<ELT>* prev() {
return _prev;
}
ELT element() {
return _element;
}
DoublyLinkedNode<ELT>* next() {
return _next;
}
void set_prev(DoublyLinkedNode<ELT>* prev) {
_prev = prev;
}
void set_element(ELT element) {
_element = element;
}
void set_next(DoublyLinkedNode<ELT>* next) {
_next = next;
}
private:
ELT _element;
DoublyLinkedNode<ELT> *_prev, *_next;
};
template<typename ELT>
class DoublyLinkedListIterator;
template <typename ELT>
class DoublyLinkedList {
friend class DoublyLinkedListIterator<ELT>;
public:
DoublyLinkedList() {
_header = new DoublyLinkedNode<ELT>(nullptr, ELT(), nullptr);
_trailer = new DoublyLinkedNode<ELT>(nullptr, ELT(), nullptr);
_header->set_next(_trailer);
_trailer->set_prev(_header);
_length = 0;
}
~DoublyLinkedList() {
clear();
delete _header;
delete _trailer;
}
int length() { return _length; }
bool is_empty() {
assert( (_header->next() == _trailer) == (0 == _length) );
return (0 == _length);
}
ELT front()
{
assert(!is_empty());
return _header->next()->element();
}
ELT back()
{
assert(!is_empty());
return _trailer->prev()->element();
}
void add_front(ELT e)
{
add_before(_header->next(), e);
}
void add_back(ELT e)
{
add_before(_trailer, e);
}
void remove_front()
{
remove(_header->next());
}
void remove_back() {
remove(_trailer->prev());
}
void clear() {
while (!is_empty())
remove_front();
}
private:
DoublyLinkedNode<ELT> *_header, *_trailer;
int _length;
void add_before(DoublyLinkedNode<ELT>* where, ELT e) {
assert(nullptr != where);
DoublyLinkedNode<ELT>* new_node = new DoublyLinkedNode<ELT>(where->prev(), e, where);
where->prev()->set_next(new_node);
where->set_prev(new_node);
_length++;
}
void remove(DoublyLinkedNode<ELT>* where) {
assert(nullptr != where);
assert(!is_empty());
where->prev()->set_next(where->next());
where->next()->set_prev(where->prev());
delete where;
_length--;
}
};
template<typename ELT>
class DoublyLinkedListIterator {
private:
DoublyLinkedNode<ELT> *_location;
public:
DoublyLinkedListIterator(DoublyLinkedList<ELT>* list) {
assert(nullptr != list);
_location = list->_header->next();
}
bool past_end() { return (nullptr == _location->next()); }
ELT get() {
assert(!past_end());
return _location->element();
}
void advance() {
assert(!past_end());
_location = _location->next();
}
};
class TollAdministrator
{
public:
DoublyLinkedList<FastTrakAccount> AccountList;
TollAdministrator()
{
}
~TollAdministrator()
{
}
int account_count() {
return AccountList.length();
}
FastTrakAccount& account(int i)
{
int counter = 0;
DoublyLinkedList<FastTrakAccount> *accountToReturn = new DoublyLinkedList<FastTrakAccount>;
accountToReturn->add_front(AccountList.front);
while(counter < i)
{
}
}
int unpaid_count() {
return 0;
}
LicenseTrip& unpaid(int index) {
return *((LicenseTrip*) nullptr);
}
int violation_count() {
return 0;
}
Violation& violation(int index) {
return *((Violation*) nullptr);
}
void register_account(FastTrakAccount* account) {
assert(account != nullptr);
AccountList.add_back(*account);
AccountList.length();
}
void register_transponder_trip(TransponderTrip* trip) {
assert(trip != nullptr);
}
void register_license_trip(LicenseTrip* trip) {
assert(trip != nullptr);
}
void one_time_payment(string& plate_number, int cents)
{
}
void debit_monthly_fees()
{
}
void update_violations(Timestamp& current_time)
{
}
private:
};
int main()
{
Timestamp monday(1, MONDAY, 2, 3);
string plate("123456");
{
assert(1 == monday.unix_time());
assert(MONDAY == monday.day_of_week());
assert(2 == monday.hour());
assert(3 == monday.minute());
assert(monday == Timestamp(1, MONDAY, 2, 3));
}
{
Timestamp sat(1, SATURDAY, 9, 0),
sun(1, SUNDAY, 9, 0),
sb_peak(1, MONDAY, 17, 1),
sb_before_peak(1, MONDAY, 15, 1),
sb_after_peak(1, MONDAY, 18, 1),
nb_peak(1, MONDAY, 8, 1),
nb_before_peak(1, MONDAY, 7, 1),
nb_after_peak(1, MONDAY, 9, 1);
assert(0 == calculate_toll(false, sat, ONRAMP_MACARTHUR, OFFRAMP_MACARTHUR));
assert(0 == calculate_toll(false, sat, ONRAMP_BRISTOL, OFFRAMP_GREENFIELD));
assert(0 == calculate_toll(true, sat, ONRAMP_UNKNOWN_SOUTHBOUND, OFFRAMP_UNKNOWN_SOUTHBOUND));
assert(0 == calculate_toll(true, sat, ONRAMP_UNKNOWN_NORTHBOUND, OFFRAMP_UNKNOWN_NORTHBOUND));
assert(102 == calculate_toll(true, sat, ONRAMP_55, OFFRAMP_BONITA_CANYON));
assert(102 == calculate_toll(true, sun, ONRAMP_55, OFFRAMP_BONITA_CANYON));
assert(748 == calculate_toll(false, nb_peak, ONRAMP_EL_TORO, OFFRAMP_55));
assert(748 == calculate_toll(false, nb_before_peak, ONRAMP_EL_TORO, OFFRAMP_55));
assert(748 == calculate_toll(false, nb_after_peak, ONRAMP_EL_TORO, OFFRAMP_55));
assert(357 == calculate_toll(false, sb_peak, ONRAMP_UNKNOWN_NORTHBOUND, OFFRAMP_EL_TORO));
assert(0 == calculate_toll(true, sb_peak, ONRAMP_FAIRVIEW, OFFRAMP_JAMBOREE));
assert(202 == calculate_toll(false, sb_peak, ONRAMP_JAMBOREE, OFFRAMP_BONITA_CANYON));
assert((525 + 153) == calculate_toll(true, sb_peak, ONRAMP_BISON, OFFRAMP_GREENFIELD));
}
{
TransponderTrip trip(monday, ONRAMP_405, OFFRAMP_5, 123);
assert(monday == trip.time());
assert(ONRAMP_405 == trip.start());
assert(OFFRAMP_5 == trip.end());
assert(123 == trip.transponder_id());
}
{
LicenseTrip trip(monday, ONRAMP_405, OFFRAMP_5, plate);
assert(monday == trip.time());
assert(ONRAMP_405 == trip.start());
assert(OFFRAMP_5 == trip.end());
assert(plate == trip.plate_number());
assert(748 == trip.toll(false, trip.time(), trip.start(), trip.end()));
}
{
FastTrakAccount account(1, 2, 3, plate);
assert(1 == account.account_number());
assert(2 == account.balance());
assert(3 == account.transponder_id());
assert(plate == account.plate_number());
account.credit(500, account);
assert(502 == account.balance());
account.debit(300, account);
assert(202 == account.balance());
}
{
Violation violation(plate, 123);
assert(plate == violation.plate_number());
assert(123 == violation.balance());
}
{
TollAdministrator admin;
assert(0 == admin.account_count());
assert(0 == admin.unpaid_count());
assert(0 == admin.violation_count());
FastTrakAccount* a1 = new FastTrakAccount(1, 0, 123, string("PLT1"));
FastTrakAccount* a2 = new FastTrakAccount(2, 0, 456, string("PLT2"));
admin.register_account(a1);
assert(1 == admin.account_count());
assert("PLT1" == admin.account(0).plate_number());
admin.register_account(a2);
assert(2 == admin.account_count());
assert("PLT1" == admin.account(0).plate_number());
assert("PLT2" == admin.account(1).plate_number());
TransponderTrip* t1 = new TransponderTrip(Timestamp(1, TUESDAY, 8, 21), ONRAMP_BEAR, OFFRAMP_GREENFIELD, 456);
admin.register_transponder_trip(t1);
assert(2 == admin.account_count());
assert(0 == admin.unpaid_count());
assert(0 == admin.violation_count());
assert(-525 == admin.account(1).balance());
TransponderTrip* t2 = new TransponderTrip(Timestamp(1, WEDNESDAY, 8, 21), ONRAMP_5, OFFRAMP_EL_TORO, 123);
admin.register_transponder_trip(t2);
assert(2 == admin.account_count());
assert(0 == admin.unpaid_count());
assert(0 == admin.violation_count());
assert(-257 == admin.account(0).balance());
TransponderTrip* t3 = new TransponderTrip(Timestamp(1, THURSDAY, 8, 21), ONRAMP_5, OFFRAMP_EL_TORO, 789);
admin.register_transponder_trip(t3);
assert(2 == admin.account_count());
assert(0 == admin.unpaid_count());
assert(0 == admin.violation_count());
assert(-257 == admin.account(0).balance());
assert(-525 == admin.account(1).balance());
LicenseTrip* t4 = new LicenseTrip(Timestamp(1, MONDAY, 18, 41), ONRAMP_405, OFFRAMP_5, string("MRBIG"));
admin.register_license_trip(t4);
assert(2 == admin.account_count());
assert(1 == admin.unpaid_count());
assert(0 == admin.violation_count());
assert("MRBIG" == admin.unpaid(0).plate_number());
assert(748 == admin.unpaid(0).toll(false, t4->time(), t4->start(), t4->end()));
LicenseTrip* t5 = new LicenseTrip(Timestamp(1, SUNDAY, 11, 42), ONRAMP_405, OFFRAMP_5, string("EWING2"));
admin.register_license_trip(t5);
assert(2 == admin.account_count());
assert(2 == admin.unpaid_count());
assert(0 == admin.violation_count());
assert("EWING2" == admin.unpaid(1).plate_number());
assert(748 == admin.unpaid(1).toll(false, t5->time(), t5->start(), t5->end()));
admin.one_time_payment(string("MRBIG"), 100);
assert(2 == admin.account_count());
assert(2 == admin.unpaid_count());
assert(0 == admin.violation_count());
admin.one_time_payment(string("OUTATIME"), 800);
assert(2 == admin.account_count());
assert(2 == admin.unpaid_count());
assert(0 == admin.violation_count());
admin.one_time_payment(string("EWING2"), 748);
assert(2 == admin.account_count());
assert(1 == admin.unpaid_count());
assert(0 == admin.violation_count());
assert("MRBIG" == admin.unpaid(0).plate_number());
admin.update_violations(Timestamp(1000, MONDAY, 12, 12));
assert(2 == admin.account_count());
assert(1 == admin.unpaid_count());
assert(0 == admin.violation_count());
admin.update_violations(Timestamp(MILLION, MONDAY, 12, 12));
assert(2 == admin.account_count());
assert(0 == admin.unpaid_count());
assert(1 == admin.violation_count());
assert("MRBIG" == admin.violation(0).plate_number());
assert((5750 + 748) == admin.violation(0).balance());
admin.debit_monthly_fees();
assert(2 == admin.account_count());
assert(0 == admin.unpaid_count());
assert(1 == admin.violation_count());
assert(-457 == admin.account(0).balance());
assert(-725 == admin.account(1).balance());
}
cout << "Success, all tests passed." << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.