From b7b74808124d8dc2ebb3e56461ee9be410e7af6c Mon Sep 17 00:00:00 2001 From: amai83 Date: Wed, 31 Jul 2024 20:06:18 -0700 Subject: [PATCH 1/4] Code for refactoring This program performs a series of calculations based on user input. --- .vscode/settings.json | 5 + chatGPT/Amy_distanceCalculator.cpp | 156 +++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 chatGPT/Amy_distanceCalculator.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..dba1ac8 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "cstdlib": "cpp" + } +} \ No newline at end of file diff --git a/chatGPT/Amy_distanceCalculator.cpp b/chatGPT/Amy_distanceCalculator.cpp new file mode 100644 index 0000000..ecbffa1 --- /dev/null +++ b/chatGPT/Amy_distanceCalculator.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +#include +using namespace std; +int main() +{ + // declaring variables + double x1, y1, x2, y2; + double slope, yInt, manDist, eucDist; + char option; + // menu display + cout << "***************************************" << endl; + cout << "* Welcome to the Distance Calculator *" << endl; + cout << "***************************************" << endl; + cout << "Slope - (s/S)" << endl; + cout << "Manhattan Distance - (m/M)" << endl; + cout << "Euclidean Distance - (e/E)" << endl; + cout << "Quit - (q/Q)" << endl; + // user selects which calculation they want + cout << "Enter an Option:" << endl; + cin >> option; + // sets outputs to 3 decimals + cout << fixed << setprecision(3); + // the following section takes in and error checks user input for slope + switch (option) + { + case 's': + case 'S': + cout << "Enter a x1 value:" << endl; + cin >> x1; + if (x1 > 100) + { + cout << "Error: invalid x value, max is 100" << endl; + return 0; + } + cout << "Enter a y1 value:" << endl; + cin >> y1; + if (y1 > 150) + { + cout << "Error: invalid y value, max is 150" << endl; + return 0; + } + cout << "Enter a x2 value:" << endl; + cin >> x2; + if (x2 > 100) + { + cout << "Error: invalid x value, max is 100" << endl; + return 0; + } + cout << "Enter a y2 value:" << endl; + cin >> y2; + if (y2 > 150) + { + cout << "Error: invalid y value, max is 150" << endl; + return 0; + } + if (!cin) + { + cout << "Error: invalid input" << endl; + return 0; + } + slope = (y2 - y1) / (x2 - x1); + yInt = y1 - (slope * x1); + cout << "Slope: " << slope << endl; + cout << "Y-interecpt: " << yInt << endl; + break; + // the following section takes in and error checks user input for manDist + case 'm': + case 'M': + cout << "Enter a x1 value:" << endl; + cin >> x1; + if (x1 > 100) + { + cout << "Error: invalid x value, max is 100" << endl; + return 0; + } + cout << "Enter a y1 value:" << endl; + cin >> y1; + if (y1 > 150) + { + cout << "Error: invalid y value, max is 150" << endl; + return 0; + } + cout << "Enter a x2 value:" << endl; + cin >> x2; + if (x2 > 100) + { + cout << "Error: invalid x value, max is 100" << endl; + return 0; + } + cout << "Enter a y2 value:" << endl; + cin >> y2; + if (y2 > 150) + { + cout << "Error: invalid y value, max is 150" << endl; + return 0; + } + if (!cin) + { + cout << "Error: invalid input" << endl; + return 0; + } + manDist = abs(x2 - x1) + abs(y2 - y1); + cout << "Manhattan Distance: " << manDist << endl; + break; + // the following section takes in and error checks user input for eucDist + case 'e': + case 'E': + cout << "Enter a x1 value:" << endl; + cin >> x1; + if (x1 > 100) + { + cout << "Error: invalid x value, max is 100" << endl; + return 0; + } + cout << "Enter a y1 value:" << endl; + cin >> y1; + if (y1 > 150) + { + cout << "Error: invalid y value, max is 150" << endl; + return 0; + } + cout << "Enter a x2 value:" << endl; + cin >> x2; + if (x2 > 100) + { + cout << "Error: invalid x value, max is 100" << endl; + return 0; + } + cout << "Enter a y2 value:" << endl; + cin >> y2; + if (y2 > 150) + { + cout << "Error: invalid y value, max is 150" << endl; + return 0; + } + if (!cin) + { + cout << "Error: invalid input" << endl; + return 0; + } + eucDist = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); + cout << "Euclidean Distance: " << eucDist << endl; + break; + // The following section is the output if quit is selected + case 'q': + case 'Q': + cout << "Thank you for using the Distance Calculator" << endl; + break; + // The following section is if the user inputs something invalid + default: + cout << "Error: Invalid selection" << endl; + } + return 0; +} \ No newline at end of file From eb679c2b5024c7235cd87d5d97573fd4e2f93429 Mon Sep 17 00:00:00 2001 From: amai83 Date: Wed, 31 Jul 2024 21:21:36 -0700 Subject: [PATCH 2/4] Refactored with chatgpt Requested chatgpt to reduce redundancy in code by creating functions to manage repeated actions --- .vscode/settings.json | 3 +- chatGPT/Amy_distanceCalculator.cpp | 222 +++++++++++------------------ 2 files changed, 86 insertions(+), 139 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index dba1ac8..4f18a04 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { - "cstdlib": "cpp" + "cstdlib": "cpp", + "iostream": "cpp" } } \ No newline at end of file diff --git a/chatGPT/Amy_distanceCalculator.cpp b/chatGPT/Amy_distanceCalculator.cpp index ecbffa1..eb975dd 100644 --- a/chatGPT/Amy_distanceCalculator.cpp +++ b/chatGPT/Amy_distanceCalculator.cpp @@ -2,14 +2,19 @@ #include #include #include + using namespace std; -int main() -{ - // declaring variables - double x1, y1, x2, y2; - double slope, yInt, manDist, eucDist; + +bool getInputAndValidate(double coords[2][2]); +void calculateSlope(double coords[2][2]); +void calculateManhattanDistance(double coords[2][2]); +void calculateEuclideanDistance(double coords[2][2]); + +int main() { + double coords[2][2]; char option; - // menu display + bool validInput; + cout << "***************************************" << endl; cout << "* Welcome to the Distance Calculator *" << endl; cout << "***************************************" << endl; @@ -17,140 +22,81 @@ int main() cout << "Manhattan Distance - (m/M)" << endl; cout << "Euclidean Distance - (e/E)" << endl; cout << "Quit - (q/Q)" << endl; - // user selects which calculation they want - cout << "Enter an Option:" << endl; + + // User selects which calculation they want + cout << "Enter an Option: "; cin >> option; - // sets outputs to 3 decimals + + // Sets outputs to 3 decimals cout << fixed << setprecision(3); - // the following section takes in and error checks user input for slope - switch (option) - { - case 's': - case 'S': - cout << "Enter a x1 value:" << endl; - cin >> x1; - if (x1 > 100) - { - cout << "Error: invalid x value, max is 100" << endl; - return 0; - } - cout << "Enter a y1 value:" << endl; - cin >> y1; - if (y1 > 150) - { - cout << "Error: invalid y value, max is 150" << endl; - return 0; - } - cout << "Enter a x2 value:" << endl; - cin >> x2; - if (x2 > 100) - { - cout << "Error: invalid x value, max is 100" << endl; - return 0; - } - cout << "Enter a y2 value:" << endl; - cin >> y2; - if (y2 > 150) - { - cout << "Error: invalid y value, max is 150" << endl; - return 0; - } - if (!cin) - { - cout << "Error: invalid input" << endl; - return 0; - } - slope = (y2 - y1) / (x2 - x1); - yInt = y1 - (slope * x1); - cout << "Slope: " << slope << endl; - cout << "Y-interecpt: " << yInt << endl; - break; - // the following section takes in and error checks user input for manDist - case 'm': - case 'M': - cout << "Enter a x1 value:" << endl; - cin >> x1; - if (x1 > 100) - { - cout << "Error: invalid x value, max is 100" << endl; - return 0; - } - cout << "Enter a y1 value:" << endl; - cin >> y1; - if (y1 > 150) - { - cout << "Error: invalid y value, max is 150" << endl; - return 0; - } - cout << "Enter a x2 value:" << endl; - cin >> x2; - if (x2 > 100) - { - cout << "Error: invalid x value, max is 100" << endl; - return 0; - } - cout << "Enter a y2 value:" << endl; - cin >> y2; - if (y2 > 150) - { - cout << "Error: invalid y value, max is 150" << endl; - return 0; - } - if (!cin) - { - cout << "Error: invalid input" << endl; - return 0; - } - manDist = abs(x2 - x1) + abs(y2 - y1); - cout << "Manhattan Distance: " << manDist << endl; - break; - // the following section takes in and error checks user input for eucDist - case 'e': - case 'E': - cout << "Enter a x1 value:" << endl; - cin >> x1; - if (x1 > 100) - { - cout << "Error: invalid x value, max is 100" << endl; - return 0; - } - cout << "Enter a y1 value:" << endl; - cin >> y1; - if (y1 > 150) - { - cout << "Error: invalid y value, max is 150" << endl; - return 0; - } - cout << "Enter a x2 value:" << endl; - cin >> x2; - if (x2 > 100) - { - cout << "Error: invalid x value, max is 100" << endl; - return 0; - } - cout << "Enter a y2 value:" << endl; - cin >> y2; - if (y2 > 150) - { - cout << "Error: invalid y value, max is 150" << endl; - return 0; + + // Validate and get input + validInput = getInputAndValidate(coords); + if (!validInput) { + cout << "Error: Invalid input" << endl; + return 0; + } + + // Processing based on user option + switch (option) { + case 's': + case 'S': + calculateSlope(coords); + break; + case 'm': + case 'M': + calculateManhattanDistance(coords); + break; + case 'e': + case 'E': + calculateEuclideanDistance(coords); + break; + case 'q': + case 'Q': + cout << "Thank you for using the Distance Calculator" << endl; + break; + default: + cout << "Error: Invalid selection" << endl; + } + + return 0; +} + +bool getInputAndValidate(double coords[2][2]) { + for (int i = 0; i < 2; ++i) { + cout << "Enter x" << i + 1 << " value: "; + cin >> coords[i][0]; + if (coords[i][0] > 100 || !cin) { + return false; } - if (!cin) - { - cout << "Error: invalid input" << endl; - return 0; + + cout << "Enter y" << i + 1 << " value: "; + cin >> coords[i][1]; + if (coords[i][1] > 150 || !cin) { + return false; } - eucDist = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); - cout << "Euclidean Distance: " << eucDist << endl; - break; - // The following section is the output if quit is selected - case 'q': - case 'Q': - cout << "Thank you for using the Distance Calculator" << endl; - break; - // The following section is if the user inputs something invalid - default: - cout << "Error: Invalid selection" << endl; } - return 0; -} \ No newline at end of file + return true; +} + +void calculateSlope(double coords[2][2]) { + double slope, yInt; + if (coords[1][0] == coords[0][0]) { + cout << "Error: Division by zero, x1 cannot be equal to x2 for slope calculation." << endl; + return; + } + slope = (coords[1][1] - coords[0][1]) / (coords[1][0] - coords[0][0]); + yInt = coords[0][1] - (slope * coords[0][0]); + cout << "Slope: " << slope << endl; + cout << "Y-intercept: " << yInt << endl; +} + +void calculateManhattanDistance(double coords[2][2]) { + double manDist = abs(coords[1][0] - coords[0][0]) + abs(coords[1][1] - coords[0][1]); + cout << "Manhattan Distance: " << manDist << endl; +} + +void calculateEuclideanDistance(double coords[2][2]) { + double eucDist = sqrt((coords[1][0] - coords[0][0]) * (coords[1][0] - coords[0][0]) + (coords[1][1] - coords[0][1]) * (coords[1][1] - coords[0][1])); + cout << "Euclidean Distance: " << eucDist << endl; +} From 6376fd04abc5317fc15990cd10a5c0f7870501c3 Mon Sep 17 00:00:00 2001 From: amai83 Date: Thu, 1 Aug 2024 11:23:31 -0700 Subject: [PATCH 3/4] Code for documentation Adding code that needs improvement in documentation --- chatGPT/Amy_balloon.cpp | 98 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 chatGPT/Amy_balloon.cpp diff --git a/chatGPT/Amy_balloon.cpp b/chatGPT/Amy_balloon.cpp new file mode 100644 index 0000000..0bb04a5 --- /dev/null +++ b/chatGPT/Amy_balloon.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +class Balloon +{ +protected: + static float PointsPerBalloon; + static float PointsMultiplier; + static float PointsTotal; + static int BalloonCount; + +public: + Balloon(); + virtual ~Balloon() = 0; + static int GetBalloonCount(); + static float GetPointTotal(); + virtual char GetIdentifier() const; +}; + +class RedBalloon : public Balloon +{ +public: + virtual char GetIdentifier() const; + ~RedBalloon(); +}; + +class BlueBalloon : public Balloon +{ +public: + virtual char GetIdentifier() const; + ~BlueBalloon(); +}; + +class YellowBalloon : public Balloon +{ +public: + virtual char GetIdentifier() const; + ~YellowBalloon(); +}; + +// Init static variables. +float Balloon::PointsPerBalloon = 1.0; +float Balloon::PointsMultiplier = 1.0; +float Balloon::PointsTotal = 1.0; +int Balloon::BalloonCount = 0; + +Balloon::Balloon() +{ + BalloonCount++; +} +Balloon::~Balloon() +{ + BalloonCount--; + PointsTotal = PointsPerBalloon * PointsMultiplier; +} +int Balloon::GetBalloonCount() +{ + return BalloonCount; +} +float Balloon::GetPointTotal() +{ + return PointsTotal; +} +char Balloon::GetIdentifier() const +{ + return '\0'; +} + +char RedBalloon::GetIdentifier() const +{ + return 'R'; +} +RedBalloon::~RedBalloon() +{ + PointsMultiplier = PointsMultiplier + 0.1; + cout << " Red Balloon - *POP* " << endl; +} + +char BlueBalloon::GetIdentifier() const +{ + return 'B'; +} + +BlueBalloon::~BlueBalloon() +{ + PointsPerBalloon = PointsPerBalloon + 0.1; + cout << " Blue Balloon - *POP* " << endl; +} + +char YellowBalloon::GetIdentifier() const +{ + return 'Y'; +} +YellowBalloon::~YellowBalloon() +{ + cout << " Yellow Balloon - *POP* " << endl; +} \ No newline at end of file From 4e255a1f97e0e6d70aba262daf8b79e74d8e79c0 Mon Sep 17 00:00:00 2001 From: amai83 Date: Thu, 1 Aug 2024 11:30:31 -0700 Subject: [PATCH 4/4] Revert "Code for documentation" This reverts commit 6376fd04abc5317fc15990cd10a5c0f7870501c3. --- chatGPT/Amy_balloon.cpp | 98 ----------------------------------------- 1 file changed, 98 deletions(-) delete mode 100644 chatGPT/Amy_balloon.cpp diff --git a/chatGPT/Amy_balloon.cpp b/chatGPT/Amy_balloon.cpp deleted file mode 100644 index 0bb04a5..0000000 --- a/chatGPT/Amy_balloon.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include - -using namespace std; - -class Balloon -{ -protected: - static float PointsPerBalloon; - static float PointsMultiplier; - static float PointsTotal; - static int BalloonCount; - -public: - Balloon(); - virtual ~Balloon() = 0; - static int GetBalloonCount(); - static float GetPointTotal(); - virtual char GetIdentifier() const; -}; - -class RedBalloon : public Balloon -{ -public: - virtual char GetIdentifier() const; - ~RedBalloon(); -}; - -class BlueBalloon : public Balloon -{ -public: - virtual char GetIdentifier() const; - ~BlueBalloon(); -}; - -class YellowBalloon : public Balloon -{ -public: - virtual char GetIdentifier() const; - ~YellowBalloon(); -}; - -// Init static variables. -float Balloon::PointsPerBalloon = 1.0; -float Balloon::PointsMultiplier = 1.0; -float Balloon::PointsTotal = 1.0; -int Balloon::BalloonCount = 0; - -Balloon::Balloon() -{ - BalloonCount++; -} -Balloon::~Balloon() -{ - BalloonCount--; - PointsTotal = PointsPerBalloon * PointsMultiplier; -} -int Balloon::GetBalloonCount() -{ - return BalloonCount; -} -float Balloon::GetPointTotal() -{ - return PointsTotal; -} -char Balloon::GetIdentifier() const -{ - return '\0'; -} - -char RedBalloon::GetIdentifier() const -{ - return 'R'; -} -RedBalloon::~RedBalloon() -{ - PointsMultiplier = PointsMultiplier + 0.1; - cout << " Red Balloon - *POP* " << endl; -} - -char BlueBalloon::GetIdentifier() const -{ - return 'B'; -} - -BlueBalloon::~BlueBalloon() -{ - PointsPerBalloon = PointsPerBalloon + 0.1; - cout << " Blue Balloon - *POP* " << endl; -} - -char YellowBalloon::GetIdentifier() const -{ - return 'Y'; -} -YellowBalloon::~YellowBalloon() -{ - cout << " Yellow Balloon - *POP* " << endl; -} \ No newline at end of file