Skip to content
Open
Changes from 4 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
54 changes: 54 additions & 0 deletions exercises/sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,57 @@
Insert the second number: 2
Sum: 3
*/

#include <iostream>

using namespace std;

const int ZERO = 48;
const int NINE = 57;
const int MAX_DIGIT = 10;
const int MINUS = 45;

const string ERROR_COLOR = "\033[31m";
const string NORMAL_COLOR = "\033[0m";

template <typename T>
T sumNumber(T n1, T n2);
double insertNumber(const string& msg);
bool checkStringIsNumber(string in);


int main() {
auto n1 = insertNumber("Insert first number:");
auto n2 = insertNumber("Insert second number:");
auto ris = sumNumber(n1, n2);
cout << "Sum: " << ris;
}

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

double insertNumber(const string& msg){
string insert;
bool isCorrect = true;
do {
if(!isCorrect) cout << ERROR_COLOR << "[Insert a valid input] " << NORMAL_COLOR;
cout << msg;
getline(cin, insert);
} while(!(isCorrect = checkStringIsNumber(insert)));

return stod(insert);
}

bool checkStringIsNumber(string in){
if(in.empty()) return false;
if(in.size() > MAX_DIGIT) return false;
for (size_t i = 0; i < in.size(); ++i){
if ((in[i] < ZERO || in[i] > NINE)){
if (i == 0 && in[i] == 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à