From b3f5a03bbae2b6eb86105f021a67166d3b460e09 Mon Sep 17 00:00:00 2001 From: Gianluca Di Paola Date: Thu, 19 Oct 2023 12:23:03 +0200 Subject: [PATCH 1/8] chore: implementation of a calculator --- exercises/calculator.cpp | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index 013cc78..31e9523 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -10,3 +10,54 @@ Multiplication: 8 Division: 2 */ + +#include +using namespace std; + +float operations(){ + + float a,b; + cout << "Insert the first number: "; + cin >> a; + cout << endl << "Insert second number: "; + cin >> b; + + int op; + float result; + while(true){ + cout << endl << "Select the operation: " << endl << "1) SUM" << endl << "2)Difference" << endl << "3)Multiplication" << endl << "4)Division" << endl; + cin >> op; + if(op <1 || op >4) + cerr << "Wrong choice, please select a valid operation number [1-4]" << endl; + else if(op == 4 && b == 0) + cerr << "Can't divide a number for 0, please select another operation" << endl; + else + break; + } + + switch(op){ + case 1: + cout << endl << "SUM = "; + result = a + b; + break; + case 2: + cout << endl << "Difference = "; + result = a - b; + break; + case 3: + cout << endl << "Multiplication = "; + result = a * b; + break; + case 4: + cout << endl << "Division = "; + result = a / b; + break; + } + return result; +} + +int main(){ + + cout << operations() << endl << endl; + +} \ No newline at end of file From 5bc2b74e8efdb0da83f3f277d42cdd5a1b4bb31d Mon Sep 17 00:00:00 2001 From: Gianluca Di Paola Date: Thu, 19 Oct 2023 12:37:04 +0200 Subject: [PATCH 2/8] Fix: added minor fixes --- exercises/calculator.cpp | 48 ++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index 31e9523..7cefc96 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -14,50 +14,66 @@ #include using namespace std; -float operations(){ - - float a,b; - cout << "Insert the first number: "; - cin >> a; - cout << endl << "Insert second number: "; - cin >> b; +float operations(float a, float b){ int op; float result; + while(true){ + cout << endl << "Select the operation: " << endl << "1) SUM" << endl << "2)Difference" << endl << "3)Multiplication" << endl << "4)Division" << endl; cin >> op; - if(op <1 || op >4) + + if(op <1 || op >4){ cerr << "Wrong choice, please select a valid operation number [1-4]" << endl; - else if(op == 4 && b == 0) + } + + else if(op == 4 && b == 0){ cerr << "Can't divide a number for 0, please select another operation" << endl; - else + } + + else{ break; + } } switch(op){ - case 1: + case 1:{ cout << endl << "SUM = "; result = a + b; break; - case 2: + } + + case 2:{ cout << endl << "Difference = "; result = a - b; break; - case 3: + } + + case 3:{ cout << endl << "Multiplication = "; result = a * b; break; - case 4: + } + + case 4:{ cout << endl << "Division = "; result = a / b; break; + } } - return result; + cout << result << endl << endl; } int main(){ - cout << operations() << endl << endl; + float a,b; + cout << "Insert the first number: "; + cin >> a; + cout << endl << "Insert second number: "; + cin >> b; + + operations(a, b); + return 0; } \ No newline at end of file From a2161a5164cc1684e7d1b81aac3ce1c659102272 Mon Sep 17 00:00:00 2001 From: Gianluca Di Paola Date: Thu, 19 Oct 2023 15:59:39 +0200 Subject: [PATCH 3/8] Minor fixes to the function --- exercises/calculator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index 7cefc96..b13e371 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -14,7 +14,7 @@ #include using namespace std; -float operations(float a, float b){ +void operations(float a, float b){ int op; float result; From db5039c72860a24030e145ac43bc398cdecc3746 Mon Sep 17 00:00:00 2001 From: Gianluca Di Paola Date: Thu, 19 Oct 2023 17:10:23 +0200 Subject: [PATCH 4/8] chore: Minor Fix to the return value of main() and added a function switchOp() --- exercises/calculator.cpp | 53 +++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index b13e371..599079f 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -13,31 +13,13 @@ #include using namespace std; +#define EXIT_SUCCESS 0 -void operations(float a, float b){ - int op; +float switchOp(int x, int a, int b){ float result; - while(true){ - - cout << endl << "Select the operation: " << endl << "1) SUM" << endl << "2)Difference" << endl << "3)Multiplication" << endl << "4)Division" << endl; - cin >> op; - - if(op <1 || op >4){ - cerr << "Wrong choice, please select a valid operation number [1-4]" << endl; - } - - else if(op == 4 && b == 0){ - cerr << "Can't divide a number for 0, please select another operation" << endl; - } - - else{ - break; - } - } - - switch(op){ + switch(x){ case 1:{ cout << endl << "SUM = "; result = a + b; @@ -62,6 +44,33 @@ void operations(float a, float b){ break; } } +} + +void operations(float a, float b){ + + int op; + float result; + + while(true){ + + cout << endl << "Select the operation: " << endl << "1) SUM" << endl << "2)Difference" << endl << "3)Multiplication" << endl << "4)Division" << endl; + cin >> op; + + if(op <1 || op >4){ + cerr << "Wrong choice, please select a valid operation number [1-4]" << endl; + } + + else if(op == 4 && b == 0){ + cerr << "Can't divide a number for 0, please select another operation" << endl; + } + + else{ + break; + } + } + + int result = switchOp(op, a, b); + cout << result << endl << endl; } @@ -75,5 +84,5 @@ int main(){ operations(a, b); - return 0; + return EXIT_SUCCESS; } \ No newline at end of file From 8498b0ec12897c6d0b2ae8dfd08efd5e0566477e Mon Sep 17 00:00:00 2001 From: Gianluca Di Paola Date: Fri, 20 Oct 2023 19:47:30 +0200 Subject: [PATCH 5/8] minor fix on the result variable --- exercises/calculator.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index 599079f..bf7873e 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -17,6 +17,7 @@ using namespace std; float switchOp(int x, int a, int b){ + float result; switch(x){ @@ -44,12 +45,12 @@ float switchOp(int x, int a, int b){ break; } } + return result; } void operations(float a, float b){ int op; - float result; while(true){ @@ -67,11 +68,8 @@ void operations(float a, float b){ else{ break; } - } - - int result = switchOp(op, a, b); - - cout << result << endl << endl; + } + cout << switchOp(op, a, b) << endl << endl; } int main(){ From 14693ec4fc7337dc782a805570b86d3092444505 Mon Sep 17 00:00:00 2001 From: Gianluca Di Paola Date: Fri, 20 Oct 2023 20:03:02 +0200 Subject: [PATCH 6/8] Fixing pipeline errors --- exercises/calculator.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index bf7873e..158bfb6 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -17,7 +17,6 @@ using namespace std; float switchOp(int x, int a, int b){ - float result; switch(x){ From 27695b1e44ff3cc596e512548ba3f520c83e2c1b Mon Sep 17 00:00:00 2001 From: Gianluca Di Paola Date: Sun, 22 Oct 2023 10:58:48 +0200 Subject: [PATCH 7/8] feat: added the possibility to make more operations --- exercises/calculator.cpp | 47 +++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index 158bfb6..b7af815 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -16,35 +16,34 @@ using namespace std; #define EXIT_SUCCESS 0 -float switchOp(int x, int a, int b){ - float result; +float switchOp(int x, float a, float b){ switch(x){ case 1:{ cout << endl << "SUM = "; - result = a + b; - break; + return a + b; } case 2:{ cout << endl << "Difference = "; - result = a - b; - break; + return a - b; } case 3:{ cout << endl << "Multiplication = "; - result = a * b; - break; + return a - b; } case 4:{ cout << endl << "Division = "; - result = a / b; - break; + return a / b; + } + + default:{ + cerr << "Invalid operand."; + return 0; } } - return result; } void operations(float a, float b){ @@ -73,13 +72,25 @@ void operations(float a, float b){ int main(){ - float a,b; - cout << "Insert the first number: "; - cin >> a; - cout << endl << "Insert second number: "; - cin >> b; - - operations(a, b); + char response; + while(true){ + float a,b; + cout << "Insert the first number: "; + cin >> a; + cout << endl << "Insert second number: "; + cin >> b; + + operations(a, b); + + do{ + cout << "Do you want to make another operation? (Y/N): "; + cin >> response; + }while(toupper(response)!= 'Y' && toupper(response) != 'N'); + + if(toupper(response) == 'N'){ + break; + } + } return EXIT_SUCCESS; } \ No newline at end of file From e6cf3a76adedd6bdca721da59831cd54ab0f593e Mon Sep 17 00:00:00 2001 From: Gianluca Di Paola Date: Sun, 22 Oct 2023 11:16:59 +0200 Subject: [PATCH 8/8] Fix of whitespaces --- exercises/calculator.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/exercises/calculator.cpp b/exercises/calculator.cpp index b7af815..63ed7ce 100644 --- a/exercises/calculator.cpp +++ b/exercises/calculator.cpp @@ -66,7 +66,7 @@ void operations(float a, float b){ else{ break; } - } + } cout << switchOp(op, a, b) << endl << endl; } @@ -82,7 +82,7 @@ int main(){ cin >> b; operations(a, b); - + do{ cout << "Do you want to make another operation? (Y/N): "; cin >> response; @@ -91,6 +91,6 @@ int main(){ if(toupper(response) == 'N'){ break; } - } + } return EXIT_SUCCESS; } \ No newline at end of file