diff --git a/pocket_calc b/pocket_calc new file mode 100644 index 0000000..ff25d9e Binary files /dev/null and b/pocket_calc differ diff --git a/pocket_calc.c b/pocket_calc.c new file mode 100644 index 0000000..b67374b --- /dev/null +++ b/pocket_calc.c @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#define FIRST_OPTION 1 +#define SECOND_OPTION 2 +#define THIRD_OPTION 3 +#define FOURTH_OPTION 4 +#define FIFTH_OPTION -1 + +int main(int argc, char* argv[]) +{ + int user_choice; + while (true) + { + bool dont_pass = true; + printf("Choose one of the following operations:\n"); + printf("\tAdd (%d)\n", FIRST_OPTION); + printf("\tSubtract (%d)\n", SECOND_OPTION); + printf("\tMultiply (%d)\n", THIRD_OPTION); + printf("\tDivide (%d)\n", FOURTH_OPTION); + printf("\tStop program (%d)\n", FIFTH_OPTION); + do + { + printf("Enter your choice: "); + scanf("%d", &user_choice); + dont_pass = user_choice < FIFTH_OPTION || user_choice > FOURTH_OPTION || user_choice == 0; + if (dont_pass) + { + printf("Input not allowed, please try again\n"); + } + + } while (dont_pass); + + if(user_choice == -1) return 0; + + double first_operand; + double second_operand; + dont_pass = true; + do + { + printf("Please enter the first operand: "); + scanf("%lf", &first_operand); + printf("Please enter the second operand: "); + scanf("%lf", &second_operand); + if(user_choice == FOURTH_OPTION && second_operand == 0) + { + printf("Division by zero\n"); + } + else if (first_operand < DBL_MIN || second_operand < DBL_MIN) + { + printf("Number underflow\n"); + } + else if(first_operand > DBL_MAX || second_operand > DBL_MAX) + { + printf("Number overflow\n"); + } + else + { + dont_pass = false; + } + } + while (dont_pass); + + double result; + switch(user_choice) + { + case FIRST_OPTION: + result = first_operand + second_operand; + printf("The result is %.3f\n", result); + break; + case SECOND_OPTION: + result = first_operand - second_operand; + printf("The result is %0.3f\n", result); + break; + case THIRD_OPTION: + result = first_operand * second_operand; + printf("The result is %0.3f\n", result); + break; + case FOURTH_OPTION: + result = first_operand / second_operand; + printf("The result is %2.3f\n", result); + break; + default: + return 0; + } + } + return 0; +} \ No newline at end of file