Browse Source

refactoring: Extracted all the operations from the programmingMode and move them into performOperation

remotes/origin/kabrel
fdai7782 11 months ago
parent
commit
db3b1e33e9
  1. 48
      src/main/c/programmingMode.c

48
src/main/c/programmingMode.c

@ -5,6 +5,27 @@
#include "taschenrechner.h" #include "taschenrechner.h"
// Calculation
int performOperation(int num1, char operator, int num2) {
switch (operator) {
case '+':
return num1 + num2;
case '-':
return num1 - num2;
case '*':
return num1 * num2;
case '/':
if (num2 != 0) {
return num1 / num2;
} else {
printf("Error: Division by zero\n");
return 0;
}
default:
printf("Invalid operator\n");
return 0;
}
}
void programmingMode() { void programmingMode() {
int num1, num2, result; int num1, num2, result;
@ -19,30 +40,9 @@ void programmingMode() {
printf("Enter second integer: "); printf("Enter second integer: ");
scanf("%d", &num2); scanf("%d", &num2);
// Calculation
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error: Division by zero\n");
result = 0;
}
break;
default:
printf("Invalid operator\n");
result = 0;
}
// Calculation + Display the result
result = performOperation(num1, operator, num2);
// Display the result
printf("Result: %d\n", result); printf("Result: %d\n", result);
} }
Loading…
Cancel
Save