diff --git a/.vscode/settings.json b/.vscode/settings.json index 378100c..99f2e92 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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" + } } \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..843e64a --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +# Author: Elijah Opoku-Nyarko and Jake Colbert + +# default target +all: project4 + +# compile the code into an executable called 'project1' using C++ 2011 +project4: project4.cc + g++ -std=c++11 -o project4 project4.cc + +# test the code against an expected output file +# this test should grow into a spaceship and end with blinkers +test-nowhere: + ./project4 < test-files/t01-nowhere.in > tests/project4.out + diff test-files/project4.out test-files/t01-nowhere.out > test.diff + wc -l test.diff diff --git a/project5.cc b/project5.cc new file mode 100644 index 0000000..42e8448 --- /dev/null +++ b/project5.cc @@ -0,0 +1,27 @@ +/** + * @file project5.cc + * @author Elijah Opoku-Nyarko and Jake Colbert + * @brief + * @version 0.1 + * @date 2022-04-14 + * + * @copyright Copyright (c) 2022 + * + * Project 5 main file + */ + +#include +#include +#include + +#include "province.h" + +using namespace std; + + +int main() { + while(!cin.eof()) { + Province province(cin); + } +} + diff --git a/province.cc b/province.cc index 2308b5c..023aa7d 100644 --- a/province.cc +++ b/province.cc @@ -5,9 +5,12 @@ #include "./province.h" #include +#include #include #include +using namespace std; + /* * Constructor * @param source File containing province: @@ -19,15 +22,36 @@ * 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; + // Read first line of input + source >> _numberOfTowns >> _numberOfRoads; + std::map townMap; // maps town names to their indexes in _towns + + // Read town names + for (int i = 0; i < _numberOfTowns; i++) { + source >> _towns.push_back(Town()); // This needs to be converted to vector, im not sure how exactly + townMap[_towns[i]._name] = i; + } + + for (int i = 0; i < _numberOfRoads; i++) { + string head, tail; + char bridge; + float distance; + source >> head >> tail >> bridge >> distance; + // add a road to the _roads array that belongs to the town at the index map.at(head) returns + _towns[townMap.at(head)]._roads.push_back(Road(townMap.at(head), townMap.at(tail), bridge, distance)); + } +} + +void Province::printAll(int start, std::ostream & output) const{ + output << "The input data is:" << endl << endl; - _towns = new Town[_numberOfTowns]; - std::map nameMap; + for (Town town : _towns) { + output << _towns[i]._name << endl; + for (Road road : _towns[i]._roads) { + output << "\t" << endl; // needs to be finished too + } - // Read town names - for (int i = 0; i < _numberOfTowns; i++) { - source >> _towns[i]._name; - nameMap[_towns[i]._name] = i; - } \ No newline at end of file + } +} \ No newline at end of file diff --git a/province.h b/province.h index e43ec97..1c114a2 100644 --- a/province.h +++ b/province.h @@ -9,6 +9,10 @@ #include #include + +// re-evaluate security of these classes + + /** * towns connected by roads */ @@ -47,7 +51,7 @@ class Province /** * Destructor */ - ~Province() { delete [] _towns; } + ~Province() {} private: @@ -70,13 +74,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; @@ -88,7 +92,7 @@ class Province */ class Town { - public: + public: // consider making some of these private (RoadList) std::string _name; typedef std::list RoadList; RoadList _roads; @@ -96,6 +100,6 @@ class Province int _numberOfTowns; int _numberOfRoads; - Town *_towns; + std::vector _towns; std::vector _roads; }; diff --git a/test-data/.gitignore b/test-data/.gitignore new file mode 100644 index 0000000..68890e9 --- /dev/null +++ b/test-data/.gitignore @@ -0,0 +1,9 @@ +# Mac files +.DS_Store + +# Mac + Google Drive files +Icon? + +# Emacs backup and temp files +*~ +\#*\# diff --git a/test-data/README.md b/test-data/README.md new file mode 100644 index 0000000..3bd8083 --- /dev/null +++ b/test-data/README.md @@ -0,0 +1,10 @@ +# cps222p5-test-data + +This repo provides sample input and output files for testing your +Project 5 code. Much more test data is needed to ensure your +code works. Remember: **if you haven't tested it, it doesn't work.** + +* .in: sample input +* .out: expected output for matching input. Please match the format of each line exactly, by comparing your output to this with the "diff" command. Try to make the output line order match, too. But if you are confident your code is producing correct output in a different valid order, that is acceptable. + +There is also a .jpg image of a diagram of some input. diff --git a/test-data/t01-nowhere.in b/test-data/t01-nowhere.in new file mode 100644 index 0000000..03ac989 --- /dev/null +++ b/test-data/t01-nowhere.in @@ -0,0 +1,2 @@ +1 0 +NOWHERESVILLE diff --git a/test-data/t01-nowhere.out b/test-data/t01-nowhere.out new file mode 100644 index 0000000..e2713fa --- /dev/null +++ b/test-data/t01-nowhere.out @@ -0,0 +1,20 @@ +The input data is: + +NOWHERESVILLE + +The shortest paths from NOWHERESVILLE are: + + +The road upgrading goal can be achieved at minimal cost by upgrading: + +Connected components in event of a major storm are: + If all bridges fail, the following towns would form an isolated group: + NOWHERESVILLE + +Destruction of any of the following would result in the province becoming +disconnected: + (None) + + +------------------------------------------------------------------ + diff --git a/test-data/t02-one-road.in b/test-data/t02-one-road.in new file mode 100644 index 0000000..07a059f --- /dev/null +++ b/test-data/t02-one-road.in @@ -0,0 +1,4 @@ +2 1 +Wenham +Hamilton +Wenham Hamilton N 1 diff --git a/test-data/t02-one-road.out b/test-data/t02-one-road.out new file mode 100644 index 0000000..06c237d --- /dev/null +++ b/test-data/t02-one-road.out @@ -0,0 +1,28 @@ +The input data is: + +Wenham + Hamilton 1 mi +Hamilton + Wenham 1 mi + +The shortest paths from Wenham are: + + The shortest path from Wenham to Hamilton is 1 mi: + Wenham + Hamilton + +The road upgrading goal can be achieved at minimal cost by upgrading: + Hamilton to Wenham + +Connected components in event of a major storm are: + If all bridges fail, the following towns would form an isolated group: + Wenham + Hamilton + +Destruction of any of the following would result in the province becoming +disconnected: + (None) + + +------------------------------------------------------------------ + diff --git a/test-data/t03-simple.in b/test-data/t03-simple.in new file mode 100644 index 0000000..afa0b91 --- /dev/null +++ b/test-data/t03-simple.in @@ -0,0 +1,6 @@ +3 2 +A +B +C +A B B 1 +B C B 1 diff --git a/test-data/t03-simple.out b/test-data/t03-simple.out new file mode 100644 index 0000000..834c730 --- /dev/null +++ b/test-data/t03-simple.out @@ -0,0 +1,41 @@ +The input data is: + +A + B 1 mi via bridge +B + A 1 mi via bridge + C 1 mi via bridge +C + B 1 mi via bridge + +The shortest paths from A are: + + The shortest path from A to B is 1 mi: + A + B + The shortest path from A to C is 2 mi: + A + B + C + +The road upgrading goal can be achieved at minimal cost by upgrading: + B to A + C to B + +Connected components in event of a major storm are: + If all bridges fail, the following towns would form an isolated group: + A + + If all bridges fail, the following towns would form an isolated group: + B + + If all bridges fail, the following towns would form an isolated group: + C + +Destruction of any of the following would result in the province becoming +disconnected: + B + + +------------------------------------------------------------------ + diff --git a/test-data/t05-local.in b/test-data/t05-local.in new file mode 100644 index 0000000..a9fd062 --- /dev/null +++ b/test-data/t05-local.in @@ -0,0 +1,12 @@ +5 6 +Salem +Wenham +Beverly +Danvers +Lynn +Beverly Danvers N 2.9 +Beverly Salem B 2.4 +Beverly Wenham N 5.2 +Danvers Wenham N 4.2 +Danvers Salem B 3.7 +Lynn Salem N 4.9 diff --git a/test-data/t05-local.out b/test-data/t05-local.out new file mode 100644 index 0000000..e3e0063 --- /dev/null +++ b/test-data/t05-local.out @@ -0,0 +1,59 @@ +The input data is: + +Salem + Beverly 2.4 mi via bridge + Danvers 3.7 mi via bridge + Lynn 4.9 mi +Beverly + Danvers 2.9 mi + Salem 2.4 mi via bridge + Wenham 5.2 mi +Danvers + Beverly 2.9 mi + Wenham 4.2 mi + Salem 3.7 mi via bridge +Lynn + Salem 4.9 mi +Wenham + Beverly 5.2 mi + Danvers 4.2 mi + +The shortest paths from Salem are: + + The shortest path from Salem to Wenham is 7.6 mi: + Salem + Beverly + Wenham + The shortest path from Salem to Beverly is 2.4 mi: + Salem + Beverly + The shortest path from Salem to Danvers is 3.7 mi: + Salem + Danvers + The shortest path from Salem to Lynn is 4.9 mi: + Salem + Lynn + +The road upgrading goal can be achieved at minimal cost by upgrading: + Beverly to Salem + Danvers to Beverly + Danvers to Wenham + Lynn to Salem + +Connected components in event of a major storm are: + If all bridges fail, the following towns would form an isolated group: + Salem + Lynn + + If all bridges fail, the following towns would form an isolated group: + Wenham + Beverly + Danvers + +Destruction of any of the following would result in the province becoming +disconnected: + Salem + + +------------------------------------------------------------------ + diff --git a/test-data/t0502-combo.in b/test-data/t0502-combo.in new file mode 100644 index 0000000..8ec6cb4 --- /dev/null +++ b/test-data/t0502-combo.in @@ -0,0 +1,10 @@ +2 1 +Wenham +Hamilton +Wenham Hamilton N 1 +3 2 +A +B +C +A B B 1 +B C B 1 diff --git a/test-data/t0502-combo.out b/test-data/t0502-combo.out new file mode 100644 index 0000000..e8d5e1b --- /dev/null +++ b/test-data/t0502-combo.out @@ -0,0 +1,69 @@ +The input data is: + +Wenham + Hamilton 1 mi +Hamilton + Wenham 1 mi + +The shortest paths from Wenham are: + + The shortest path from Wenham to Hamilton is 1 mi: + Wenham + Hamilton + +The road upgrading goal can be achieved at minimal cost by upgrading: + Hamilton to Wenham + +Connected components in event of a major storm are: + If all bridges fail, the following towns would form an isolated group: + Wenham + Hamilton + +Destruction of any of the following would result in the province becoming +disconnected: + (None) + + +------------------------------------------------------------------ + +The input data is: + +A + B 1 mi via bridge +B + A 1 mi via bridge + C 1 mi via bridge +C + B 1 mi via bridge + +The shortest paths from A are: + + The shortest path from A to B is 1 mi: + A + B + The shortest path from A to C is 2 mi: + A + B + C + +The road upgrading goal can be achieved at minimal cost by upgrading: + B to A + C to B + +Connected components in event of a major storm are: + If all bridges fail, the following towns would form an isolated group: + A + + If all bridges fail, the following towns would form an isolated group: + B + + If all bridges fail, the following towns would form an isolated group: + C + +Destruction of any of the following would result in the province becoming +disconnected: + B + + +------------------------------------------------------------------ + diff --git a/test-data/t08-diagram.jpg b/test-data/t08-diagram.jpg new file mode 100644 index 0000000..6faf188 Binary files /dev/null and b/test-data/t08-diagram.jpg differ diff --git a/test-data/t08-local.in b/test-data/t08-local.in new file mode 100644 index 0000000..63c0126 --- /dev/null +++ b/test-data/t08-local.in @@ -0,0 +1,22 @@ +9 12 +SALEM +AMESBURY +BEVERLY +DANVERS +HAMILTON +IPSWICH +LYNN +PEABODY +WENHAM +SALEM BEVERLY B 2 +SALEM DANVERS N 5 +SALEM LYNN B 4 +SALEM PEABODY N 3 +BEVERLY DANVERS N 2 +BEVERLY WENHAM N 5 +DANVERS PEABODY N 2 +DANVERS WENHAM N 5 +HAMILTON WENHAM N 1 +HAMILTON IPSWICH N 4 +LYNN PEABODY N 3 +IPSWICH AMESBURY B 10 diff --git a/test-data/t08-local.out b/test-data/t08-local.out new file mode 100644 index 0000000..05d6d04 --- /dev/null +++ b/test-data/t08-local.out @@ -0,0 +1,107 @@ +The input data is: + +SALEM + BEVERLY 2 mi via bridge + DANVERS 5 mi + LYNN 4 mi via bridge + PEABODY 3 mi +BEVERLY + SALEM 2 mi via bridge + DANVERS 2 mi + WENHAM 5 mi +DANVERS + SALEM 5 mi + BEVERLY 2 mi + PEABODY 2 mi + WENHAM 5 mi +LYNN + SALEM 4 mi via bridge + PEABODY 3 mi +PEABODY + SALEM 3 mi + DANVERS 2 mi + LYNN 3 mi +WENHAM + BEVERLY 5 mi + DANVERS 5 mi + HAMILTON 1 mi +HAMILTON + WENHAM 1 mi + IPSWICH 4 mi +IPSWICH + HAMILTON 4 mi + AMESBURY 10 mi via bridge +AMESBURY + IPSWICH 10 mi via bridge + +The shortest paths from SALEM are: + + The shortest path from SALEM to AMESBURY is 22 mi: + SALEM + BEVERLY + WENHAM + HAMILTON + IPSWICH + AMESBURY + The shortest path from SALEM to BEVERLY is 2 mi: + SALEM + BEVERLY + The shortest path from SALEM to DANVERS is 4 mi: + SALEM + BEVERLY + DANVERS + The shortest path from SALEM to HAMILTON is 8 mi: + SALEM + BEVERLY + WENHAM + HAMILTON + The shortest path from SALEM to IPSWICH is 12 mi: + SALEM + BEVERLY + WENHAM + HAMILTON + IPSWICH + The shortest path from SALEM to LYNN is 4 mi: + SALEM + LYNN + The shortest path from SALEM to PEABODY is 3 mi: + SALEM + PEABODY + The shortest path from SALEM to WENHAM is 7 mi: + SALEM + BEVERLY + WENHAM + +The road upgrading goal can be achieved at minimal cost by upgrading: + WENHAM to HAMILTON + BEVERLY to SALEM + DANVERS to BEVERLY + PEABODY to DANVERS + PEABODY to LYNN + IPSWICH to HAMILTON + WENHAM to BEVERLY + IPSWICH to AMESBURY + +Connected components in event of a major storm are: + If all bridges fail, the following towns would form an isolated group: + SALEM + DANVERS + PEABODY + BEVERLY + WENHAM + LYNN + HAMILTON + IPSWICH + + If all bridges fail, the following towns would form an isolated group: + AMESBURY + +Destruction of any of the following would result in the province becoming +disconnected: + IPSWICH + HAMILTON + WENHAM + + +------------------------------------------------------------------ +