refactor(c++): stop using namespace std
-
This commit is contained in:
parent
9a7640a898
commit
7d4d0f47ee
6 changed files with 65 additions and 44 deletions
14
.gitignore
vendored
14
.gitignore
vendored
|
@ -5,3 +5,17 @@ __pycache__
|
||||||
*.out
|
*.out
|
||||||
node_modules
|
node_modules
|
||||||
*.csv
|
*.csv
|
||||||
|
tmp
|
||||||
|
|
||||||
|
CMakeLists.txt.user
|
||||||
|
CMakeCache.txt
|
||||||
|
CMakeFiles
|
||||||
|
CMakeScripts
|
||||||
|
Testing
|
||||||
|
Makefile
|
||||||
|
cmake_install.cmake
|
||||||
|
install_manifest.txt
|
||||||
|
compile_commands.json
|
||||||
|
CTestTestfile.cmake
|
||||||
|
_deps
|
||||||
|
bin
|
||||||
|
|
12
.vscode/settings.json
vendored
12
.vscode/settings.json
vendored
|
@ -12,5 +12,17 @@
|
||||||
"challenges/euler/build": true,
|
"challenges/euler/build": true,
|
||||||
"commitlint.config.js": true,
|
"commitlint.config.js": true,
|
||||||
"**/pnpm-lock.yaml": 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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
int sqrt(int number) {
|
int sqrt(int number) {
|
||||||
int initialGuess = 2;
|
int initialGuess = 2;
|
||||||
|
@ -12,10 +11,10 @@ int sqrt(int number) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
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;
|
int number;
|
||||||
cin >> number;
|
std::cin >> number;
|
||||||
|
|
||||||
cout << sqrt(number);
|
std::cout << sqrt(number);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class Dice {
|
class Dice {
|
||||||
private:
|
private:
|
||||||
|
@ -23,8 +22,8 @@ class Dice {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Roll the dice many times and return thre result in a vector
|
// Roll the dice many times and return thre result in a vector
|
||||||
vector<int> rollMany(int times) {
|
std::vector<int> rollMany(int times) {
|
||||||
vector<int> rolls{};
|
std::vector<int> rolls{};
|
||||||
|
|
||||||
for (int i = 0; i < times; i++) {
|
for (int i = 0; i < times; i++) {
|
||||||
int rolled = roll();
|
int rolled = roll();
|
||||||
|
@ -36,14 +35,14 @@ class Dice {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Print a vector of integers onto one line
|
// Print a vector of integers onto one line
|
||||||
void printResults(vector<int> data) {
|
void printResults(std::vector<int> data) {
|
||||||
int length = data.size();
|
int length = data.size();
|
||||||
|
|
||||||
for (int i = 0; i < length; i++) {
|
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 += ", ";
|
if (i != length - 1) out += ", ";
|
||||||
|
|
||||||
cout << out;
|
std::cout << out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,12 +55,12 @@ int main() {
|
||||||
Dice twenty(20);
|
Dice twenty(20);
|
||||||
|
|
||||||
// Roll them
|
// Roll them
|
||||||
vector<int> sixRolled = six.rollMany(10);
|
std::vector<int> sixRolled = six.rollMany(10);
|
||||||
vector<int> twentyRolled = twenty.rollMany(10);
|
std::vector<int> twentyRolled = twenty.rollMany(10);
|
||||||
|
|
||||||
// Display their outputs
|
// Display their outputs
|
||||||
cout << "Six Sided Dice:" << endl;
|
std::cout << "Six Sided Dice:\n";
|
||||||
printResults(sixRolled);
|
printResults(sixRolled);
|
||||||
cout << endl << endl << "Twenty Sided Dice:" << endl;
|
std::cout << "\n\nTwenty Sided Dice:\n";
|
||||||
printResults(twentyRolled);
|
printResults(twentyRolled);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,19 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
using namespace std;
|
|
||||||
using namespace std::chrono;
|
|
||||||
|
|
||||||
typedef high_resolution_clock Clock;
|
typedef std::chrono::high_resolution_clock Clock;
|
||||||
typedef Clock::time_point ClockTime;
|
typedef Clock::time_point ClockTime;
|
||||||
|
|
||||||
double karatsuba(double num1, double num2) {
|
double karatsuba(double num1, double num2) {
|
||||||
int num1Length = to_string((int)num1).size();
|
int num1Length = std::to_string((int)num1).size();
|
||||||
int num2Length = to_string((int)num2).size();
|
int num2Length = std::to_string((int)num2).size();
|
||||||
|
|
||||||
// Fallback to traditional multiplication
|
// Fallback to traditional multiplication
|
||||||
if (num1Length == 1 || num2Length == 1) {
|
if (num1Length == 1 || num2Length == 1) {
|
||||||
return num1 * num2;
|
return num1 * num2;
|
||||||
} else {
|
} else {
|
||||||
double n = floor(max(num1Length, num2Length) / 2);
|
double n = floor(std::max(num1Length, num2Length) / 2);
|
||||||
|
|
||||||
double lowNum1 = fmod(num1, pow(10, n));
|
double lowNum1 = fmod(num1, pow(10, n));
|
||||||
double lowNum2 = fmod(num2, pow(10, n));
|
double lowNum2 = fmod(num2, pow(10, n));
|
||||||
|
@ -32,28 +30,28 @@ double karatsuba(double num1, double num2) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
cout << "Give me a number! ";
|
std::cout << "Give me a number! ";
|
||||||
|
|
||||||
double num1;
|
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;
|
double num2;
|
||||||
cin >> num2;
|
std::cin >> num2;
|
||||||
|
|
||||||
ClockTime karatsubaBegin, karatsubaEnd;
|
ClockTime karatsubaBegin, karatsubaEnd;
|
||||||
karatsubaBegin = Clock::now();
|
karatsubaBegin = Clock::now();
|
||||||
double karatsubaResult = karatsuba(num1, num2);
|
double karatsubaResult = karatsuba(num1, num2);
|
||||||
karatsubaEnd = Clock::now();
|
karatsubaEnd = Clock::now();
|
||||||
double karatsubaTime = duration_cast<nanoseconds>(karatsubaEnd - karatsubaBegin).count();
|
double karatsubaTime = std::chrono::duration_cast<std::chrono::nanoseconds>(karatsubaEnd - karatsubaBegin).count();
|
||||||
|
|
||||||
ClockTime quadraticBegin, quadraticEnd;
|
ClockTime quadraticBegin, quadraticEnd;
|
||||||
quadraticBegin = Clock::now();
|
quadraticBegin = Clock::now();
|
||||||
double quadraticResult = num1 * num2;
|
double quadraticResult = num1 * num2;
|
||||||
quadraticEnd = Clock::now();
|
quadraticEnd = Clock::now();
|
||||||
double quadraticTime = duration_cast<nanoseconds>(quadraticEnd - quadraticBegin).count();
|
double quadraticTime = std::chrono::duration_cast<std::chrono::nanoseconds>(quadraticEnd - quadraticBegin).count();
|
||||||
|
|
||||||
cout << endl << "Quadratic Result: " << quadraticResult << " (" << quadraticTime << "ns)" << endl;
|
std::cout << "\nQuadratic Result: " << quadraticResult << " (" << quadraticTime << "ns)\n";
|
||||||
cout << "Karatsuba Result: " << karatsubaResult << " (" << karatsubaTime << "ns)";
|
std::cout << "Karatsuba Result: " << karatsubaResult << " (" << karatsubaTime << "ns)";
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
// Calculate Pascal's triangle
|
// Calculate Pascal's triangle
|
||||||
vector<vector<long long>> pascal(int rowCount) {
|
std::vector<std::vector<long long>> pascal(int rowCount) {
|
||||||
vector<vector<long long>> rows{{1}};
|
std::vector<std::vector<long long>> rows{{1}};
|
||||||
|
|
||||||
for (int i = 0; i < rowCount; i++) {
|
for (int i = 0; i < rowCount; i++) {
|
||||||
vector<long long> previousRow = rows[rows.size() - 1];
|
std::vector<long long> previousRow = rows[rows.size() - 1];
|
||||||
vector<long long> newRow{1};
|
std::vector<long long> newRow{1};
|
||||||
|
|
||||||
for (int j = 0; j < previousRow.size() - 1; j++) {
|
for (int j = 0; j < previousRow.size() - 1; j++) {
|
||||||
newRow.push_back(previousRow[j] + previousRow[j + 1]);
|
newRow.push_back(previousRow[j] + previousRow[j + 1]);
|
||||||
|
@ -22,11 +21,11 @@ vector<vector<long long>> pascal(int rowCount) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find the length of a vector if it were outputted as a string with a space between each value
|
// Find the length of a vector if it were outputted as a string with a space between each value
|
||||||
int vectorStringLength(vector<long long> vector) {
|
int vectorStringLength(std::vector<long long> vector) {
|
||||||
int value = vector.size();
|
int value = vector.size();
|
||||||
|
|
||||||
for (int i = 0; i < vector.size(); i++) {
|
for (int i = 0; i < vector.size(); i++) {
|
||||||
value += to_string(vector[i]).size();
|
value += std::to_string(vector[i]).size();
|
||||||
}
|
}
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
|
@ -34,17 +33,17 @@ int vectorStringLength(vector<long long> vector) {
|
||||||
|
|
||||||
// Draw Pascal's Triangle
|
// Draw Pascal's Triangle
|
||||||
void drawPascalTriangle(int rowCount) {
|
void drawPascalTriangle(int rowCount) {
|
||||||
vector<vector<long long>> pascalsTriangle = pascal(rowCount);
|
std::vector<std::vector<long long>> pascalsTriangle = pascal(rowCount);
|
||||||
int bottomRowSize = vectorStringLength(pascalsTriangle[pascalsTriangle.size() - 1]);
|
int bottomRowSize = vectorStringLength(pascalsTriangle[pascalsTriangle.size() - 1]);
|
||||||
|
|
||||||
for (int i = 0; i < pascalsTriangle.size() - 1; i++) {
|
for (int i = 0; i < pascalsTriangle.size() - 1; i++) {
|
||||||
vector<long long> currentRow = pascalsTriangle[i];
|
std::vector<long long> currentRow = pascalsTriangle[i];
|
||||||
|
|
||||||
int rowSize = vectorStringLength(currentRow);
|
int rowSize = vectorStringLength(currentRow);
|
||||||
|
|
||||||
int sizeDifference = bottomRowSize - rowSize;
|
int sizeDifference = bottomRowSize - rowSize;
|
||||||
|
|
||||||
string spacing = "";
|
std::string spacing = "";
|
||||||
|
|
||||||
for (int j = 0; j < sizeDifference / 2; j++) {
|
for (int j = 0; j < sizeDifference / 2; j++) {
|
||||||
spacing += " ";
|
spacing += " ";
|
||||||
|
@ -52,27 +51,27 @@ void drawPascalTriangle(int rowCount) {
|
||||||
|
|
||||||
for (int j = 0; j < currentRow.size(); j++) {
|
for (int j = 0; j < currentRow.size(); j++) {
|
||||||
if (j == 0) {
|
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) {
|
if (j == currentRow.size() - 1) {
|
||||||
cout << spacing;
|
std::cout << spacing;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (i != pascalsTriangle.size() - 1) {
|
if (i != pascalsTriangle.size() - 1) {
|
||||||
cout << endl;
|
std::cout << "\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
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;
|
int rowCount;
|
||||||
cin >> rowCount;
|
std::cin >> rowCount;
|
||||||
|
|
||||||
drawPascalTriangle(rowCount);
|
drawPascalTriangle(rowCount);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue