Skip to content

Commit 4da429a

Browse files
committed
refactor(calculator.cpp): add namespace and refactor insertNumber's function
1 parent 56d8bca commit 4da429a

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

exercises/calculator.cpp

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@
1414
#include <iostream>
1515
#include <string>
1616

17-
const std::string ERROR_COLOR = "\033[31m";
18-
const std::string NORMAL_COLOR = "\033[0m";
17+
using namespace std;
18+
19+
const string ERROR_COLOR = "\033[31m";
20+
const string NORMAL_COLOR = "\033[0m";
1921

2022
const int ZERO = 48;
2123
const int NINE = 57;
2224
const int MAX_DIGIT = 10;
2325
const int MINUS = 45;
2426

25-
int insertNumber(std::string position);
26-
bool checkStringIsNumber(std::string in);
27+
int insertNumber(string position);
28+
bool checkStringIsNumber(string in);
2729

2830
float doSum(float n1, float n2);
2931
float doSub(float n1, float n2);
@@ -37,27 +39,32 @@ int main(){
3739
float num2 = insertNumber("second");
3840

3941
doOperation(num1, num2);
42+
return EXIT_SUCCESS;
4043
}
4144

4245

4346

44-
int insertNumber(std::string position){
45-
std::string insert;
47+
int insertNumber(string position){
48+
string insert;
4649
bool isCorrect = true;
4750
do {
48-
if(!isCorrect) std::cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR;
49-
std::cout << "Insert "<< position <<" number: ";
50-
getline(std::cin, insert);
51-
} while(!(isCorrect = checkStringIsNumber(insert)));
51+
if(!isCorrect) cout << ERROR_COLOR+("[Insert a valid input] ")+NORMAL_COLOR;
52+
cout << "Insert "<< position <<" number: ";
53+
getline(cin, insert);
54+
55+
isCorrect = checkStringIsNumber(insert);
56+
} while(!(isCorrect));
5257

53-
return std::stoi(insert);
58+
return stoi(insert);
5459
}
5560

56-
bool checkStringIsNumber(std::string in){
61+
bool checkStringIsNumber(string in){
5762
if(in.empty()) return false;
5863
if(in.size() > MAX_DIGIT) return false;
64+
5965
for(int i = 0; i < in.size(); i++)
60-
if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS) return false;
66+
if((in.at(i) < ZERO || in.at(i) > NINE) && in.at(i) != MINUS)
67+
return false;
6168

6269
return true;
6370
}
@@ -76,8 +83,8 @@ float doDiv(float n1, float n2) {
7683
}
7784

7885
void doOperation(float n1, float n2) {
79-
std::cout << "SUM: " << doSum(n1, n2) << std::endl;
80-
std::cout << "Difference: " << doSub(n1, n2) << std::endl;
81-
std::cout << "Moltiplication: " << doMolt(n1, n2) << std::endl;
82-
std::cout << "Division: " << doDiv(n1, n2) << std::endl;
86+
cout << "SUM: " << doSum(n1, n2) << endl;
87+
cout << "Difference: " << doSub(n1, n2) << endl;
88+
cout << "Moltiplication: " << doMolt(n1, n2) << endl;
89+
cout << "Division: " << doDiv(n1, n2) << endl;
8390
}

0 commit comments

Comments
 (0)