From d23e932c645bdc9b835483c66ec6e3a3e87307233c18692381bd5119cd7c9d01 Mon Sep 17 00:00:00 2001 From: Tyler Beckman Date: Fri, 20 Sep 2024 00:57:41 -0600 Subject: [PATCH] upload --- .vscode/c_cpp_properties.json | 18 +++++++++ .vscode/launch.json | 24 ++++++++++++ .vscode/settings.json | 59 ++++++++++++++++++++++++++++++ Makefile | 69 +++++++++++++++++++++++++++++++++++ coordinate_conversion.cpp | 17 +++++++++ coordinate_conversion.h | 26 +++++++++++++ main.cpp | 43 ++++++++++++++++++++++ 7 files changed, 256 insertions(+) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 Makefile create mode 100644 coordinate_conversion.cpp create mode 100644 coordinate_conversion.h create mode 100644 main.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..357fb81 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8c31ae0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/ty/Dev/School/CSCI200/Sets/2/L2C", + "program": "/home/ty/Dev/School/CSCI200/Sets/2/L2C/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..85a5a9c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..33455c6 --- /dev/null +++ b/Makefile @@ -0,0 +1,69 @@ +TARGET = L2C +SRC_FILES = main.cpp coordinate_conversion.cpp + +# Tyler's custom makefile extensions +.DEFAULT_GOAL := all # Necessary so `make` doesn't run the "pack" target, as it is declared before "all" +.PHONY: pack clean-run c run + +## Adds only the necessary files for build into a .tar.gz file, named appropriately +ARCHIVED_FILES = Makefile $(SRC_FILES) $(SRC_FILES:.cpp=.h) $(SRC_FILES:.cpp=.hpp) +pack: + tar --ignore-failed-read -czvf $(TARGET).tar.gz $(shell echo $(ARCHIVED_FILES) | xargs ls -d 2>/dev/null) + +## Runs the pack target and then attempts to build & run the program to make sure it functions correctly +pack-test: pack + $(eval TMP := $(shell mktemp -d)) + tar -xvzf $(TARGET).tar.gz --directory $(TMP) + make -C $(TMP) + $(TMP)/$(TARGET) + rm -rf $(TMP) + +## An extension of the clean command that is shorter to type and removes a potential .tar.gz file +c: clean + $(DEL) -f $(TARGET).tar.gz + +## Simply builds and then executes the program +run: all + ./$(TARGET) + +# NO EDITS NEEDED BELOW THIS LINE + +CXX = g++ +CXXFLAGS = -O2 +CXXFLAGS_DEBUG = -g +CXXFLAGS_WARN = -Wall -Wextra -Wunreachable-code -Wshadow -Wpedantic +CPPVERSION = -std=c++17 + +OBJECTS = $(SRC_FILES:.cpp=.o) + +ifeq ($(shell echo "Windows"), "Windows") + TARGET := $(TARGET).exe + DEL = del + Q = +else + DEL = rm -f + Q = " +endif + +all: $(TARGET) + +$(TARGET): $(OBJECTS) + $(CXX) -o $@ $^ + +.cpp.o: + $(CXX) $(CXXFLAGS) $(CPPVERSION) $(CXXFLAGS_DEBUG) $(CXXFLAGS_WARN) -o $@ -c $< + +clean: + $(DEL) -f $(TARGET) $(OBJECTS) Makefile.bak + +depend: + @sed -i.bak '/^# DEPENDENCIES/,$$d' Makefile + @$(DEL) sed* + @echo $(Q)# DEPENDENCIES$(Q) >> Makefile + @$(CXX) -MM $(SRC_FILES) >> Makefile + +.PHONY: all clean depend + +# DEPENDENCIES +main.o: main.cpp coordinate_conversion.h +coordinate_conversion.o: coordinate_conversion.cpp diff --git a/coordinate_conversion.cpp b/coordinate_conversion.cpp new file mode 100644 index 0000000..06c5663 --- /dev/null +++ b/coordinate_conversion.cpp @@ -0,0 +1,17 @@ +#ifndef COORDINATE_CONVERSION_H +#define COORDINATE_CONVERSION_H +#include + +void polar_to_cartesian(double radius, double angle, double *xCoordinate, + double *yCoordinate) { + *xCoordinate = radius * std::cos(angle); + *yCoordinate = radius * std::sin(angle); +} + +void cartesian_to_polar(double xCoordinate, double yCoordinate, double *radius, + double *angle) { + *radius = std::sqrt(std::pow(xCoordinate, 2) + std::pow(yCoordinate, 2)); + *angle = std::atan(xCoordinate); +} + +#endif // COORDINATE_CONVERSION_H diff --git a/coordinate_conversion.h b/coordinate_conversion.h new file mode 100644 index 0000000..8b24e4b --- /dev/null +++ b/coordinate_conversion.h @@ -0,0 +1,26 @@ +#ifndef COORDINATE_CONVERSION_H +#define COORDINATE_CONVERSION_H + +/** + * @brief Converts passed polar coordinates to their cartesian equivalent, using + * pass-by-pointer + * + * @param radius The radius of the polar coordinates + * @param angle The angle of the polar coordinates + * @param xCoordinate A pointer referencing the variable to place the x component of the cartesian coordinates into + * @param yCoordinate A pointer referencing the variable to place the y component of the cartesian coordinates into + */ +void polar_to_cartesian(double radius, double angle, double* xCoordinate, double* yCoordinate); + +/** + * @brief Converts passed cartesian coordinates to their polar equivalent, using + * pass-by-pointer + * + * @param x The x component of the cartesian coordinates + * @param y The y component of the cartesian coordinates + * @param radius A pointer referencing the variable to put the radius of the polar coordinates into + * @param angle A pointer referencing the variable to put the angle of the polar coordinates into + */ +void cartesian_to_polar(double xCoordinate, double yCoordinate, double* radius, double* angle); + +#endif // COORDINATE_CONVERSION_H diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..437b496 --- /dev/null +++ b/main.cpp @@ -0,0 +1,43 @@ +/* CSCI 200: Lab 2C (Coordinate Conversion By Pointer): Tyler Beckman + * + * Author: Tyler Beckman + * + * TODO + */ +#include "coordinate_conversion.h" + +#include + +int main(void) { + char direction = 'P'; + + std::cout << "Which coordinate system would you like to convert to? [P]olar - (x, y) -> (r, θ), or [C]artesian - (r, θ) -> (x, y)? "; + std::cin >> direction; + + double radius = 0; + double angle = 0; + double xCoordinate = 0; + double yCoordinate = 0; + + switch (direction) { + case 'P': + std::cout << "Please enter the (x, y) coordinates, separated by a space: "; + std::cin >> xCoordinate >> yCoordinate; + + cartesian_to_polar(xCoordinate, yCoordinate, &radius, &angle); + + std::cout << "The result (r, θ) is: (" << radius << ", " << angle << ")"; + break; + case 'C': + std::cout << "Please enter the (r, θ) coordinates, separated by a space: "; + std::cin >> radius >> angle; + + polar_to_cartesian(radius, angle, &xCoordinate, &yCoordinate); + + std::cout << "The result (x, y) is: (" << xCoordinate << ", " << yCoordinate << ")"; + break; + default: + std::cout << "\"" << direction << "\" is not a valid option, exiting"; + return 1; + } +} \ No newline at end of file