Commit d5f28141 authored by Colby Green's avatar Colby Green
Browse files

initial commit

parents
// Project Identifier: 5949F553E20B650AB0FB2266D3C0822B13D248B0
#include <vector>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <string>
using namespace std;
struct Pokemon {
Pokemon(){}
Pokemon(int xcord, int ycord){
this->xcord = xcord;
this->ycord = ycord;
minDistanceTo = numeric_limits<double>::infinity();
precedingPokemonIndex = -1;
if ((xcord == 0 && ycord <= 0) || (xcord <= 0 && ycord == 0)){
terrain = 'c';
}else if (xcord <= 0 && ycord <= 0){
terrain = 'w';
}else {
terrain = 'l';
}
}
char terrain;
double minDistanceTo;
int precedingPokemonIndex;
int xcord;
int ycord;
};
double pokeDistance(Pokemon &poke1, Pokemon &poke2){
if ((poke1.terrain == 'l' && poke2.terrain == 'w') || (poke1.terrain == 'w' && poke2.terrain == 'l')){
return numeric_limits<double>::infinity();
}else {
double x = poke1.xcord - poke2.xcord;
double y = poke1.ycord - poke2.ycord;
double dist = (x * x) + (y * y);
dist = sqrt(dist);
return dist;
}
}
class Mst {
public:
void readFile(){
cin >> numPokemon;
int xcord, ycord;
int index = 0;
while (cin >> xcord && cin >> ycord){
Pokemon pokemon(xcord, ycord);
if (!index){
pokemon.minDistanceTo = 0;
}
index++;
pokeDex.push_back(pokemon);
}
}
void findMst(){
vector<size_t> innies;
vector<size_t> outies(pokeDex.size());
iota(outies.begin(), outies.begin() + int(pokeDex.size()), 0);
while (!outies.empty()){
double minDistance = numeric_limits<double>::infinity();
size_t minIndex = numeric_limits<size_t>::infinity();
for (size_t i = 0; i < outies.size(); i++){
size_t curPokeIndex = outies[i];
if (pokeDex[curPokeIndex].minDistanceTo < minDistance){
minDistance = pokeDex[curPokeIndex].minDistanceTo;
minIndex = i;
}
}
size_t pokemonIndex = outies[minIndex];
innies.push_back(pokemonIndex);
outies.erase(outies.begin() + int(minIndex));
for (size_t i = 0; i < outies.size(); i++){
size_t curPokeIndex = outies[i];
double distance = pokeDistance(pokeDex[pokemonIndex], pokeDex[curPokeIndex]);
if (distance < pokeDex[curPokeIndex].minDistanceTo){
pokeDex[curPokeIndex].minDistanceTo = distance;
pokeDex[curPokeIndex].precedingPokemonIndex = int(pokemonIndex);
}
}
} //while
string coords;
double total = 0;
for (size_t i = 0; i < pokeDex.size(); i++){
if (pokeDex[i].precedingPokemonIndex != -1){
total += pokeDex[i].minDistanceTo;
if (i < size_t(pokeDex[i].precedingPokemonIndex)){
coords += to_string(i) + " " + to_string(pokeDex[i].precedingPokemonIndex) + "\n";
}else {
coords += to_string(pokeDex[i].precedingPokemonIndex) + " " + to_string(i) + "\n";
}
}
}
cout << total << "\n";
cout << coords;
}
private:
vector<Pokemon> pokeDex;
int numPokemon;
};
## EECS 281 Advanced Makefile
# How to use this Makefile...
###################
###################
## ##
## $ make help ##
## ##
###################
###################
# IMPORTANT NOTES:
# 1. Set EXECUTABLE to the command name from the project specification.
# 2. To enable automatic creation of unit test rules, your program logic
# (where main() is) should be in a file named project*.cpp or
# specified in the PROJECTFILE variable.
# 3. Files you want to include in your final submission cannot match the
# test*.cpp pattern.
#######################
# TODO (begin) #
#######################
# Change IDENTIFIER to match the project identifier given in the project spec.
IDENTIFIER = 5949F553E20B650AB0FB2266D3C0822B13D248B0
# Change EXECUTABLE to match the command name given in the project spec.
EXECUTABLE = poke
# The following line looks for a project's main() in files named project*.cpp,
# executable.cpp (substituted from EXECUTABLE above), or main.cpp
PROJECTFILE = $(or $(wildcard project*.cpp $(EXECUTABLE).cpp), main.cpp)
# If main() is in another file delete line above, edit and uncomment below
#PROJECTFILE = mymainfile.cpp
#######################
# TODO (end) #
#######################
# enables c++17 on CAEN or 281 autograder
PATH := /usr/um/gcc-6.2.0/bin:$(PATH)
LD_LIBRARY_PATH := /usr/um/gcc-6.2.0/lib64
LD_RUN_PATH := /usr/um/gcc-6.2.0/lib64
# This is the path from the CAEN home folder to where projects will be
# uploaded. (eg. /home/mmdarden/eecs281/project1)
# Change this if you prefer a different path.
# REMOTE_BASEDIR := w18/eecs281 # /home/mmdarden/w18/eecs281/project0
REMOTE_BASEDIR := eecs281
# designate which compiler to use
CXX = g++
# list of test drivers (with main()) for development
TESTSOURCES = $(wildcard test*.cpp)
# names of test executables
TESTS = $(TESTSOURCES:%.cpp=%)
# list of sources used in project
SOURCES = $(wildcard *.cpp)
SOURCES := $(filter-out $(TESTSOURCES), $(SOURCES))
# list of objects used in project
OBJECTS = $(SOURCES:%.cpp=%.o)
# name of the tarball created for submission
PARTIAL_SUBMITFILE = partialsubmit.tar.gz
FULL_SUBMITFILE = fullsubmit.tar.gz
# name of the perf data file, only used by the clean target
PERF_FILE = perf.data*
#Default Flags (we prefer -std=c++17 but Mac/Xcode/Clang doesn't support)
CXXFLAGS = -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic
# make release - will compile "all" with $(CXXFLAGS) and the -O3 flag
# also defines NDEBUG so that asserts will not check
release: CXXFLAGS += -O3 -DNDEBUG
release: $(EXECUTABLE)
# make debug - will compile sources with $(CXXFLAGS) and the -g3 flag
# also defines DEBUG, so "#ifdef DEBUG /*...*/ #endif" works
debug: CXXFLAGS += -g3 -DDEBUG
debug:
$(CXX) $(CXXFLAGS) $(SOURCES) -o $(EXECUTABLE)_debug
# make profile - will compile "all" with $(CXXFLAGS) and the -pg flag
profile: CXXFLAGS += -pg
profile:
$(CXX) $(CXXFLAGS) $(SOURCES) -o $(EXECUTABLE)_profile
# make static - will perform static analysis in the matter currently used
# on the autograder
static:
cppcheck --enable=all --suppress=missingIncludeSystem \
$(SOURCES) *.h *.hpp
# make identifier - will check to ensure that all source code and header files
# include the project identifier; skip subdirectories;
# also removes old submit tarballs, they are outdated
identifier:
@if [ $$(grep --include=*.{h,hpp,c,cpp} --exclude=xcode_redirect.hpp --directories=skip -L $(IDENTIFIER) * | wc -l) -ne 0 ]; then \
printf "Missing project identifier in file(s): "; \
echo `grep --include=*.{h,hpp,c,cpp} --directories=skip -L $(IDENTIFIER) *`; \
rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE); \
exit 1; \
fi
# Build all executables
all: release debug profile
$(EXECUTABLE): $(OBJECTS)
ifeq ($(EXECUTABLE), executable)
@echo Edit EXECUTABLE variable in Makefile.
@echo Using default a.out.
$(CXX) $(CXXFLAGS) $(OBJECTS)
else
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(EXECUTABLE)
endif
# Automatically generate any build rules for test*.cpp files
define make_tests
ifeq ($$(PROJECTFILE),)
@echo Edit PROJECTFILE variable to .cpp file with main\(\)
@exit 1
endif
SRCS = $$(filter-out $$(PROJECTFILE), $$(SOURCES))
OBJS = $$(SRCS:%.cpp=%.o)
HDRS = $$(wildcard *.h *.hpp)
$(1): CXXFLAGS += -g3 -DDEBUG
$(1): $$(OBJS) $$(HDRS) $(1).cpp
$$(CXX) $$(CXXFLAGS) $$(OBJS) $(1).cpp -o $(1)
endef
$(foreach test, $(TESTS), $(eval $(call make_tests, $(test))))
alltests: $(TESTS)
# rule for creating objects
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $*.cpp
# make clean - remove .o files, executables, tarball
clean:
rm -f $(OBJECTS) $(EXECUTABLE) $(EXECUTABLE)_debug $(EXECUTABLE)_profile \
$(TESTS) $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE) $(PERF_FILE)
rm -Rf *.dSYM
# make partialsubmit.tar.gz - cleans, creates tarball
# omitting test files
PARTIAL_SUBMITFILES=$(filter-out $(TESTSOURCES), \
$(wildcard Makefile *.h *.hpp *.cpp))
$(PARTIAL_SUBMITFILE): $(PARTIAL_SUBMITFILES)
rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE)
COPYFILE_DISABLE=true tar -vczf $(PARTIAL_SUBMITFILE) \
$(PARTIAL_SUBMITFILES)
@echo !!! WARNING: No test files included. Use 'make fullsubmit' to include test files. !!!
# make fullsubmit.tar.gz - cleans, runs dos2unix, creates tarball
# including test files
FULL_SUBMITFILES=$(filter-out $(TESTSOURCES), \
$(wildcard Makefile *.h *.hpp *.cpp test*.txt))
$(FULL_SUBMITFILE): $(FULL_SUBMITFILES)
rm -f $(PARTIAL_SUBMITFILE) $(FULL_SUBMITFILE)
COPYFILE_DISABLE=true tar -vczf $(FULL_SUBMITFILE) $(FULL_SUBMITFILES)
@echo !!! Final submission prepared, test files included... READY FOR GRADING !!!
# shortcut for make submit tarballs
partialsubmit: identifier $(PARTIAL_SUBMITFILE)
fullsubmit: identifier $(FULL_SUBMITFILE)
sync2caen: REMOTE_PATH := ${REMOTE_BASEDIR}_${EXECUTABLE}_sync
sync2caen:
# Synchronize local files into target directory on CAEN
rsync \
-av \
--exclude '.git*' \
--exclude '.vs*' \
--exclude '*.code-workspace' \
--filter=":- .gitignore" \
"."/ \
"login.engin.umich.edu:'${REMOTE_PATH}/'"
echo "Files synced to CAEN at ~/${REMOTE_PATH}/"
define MAKEFILE_HELP
EECS281 Advanced Makefile Help
* This Makefile uses advanced techniques, for more information:
$$ man make
* General usage
1. Follow directions at each "TODO" in this file.
a. Set EXECUTABLE equal to the name from the project specification.
b. Set PROJECTFILE equal to the name of the source file with main()
c. Add any dependency rules specific to your files.
2. Build, test, submit... repeat as necessary.
* Preparing submissions
A) To build 'partialsubmit.tar.gz', a tarball without tests used to
find buggy solutions in the autograder.
*** USE THIS ONLY FOR TESTING YOUR SOLUTION! ***
This is useful for faster autograder runs during development and
free submissions if the project does not build.
$$ make partialsubmit
B) Build 'fullsubmit.tar.gz' a tarball complete with autograder test
files.
*** ALWAYS USE THIS FOR FINAL GRADING! ***
It is also useful when trying to find buggy solutions in the
autograder.
$$ make fullsubmit
* Unit testing support
A) Source files for unit testing should be named test*.cpp. Examples
include test_input.cpp or test3.cpp.
B) Automatic build rules are generated to support the following:
$$ make test_input
$$ make test3
$$ make alltests (this builds all test drivers)
C) If test drivers need special dependencies, they must be added
manually.
D) IMPORTANT: NO SOURCE FILES WITH NAMES THAT BEGIN WITH test WILL BE
ADDED TO ANY SUBMISSION TARBALLS.
* Static Analysis support
A) Matches current autograder style grading tests
B) Usage:
$$ make static
* Sync to CAEN support
A) Requires an .ssh/config file with a login.engin.umich.edu host
defined, SSH Multiplexing enabled, and an open SSH connection.
B) Edit the REMOTE_BASEDIR variable if default is not preferred.
C) Usage:
$$ make sync2caen
endef
export MAKEFILE_HELP
help:
@echo "$$MAKEFILE_HELP"
#######################
# TODO (begin) #
#######################
# individual dependencies for objects
# Examples:
# "Add a header file dependency"
# project2.o: project2.cpp project2.h
#
# "Add multiple headers and a separate class"
# HEADERS = some.h special.h header.h files.h
# myclass.o: myclass.cpp myclass.h $(HEADERS)
# project5.o: project5.cpp myclass.o $(HEADERS)
#
# SOME EXAMPLES
#
#test_thing: test_thing.cpp class.o functions.o
#class.o: class.cpp class.h
#functions.o: functions.cpp functions.h
#project0.o: project0.cpp class.h functions.h
#
# THE COMPILER CAN GENERATE DEPENDENCIES FROM SOURCE CODE
#
# % g++ -MM *.cpp
#
# ADD YOUR OWN DEPENDENCIES HERE
######################
# TODO (end) #
######################
# these targets do not create any files
.PHONY: all release debug profile static clean alltests partialsubmit \
fullsubmit sync2caen help identifier
# disable built-in rules
.SUFFIXES:
File added
// Project Identifier: 5949F553E20B650AB0FB2266D3C0822B13D248B0
#include <iostream>
#include <getopt.h>
#include <iomanip>
#include "MST.h"
#include "xcode_redirect.hpp"
using namespace std;
void getOptions(int argc, char* argv[], string &mode);
int main(int argc, char* argv[]){
ios_base::sync_with_stdio(false);
xcode_redirect(argc, argv);
cout << std::setprecision(2); //Always show 2 decimal places
cout << std::fixed; //Disable scientific notation for large numbers
string mode;
getOptions(argc, argv, mode);
if (mode == "MST"){
Mst mst;
mst.readFile();
mst.findMst();
}
return 0;
}
void getOptions(int argc, char* argv[], string &mode){
// These are used with getopt_long()
opterr = true; // Give us help with errors
int choice;
int option_index = 0;
option long_options[] = {
{ "mode", required_argument, nullptr, 'm'},
{ "help", no_argument, nullptr, 'h' },
{ nullptr, 0, nullptr, '\0' }
};
while ((choice = getopt_long(argc, argv, "m:h",
long_options, &option_index)) != -1){
switch (choice) {
case 'h':
cout << "Figure it out.\n";
exit(1);
case 'm':
mode = string(optarg);
break;
default:
exit(1);
}
}
}
File added
5
6 1
2 3
-5 -4
-1 6
0 -1
//
// xcode_redirect.hpp
//
// Created by Marcus Darden on 1/13/19.
// Copyright © 2019 Marcus Darden. All rights reserved.
//
// Usage: add the following line to main().
// xcode_redirect(argc, argv);
#ifndef xcode_redirect_hpp
#define xcode_redirect_hpp
#include <iostream>
#include <cstdio>
#include <cstring>
inline char *get_filename_and_move_count(int &optind, char *argv[], int &move_count);
inline void rotate_argv(int move_count, char *argv[], int &optind, int &argc);
// REQUIRES: argc & argv from main(), and optind from getopt.h (optional)
// MODIFIES: optind is incremented until it equals argc
// argc is decremented to discount any redirected members of argv
// EFFECTS:
// Looks for extra arguments after getopt() finishes reading options.
// It replaces simple shell functionality by using freopen() to change
// stdin (from < infilename or <infilename),
// stdout (from > outfilename or >outfilename), and/or
// stderr (from 2> errfilename or 2>errfilename).
inline void xcode_redirect(int &argc, char *argv[], int optind = 1) {
char *infile = nullptr, *outfile = nullptr, *errfile = nullptr;
int move_count = 0;
while (optind < argc) {
switch (*argv[optind]) {
case '<': // redirect input formatted as '< file' or '<file'
infile = get_filename_and_move_count(optind, argv, move_count);
if (!freopen(infile, "r", stdin)) {
std::cerr << "Unable to open input file: " << infile << std::endl;
exit(1);
} // if
break;
case '>': // redirect output formatted as '> file' or '>file'
outfile = get_filename_and_move_count(optind, argv, move_count);
if (!freopen(outfile, "w", stdout)) {
std::cerr << "Unable to open output file: " << outfile << std::endl;
exit(1);
} // if
break;
case '2': // redirect error formatted as '2> file' or '2>file'
if (*(argv[optind] + 1) == '>') {
errfile = get_filename_and_move_count(optind, argv, move_count);
if (!freopen(errfile, "w", stderr)) {
std::cerr << "Unable to open error file: " << errfile << std::endl;
exit(1);
} // if
} // if
break;
default: // not a redirection parameter
move_count = 0;
} // switch
++optind;
if (move_count > 0) {
rotate_argv(move_count, argv, optind, argc);
}
} // while
} // xcode_redirect()
inline void rotate_argv(int move_count, char *argv[], int &optind, int &argc) {
int moveind = optind - move_count;
char *temp_pre1 = argv[moveind];
char *temp_pre2 = argv[moveind + 1];
while (moveind + move_count < argc) {
argv[moveind] = argv[moveind + move_count];
++moveind;
} // while
argv[moveind++] = temp_pre1;
if (move_count == 2) {
argv[moveind] = temp_pre2;
} // if
optind -= move_count;
argc -= move_count;
} // rotate_argv()
inline char *get_filename_and_move_count(int &optind, char *argv[], int &move_count) {
if (strlen(argv[optind]) > 2) {
move_count = 1;
// Must offset by 1 character (the <), offset by more if they put space(s) before filename
int offset = 1;
// Must also offset more for 2> (redirecting error output)
if (argv[optind][0] == '2')
++offset;
while (argv[optind][offset] == ' ')
++offset;
return argv[optind] + offset;
} // if
move_count = 2;
return argv[++optind];
} // get_filename_and_move_count()
#endif /* xcode_redirect_hpp */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment