From 1a5c0a96afc46a95f6845a64bd11700763d3c260 Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Tue, 4 Nov 2025 12:43:52 +0100 Subject: [PATCH 1/4] feat: do a main operator with user input --- exercises/calculator.cpp | 71 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index 013cc786..60bb6482 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -10,3 +10,74 @@ Multiplication: 8 Division: 2 */ + +#include +#include + +const std::string ERROR_COLOR = "\033[31m"; +const std::string NORMAL_COLOR = "\033[0m"; + +const int ZERO = 48; +const int NINE = 57; +const int MAX_DIGIT = 10; +const int MINUS = 45; + +int insertNumber(std::string position); +bool checkStringIsNumber(std::string in); + +float doSum(float n1, float n2); +float doSub(float n1, float n2); +float doMolt(float n1, float n2); +float doDiv(float n1, float n2); + +void doOperation(float n1, float n2); + +int main(){ + float num1 = insertNumber("first"); + float num2 = insertNumber("second"); + + doOperation(num1, num2); +} + + + +int insertNumber(std::string position){ + std::string insert; + bool isCorrect = true; + do { + if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; + std::cout << "Insert "<< position <<" number: "; + getline(std::cin, insert); + } while(!(isCorrect = checkStringIsNumber(insert))); + + return std::stoi(insert); +} + +bool checkStringIsNumber(std::string in){ + if(in.empty()) return false; + if(in.size() > MAX_DIGIT) return false; + for(int i = 0; i < in.size(); i++) + if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) return false; + + return true; +} + +float doSum(float n1, float n2) { + return n1+n2; +} +float doSub(float n1, float n2) { + return n1-n2; +} +float doMolt(float n1, float n2) { + return n1*n2; +} +float doDiv(float n1, float n2) { + return (n2 != 0) ? n1/n2 : -1; +} + +void doOperation(float n1, float n2) { + std::cout << "SUM: " << doSum(n1, n2) << std::endl; + std::cout << "Difference: " << doSub(n1, n2) << std::endl; + std::cout << "Moltiplication: " << doMolt(n1, n2) << std::endl; + std::cout << "Division: " << doDiv(n1, n2) << std::endl; +} \ No newline at end of file From 56d8bca3b0d36ee6b3898c797a76e2ee92f77893 Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Tue, 4 Nov 2025 16:43:58 +0100 Subject: [PATCH 2/4] style: erase whitespace --- exercises/calculator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index 60bb6482..2095235e 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -56,7 +56,7 @@ int insertNumber(std::string position){ bool checkStringIsNumber(std::string in){ if(in.empty()) return false; if(in.size() > MAX_DIGIT) return false; - for(int i = 0; i < in.size(); i++) + for(int i = 0; i < in.size(); i++) if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) return false; return true; From 4da429abbf4ca9c84713eec8bd17f9a92119c82c Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Wed, 5 Nov 2025 17:57:27 +0100 Subject: [PATCH 3/4] refactor(calculator.cpp): add namespace and refactor insertNumber's function --- exercises/calculator.cpp | 41 +++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index 2095235e..66565eee 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -14,16 +14,18 @@ #include #include -const std::string ERROR_COLOR = "\033[31m"; -const std::string NORMAL_COLOR = "\033[0m"; +using namespace std; + +const string ERROR_COLOR = "\033[31m"; +const string NORMAL_COLOR = "\033[0m"; const int ZERO = 48; const int NINE = 57; const int MAX_DIGIT = 10; const int MINUS = 45; -int insertNumber(std::string position); -bool checkStringIsNumber(std::string in); +int insertNumber(string position); +bool checkStringIsNumber(string in); float doSum(float n1, float n2); float doSub(float n1, float n2); @@ -37,27 +39,32 @@ int main(){ float num2 = insertNumber("second"); doOperation(num1, num2); + return EXIT_SUCCESS; } -int insertNumber(std::string position){ - std::string insert; +int insertNumber(string position){ + string insert; bool isCorrect = true; do { - if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; - std::cout << "Insert "<< position <<" number: "; - getline(std::cin, insert); - } while(!(isCorrect = checkStringIsNumber(insert))); + if(!isCorrect) cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; + cout << "Insert "<< position <<" number: "; + getline(cin, insert); + + isCorrect = checkStringIsNumber(insert); + } while(!(isCorrect)); - return std::stoi(insert); + return stoi(insert); } -bool checkStringIsNumber(std::string in){ +bool checkStringIsNumber(string in){ if(in.empty()) return false; if(in.size() > MAX_DIGIT) return false; + for(int i = 0; i < in.size(); i++) - if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) return false; + if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) + return false; return true; } @@ -76,8 +83,8 @@ float doDiv(float n1, float n2) { } void doOperation(float n1, float n2) { - std::cout << "SUM: " << doSum(n1, n2) << std::endl; - std::cout << "Difference: " << doSub(n1, n2) << std::endl; - std::cout << "Moltiplication: " << doMolt(n1, n2) << std::endl; - std::cout << "Division: " << doDiv(n1, n2) << std::endl; + cout << "SUM: " << doSum(n1, n2) << endl; + cout << "Difference: " << doSub(n1, n2) << endl; + cout << "Moltiplication: " << doMolt(n1, n2) << endl; + cout << "Division: " << doDiv(n1, n2) << endl; } \ No newline at end of file From a72a67cc6cb4bf135032cce323bb9068b9823197 Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Wed, 5 Nov 2025 22:37:31 +0100 Subject: [PATCH 4/4] style(calculator.cpp): improve readability and naming consistency --- exercises/calculator.cpp | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index 66565eee..091e6aaa 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -16,13 +16,13 @@ using namespace std; -const string ERROR_COLOR = "\033[31m"; -const string NORMAL_COLOR = "\033[0m"; +const string COLOR_ERROR = "\033[31m"; +const string COLOR_RESET = "\033[0m"; -const int ZERO = 48; -const int NINE = 57; -const int MAX_DIGIT = 10; -const int MINUS = 45; +const int ASCII_ZERO = 48; +const int ASCII_NINE = 57; +const int ASCII_MINUS = 45; +const int MAX_DIGITS = 10; int insertNumber(string position); bool checkStringIsNumber(string in); @@ -48,7 +48,7 @@ int insertNumber(string position){ string insert; bool isCorrect = true; do { - if(!isCorrect) cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; + if(!isCorrect) cout << COLOR_ERROR+("[Insert a valid input] ")+COLOR_RESET; cout << "Insert "<< position <<" number: "; getline(cin, insert); @@ -59,12 +59,15 @@ int insertNumber(string position){ } bool checkStringIsNumber(string in){ - if(in.empty()) return false; - if(in.size() > MAX_DIGIT) return false; + if(in.empty()) + return false; + if(in.size() > MAX_DIGITS) + return false; for(int i = 0; i < in.size(); i++) - if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) - return false; + if((in.at(i) < ASCII_ZERO || in.at(i) > ASCII_NINE)) + if(in.at(i) != ASCII_MINUS) + return false; return true; }