Skip to content

Commit

Permalink
Merge pull request #1 from gordon-cs/requirement-1
Browse files Browse the repository at this point in the history
Pair programmed some parts and individual commits from both partners - progress for Lab 12 - Jake and Elijah
  • Loading branch information
elijahbigk77 authored Apr 21, 2022
2 parents b78b2c7 + 3a86b6f commit 2860e2e
Show file tree
Hide file tree
Showing 26 changed files with 791 additions and 32 deletions.
Binary file added .DS_Store
Binary file not shown.
47 changes: 47 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# From a github template for removing unneccessary C++ files

# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.app

# The project1 executable and related files
project5

# Files returned by tests
*.diff
test-data/project5.out

# Mac setup files
*.DS_Store

# VS Code setup files
*.vscode

92 changes: 91 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,95 @@
"C_Cpp_Runner.cppStandard": "",
"C_Cpp_Runner.excludeSearch": [],
"C_Cpp_Runner.enableWarnings": true,
"C_Cpp_Runner.warningsAsError": false
"C_Cpp_Runner.warningsAsError": false,
"files.associations": {
"iostream": "cpp",
"map": "cpp",
"__string": "cpp",
"ios": "cpp",
"__bit_reference": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__tuple": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"exception": "cpp",
"forward_list": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"queue": "cpp",
"random": "cpp",
"ratio": "cpp",
"semaphore": "cpp",
"sstream": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"thread": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"variant": "cpp",
"vector": "cpp",
"__functional_base": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"utility": "cpp",
"*.tcc": "cpp",
"memory_resource": "cpp",
"stop_token": "cpp"
}
}
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Author: Elijah Opoku-Nyarko and Jake Colbert

# default target
all: project5

# compile the code into an executable called 'project1' using C++ 2011
project5: project5.cc province.cc province.h
g++ -std=c++11 -o project5 project5.cc province.cc

# test the code against an expected output file
# this test should grow into a spaceship and end with blinkers
test-requirement1: project5
./project5 < test-data/requirement1.in > test-data/project5.out
diff test-data/project5.out test-data/requirement1.out > test.diff
wc -l test.diff
Binary file added project5
Binary file not shown.
51 changes: 51 additions & 0 deletions project5.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* @file: project5.cc
*
*@brief:
* Main program for CPS222 project 5 - test driver to read in a graph
* from standard input and test the read graph
*
* @Authors: Elijah Opoku-Nyarko & Jake Colbert
*/

#include <iostream>
#include <string>
#include "./province.h"

using namespace std;

/*
* check if we are at the end of the file
*/
bool eof() {
char c;
cin >> c;
// if the fie is ended, return true
if (cin.eof()) {
return true;
} else {
// if the file contains more data, return the previously gotten
// data to cin
cin.unget();
return false;
}
}

int main(int argc, char *argv[]) {
// Repeatedly read input from standard input
while (!eof()) {
// create a new graph for each loop, which will read all of the
// corresponding data per graph
Province theProvince(cin);

cout << endl;
cout << "------------------------------------------------" << endl;
cout << "---------------- New DataSet: ------------------" << endl;
cout << "------------------------------------------------" << endl;
cout << endl;

theProvince.printAll(0, cout);


}
}
105 changes: 81 additions & 24 deletions province.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,88 @@
* Authors: Elijah Opoku-Nyarko and Jake Colbert
*/

#include "./province.h"
#include <algorithm>
#include <string>
#include <stack>
#include <cfloat>

/*
* Constructor
* @param source File containing province:
* 1. One line: number of towns (n), number of roads (p)
* as integers
* 2. n lines: names of towns, all uppercase
* 3. p lines: roads, defined as names of towns they
* connect, bridge/not bridge, and length in miles
* ex: BEVERLY DANVERS N 2.9 (connects Beverly and
* Danvers, not a bridge, 2.9 miles long)
*/
Province::Province(std::istream &source) {
// Read first line of input
source >> _numberOfTowns >> _numberOfRoads;

_towns = new Town[_numberOfTowns];
std::map<std::string, int> nameMap;

// Read town names
for (int i = 0; i < _numberOfTowns; i++) {
source >> _towns[i]._name;
nameMap[_towns[i]._name] = i;
}
#include "province.h"

using namespace std;

Province::Province(std::istream & source) {
// Read first line of input
source >> _numberOfTowns >> _numberOfRoads;
_towns = new Town[_numberOfTowns];
std::map<std::string, int> townMap; // maps town names to their indexes in _towns

// Read town names
for (int i = 0; i < _numberOfTowns; i++) {
source >> _towns[i]._name;
townMap[_towns[i]._name] = i;
}

// Read roads
for (int i = 0; i < _numberOfRoads; i++) {
string tail, head;
char bridgeFlag;
double length;
source >> tail >> head >> bridgeFlag >> length;
bool isBridge = (bridgeFlag == 'B');
_towns[townMap.at(tail)]._roads.push_back(Road(townMap.at(head), townMap.at(tail), isBridge, length));
_towns[townMap.at(head)]._roads.push_back(Road(townMap.at(tail), townMap.at(head), isBridge, length));
}
}

void Province::printAll(int start, std::ostream & output) {
// keep track of whether a town(vertex) has been scheduled to be visited
bool scheduled[_numberOfTowns];
for (int i = 0; i < _numberOfTowns; i++){
scheduled[i] = false;
}

// Keep track of which towns have been visited
queue <int> toVisit; // use queue to keep track of which town to visit next
toVisit.push(start);
scheduled[start] = true;
output << "The input data is :" << endl << endl;

// Visit every town in the queue
while (!toVisit.empty()) {
int current = toVisit.front();
toVisit.pop();

output << " ";
output << _towns[current]._name << endl;

// Add current town's(vertex's) unscheduled neighbors to the queue
for (Town::RoadList::iterator neighbor = _towns[current]._roads.begin();
neighbor != _towns[current]._roads.end(); neighbor++) {
std::string neighborName = _towns[neighbor->_head]._name;

output << " ";
output << neighborName << " " << neighbor->_length << " mi";

// if the type is bridge, then add to output
if (neighbor ->_isBridge) {
output << " via bridge";
}

output << endl;

int head = neighbor ->_head;

// If neighbour is not scheduled, add neighbor to the queue
if (!scheduled[head]) {
toVisit.push(head);
scheduled[head] = true;
}
}
}

output << endl << endl;

}



16 changes: 9 additions & 7 deletions province.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include <map>
#include <queue>


// re-evaluate security of these classes


/**
* towns connected by roads
*/
Expand All @@ -24,11 +28,9 @@ class Province

/**
* Print towns and roads in province in breadth-first search order
* @param start Index to start traversal at
* @param output Stream to print data to
*/
void printAll(int start, std::ostream & output) const;

void printAll(int start, std::ostream & output);
void printShortestPath(std::ostream & output) const;

/**
Expand All @@ -47,7 +49,7 @@ class Province
/**
* Destructor
*/
~Province() { delete [] _towns; }
~Province() {}

private:

Expand All @@ -70,13 +72,13 @@ class Province
* @param isBridge Whether or not the road is a bridge
* @param length Length of the road in miles
*/
Road(int head, int tail, bool isBridge, double length)
Road(int head, int tail, char isBridge, double length)
: _head(head), _tail(tail), _isBridge(isBridge), _length(length)
{}

int _head; // Index of originating town in vertex array
int _tail;
bool _isBridge;
char _isBridge;
double _length;

bool operator < (Road road2) const;
Expand All @@ -88,7 +90,7 @@ class Province
*/
class Town
{
public:
public: // consider making some of these private (RoadList)
std::string _name;
typedef std::list <Road> RoadList;
RoadList _roads;
Expand Down
9 changes: 9 additions & 0 deletions test-data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Mac files
.DS_Store

# Mac + Google Drive files
Icon?

# Emacs backup and temp files
*~
\#*\#
Loading

0 comments on commit 2860e2e

Please sign in to comment.