diff --git a/.gitignore b/.gitignore index 635f2a2..e4cad55 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,17 @@ __pycache__ *.out node_modules *.csv +tmp + +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps +bin diff --git a/.vscode/settings.json b/.vscode/settings.json index 47ffa6f..9c79bdc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,5 +12,17 @@ "challenges/euler/build": true, "commitlint.config.js": true, "**/pnpm-lock.yaml": true + }, + "C_Cpp.errorSquiggles": "Disabled", + "files.associations": { + "*.tcc": "cpp", + "complex": "cpp", + "fstream": "cpp", + "cmath": "cpp", + "functional": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "string_view": "cpp", + "vector": "cpp" } } diff --git a/languages/c++/code/babylonian.cc b/languages/c++/code/babylonian.cc index 9f2021b..680f9ac 100644 --- a/languages/c++/code/babylonian.cc +++ b/languages/c++/code/babylonian.cc @@ -1,5 +1,4 @@ #include -using namespace std; int sqrt(int number) { int initialGuess = 2; @@ -12,10 +11,10 @@ int sqrt(int number) { } int main() { - cout << "Type a number to predict the square root of: "; + std::cout << "Type a number to predict the square root of: "; int number; - cin >> number; + std::cin >> number; - cout << sqrt(number); + std::cout << sqrt(number); } diff --git a/languages/c++/code/dice.cc b/languages/c++/code/dice.cc index df9791a..30a7ed2 100644 --- a/languages/c++/code/dice.cc +++ b/languages/c++/code/dice.cc @@ -1,6 +1,5 @@ #include #include -using namespace std; class Dice { private: @@ -23,8 +22,8 @@ class Dice { } // Roll the dice many times and return thre result in a vector - vector rollMany(int times) { - vector rolls{}; + std::vector rollMany(int times) { + std::vector rolls{}; for (int i = 0; i < times; i++) { int rolled = roll(); @@ -36,14 +35,14 @@ class Dice { }; // Print a vector of integers onto one line -void printResults(vector data) { +void printResults(std::vector data) { int length = data.size(); for (int i = 0; i < length; i++) { - string out = to_string(data[i]); + std::string out = std::to_string(data[i]); if (i != length - 1) out += ", "; - cout << out; + std::cout << out; } } @@ -56,12 +55,12 @@ int main() { Dice twenty(20); // Roll them - vector sixRolled = six.rollMany(10); - vector twentyRolled = twenty.rollMany(10); + std::vector sixRolled = six.rollMany(10); + std::vector twentyRolled = twenty.rollMany(10); // Display their outputs - cout << "Six Sided Dice:" << endl; + std::cout << "Six Sided Dice:\n"; printResults(sixRolled); - cout << endl << endl << "Twenty Sided Dice:" << endl; + std::cout << "\n\nTwenty Sided Dice:\n"; printResults(twentyRolled); } diff --git a/languages/c++/code/karatsuba.cc b/languages/c++/code/karatsuba.cc index 9ca95ee..e84262a 100644 --- a/languages/c++/code/karatsuba.cc +++ b/languages/c++/code/karatsuba.cc @@ -1,21 +1,19 @@ #include #include #include -using namespace std; -using namespace std::chrono; -typedef high_resolution_clock Clock; +typedef std::chrono::high_resolution_clock Clock; typedef Clock::time_point ClockTime; double karatsuba(double num1, double num2) { - int num1Length = to_string((int)num1).size(); - int num2Length = to_string((int)num2).size(); + int num1Length = std::to_string((int)num1).size(); + int num2Length = std::to_string((int)num2).size(); // Fallback to traditional multiplication if (num1Length == 1 || num2Length == 1) { return num1 * num2; } else { - double n = floor(max(num1Length, num2Length) / 2); + double n = floor(std::max(num1Length, num2Length) / 2); double lowNum1 = fmod(num1, pow(10, n)); double lowNum2 = fmod(num2, pow(10, n)); @@ -32,28 +30,28 @@ double karatsuba(double num1, double num2) { } int main() { - cout << "Give me a number! "; + std::cout << "Give me a number! "; double num1; - cin >> num1; + std::cin >> num1; - cout << "Give me a number to multiply " + to_string(num1) + " by! "; + std::cout << "Give me a number to multiply " + std::to_string(num1) + " by! "; double num2; - cin >> num2; + std::cin >> num2; ClockTime karatsubaBegin, karatsubaEnd; karatsubaBegin = Clock::now(); double karatsubaResult = karatsuba(num1, num2); karatsubaEnd = Clock::now(); - double karatsubaTime = duration_cast(karatsubaEnd - karatsubaBegin).count(); + double karatsubaTime = std::chrono::duration_cast(karatsubaEnd - karatsubaBegin).count(); ClockTime quadraticBegin, quadraticEnd; quadraticBegin = Clock::now(); double quadraticResult = num1 * num2; quadraticEnd = Clock::now(); - double quadraticTime = duration_cast(quadraticEnd - quadraticBegin).count(); + double quadraticTime = std::chrono::duration_cast(quadraticEnd - quadraticBegin).count(); - cout << endl << "Quadratic Result: " << quadraticResult << " (" << quadraticTime << "ns)" << endl; - cout << "Karatsuba Result: " << karatsubaResult << " (" << karatsubaTime << "ns)"; + std::cout << "\nQuadratic Result: " << quadraticResult << " (" << quadraticTime << "ns)\n"; + std::cout << "Karatsuba Result: " << karatsubaResult << " (" << karatsubaTime << "ns)"; } diff --git a/languages/c++/code/pascal.cc b/languages/c++/code/pascal.cc index c88e002..fb42848 100644 --- a/languages/c++/code/pascal.cc +++ b/languages/c++/code/pascal.cc @@ -1,14 +1,13 @@ #include #include -using namespace std; // Calculate Pascal's triangle -vector> pascal(int rowCount) { - vector> rows{{1}}; +std::vector> pascal(int rowCount) { + std::vector> rows{{1}}; for (int i = 0; i < rowCount; i++) { - vector previousRow = rows[rows.size() - 1]; - vector newRow{1}; + std::vector previousRow = rows[rows.size() - 1]; + std::vector newRow{1}; for (int j = 0; j < previousRow.size() - 1; j++) { newRow.push_back(previousRow[j] + previousRow[j + 1]); @@ -22,11 +21,11 @@ vector> pascal(int rowCount) { } // Find the length of a vector if it were outputted as a string with a space between each value -int vectorStringLength(vector vector) { +int vectorStringLength(std::vector vector) { int value = vector.size(); for (int i = 0; i < vector.size(); i++) { - value += to_string(vector[i]).size(); + value += std::to_string(vector[i]).size(); } return value; @@ -34,17 +33,17 @@ int vectorStringLength(vector vector) { // Draw Pascal's Triangle void drawPascalTriangle(int rowCount) { - vector> pascalsTriangle = pascal(rowCount); + std::vector> pascalsTriangle = pascal(rowCount); int bottomRowSize = vectorStringLength(pascalsTriangle[pascalsTriangle.size() - 1]); for (int i = 0; i < pascalsTriangle.size() - 1; i++) { - vector currentRow = pascalsTriangle[i]; + std::vector currentRow = pascalsTriangle[i]; int rowSize = vectorStringLength(currentRow); int sizeDifference = bottomRowSize - rowSize; - string spacing = ""; + std::string spacing = ""; for (int j = 0; j < sizeDifference / 2; j++) { spacing += " "; @@ -52,27 +51,27 @@ void drawPascalTriangle(int rowCount) { for (int j = 0; j < currentRow.size(); j++) { if (j == 0) { - cout << spacing; + std::cout << spacing; } - cout << to_string(currentRow[j]) + " "; + std::cout << std::to_string(currentRow[j]) + " "; if (j == currentRow.size() - 1) { - cout << spacing; + std::cout << spacing; } } if (i != pascalsTriangle.size() - 1) { - cout << endl; + std::cout << "\n"; } } } int main() { - cout << "How many rows of Pascal's Triangle would you like to calculate? "; + std::cout << "How many rows of Pascal's Triangle would you like to calculate? "; int rowCount; - cin >> rowCount; + std::cin >> rowCount; drawPascalTriangle(rowCount); }