diff --git a/Algorithm Workbook/hw0-test.cpp b/Algorithm Workbook/hw0-test.cpp new file mode 100644 index 0000000..e3eeb36 --- /dev/null +++ b/Algorithm Workbook/hw0-test.cpp @@ -0,0 +1,65 @@ +// +// hw0-test.cpp +// pic10b +// +// Created by Michael James Murray on 12/30/23. +// + +#include +#include +#include +#include "hw0.hpp" + +int main(){ + { + // Question 1 test + std::vector> v = {{1,2,3}, {-1,2}, {7}, {}}; + std::cout << max_element(v) << std::endl; + } + + { + // Question 2 test + int x = 1; + int y = 2; + int*p1 = &x; + int*p2 = &y; + pointer_swap(p1,p2); + std::cout << (p1 == &y) << " " << (p2 == &x) << std::endl; + std::cout<< x << " " << y << std::endl; + } + { + // // Question 3 test for Caesar: this string is the opening paragraph of the Hitchhiker's guide to the galaxy, a wonderful book! + int offset = -12; + std::string s = "Far out in the uncharted backwaters of the unfashionable end of the Western Spiral arm of the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-eight million miles is an utterly insignificant little blue-green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea."; + + // First test the Caesar cipher 1 + // Lets just print the first 40 characters + std::string encoded_s = caesar_cipher1(s,offset); + std::cout << encoded_s.substr(0, 30) << std::endl; + + std::string decoded_s = caesar_cipher1(encoded_s,-offset); + std::cout << decoded_s.substr(0, 30) << std::endl; + + std::cout << std::boolalpha << (decoded_s == s) << std::endl; + + // Now test Caesar cipher 2 + +// caesar_cipher2(s,offset); +// std::cout << (encoded_s == s) << std::endl; +// +// caesar_cipher2(s,-offset); +// std::cout << (decoded_s == s) << std::endl; + return 0; + } + + { + // Question 4 test + const Undergrad s1; + Undergrad s2("Tim", 12345, 91); + std::cout << s1.name << " " << s1.ID << " " << s1.get_grade() << std::endl; + std::cout << s2.name << " " << s2.ID << " " << s2.get_grade() << std::endl; + s2.update_grade(102); + s2.update_grade(95); + std::cout << s2.name << " " << s2.ID << " " << s2.get_grade() << std::endl; + } +} diff --git a/Algorithm Workbook/hw0.cpp b/Algorithm Workbook/hw0.cpp new file mode 100644 index 0000000..febe80a --- /dev/null +++ b/Algorithm Workbook/hw0.cpp @@ -0,0 +1,206 @@ +// +// hw0.cpp +// pic10b +// +// Created by Michael James Murray on 1/4/24. +// +#include +#include +#include +#include "hw0.hpp" +using namespace std; + +// Question 1: max element function +int max_element(vector> vmax){ + ////first it is the vector of vectors that gets counted to see if it is empty + /// then we check the elements of the said chosen vector to see if those are empty + /// + if (vmax.size() == 0 || vmax[0].size() == 0){ + return -1; + } + + + int maxes = vmax[0][0]; + + + for (size_t i = 0; i != vmax.size(); ++i){ + + for (size_t j = 0; j != vmax[i].size(); ++j){ + + if (vmax[i][j] > maxes) { + maxes = vmax[i][j]; + } + } + } + return maxes; +} + +// Question 2: pointer swap function +void pointer_swap(int*& one, int*& two){ + int* x = one; + int* y = two; + two = x; + one = y; + + int temp = *one; + *one = *two; + *two = temp; + +} + +// Question 3a: caesar cipher function + +string caesar_cipher1(const string& text, int offsetchar) { + // Initialize the output string with the input text + string output = text; + + // Define the alphabet used for the cipher + const string alphabet = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; + + // Ensure the offsetchar is within the size of the alphabet + offsetchar %= alphabet.size(); + + // Adjust the offset for negative values + if (offsetchar < 0) { + offsetchar += alphabet.size(); + } + + // Iterate through each character in the input text + for (size_t i = 0; i < text.size(); ++i) { + char& currentChar = output[i]; // Get a reference to the current character + + // Check if the character is printable or a space + if (isprint(currentChar) || currentChar == ' ') { + // Find the position of the current character in the alphabet + size_t pos = alphabet.find(currentChar); + + // If the character is found in the alphabet + if (pos != string::npos) { + // Apply the offset to shift the character + pos = (pos + offsetchar) % alphabet.size(); + + // Update the character in the output string with the shifted character from the alphabet + currentChar = alphabet[pos]; + } + } + } + + // Return the encoded text + return output; +} + + + +// string caesar_cipher1(const string& text, int offsetchar){ +// +// string output = text; +// const string alphabet = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; +// +// +// offsetchar %= alphabet.size(); +// +// // Adjust the offset for negative values +// if (offsetchar < 0) { +// offsetchar += alphabet.size(); +// } +// string printed; +// +// // Iterate through each character in the input text +// for (size_t i = 0; i < text.size(); ++i) { +// char& currentChar = output[i]; // Get a reference to the current character +// +// // Check if the character is printable or a space +// if (isprint(currentChar) || currentChar == ' ') { +// // Find the position of the current character in the alphabet +// size_t pos = alphabet.find(currentChar); +// +// // If the character is found in the alphabet +// if (pos != std::string::npos) { +// // Apply the offset to shift the character +// pos = (pos + offsetchar) % alphabet.size(); +// +// // Update the character in the output string with the shifted character from the alphabet +// currentChar = alphabet[pos]; +// } +// } +// printed.push_back(currentChar); +// // output.swap(¤tChar); +// } +//return printed; +//} +// +//string caesar_ciper1(const string& text, int offsetchar){ +// +// string output = text; +// const string alphabet = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; +// +// +// if (offsetchar > alphabet.size() || offsetchar < -alphabet.size()){ +// offsetchar %= alphabet.size(); +// } +//// if (offsetchar < 0){ +//// offsetchar = abs(offsetchar); +//// } +// +// for (size_t i = 0; i < text.size(); ++i){ +// size_t pos = alphabet.find(text[i]); +// pos += offsetchar; +// // output[i] += alphabet[pos]; +// output.push_back(alphabet[pos]); +//// if (offsetchar < 0){ +//// //do the minus down the alphabet +//// pos += offsetchar; +//// output[i] += alphabet[pos]; +//// } +// //if (text[i] == alphabet[i]){ +// // string found = &text[i]; +// // output.push_back(found); +// // output[text[i]] += offsetchar; +// // } +// } +// return output; +//} + +//if (message[". "]) +// do capital + + +// Question 3b: in-place caesar cipher function +//string caesar_cipher2(const string& message2, int offset2){ +// +// +// +//} + + + + +// Question 4: member functions of Undergrad class +//Undergrad(string name, size_t ID, int grade): name(name), ID(ID), grade(grade){} + +Undergrad::Undergrad(){ + string name = "Test"; + size_t ID = 0; + int grade = 0; +} +Undergrad::Undergrad(string name, size_t ID, int grade){} + + int Undergrad::get_grade() const{ + if (grade < 0 || grade > 100){ + cout << "Grade is not valid, must be between 0 and 100." << endl; + return 0; + } + return grade; +} + + +void Undergrad::update_grade(int newgrade){ + + if (newgrade < 0 || newgrade > 100){ + cout << "Grade is not valid, must be between 0 and 100." << endl; + } + else{ + grade = newgrade; + } +} + diff --git a/Algorithm Workbook/hw0.hpp b/Algorithm Workbook/hw0.hpp new file mode 100644 index 0000000..1023cae --- /dev/null +++ b/Algorithm Workbook/hw0.hpp @@ -0,0 +1,49 @@ +// +// hw0.hpp +// pic10b +// +// Created by Michael James Murray on 1/4/24. +// + +#ifndef hw0_hpp +#define hw0_hpp + +#include +#include +#include + +using namespace std; +// Question 1: declaration max element function +// XXXX max_element(XXXX); +int max_element(vector> vect); + +////Question 2: declaration pointer swap function +void pointer_swap(int*& one, int*& two); +// +// Question 3a: declaration caesar cipher function + string caesar_cipher1(const string& message, int offset); + +// Question 3b: declaration in-place caesar cipher function + +string caesar_cipher2(const string message2, int offset2); + + +// Question 4: declaration of Undergrad class +class Undergrad{ +private: + int grade; + +public: + string name; + size_t ID; + Undergrad(); + Undergrad(string name, size_t ID, int grade); + + void update_grade(int newgrade); + int get_grade() const; + + // Undergrad(const string name, const size_t id, int grade) : name(name), ID(id), grade(grade){} + +}; + +#endif /* hw0_hpp */ diff --git a/Pointer Matching/README.md b/Pointer Matching/README.md new file mode 100644 index 0000000..966b424 --- /dev/null +++ b/Pointer Matching/README.md @@ -0,0 +1,2 @@ +Again thiis is another method of showing static data manipulation. +Clearly looking for defined integers within an array by recursively calling their pointers to ommit copys. diff --git a/Projects/Binary Search b/Projects/Binary Search deleted file mode 100644 index c467e6b..0000000 Binary files a/Projects/Binary Search and /dev/null differ diff --git a/Projects/Linked List b/Projects/Linked List deleted file mode 100644 index 9024b01..0000000 Binary files a/Projects/Linked List and /dev/null differ diff --git a/Projects/Magic Square b/Projects/Magic Square deleted file mode 100644 index 661d460..0000000 Binary files a/Projects/Magic Square and /dev/null differ diff --git a/Projects/Matching Pointer Search b/Projects/Matching Pointer Search deleted file mode 100644 index 8b13789..0000000 --- a/Projects/Matching Pointer Search +++ /dev/null @@ -1 +0,0 @@ - diff --git a/Projects/Polymorphism b/Projects/Polymorphism deleted file mode 100644 index 9177e49..0000000 Binary files a/Projects/Polymorphism and /dev/null differ diff --git a/README.md b/README.md index b0f2aa6..371a89d 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,20 @@ # 1. Matching Pointer Search - Arrays Provide an array with a specific value and length to try to find them using pointers to be more efficient in data manipulation -# 2. Magic Square +# 2. Rational # Operator Overloading +Overloads all basic arithemtic operators and istream ostream for custom dynamic data manipulation using static containers. + +# 3. Magic Square Algorithm This project makes sure to organize random arrangements of a non-conflicting matrix as many times as possible. -# 3. Linked List +# 4. Linked List A linked list iterator that iterates forward and reverse. -# 4. Binary Search Tree +# 5. Binary Search Tree Navigating through a search tree to find a particular number in the tree. -# 5. Polymorphism +# 6. Polymorphism Using polygon as an analogy for being able to use objects in a polymorphic inheritance. + +### Basic Fun Tasks +Simple tasks for algorithm reference diff --git a/Rational Number Arithmetic/README.md b/Rational Number Arithmetic/README.md new file mode 100644 index 0000000..e1ba35d --- /dev/null +++ b/Rational Number Arithmetic/README.md @@ -0,0 +1,3 @@ +New Activity Touching on Arithmetic Basics to show understang of object oriented data manipulation. +Mathematically this is a clear demonstration of how object oriented programming can use dynamic and static data manipulation. +Clearly using efficient systems like pointers to reduce time and storage capacity of machines. diff --git a/Rational Number Arithmetic/main.cpp b/Rational Number Arithmetic/main.cpp new file mode 100644 index 0000000..fdb489e --- /dev/null +++ b/Rational Number Arithmetic/main.cpp @@ -0,0 +1,35 @@ +#include +#include "rational.hpp" + +int main() { + Rational& r(); + Rational& r2(); + Rational& r3(); + Rational& r4(); + Rational& r5(); + + std::cout << "Type a rational in the form num/den, and I'll say it back at you: "; + std::cin >> r; + std::cout << "This is your rational: " << &r << std::endl; + std::cout << "Type a rational in the form num/den, and I'll say it back at you: "; + std::cin >> r2; + std::cout << "This is your rational: " << &r2 << std::endl; + std::cout << "Type a rational in the form num/den, and I'll say it back at you: "; + std::cin >> r3; + std::cout << "This is your rational: " << &r3 << std::endl; + std::cout << "Type a rational in the form num/den, and I'll say it back at you: "; + std::cin >> r4; + std::cout << "This is your rational: " << &r4 << std::endl; + std::cout << "Type a rational in the form num/den, and I'll say it back at you: "; + std::cin >> r5; + std::cout << "This is your rational: " << &r5 << std::endl; + + Rational& operator+= (Rational& aduh); + std::cout << operator+=(&aduh) << std::endl; + +// Rational operator-=(r3); +// Rational& operator*=(multi); +// Rational& operator/=(const Rational& divi); + return 0; + +} diff --git a/Rational Number Arithmetic/rational.cpp b/Rational Number Arithmetic/rational.cpp new file mode 100644 index 0000000..9460897 --- /dev/null +++ b/Rational Number Arithmetic/rational.cpp @@ -0,0 +1,232 @@ +#include "rational.hpp" + +// the case for 0 +Rational::Rational() : num( 0), den( 1) {}//clean constructor +//case for whole number rational +Rational::Rational(int _num) : num(_num), den( 1) {}//initialized constructor + +//actual rational +Rational::Rational(int _num, int _den) : num(_num), den(_den) { + assert(den != 0); + make_den_pos(); + simplify(); +} + +/** +This member function makes sure that +the denominator of a rational number is positive, +a convenient choice. +*/ +void Rational::make_den_pos() { + if (den < 0) { + num *= -1; // turns numerator negative + den *= -1; // turns the denominator positive + } +} + +/** +This member function simplifies a rational number. +It does so by computing the highest common factor +of the numerator and denominator and dividing +the numerator and denominator by it. + +To compute the highest common factor, +the Euclidean algorithm is used. +Do not worry if you don't know the Euclidean algorithm. +*/ +void Rational::simplify() { + int a = abs(num); //makes it positive + int b = abs(den); //makes it positive + + while (b != 0) { + int r = a % b; + a = b; + b = r; + } + num /= a; + den /= a; +} + +std::istream& operator>>(std::istream& in, Rational& r) { + int num; + int den; + + char forwardslash; + + in >> num >> forwardslash >> den; + + if (forwardslash != '/') { + in.setstate(std::ios_base::failbit); + } + if (in) { + r = Rational(num, den); + } + + return in; // in is the altered rational that they originally put in but alterd -- use and operator most likely "/" that will be the function for removing the rounding decimals +} +//#1 +std::ostream& operator<<(std::ostream& out, const Rational& r){ + int num = r.num; + int den = r.den; + char forwardslash = '/'; + + out << num << forwardslash << den; + + return out; +} + + +//#2 +Rational& Rational::operator+=(const Rational& plus){ + // a d b c + num = num * (plus.den) + plus.num * (den); + den = den* plus.den; // b d + simplify(); + + return *this; +} +//#2 +Rational& Rational::operator-=(const Rational& minus){ + + // a d c b + num = num * (minus.den) - minus.num * den; + // b d + den = den * (minus.den); + + simplify(); + return *this; + +} +//#2 +Rational& Rational::operator*=(const Rational& multi){ + // a c + num = num * multi.num; + // b d + den = den * multi.den; + simplify(); + + return *this; +} +//#2 +Rational& Rational::operator/=(const Rational& divi){ + // a d + num = num * divi.den; + // b c + den = den * divi.num; + + simplify(); + return *this; +} + +//#3 = post +Rational& Rational::operator++() { + num+=den; + simplify(); + return *this; +} +//#3 = post +Rational& Rational::operator--() { + num-=den; + simplify(); + return *this; +} + +//#3 +Rational Rational::operator++(int unused) { + Rational pre_increment(*this); // using implicitly defined copy constructor + ++(*this); + simplify(); + return pre_increment; +} +//#3 +Rational Rational::operator--(int unused) { + Rational pre_decrement(*this); // using implicitly defined copy constructor + --(*this); + simplify(); + return pre_decrement; +} + +//#4 +Rational Rational::operator+() const{ + return *this; + } + +//#4 + +Rational Rational::operator-() const{ + Rational copy(*this); + copy.num = (copy.num * -1); + return copy; + + } +// Operator+ +Rational Rational::operator+(const Rational& r2) const { + Rational resultPlus; + resultPlus.num = num * r2.den + r2.num * den; + resultPlus.den = den * r2.den; + return resultPlus; +} + +// Operator- +Rational Rational::operator-(const Rational& r2) const { + Rational resultMin; + resultMin.num = num * r2.den - r2.num * den; + resultMin.den = den * r2.den; + return resultMin; +} + +// Operator* +Rational Rational::operator*(const Rational& r2) const { + Rational resultMul; + resultMul.num = num * r2.num; + resultMul.den = den * r2.den; + return resultMul; +} + +// Operator/ +Rational Rational::operator/(const Rational& r2) const { + Rational resultDiv; + resultDiv.num = num * r2.den; + resultDiv.den = den * r2.num; + return resultDiv; +} + +//#6 +bool operator==(const Rational& first, const Rational& second){ + return first.num == second.num && first.den == second.den; +} +//#6 +bool operator < (const Rational& first, const Rational& second){ + + return first.num * second.den < second.num * first.den; + +} +//#7 +bool operator!=(const Rational& firstComp, const Rational& secondComp){ + return !(firstComp == secondComp); +} +//#7 + +bool operator>(const Rational& firstComp, const Rational& secondComp){ + return !(firstComp < secondComp); +} +//#7 + +bool operator <=(const Rational& firstComp, const Rational& secondComp){ + return (firstComp < secondComp || firstComp == secondComp); + +} +//#7 + +bool operator >= (const Rational& firstComp, const Rational& secondComp) { + return (firstComp== secondComp || firstComp > secondComp); +} +//#8 +Rational::operator double() const{ + + double one = num/1.0; + double two = den/1.0; + + return one/two; + +} diff --git a/Rational Number Arithmetic/rational.hpp b/Rational Number Arithmetic/rational.hpp new file mode 100644 index 0000000..b7e5ff2 --- /dev/null +++ b/Rational Number Arithmetic/rational.hpp @@ -0,0 +1,85 @@ +#ifndef rational_hpp +#define rational_hpp + +#include +#include + +/* + This is a class for creating instances of rational numbers (also known as fractions). + A rational number (or fraction) consists of an integer numerator and an integer denominator. + For this reason, our class has member variables called 'num' and 'den' of type int. + + The default constructor creates 0. + We can write 0 as 0/1, i.e. with numerator 0 and denominator 1. + + Any integer can be regarded as a fraction with denominator equal to 1. + There's a constructor based on this idea too. + + Finally, a user can specify a numerator and denominator + in order to construct a rational of their choice. + + Rationals are always stored in a simplified form: + - den is strictly positive, + - num and den have a highest common factor of 1. + + We have implemented all the usual arithmetic operations: + + (unary and binary), - (unary and binary), *, /, + +=, -+, *=, /=, ++ (pre and post), -- (pre and post). + + We have implemented the comparison operators: + ==, !=, <, >, <=, >=. + + We have defined how to static_cast a Rational to a double. + + Finally, we have defined operator<< and operator>>. +*/ +class Rational { +public: + Rational(); + Rational(int _num); + Rational(int _num, int _den); + + //arithmetic update + Rational& operator+=(const Rational& plus); + Rational& operator-=(const Rational& minus); + Rational& operator*=(const Rational& multi); + Rational& operator/=(const Rational& divi); + + //post + Rational& operator++(); + Rational& operator--(); + //pre + Rational operator++(int); + Rational operator--(int); + //unary + Rational operator+() const; + Rational operator-() const; + + //binary + Rational operator+(const Rational& r2) const; + Rational operator-(const Rational& r2) const; + Rational operator*(const Rational& r2) const; + Rational operator/(const Rational& r2) const; + + //decimal/doouble + operator double() const; + +private: + int num; + int den; + + void make_den_pos(); + void simplify(); + + ///NON-MEMBER FUNCTION ACCESS + /// + friend bool operator==(const Rational&, const Rational&); + friend bool operator< (const Rational&, const Rational&); + + friend std::ostream& operator<<(std::ostream&, const Rational&); +}; + + +std::istream& operator>>(std::istream&, Rational&); + +#endif /* rational_hpp */