Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions exercises/sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,77 @@
Insert the second number: 2
Sum: 3
*/

#include <iostream>
#include <string>

using namespace std;

// --- Costant ---
const int ASCII_ZERO = 48;
const int ASCII_NINE = 57;
const int MAX_DIGITS = 10;
const int ASCII_MINUS = 45;

const string COLOR_ERROR = "\033[31m";
const string COLOR_RESET = "\033[0m";

// --- Prototype ---
template <typename T>
T sumNumber(T n1, T n2);

double promptNumber(const string& msg);
bool isValidNumber(const string& in);

// --- Main ---
int main() {
double n1 = promptNumber("Insert first number:");
double n2 = promptNumber("Insert second number:");

double ris = sumNumber(n1, n2);
cout << "Sum: " << ris << endl;

return EXIT_SUCCESS;
}

// --- Template Function ---
template <typename T>
T sumNumber(T n1, T n2) {
return n1 + n2;
}

// --- User Validate Input ---
double promptNumber(const string& msg){
string insert;
bool valid = true;

do {
if(!valid)
cout << COLOR_ERROR << "[Insert a valid input] " << COLOR_RESET;

cout << msg;
getline(cin, insert);

valid = isValidNumber(insert);
} while(!valid);

return stod(insert);
}

// --- Validate Numeric String
bool isValidNumber(const string& in){
if(in.empty())
return false;
if(in.size() > MAX_DIGITS)
return false;

for (int i = 0; i < in.size(); ++i){
if ((in[i] < ASCII_ZERO || in[i] > ASCII_NINE)){
if (i == 0 && in[i] == ASCII_MINUS)
continue;
return false;
}
}

return true;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: penso che il codice potrebbe essere migliorato in termini di leggibilità