From 13be537d1cc394b70ed5736db358053fb359edee Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Tue, 4 Nov 2025 17:16:43 +0100 Subject: [PATCH 1/6] feat: git commit -m feat: I use maps to simplify use --- exercises/ex4.cpp | 95 +++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/exercises/ex4.cpp b/exercises/ex4.cpp index 9d93d8ac..95d3b672 100644 --- a/exercises/ex4.cpp +++ b/exercises/ex4.cpp @@ -1,56 +1,55 @@ /* Surprise me. */ -#include +#include +#include +#include -int main() -{ - int month; +const int ZERO = 48; +const int NINE = 57; +const int MAX_NUMBER = 10; +const int MINUS = 45; - /* Input month number from user */ - printf("Enter month number(1-12): "); - scanf("%d", &month); +const std::string ERROR_COLOR = "\033[31m"; +const std::string NORMAL_COLOR = "\033[0m"; - switch (month) - { - case 1: - printf("31 days"); - break; - case 2: - printf("28/29 days"); - break; - case 3: - printf("31 days"); - break; - case 4: - printf("30 days"); - break; - case 5: - printf("31 days"); - break; - case 6: - printf("30 days"); - break; - case 7: - printf("31 days"); - break; - case 8: - printf("31 days"); - break; - case 9: - printf("30 days"); - break; - case 10: - printf("31 days"); - break; - case 11: - printf("30 days"); - break; - case 12: - printf("31 days"); - break; - default: - printf("Invalid input! Please enter month number between 1-12"); - } +int insertNumber(); +bool checkStringIsNumber(std::string in); +int main(){ + int month = insertNumber(); + std::map calendary = { + {1, "31"}, + {2, "28/29"}, + {3, "31"}, + {4, "30"}, + {5, "31"}, + {6, "30"}, + {7, "31"}, + {8, "31"}, + {9, "30"}, + {10, "31"}, + {11, "30"}, + {12, "31"}, + }; + std::cout << calendary[month] << " days"; return 0; +} + +int insertNumber(){ + std::string insert; + bool isCorrect = true; + do { + if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; + std::cout << "Insert Mounth: "; + getline(std::cin, insert); + } while(!(isCorrect = checkStringIsNumber(insert))); + return std::stoi(insert); +} + +bool checkStringIsNumber(std::string in){ + if(in.empty()) 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(stoi(in) > MAX_NUMBER) return false; + return true; } \ No newline at end of file From 04e4e68a96ec4d6c5dac813dbfeb64f55d2db548 Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Tue, 4 Nov 2025 17:18:11 +0100 Subject: [PATCH 2/6] refactor: load old code --- exercises/ex4.cpp | 95 ++++++++++++++++++++++++----------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/exercises/ex4.cpp b/exercises/ex4.cpp index 95d3b672..9d93d8ac 100644 --- a/exercises/ex4.cpp +++ b/exercises/ex4.cpp @@ -1,55 +1,56 @@ /* Surprise me. */ -#include -#include -#include +#include -const int ZERO = 48; -const int NINE = 57; -const int MAX_NUMBER = 10; -const int MINUS = 45; +int main() +{ + int month; -const std::string ERROR_COLOR = "\033[31m"; -const std::string NORMAL_COLOR = "\033[0m"; + /* Input month number from user */ + printf("Enter month number(1-12): "); + scanf("%d", &month); -int insertNumber(); -bool checkStringIsNumber(std::string in); + switch (month) + { + case 1: + printf("31 days"); + break; + case 2: + printf("28/29 days"); + break; + case 3: + printf("31 days"); + break; + case 4: + printf("30 days"); + break; + case 5: + printf("31 days"); + break; + case 6: + printf("30 days"); + break; + case 7: + printf("31 days"); + break; + case 8: + printf("31 days"); + break; + case 9: + printf("30 days"); + break; + case 10: + printf("31 days"); + break; + case 11: + printf("30 days"); + break; + case 12: + printf("31 days"); + break; + default: + printf("Invalid input! Please enter month number between 1-12"); + } -int main(){ - int month = insertNumber(); - std::map calendary = { - {1, "31"}, - {2, "28/29"}, - {3, "31"}, - {4, "30"}, - {5, "31"}, - {6, "30"}, - {7, "31"}, - {8, "31"}, - {9, "30"}, - {10, "31"}, - {11, "30"}, - {12, "31"}, - }; - std::cout << calendary[month] << " days"; return 0; -} - -int insertNumber(){ - std::string insert; - bool isCorrect = true; - do { - if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; - std::cout << "Insert Mounth: "; - getline(std::cin, insert); - } while(!(isCorrect = checkStringIsNumber(insert))); - return std::stoi(insert); -} - -bool checkStringIsNumber(std::string in){ - if(in.empty()) 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(stoi(in) > MAX_NUMBER) return false; - return true; } \ No newline at end of file From 1b7abbb0205e0394bc8ff6a40aea853e5f9c380e Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Tue, 4 Nov 2025 17:18:42 +0100 Subject: [PATCH 3/6] feat: git commit -m feat: I use maps to simplify use --- exercises/ex4.cpp | 95 +++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/exercises/ex4.cpp b/exercises/ex4.cpp index 9d93d8ac..95d3b672 100644 --- a/exercises/ex4.cpp +++ b/exercises/ex4.cpp @@ -1,56 +1,55 @@ /* Surprise me. */ -#include +#include +#include +#include -int main() -{ - int month; +const int ZERO = 48; +const int NINE = 57; +const int MAX_NUMBER = 10; +const int MINUS = 45; - /* Input month number from user */ - printf("Enter month number(1-12): "); - scanf("%d", &month); +const std::string ERROR_COLOR = "\033[31m"; +const std::string NORMAL_COLOR = "\033[0m"; - switch (month) - { - case 1: - printf("31 days"); - break; - case 2: - printf("28/29 days"); - break; - case 3: - printf("31 days"); - break; - case 4: - printf("30 days"); - break; - case 5: - printf("31 days"); - break; - case 6: - printf("30 days"); - break; - case 7: - printf("31 days"); - break; - case 8: - printf("31 days"); - break; - case 9: - printf("30 days"); - break; - case 10: - printf("31 days"); - break; - case 11: - printf("30 days"); - break; - case 12: - printf("31 days"); - break; - default: - printf("Invalid input! Please enter month number between 1-12"); - } +int insertNumber(); +bool checkStringIsNumber(std::string in); +int main(){ + int month = insertNumber(); + std::map calendary = { + {1, "31"}, + {2, "28/29"}, + {3, "31"}, + {4, "30"}, + {5, "31"}, + {6, "30"}, + {7, "31"}, + {8, "31"}, + {9, "30"}, + {10, "31"}, + {11, "30"}, + {12, "31"}, + }; + std::cout << calendary[month] << " days"; return 0; +} + +int insertNumber(){ + std::string insert; + bool isCorrect = true; + do { + if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; + std::cout << "Insert Mounth: "; + getline(std::cin, insert); + } while(!(isCorrect = checkStringIsNumber(insert))); + return std::stoi(insert); +} + +bool checkStringIsNumber(std::string in){ + if(in.empty()) 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(stoi(in) > MAX_NUMBER) return false; + return true; } \ No newline at end of file From 9c2791f417b022a993eac86f14b4cc615d4acc14 Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Wed, 5 Nov 2025 18:10:23 +0100 Subject: [PATCH 4/6] refactor(ex4.cpp): add namespace, remove std:: and add more spaces --- exercises/ex4.cpp | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/exercises/ex4.cpp b/exercises/ex4.cpp index 95d3b672..f3c85aa5 100644 --- a/exercises/ex4.cpp +++ b/exercises/ex4.cpp @@ -4,20 +4,22 @@ #include #include +using namespace std; + const int ZERO = 48; const int NINE = 57; const int MAX_NUMBER = 10; const int MINUS = 45; -const std::string ERROR_COLOR = "\033[31m"; -const std::string NORMAL_COLOR = "\033[0m"; +const string ERROR_COLOR = "\033[31m"; +const string NORMAL_COLOR = "\033[0m"; int insertNumber(); -bool checkStringIsNumber(std::string in); +bool checkStringIsNumber(string in); int main(){ int month = insertNumber(); - std::map calendary = { + map calendary = { {1, "31"}, {2, "28/29"}, {3, "31"}, @@ -31,25 +33,35 @@ int main(){ {11, "30"}, {12, "31"}, }; - std::cout << calendary[month] << " days"; + cout << calendary[month] << " days"; return 0; } int insertNumber(){ - std::string insert; + string insert; bool isCorrect = true; + do { - if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; - std::cout << "Insert Mounth: "; - getline(std::cin, insert); - } while(!(isCorrect = checkStringIsNumber(insert))); - return std::stoi(insert); + if(!isCorrect) cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; + + cout << "Insert Mounth: "; + getline(cin, insert); + + isCorrect = checkStringIsNumber(insert); + } while(!isCorrect); + + return stoi(insert); } -bool checkStringIsNumber(std::string in){ - if(in.empty()) return false; +bool checkStringIsNumber(string in){ + if(in.empty()) + 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; + if(stoi(in) > MAX_NUMBER) return false; + return true; } \ No newline at end of file From 6e8a4da645130f10bc0ce62a2300c7ee8bfd3fa8 Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Wed, 5 Nov 2025 21:34:25 +0100 Subject: [PATCH 5/6] style(ex4.cpp): remove whitespaces --- exercises/ex4.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/exercises/ex4.cpp b/exercises/ex4.cpp index f3c85aa5..564fe007 100644 --- a/exercises/ex4.cpp +++ b/exercises/ex4.cpp @@ -43,10 +43,10 @@ int insertNumber(){ do { if(!isCorrect) cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; - + cout << "Insert Mounth: "; getline(cin, insert); - + isCorrect = checkStringIsNumber(insert); } while(!isCorrect); @@ -54,14 +54,14 @@ int insertNumber(){ } bool checkStringIsNumber(string in){ - if(in.empty()) + if(in.empty()) return false; for(int i = 0; i < in.size(); i++) - if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) + if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) return false; - + if(stoi(in) > MAX_NUMBER) return false; - + return true; } \ No newline at end of file From 26ab2f8259722453b35be175c0e95a7c7791576e Mon Sep 17 00:00:00 2001 From: LeottaAlberto Date: Fri, 7 Nov 2025 16:36:52 +0100 Subject: [PATCH 6/6] style(ex4.cpp): remove whitespace --- exercises/ex4.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/ex4.cpp b/exercises/ex4.cpp index 564fe007..6cc2f24c 100644 --- a/exercises/ex4.cpp +++ b/exercises/ex4.cpp @@ -43,7 +43,7 @@ int insertNumber(){ do { if(!isCorrect) cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR; - + cout << "Insert Mounth: "; getline(cin, insert);